Integrity Rules: Relational Integrity Constraints Applicable to a Particular • Not included in Codd’s original definition • E.G. An employee’s salary must be in the of relations. range $30,000 to $100,000. • Not supported in initial commercial • See section 8.6 Elmasri & Navathe. systems. • Supported in the SQL 2 standard and • Now supported in all major relational available in most relational systems today. database systems. • Not discussed now.

Relational integrity rules 1 Relational integrity rules 2

Integrity Rules: Foreign Keys - a Definition EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) Applicable to All DEPT( DNO, DNAME, LOC) • Key constraint DNO in EMP - should be allowed only if that • Entity integrity DNO appears as • Referential integrity in DEPT. MGR in EMP - should be allowed only if that MGR appears as primary key in relation EMP.

The attributes DNO and MGR called foreign keys in relation EMP.

Relational integrity rules 3 Relational integrity rules 4

Def. , FK, an attribute (or combination of attributes in one The Three Integrity Rules Relation Schema, R1, such that: • Attributes in FK of R1 have same domain as • Key constraint the primary key attributes, PK, of relation Candidate keys are specified for each relation schema. schema R1. values must be unique for every • A value of FK in a tuple t1 of R1 either occurs tuple in any relation instance of that schema.

– As a value of PK of some tuple t2 of R2 ,

• i.e. T1 [FK] = t2 [PK] – Or is .

Relational integrity rules 5 Relational integrity rules 6

1 The Three Integrity Rules, Continued The Three Integrity Rules, Continued • Referential integrity:

– If relation schema R1 includes a foreign key, FK, • Entity integrity: matching the primary key of relation schema R2 , – No attribute participating in the primary key is then a value of FK in a tuple t1 of R1 must either allowed to accept null values. • Be equal to the value of PK in some tuple t2 of R2 ,

i.e. T1 [FK] = t2 [PK], or • Be wholly null (i.e. Each attribute value participating in Justification: we must be able to identify that FK value must be null). each tuple uniquely. Note: R1 and R2 need not be distinct.

Justification: if some tuple t1 references some tuple t2 , then tuple t2 must exist.

Relational integrity rules 7 Relational integrity rules 8

Implications of the Three Example: Delete DEPT Where DNO = D2 Integrity Rules What should happen if there are employees in DEPT with DNO = D2? What should happen if an operation on the database is about to cause the violation of one of the integrity rules? – Operation could be rejected. i.e. about to put the database into an “illegal” state. – Employees in DEPT D2 could also be deleted, i.e. Cascade the deletion. • System could reject the operation. • System could accept the operation, but Choice should be up to the “user”, perform some additional operations so as to the database designer. reach a new legal state.

Relational integrity rules 9 Relational integrity rules 10

Notes: Foreign Key - General Rules

• Not included in Codd’s original definition • Foreign key • Deleting target of a foreign key of relations. with null reference. • Not supported in initial commercial value. • Updating target of a foreign key relational database systems. reference. • Now supported in all major relational • Inserting into the relation with database systems. the foreign key. • Actually introduced in network model.

Relational integrity rules 11 Relational integrity rules 12

2 Deleting Target of a Foreign Key Reference Foreign Key With Null Value. Example: Delete department where there are still employees in the department. Restricted - is restricted to case where • May an employee not be assigned to a there are no employees in the department? department. – Probably Cascades - delete cascades to remove employees also. • May an employee not have a manager? Nullifies - foreign key is set to null, – Let’s assume no. i.e. Employees are no longer in any department. Then department is deleted. Relational integrity rules 13 Relational integrity rules 14

Updating Target of a Foreign Key Reference. Inserting into Relation With the Foreign Key. Example: Change department number (DNO) D50 to D75. Example: Insert a new employee into department D10, but there is no Restricted - is restricted to case where tuple in the DEPT relation for department D10. there are no employees in the Restricted - is restricted to case where department. there are already tuples for the Cascades - update cascades to change the corresponding department. department numbers of all Cascades - if done interactively, user could be employees in department D50. asked to enter tuple for Nullifies - foreign key, department D10. i.e. DNO in EMP is set to null. Nullifies - cannot be done, since user has specified the department number. Department DNO is updated. Relational integrity rules 15 Relational integrity rules 16

Terminology Summary Relational Traditional Databases “Tables” Files • relation • • file • tuple • • record • attribute • • field Distinctions: • A file may contain records of different types. • In a file, field order, left-to-right matters. • In a file, record order may matter, i.e. it may convey semantics. • In a file, records need not have a unique identifier field or field combination (primary key).

Relational integrity rules 17

3