Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.

Similar presentations


Presentation on theme: "Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15."— Presentation transcript:

1 Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15

2 Dec 8, 2003Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNumber int, sName varchar (20), dept char (2), CONSTRAINT key PRIMARY KEY (sNumber), CONSTRAINT uniqueName UNIQUE (sName));

3 Dec 8, 2003Murali Mani Unique vs. primary keys Attribute values may be null even if they are declared unique (primary key attributes should not be null). We can have any number of unique constraints for a table (only one primary key constraint can be defined for a table).

4 Dec 8, 2003Murali Mani Foreign Keys: Referential Integrity Constraints Specified for a table as CONSTRAINT [ ] FOREIGN KEY ( ) REFERENCES ( ) eg: FOREIGN KEY R (a, b) REFERENCES S (a, b) Requires (a, b) be unique or primary key of S. Consider a row in R with values of a as a1, and b as b1 where a1, b1 are both non-null. There must be a row in S with values for (a, b) as (a1, b1).

5 Dec 8, 2003Murali Mani Maintaining referential integrity: Inserts/Deletes/Updates Default: reject any modification that violates the constraints. We can specify other policies for delete/update as set null/cascade. Eg: FOREIGN KEY (name) references Student (sName) ON DELETE SET NULL ON UPDATE CASCADE SET NULL: if the update violates foreign key constraint, the foreign key attributes are set to null. CASCADE: if the update violates foreign key constraint, the foreign key attributes are modified.

6 Dec 8, 2003Murali Mani Constraints on Attributes We can specify attributes as NULL or NOT NULL. Eg: sName varchar (20) NOT NULL We can specify CHECK constraints. Eg: gender char (1) CHECK (gender IN (‘F’, ‘M’)) salary int CHECK (salary >= 60000) CONSTRAINT c1 check (salary >= 60000) professor varchar (30) CHECK (professor IN select pname from professor); Constraints are checked only when inserting/updating the table.

7 Dec 8, 2003Murali Mani Constraints for a tuple CONSTRAINT CHECK ( ) Eg: CREATE TABLE Marks (midTerm int, final int, CHECK (midTerm + final >= 25)); Note: Constraints are checked only during insertion/update

8 Dec 8, 2003Murali Mani Altering Constraints ALTER TABLE ADD CONSTRAINT ALTER TABLE DROP CONSTRAINT

9 Dec 8, 2003Murali Mani Constraints on the entire relational schema Assertions: CREATE ASSERTION CHECK ( ) Eg: CREATE ASSERTION CHECK ( NOT EXISTS (SELECT * FROM PROFESSOR WHERE salary < 60000)); Condition is any condition that can appear in WHERE clause For any database modification, the assertion must be true.

10 Dec 8, 2003Murali Mani Triggers (Event, Condition, Action rules) We specify triggers as Event, Condition, Action rules. When event occurs, and condition is satisfied, the action is performed.

11 Dec 8, 2003Murali Mani Triggers CREATE TRIGGER BEFORE | AFTER INSERT|DELETE|UPDATE [OF ] ON | [REFERENCING OLD AS, NEW AS ] FOR EACH ROW [WHEN ( )] ;

12 Dec 8, 2003Murali Mani Triggers: example CREATE TRIGGER t1 AFTER INSERT ON Student REFERENCING NEW as newTuple FOR EACH ROW WHEN (newTuple.professor IS NOT NULL) BEGIN INSERT INTO Professor (pname) values (:newTuple.professor); END;. run;

13 Dec 8, 2003Murali Mani Statement Level: Triggers CREATE TRIGGER t1 AFTER INSERT ON Student REFERENCING NEW as newTable BEGIN INSERT INTO Professor (pname) SELECT professor FROM Student where (professor IS NOT NULL AND professor NOT IN (SELECT pname FROM Professor)); END;. run;


Download ppt "Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15."

Similar presentations


Ads by Google