Creating Tables and Enforcing Data Integrity: Objectives
Creating Tables and Enforcing Data Integrity: Objectives
Objectives
In this lesson, you will learn to: Create tables Insert rows into tables Delete tables Create user-defined datatype List various types of data integrity Add the following constraints to tables: PRIMARY KEY UNIQUE FOREIGN KEY CHECK DEFAULT
SQL/Lesson 5/Slide 1 of 52
Attributes Newspaper Code Newspaper Name Region Type of Newspaper Contact Person HO Address City State Zip Country Code Fax Phone
Data 0001 Texas Times Texas General Jackson Demello 4723 West Alabama Houston Texas 77015-4568 001 (713)451-6797 (713)451-6850 SQL/Lesson 5/Slide 2 of 52
SQL/Lesson 5/Slide 3 of 52
vTypeOfNewsPaper varchar(20),
vContactPerson varchar(35), vHOAddress varchar(35),
SQL/Lesson 5/Slide 5 of 52
SQL/Lesson 5/Slide 6 of 52
Press F5 to execute
SQL/Lesson 5/Slide 7 of 52
Syntax
INSERT [INTO] table_name [column_list] VALUES (values_list)
SQL/Lesson 5/Slide 8 of 52
SQL/Lesson 5/Slide 10 of 52
SQL/Lesson 5/Slide 11 of 52
SQL/Lesson 5/Slide 12 of 52
SQL/Lesson 5/Slide 13 of 52
SQL/Lesson 5/Slide 14 of 52
SQL/Lesson 5/Slide 15 of 52
SQL/Lesson 5/Slide 16 of 52
Syntax
sp_addtype name, [system_data_type] [, 'null_type']
SQL/Lesson 5/Slide 17 of 52
sp_droptype type
SQL/Lesson 5/Slide 18 of 52
SQL/Lesson 5/Slide 19 of 52
SQL/Lesson 5/Slide 20 of 52
SQL/Lesson 5/Slide 22 of 52
SQL/Lesson 5/Slide 23 of 52
SQL/Lesson 5/Slide 24 of 52
Action:
In the Query Analyzer window, type: sp_help typNewspaperCode Press F5 to execute
SQL/Lesson 5/Slide 25 of 52
SQL/Lesson 5/Slide 26 of 52
SQL/Lesson 5/Slide 27 of 52
Ensures that the values of the foreign key match with the value of the corresponding primary key
User-Defined Integrity
Refers to a set of rules specified by a user, which do not belong to the entity, domain, and referential integrity categories
SQL/Lesson 5/Slide 28 of 52
SQL/Lesson 5/Slide 29 of 52
Table level
SQL/Lesson 5/Slide 30 of 52
SQL/Lesson 5/Slide 32 of 52
Is defined on a column or a set of columns whose values uniquely identify rows in a table
Syntax
[CONSTRAINT constraint_name PRIMARY KEY [CLUSTERED|NONCLUSTERED](col_name [, col_name [, col_name [, ]]])]
SQL/Lesson 5/Slide 33 of 52
SQL/Lesson 5/Slide 34 of 52
SQL/Lesson 5/Slide 35 of 52
SQL/Lesson 5/Slide 36 of 52
It is used to assign a constant value to a column Only one DEFAULT constraint can be created for a column The column cannot be an IDENTITY column
SQL/Lesson 5/Slide 37 of 52
SQL/Lesson 5/Slide 38 of 52
SQL/Lesson 5/Slide 40 of 52
The phone number format can be given using the CHECK constraint The country code can be given using the DEFAULT constraint
The newspaper code can be made the primary key using the PRIMARY KEY constraint
SQL/Lesson 5/Slide 41 of 52
The cNewsAdNo column can be made the primary key using the PRIMARY KEY constraint The cNewspaperCode attribute can be made the foreign key using the FOREIGN KEY constraint
SQL/Lesson 5/Slide 42 of 52
vTypeOfNewspaper varchar(20),
vContactPerson varchar(35), vHOAddress varchar(35),
SQL/Lesson 5/Slide 43 of 52
DEFAULT(001),
cFax char(15), cPhone char(15) CONSTRAINT chkPhone CHECK(cPhone LIKE('([0-9][0-9][0-9])[0-9][0-9][0-9]-[0-9][09][0-9][0-9]')))
SQL/Lesson 5/Slide 44 of 52
SQL/Lesson 5/Slide 46 of 52
cPhone
The row would not be inserted, as the telephone number contains character data The row would be inserted, as this is a valid format for a telephone number The row would be inserted with 005 in the cCountryCode attribute The row when inserted would give an error, since 0001 already exists for cNewspaperCode in the Newspaper table The row would be inserted, as 0090 for cNewspaperCode does not exist in the Newspaper table
cPhone
(212)345-2467
cCountrycode
005
cNewspaperCode
0001 (Already present in Newspaper table) 0090 (not present in the Newspaper table)
cNewspaperCode
SQL/Lesson 5/Slide 47 of 52
cNewsAdNo
The row when inserted would give an error since 0001 is already present for the cNewsAdNo attribute in the NewsAd table The row would be inserted, since 0035 does not exist in the NewsAd table The row,when inserted would give an error, as 0045 does not exist for cNewspaperCode in the Newspaper table The row would be inserted, since 0001 for cNewspaperCode does not exist in the Newspaper table
cNewsAdNo
cNewspaperCode
cNewspaperCode
SQL/Lesson 5/Slide 48 of 52
SQL/Lesson 5/Slide 50 of 52
SQL/Lesson 5/Slide 52 of 52