Download presentation
Presentation is loading. Please wait.
1
cs3431 Constraints Sections 6.1 – 6.5
2
cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum), CONSTRAINT uk1 UNIQUE (sname), CONSTRAINT FOREIGN KEY (prof) REFERENCES Professor (pNum) ON DELETE SET NULL);
3
cs3431 Unique vs. primary keys Primary key attributes should not be null Attribute values may be null even if they are declared unique. We can have any number of unique constraints for a table; but only one primary key constraint.
4
cs3431 Foreign Keys: Referential Integrity Constraints Specified for a table, say for table R, as: CONSTRAINT F FOREIGN KEY(x,y) REFERENCES S(a,b) Requires (a, b) be unique or primary key of S. Default: reject any modification violates constraints. Other policies for delete/update can be specified as “set null/cascade”. Eg: for Student relation FOREIGN KEY (prof) references Professor (pNum) ON DELETE SET NULL ON UPDATE CASCADE
5
cs3431 Other tips While dropping a table such as S, where S is referenced by a FK from R, we can specify as ALTER TABLE S DROP COLUMN a CASCADE CONSTRAINTS; DROP TABLE S CASCADE CONSTRAINTS;
6
cs3431 Column Check constraints Constraints specified on a column 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’))
7
cs3431 More Column Check constraints salary int CONSTRAINT minSalary CHECK (salary>=60000) CONSTRAINT minSalary check (salary >= 60000)
8
cs3431 Check Constraints Can use queries to express constraint. CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 ))
9
cs3431 Explicit Domain Constraints CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating values-of-ratings, age REAL, PRIMARY KEY (sid)) CREATE DOMAIN values-of-ratings INTEGER DEFAULT 1 CHECK ( VALUE >= 1 AND VALUE <= 10)
10
cs3431 Explicit Domain Constraints CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating values-of-ratings, age REAL, PRIMARY KEY (sid)) CREATE DOMAIN values-of-ratings INTEGER DEFAULT 1 CHECK ( VALUE >= 1 AND VALUE <= 10)
11
cs3431 More Check/ Table Constraints CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT noInterlakeRes CHECK (`Interlake’ <> ( SELECT B.bname FROM Boats B WHERE B.bid= bid))) Constraint that Interlake boats cannot be reserved: If condition evaluates to FALSE, update is rejected.
12
cs3431 On Column Check constraints Constraint on relation R only checked when tuple inserted into R/updated in R Only needs to hold TRUE when table is non-empty Changes on other tables besides R do not lead to a checking of R’s constraints Good Design : check constraint should only involve attributes of the tuple being “checked” and have no sub- queries to assure the constraint always holds.
13
cs3431 Table Constraints Associated with one table Only needs to hold TRUE when table is non-empty.
14
cs3431 Complex CHECK Constraint ? CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Symmetric constraint, yet associated with Sailors. If Sailors is empty, the number of Boats tuples can be anything! Number of boats plus number of sailors is < 100
15
cs3431 Assertions: Constraints over Multiple Relations CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) ASSERTION not associated with either table. ASSERTION is a schema element CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Number of boats plus number of sailors is < 100
16
cs3431 Assertion : On entire schema Assertion Example : 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. Assertions not supported by Oracle – could be very inefficient.
17
cs3431 Altering Constraints Constraints can be added to an existing table. ALTER TABLE ADD CONSTRAINT [ ] Any constraint that has a name can be dropped ALTER TABLE DROP CONSTRAINT
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.