Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.

Similar presentations


Presentation on theme: "SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley."— Presentation transcript:

1 SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley 2002)

2 DDL Overview Data types –Char (n), varchar (n), integer, decimal(m,n), date, time Domains (and domain constraints) Entity and Referential Integrity Constraints –Primary key and Foreign Key –On Update and On Delete conditions Other (enterprise) constraints –Assertions Commands –Create, Alter, Drop –… Schema (database), Domain, Table, View –Schemas and Views cannot be altered

3 DataType examples Char(5)‘hello’, ‘today’, ‘12345’ Varchar(5)‘hi’, ‘hello’, ‘ciao’ Integer17, 12345, 100000000 Decimal(5,2)99999.99, 25.00, 0.12 Date2000-01-01, 2002-09-21 Time13:01:00

4 Integrity Enhancement Features Required Data(NOT NULL) –… position VARCHAR(10) NOT NULL Domain constraints (CHECK) –… sex CHAR … CHECK (sex IN (‘M’,’F’)) Entity Integrity –PRIMARY KEY (attributes) –UNIQUE (attributes) -- use for alternate keys Referential Integrity –FOREIGN KEY …

5 Foreign Key constraints FOREIGN KEY (attributes) REFERENCES (table) ON DELETE (or ON UPDATE) –Cascade Delete parent tuple, and all child tuples –Set Null Delete parent tuple, set all child attributes null –Set Default Delete parent tuple, set all child attributes to default –No Action Do not delete

6 Create Domain CREATE DOMAIN name AS datatype [DEFAULT value ] [CHECK (condition) ] CREATE DOMAIN ClassYear AS CHAR[4] DEFAULT ‘2006’ CHECK (VALUE IN (‘2003’, ‘2004’, ‘2005’, ‘2006’); ClassYear can now be used as a data type

7 Create Table CREATE TABLE name ( Attribute DataType conditions, Attribute DataType conditions, … CONSTRAINT name check-condition, … PRIMARY KEY (attributes), Foreign key constraints Check conditions )

8 Example Relation (PhoneBook) FirstLastDeptEmailPhoneTitle ObieSlotterbeckCSObie5275Prof IrinaLomonosovCSLomonosovI5002Inst EllenWalkerCSWalkerEL5250Prof AngelaGuercioCSGuercioA6048Asst JaneRoseWECRoseJP5163Dean MarioRenziECNRenziMA5420Prof

9 PhoneBook table attributes… CREATE TABLE PhoneBook ( Emailvarchar(32)NOT NULL, Firstvarchar(32), Lastvarchar(32)NOT NULL, PhonePhNum, Title ProfTitle, Deptvarchar(4),

10 Phonebook table constraints PRIMARY KEY (Email), FOREIGN KEY (Dept) REFERENCES Department ON UPDATE CASCADE, ON DELETE SET NULL );

11 Domains CREATE DOMAIN PhNum AS Integer CHECK (VALUE >5000 and VALUE < 6999); CREATE DOMAIN ProfTitle AS CHAR(4) CHECK (VALUE IN (Inst, Asst, Assc, Prof, Dean));

12 Altering a Table ALTER TABLE –ADD attribute description –DROP attribute [RESTRICT | CASCADE] – ADD constraint conditions –DROP constraint –ALTER attribute SET DEFAULT value –ALTER attribute DROP DEFAULT

13 Creating a View CREATE VIEW name [(new column names)] AS SELECT … CREATE VIEW Locations AS SELECT fName, lName, dept, bldg FROM PhoneBook, Department WHERE dept = Department.name

14 Notes on the end of Ch. 6 Section 6.5 will be covered along with Transactions Section 6.6 will be covered along with Security


Download ppt "SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley."

Similar presentations


Ads by Google