1. r
Lecture 1 �-
·-·
Implementing
Data Integrity
""''"�•••J2c-,..,..,. ,,_.,.,..,,.-,..,�,.___,..,,,_ ......,..,,,,,.�..:,,,.,,,__,.3,,,,,.,,..,.,.;.iiii.:�--:,,i�....lf±'-"
·"".<•;..;:•,. ··.. ;-�,.,,..,.,.___,.__.;,;;;,;..,c-· "-"' >c-::-<!.,_._�..x•>-� .,......_..,,__,...,_,.L, ;, ,,.,; f
Dr. Mohammed Zayed-
5. lf a primary key constraint is defined on a column that already contains. data, then the
existing data in the column is screened. If any duplicate values are found, thenthe
primary key constraint is rejected. The syntax of applying the primary key constraint
when creating tabTe is:
CREATE TJI.BLE table name
col name [CONSTRAINT constraint name PRIMARY KEY
[CLUSTEREDINONCLUSTERED)
col name [, col name [, col name [, ...] ) )
where,
constraint_name specifies the name of the constraint to be created.
CLUSTERED I NONCLUSTERED are keywords that specify if a clustered or a nonclustered
index is to be created for the primary key constraint.
col_name specifies the name ofthe column(s) on which the primary key constraint is to
.be defmed.
Note
You will learn more about indexes in Chapter 6.
In the preceding example of the EmployeeLeave table, you can add a primaiy .key
constraint, while creating the table. You can set the EmployeeID and the LeaveStartDate
columns ofthe EmployeeLeave table as a composite primary key. You can use the
following·statement to apply the·primary key constraint:
CREATE TABLE HumanResources.EmployeeLeave
ErnployeeID int,
LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY
KEY(EmployeeID, LeaveStartDate),
---.-.---
r - -···· --
The preceding statement creates the EmployeeLeave table with a composite primary key
constraint on EmployeeID and LeaveStartDate. The name ofthe constraint is
cpk:LeaveStartDate.
Managing Databases and Tab_les 4.15
13. LeaveType char(2) CONSTRAINT chkLeave CHECK(LeaveType
IN('CL', 'SL', 'PL')) CONSTRAINT chkDefLeave DEFAULT 'PL'
Just a minute:
You want to create a rule, rule1, which allows the user to enter any ofthefour values:
Tea, Coffee, Soup, or Miranda in a column. Which command shouldyou execuie?
Answer:
CREATE RULE rulel
AS@TypeRule IN ('Tea', 'Coffee', 'Soup', 'Miranda')
, }
Managing Databases and Tables 4.23