© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language
© 2002 by Prentice Hall 2 Data Definition Language (DDL) Subset of SQL that creates, deletes & modifies tables & columns –Create –Drop –Alter Structure of database is dynamic
© 2002 by Prentice Hall 3 Creating Tables CREATE TABLE name Column(s): name, type Data Integrity Constraint(s): default, required, unique, domain Primary and Foreign keys
© 2002 by Prentice Hall 4 Create Table Example CREATE TABLE ORDERS ( ORDER_NUM INTEGER, ORDER_DATE DATE, CUSTINTEGER, REP INTEGER, MFRCHAR(3), PRODUCTVARCHAR2(5), QTYINTEGER, AMOUNTNUMBER(9,0) );
© 2002 by Prentice Hall 5 Data Types CHAR (len)fixed length character strings VARCHAR2 (len)variable length character strings INTinteger numbers NUMBER (w, d)decimal numbers DATEcalendar date BLOBbinary object, up to 4 GB CLOBtext object, up to 4 GB
© 2002 by Prentice Hall 6 Alter Table: Columns Can make many table & column modifications. Alter Table Orders Add (column type constraints); Modify (column type constraints); Drop (column);
© 2002 by Prentice Hall 7 Defaults Uses the DEFAULT when insert doesn’t specify a value CREATE TABLE ORDERS ( ORDER_NUM integer, ORDER_DATE date DEFAULT sysdate, CUSTinteger, REPinteger, MFRchar(3), PRODUCTvarchar2(5), QTYinteger, AMOUNTnumber(9,0) );
© 2002 by Prentice Hall 8 Required Data If data is required, then NULL values NOT allowed. CREATE TABLE ORDERS ( ORDER_NUM integer NOT NULL, ORDER_DATE date default sysdate NOT NULL, CUSTinteger NOT NULL, REPinteger NOT NULL, MFRchar(3) NOT NULL, PRODUCTvarchar2(5) NOT NULL, QTYinteger NOT NULL, AMOUNTnumber(9,0) NOT NULL );
© 2002 by Prentice Hall 9 Uniqueness Whether or not the column is required to be UNIQUE. CREATE TABLE ORDERS ( ORDER_NUM integer UNIQUE, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger, AMOUNTnumber(9,0) );
© 2002 by Prentice Hall 10 Domain Use the CHECK command to enforce a domain… CREATE TABLE ORDERS ( ORDER_NUM integer unique, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger CHECK (QTY <> 0), AMOUNTnumber(9,0) );
© 2002 by Prentice Hall 11 Primary Key The unique identifier(s) for each row use the PRIMARY KEY command… CREATE TABLE ORDERS ( ORDER_NUM integer, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger check (qty <> 0), AMOUNTnumber(9,0), CONSTRAINT order_pk PRIMARY KEY (order_num) );
© 2002 by Prentice Hall 12 Unique vs Primary Key If the primary key is one identifier, then use of UNIQUE command will suffice. If the primary key is composite, then must use PRIMARY KEY command.
© 2002 by Prentice Hall 13 Composite Primary Key CREATE TABLE ORDERS ( ORDER_NUM integer unique, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger check (qty <> 0), AMOUNTnumber(9,0), CONSTRAINT order_pk PRIMARY KEY (order_date, cust) ); Using UNIQUE with order_date, cust will cause problems!
© 2002 by Prentice Hall 14 Foreign Key Specifies the relationship to another parent table(s). –Column(s) that form the foreign key –Table that is referenced by the foreign key –Name of the relationship (optional) –Optional delete, update & check constraint rules
© 2002 by Prentice Hall 15 Foreign Key in ORDERS Relation Descriptions: CUSTOMERS (Cust_Num, Company, …) SALESREPS (Empl_Num, Name, …) PRODUCTS (Mfr_ID, Product_ID, Description, …) ORDERS (Order_Num, Order_Date, Cust, Rep, Mfr, Product, Qty, Amount) Referential Integrity Constraints: Cust in Orders must exist in Cust_Num in Cust Rep in Orders must first exist in Empl_Num in Salesreps (Mfr, Product) in Orders must exist in (Mfr_ID, Product_ID) in Products
© 2002 by Prentice Hall 16 Foreign Key in ORDERS FOREIGN KEY (Cust) references Customers(Cust_Num) FOREIGN KEY (Rep) references Salesreps(Empl_Num) FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID)
© 2002 by Prentice Hall 17 Alter Table: Keys ALTER TABLE ORDERS ADD CONSTRAINT Cust_fk FOREIGN KEY (Cust) references customers(Cust_Num); Rep_fk FOREIGN KEY (Rep) references Salesreps(Empl_Num); Prod_fk FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID);
© 2002 by Prentice Hall 18 On Delete Specifies action to take on delete of a parent record On Delete Cascade – delete child rows automatically Set Null – set child rows to null Restrict – parent can be deleted if no children