Presentation is loading. Please wait.

Presentation is loading. Please wait.

CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.

Similar presentations


Presentation on theme: "CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together."— Presentation transcript:

1 CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together in such a fashion that the meaning of the information is maintained despite attempts to change a subset of the data Contents –Supported Data Types –Column Attributes –Domains –Table definition –Practical 6-1 –Referential Integrity further clarified –Further ANSI Column Attributes –Referential Integrity Rules –CREATE TABLE using DRI –Other methods to safeguard data –Practical 6-2

2 CDT/2 SQL92 Standard Data Types FLOAT (precision) CHARACTER (Char) CHARACTER VARYING (VarChar) CHARACTER (Char) CHARACTER VARYING (VarChar) ‘Fred Smith’ ‘Blue Widgets’ INTEGER SMALLINT INTEGER SMALLINT 1234 -98321 NUMERIC (precision, scale) DECIMAL (precision, scale) NUMERIC (precision, scale) DECIMAL (precision, scale) 123.4567 2.4319E38 0.234E-35 BIT BIT VARYING BIT BIT VARYING 110101 DATE TIME TIMESTAMP DATE TIME TIMESTAMP DATE ‘1995-10-23’ TIME ‘17:45:45.75’

3 CDT/3 ANSI Column Attributes - 1 NOT NULL (Mandatory column) NULL (Nullable/Optional column) DEFAULT { Literal | Scalar System function } –DEFAULT ‘Sales’ –DEFAULT CURRENT_DATE

4 CDT/4 ANSI Column Attributes - 2 UNIQUE –used for non-key column values –e.g. dept_name, driver_licence_no –NOT used for Primary Key column CHECK –specifies a range check for this column (similar to a WHERE clause) –must be resolvable by examining just the row being modified

5 CDT/5 CREATE TABLE contact ( company_noInteger NOT NULL, contact_codeChar(3)NOT NULL, nameVarChar(20) NULL, job_titleVarChar(25) NULL, telVarChar(20) NULL, notesVarChar(60) NULL ) Note, 6 columns, but 5 commas! – it’s a comma separated list in parentheses, often seen in SQL To remove the table, and all its columns & constraints: DROP TABLE contact CREATE TABLE

6 CDT/6 Adding, modifying and deleting columns Features rarely supported –Deleting columns from existing tables –Adding NOT NULL columns to existing table –Changing a column’s length or nullability ALTER TABLE ALTER TABLE dept ADD location char(30)NULL ALTER TABLE sale ALTER order_value DROP DEFAULT ALTER TABLE salesperson DROP sales_target

7 CDT/7 Ch8Practical1 - Basic DDL Create and Alter a Table corresponding to the description supplied in the practical file

8 CDT/8 What is Referential Integrity? The Referential Integrity question “What should happen if sales exist for company 2 and someone tries to delete company 2 from the company table?” –Should it be disallowed? –Should the system delete the sales as well? –Should it keep the sales but change the company_no to NULL? ? companysale

9 CDT/9 Key definition PRIMARY KEY (CREATE or ALTER TABLE) [CONSTRAINT constraintname] PRIMARY KEY (column1,column2,..) FOREIGN KEY (CREATE or ALTER TABLE) [CONSTRAINT constraintname] FOREIGN KEY (column1,column2,..) REFERENCES tablename[(column1,column2,..) [ON DELETE|UPDATE CASCADE|RESTRICT|SET NULL|SET DEFAULT]

10 CDT/10 Referential Integrity Rules Foreign key column values added to or updated in a dependent table are checked against the parent table’s Primary Key Rows to be deleted from the parent table are checked for usage in the dependent table. SQL2 supports 4 behaviour options: RESTRICT, CASCADE, SET DEFAULT and SET NULL Many DBMS’s do not support all options e.g. MS SQL Server only offers RESTRICT

11 CDT/11 CREATE TABLE using DRI CREATE TABLE contact ( company_noINTEGER NOT NULL, contact_codeCHAR(3)NOT NULL, nameVARCHAR(20) NULL, job_titleVARCHAR(25) NULL, telVARCHAR(20) NULL, notesVARCHAR(60) NULL, PRIMARY KEY (company_no, contact_code), FOREIGN KEY (company_no) REFERENCES company ON DELETE CASCADE-- not in SQL Server ON UPDATE RESTRICT-- not in SQL Server )

12 CDT/12 Other methods to safeguard data IDDept Salary 100 234 20000 101 124 30000 102 234 20000 212 100 5000 213 124 25000 214 124 21000 282 1008000 IDDept Salary 100 234 20000 101 124 30000 102 234 20000 212 100 5000 213 124 25000 214 124 21000 282 1008000 Changes If changes occur, execute this SQL If changes occur, execute this SQL

13 CDT/13 Ch8Practical2 - Referential Integrity Follow the instructions in this tutorial to define primary keys, foreign keys and to observe resultant behaviour.

14 CDT/14 SUMMARY Tables –Used to store data one row per entity occurrence –Each row is divided into column definitions which together describe the entity occurrence –Column definitions –Control the type of data that is inserted into a given column/ row intersection Column attributes –Enable us to define valid entries into a column –Nulls, Defaults, Checks, Uniqueness etc Tables can be Altered, after populating them with data Referential Integrity is important –It ensures data is updated in multiple locations when necessary or that updates are prevented, depending on business requirements –SQL92 syntax allows for the definition of DRI via Primary and Foreign Key constraints, but DRI is not fully supported


Download ppt "CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together."

Similar presentations


Ads by Google