200 likes | 287 Views
Assignment 6-1. Friday. Assignment 5–2 Note 15 and 16 Quiz 2. Relational Integrity. Entity Integrity Referential Integrity Enterprise Constraints. Null Value. The value of an attribute could be NULL NOT known at the moment or NOT Applicable Example
E N D
Friday Assignment 5–2 Note 15 and 16 Quiz 2
Relational Integrity • Entity Integrity • Referential Integrity • Enterprise Constraints
Null Value The value of an attribute could be NULL NOT known at the moment or NOT Applicable Example Cell Phone number BranchNo in Staff table if BranchNo is not required for all staff
RDBMS Relational DBMS should be able to handle NULL One issue Null = Null? List all staff who have not been assigned to a branch yet BranchNo = Null -- Will not work BranchNo is null - This will work
Entity Integrity In a base relation, no attribute of a primary (candidate) key can be NULL. Candidate Key: Minimum set of attributes to uniquely identify records.
Referential Integrity Foreign key • Must match a primary key in the parent relation, or • Wholly NULL For any table instance!
Example Which branch is SA200 in? Foreign key • Must match a primary key in the parent relation, or • Wholly NULL • SG363 is OK: has not been signed to a branch yet
Example Which room is the first booking for? Foreign key • Must match a primary key in the parent relation, or • Wholly NULL
Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…)
In what order to create the tables? Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) Will it work? YES! Branch (Bno…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Viewing (Rno, Pno, ViewDate…) Renter (Rno…) Will it work? NO!
In what order to drop the tables? Viewing (Rno, Pno, ViewDate…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Renter (Rno…) Branch (Bno…) Will it work? YES! Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) Will it work? NO!
Insert Rows The PK must be unique The foreign key value must exist in the parent table if FK is provided FK can be null, meaning not known at the time
Insert Rows Insert into Branch: no problem Insert into Staff: Cannot insert a staff with Bno being ‘B123’ if Branch table has no ‘B123’ in Bno column FK can be null, meaning not known at the time
Delete Rows No records in other tables reference the record to be deleted.
Delete Rows Delete from Staff: no problem Delete from Branch: Delete branch 'B101' What about staff in 'B101‘?
ANSI SQL Solutions In ANSI SQL, there are five choices • No Action Cannot delete • Set to Null • Set to Default • No Check No good • Cascade Delete all staff in 'B101' from Staff table when deleting branch ‘B101’ Dangerous!
Oracle Solutions In Oracle, only two choices are implemented • No Action Cannot delete • Cascade Delete all staff in 'B101' from Staff table when deleting branch ‘B101’ • Set to Null: not implemented • Set to Default : not implemented • No Check : not implemented
Update Record Update table Staff ‘B101’ of SG100 to ‘B205’ New value must exist in Branch Update table Branch ‘B101’ to ‘B303’ Five choices in ANSI SQL, only No Action is implemented in Oracle
Enterprise Constraints • Business rules need to be enforced in database • Functional Dependency • Domain for each attribute HotelRoom (HotelNo, RoomNo…Type…) Type is a string, but three possible values: Family, Double, Single • Trigger