CSIS 350 Lecture 23: SQL DDL and Data Manipulation, Study notes of Database Management Systems (DBMS)

Sql ddl, focusing on the create table statement and its use of constraints, minimum cardinalities, and default values. It also introduces sql dml statements for inserting, updating, and deleting data. Examples and exercises.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-cse
koofers-user-cse ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 350 โ€“ Lecture 23
Announcments
Homework on web site โ€“ due Thursday
Any problems creating a database?
CSIS 332 Java for the Internet
Chapter 7
Last time we started looking at SQL DDL: The CREATE TABLE statement
- define columns: name, type, constraints
- define surrogate keys
oSQL Server: IDENTITY
omySQL: AUTO_INCREMENT
- using CONSTRAINT to define FOREIGN KEYs:
oUPDATE NO ACTION, UPDATE CASCADE
oDELETE NO ACTION, DELETE CASCADE
*** You must define parent tables before child tables. This CREATE TABLE statement
will fail if the ARTIST table does not exist.
See Slides 1 & 2: Create Table statement in SQL Server and mySQL. Note the
AUTO_INCREMENT differences for defining surrogate key.
Implementing minimum cardinalities
See slide 3. Note that minimum cardinalities are enforced through the foreign key and
with the ON UPDATE and ON DELETE clauses.
Q: Why doesn't this slide show N:M relationships?
Default Value
Add DEFAULT clause to column definition. See slide 4.
Implementing data constraints
Slide 4.
Domain constraints
Range constraints
Intrarelation constraints
Note the mySQL parses these statements, but does not implement check constraints!
Also, foreign key constraints are also ignored depending upon the DB engine used.
pf3
pf4

Partial preview of the text

Download CSIS 350 Lecture 23: SQL DDL and Data Manipulation and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CSIS 350 โ€“ Lecture 23

Announcments Homework on web site โ€“ due Thursday Any problems creating a database? CSIS 332 Java for the Internet Chapter 7 Last time we started looking at SQL DDL: The CREATE TABLE statement

  • define columns: name, type, constraints
  • define surrogate keys o SQL Server: IDENTITY o mySQL: AUTO_INCREMENT
  • using CONSTRAINT to define FOREIGN KEYs: o UPDATE NO ACTION, UPDATE CASCADE o DELETE NO ACTION, DELETE CASCADE *** You must define parent tables before child tables. This CREATE TABLE statement will fail if the ARTIST table does not exist. See Slides 1 & 2 : Create Table statement in SQL Server and mySQL. Note the AUTO_INCREMENT differences for defining surrogate key. Implementing minimum cardinalities See slide 3. Note that minimum cardinalities are enforced through the foreign key and with the ON UPDATE and ON DELETE clauses. Q: Why doesn't this slide show N:M relationships? Default Value Add DEFAULT clause to column definition. See slide 4. Implementing data constraints Slide 4. Domain constraints Range constraints Intrarelation constraints Note the mySQL parses these statements, but does not implement check constraints! Also, foreign key constraints are also ignored depending upon the DB engine used.

Use the following SQL to show the constraints on the ARTIST table: SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS where table_name = 'ARTIST'; Alter Statement The ALTER TABLE statement is used to modify the definition of a table after it has been created:

  • Add/Remove columns
  • Change column properties
  • Add/Remove constraints Examples: Alter Table Artist add Style varchar(20) null; Describe artist; Alter Table Artist drop column Style; Describe artist; alter table Artist add constraint Removing Tables The DROP TABLE statement removes a table and all of its data from a database. DROP TABLE ARTIST; If another table contains a FK referencing this table, the drop table command will fail. If you really want to drop the table, you must first either:
  1. Drop the child table, or
  2. Drop the FK constraint from the child table [Note that this is not always true in mySQL, which can use different DB engines. FK constraints are not enforced in all of them!] SQL DML SQL DML (data manipulation language) is used to store and modify data in the tables we have created. We're going to look at three statements: insert, update, and delete. Insert The SQL INSERT statement adds a new row to a table. Form: INSERT INTO ( ) VALUES ( <valueList );

    Derive the following in steps based on student input โ€“ try to derive subquery first update purchaseOrder set total = (select sum(unitPrice * quantity) from orderLine where orderLine.orderNumber = purchaseOrder.orderNumber group by orderNumber) where orderNumber = 1; Delete The SQL DELETE statement removes one or more rows from a table. DELETE FROM <table_name> where <where_clause>; Examples: DELETE FROM CUSTOMER WHERE CustomerID = 1000; DELETE FROM CUSTOMER WHERE CustomerID BETWEEN 1000 AND 1100; DELETE FROM CUSTOMER; Moral: Be Careful! You can delete every row from a table without much effort! Note that DELETES may be cascaded or prohibited, depending upon constraints placed on FKs. In-class exercise Exercises 7.7, 7.9 โ€“ 7.15, 7.19 โ€“ 7.23. Note that problem 7.19 has an error, and should refer to problems 7.17 and 7.18.