Matakuliah : T0413/Current Popular IT II Tahun : 2007 Maintaining Referential Integrity Pertemuan 2 AGENDA: •Constraining the Values of Data •Maintaining Referential Integrity •Entering, Deleting, and Changing Data Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : 4 – 6 2 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 3 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. 4 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)); 5 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); 6 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’)); 7 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 LuckyBarcelona CHECK (comm < .15 OR city = ‘Barcelona’)); • Dropping constraints: ALTER TABLE Salespeople DROP CONSTRAINT LuckyBarcelona; 8 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 9 Maintaining Referential Integrity • • Foreign Key and Parent Key Declaring Columns As Foreign Key: 1. FOREIGN KEY as a table constraint CREATE 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. 10 Maintaining Referential Integrity (cont’d) 2. FOREIGN KEY as a column constraint CREATE 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) 11 Maintaining Referential Integrity (cont’d) 3. Using FOREIGN KEY to predetermined valid input values CREATE 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)); 12 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 13 Entering, Deleting, and Changing Data The INSERT statement • Entering values into tables • Syntax: INSERT INTO table name VALUES (value,…); • Example: INSERT INTO Salespeople VALUES (1001, ‘Peel’, ‘London’, .12); • Inserting NULLs value INSERT INTO Salespeople VALUES (1001, ‘Peel’, NULL, .12); 14 Entering, Deleting, and Changing Data (cont’d) • Naming columns for INSERT INSERT INTO Customers (city, cname, cnum) VALUES (‘London’, ’Hoffman’, 2001); • Inserting the Result of a Query INSERT INTO Londonstaff SELECT * FROM Salespeople WHERE city = ‘London’; INSERT INTO Londonstaff SELECT snum, sname, city, comm * 1.1 FROM Salespeople WHERE city = ‘London’; 15 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’; 16 Entering, Deleting, and Changing Data (cont’d) The UPDATE Statement • Changing field values • Example: UPDATE Customers SET rating = 200; • Updating only certain rows UPDATE Customers SET rating = 200 WHERE snum = 1001; 17 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 UPDATE UPDATE Salespeople SET comm = comm * 2 WHERE city = ‘London’; • Updating to NULL value UPDATE Customers SET rating = NULL Where city = ‘London’; 18 End of Maintaining Referential Integrity Thank you 19