Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.

Similar presentations


Presentation on theme: "DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language."— Presentation transcript:

1 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language II

2 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-2 Data Definition Language (DDL) Subset of SQL that creates, deletes & modifies tables & columns –Create –Drop –Alter Structure of database is dynamic

3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-3 Creating Tables CREATE TABLE name Column(s): name, type Data Integrity Constraint(s): default, required, unique, domain Primary and Foreign keys

4 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-4 Create Table Example CREATE TABLE ORDERS ( ORDER_NUM INTEGER, ORDER_DATE DATE, CUSTINTEGER, REPINTEGER, MFRCHAR(3), PRODUCTVARCHAR2(5), QTYINTEGER, AMOUNTNUMBER(9,0) );

5 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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

6 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-6 Alter Table: Columns Can make many table & column modifications. Alter Table Orders Add (column type constraints); Modify (column type constraints); Drop (column);

7 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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) );

8 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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 );

9 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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) );

10 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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) );

11 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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) );

12 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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.

13 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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!

14 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-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

15 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-15 Foreign Key in ORDERS Relation Descriptions: PRODUCTS (Mfr_ID, Product_ID, Description, …) CUSTOMERS (Cust_Num, Company, …) ORDERS (Order_Num, Order_Date, Cust, Rep, Mfr, Product, Qty, Amount) Referential Integrity Constraints: Orders.Cust must first exist in Customers.Cust_Num Orders.(Mfr, Product) must first exist in Products.(Mfr_ID,Product_ID)

16 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-16 Foreign Key in ORDERS FOREIGN KEY (Cust) references Customers(Cust_Num) FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID)

17 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-17 Foreign Key in ORDERS 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), FOREIGN KEY (Cust) references Customers(Cust_Num), FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID) );

18 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-18 Alter Table: Keys ALTER TABLE ORDERS ADD CONSTRAINT Cust_fk FOREIGN KEY (Cust) references customers(Cust_Num); Prod_fk FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID);

19 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-19 On Delete Specifies action to take on delete of a parent record On Delete Cascade – delete child rows automatically Restrict – parent can be deleted if no children (In Oracle, this is the default) Set Null – set child rows to null

20 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-20 Order of Drops and Creates When Dropping tables – Drop child tables first, then parent(s). When Creating tables – Create parent tables first, then child(ren).


Download ppt "DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language."

Similar presentations


Ads by Google