View
220
Download
1
Category
Tags:
Preview:
Citation preview
Maintaining Referential IntegrityPertemuan 2
Matakuliah : T0413/Current Popular IT IITahun : 2007
2
AGENDA:•Constraining the Values of Data •Maintaining Referential Integrity •Entering, Deleting, and Changing Data
Book:Mastering SQL by Martin GruberSybex (2000)Chapter : 4 – 6
3
Constraining the Values of Data• Declaring constraints
– Values that can be entered into its columns on a table– SQL will generally reject any values that violate the
criteria you created before
• Syntax:CREATE TABLE tablename
({columnname datatype column constraint} …,[table constraint (columnname ,…) ,…];
• Alternative : ALTER TABLE
4
Constraining the Values of Data (cont’d)
• Using Constraints to Exclude NULLs– To prevent a column from permitting NULLs as a value– Use CREATE TABLE statement with NOT NULLs
constraint
• Example:CREATE TABLE Salespeople
(snum INTEGER NOT NULL,sname CHAR(10) NOT NULL,city CHAR(10),comm DECIMAL);
• Column with NOT NULL constraint must be assigned values in every INSERT clause.
5
Constraining the Values of Data (cont’d)
• Specifying the Primary Key• Example:
CREATE TABLE Salespeople(snum INTEGER NOT NULL PRIMARY KEY,sname CHAR(10) NOT NULL,city CHAR(10),comm DECIMAL);
• Primary Keys of More Than One Column• Example:
CREATE TABLE Namefield(firstname CHAR(10) NOT NULL,lastname CHAR(10) NOT NULL,city CHAR(10),PRIMARY KEY (firstname, lastname));
6
Constraining the Values of Data (cont’d)
• Making Sure Values Are Unique– Using UNIQUE constraint– UNIQUE vs. PRIMARY KEY:
• PRIMARY KEY constraint can only be used on one column or group of columns. UNIQUE constraint may be used any number of times.
• PRIMARY KEY column may not contain NULLs, UNIQUE constraint may.
– Example:CREATE TABLE Salespeople
(snum INTEGER NOT NULL PRIMARY KEY,sname CHAR(10) NOT NULL UNIQUE,city CHAR(10),comm DECIMAL);
7
Constraining the Values of Data (cont’d)
• Checking Column Values– Using CHECK to Predetermine Valid Input Values– Example:
CREATE TABLE Salespeople(snum INTEGER NOT NULL PRIMARY KEY,sname CHAR(10) NOT NULL,city CHAR(10) CHECK,(city IN (‘London’, ‘New York’, ‘San Jose’, ‘Barcelona’)),comm DECIMAL CHECK (comm < 1));
– Check Conditions Based on Multiple Columns– Example:
CREATE TABLE Salespeople(snum INTEGER NOT NULL PRIMARY KEY,sname CHAR(10) NOT NULL UNIQUE,city CHAR(10),comm DECIMAL,CHECK (comm < .15 OR city = ‘Barcelona’));
8
Constraining the Values of Data (cont’d)
• Naming and Dropping Constraints• Example:
CREATE TABLE Salespeople(snum INTEGER NOT NULL PRIMARY KEY,sname CHAR(10) NOT NULL UNIQUE,city CHAR(10),comm DECIMAL,CONSTRAINT LuckyBarcelonaCHECK (comm < .15 OR city = ‘Barcelona’));
• Dropping constraints:ALTER TABLE Salespeople DROP CONSTRAINT
LuckyBarcelona;
9
Constraining the Values of Data (cont’d)
• Assigning Default Values• Example:
CREATE TABLE Salespeople(snum INTEGER NOT NULL PRIMARY KEY,sname CHAR(10) NOT NULL,city CHAR(10) DEFAULT = ‘New York’,comm DECIMAL CHECK (comm < 1));
• Default Values and NULLs
10
Maintaining Referential Integrity• Foreign Key and Parent Key• Declaring Columns As Foreign Key:
1. FOREIGN KEY as a table constraintCREATE TABLE Customers
(cnum INTEGER NOT NULL PRIMARY KEY,cname CHAR(10),city CHAR(10),snum INTEGER,FOREIGN KEY (snum) REFERENCES Salespeople(snum));
Note: Customers table with snum defined as a foreign key referencing the Salespeople table.
11
Maintaining Referential Integrity (cont’d)
2. FOREIGN KEY as a column constraintCREATE TABLE Customers
(cnum INTEGER NOT NULL PRIMARY KEY,cname CHAR(10),city CHAR(10),snum INTEGER REFERENCES Salespeople(snum));
Note: Customers table with snum as a foreign key whose parent key is Salespeople.snum.It is equivalent to this table constraint:FOREIGN KEY (snum) REFERENCES Salespeople (snum)
12
Maintaining Referential Integrity (cont’d)
3. Using FOREIGN KEY to predetermined valid input valuesCREATE TABLE Officecities
(office (CHAR(10) NOT NULL PRIMARY KEY);
CREATE TABLE Salespeople ( snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL, city CHAR(10) REFERENCES Officecities, comm DECIMAL CHECK (comm < 1));
13
Maintaining Referential Integrity (cont’d)
• PRIMARY vs. UNIQUE Parent Keys• Multicolumn Foreign Keys• Referential Triggered Actions :
– how parent-key changes affect foreign keys– It enable you to specify when creating a FOREIGN KEY
constraint what you would like to have happen to the foreign-key values when the referenced parent-key values are deleted or changed.
– UPDATE : what happens to a foreign key value when the parent key value it references is changed.
– DELETE : what happens to a foreign key value when the parent key row it references is deleted.
– Solutions : CASCADE, SET NULL, SET DEFAULT, NO ACTION
14
Entering, Deleting, and Changing DataThe INSERT statement• Entering values into tables• Syntax:
INSERT INTO table nameVALUES (value,…);
• Example:INSERT INTO SalespeopleVALUES (1001, ‘Peel’, ‘London’, .12);
• Inserting NULLs valueINSERT INTO SalespeopleVALUES (1001, ‘Peel’, NULL, .12);
15
Entering, Deleting, and Changing Data (cont’d)
• Naming columns for INSERTINSERT INTO Customers (city, cname, cnum)VALUES (‘London’, ’Hoffman’, 2001);
• Inserting the Result of a QueryINSERT INTO Londonstaff
SELECT * FROM SalespeopleWHERE city = ‘London’;
INSERT INTO LondonstaffSELECT snum, sname, city, comm * 1.1FROM SalespeopleWHERE city = ‘London’;
16
Entering, Deleting, and Changing Data (cont’d)
The DELETE statement• Removing Rows from tables• DELETE FROM Salespeople;• DELETE FROM Salespeople WHERE snum = 1003;• DELETE FROM Salespeople WHERE city =
‘London’;
17
Entering, Deleting, and Changing Data (cont’d)
The UPDATE Statement• Changing field values• Example:
UPDATE CustomersSET rating = 200;
• Updating only certain rowsUPDATE Customers
SET rating = 200WHERE snum = 1001;
18
Entering, Deleting, and Changing Data (cont’d)
• Updating multiple columns at once UPDATE Salespeople
SET sname = ‘Gibson’, city = ‘Boston’, comm = .10
WHERE snum = 1004;
• Using value expression in UPDATEUPDATE Salespeople
SET comm = comm * 2WHERE city = ‘London’;
• Updating to NULL valueUPDATE Customers
SET rating = NULLWhere city = ‘London’;
19
End ofMaintaining Referential Integrity
Thank you
Recommended