Download presentation
Presentation is loading. Please wait.
Published byJulie Saile Modified over 10 years ago
1
Referential Integrity & Foreign Keys Objectives of the Lecture : To consider Referential Integrity & Foreign Keys; To consider Referential Integrity Constraints in SQL.
2
Example of Referential Integrity (1) 1 2 3 4 ENo 3 1 2 4 1E3 E5 E1 4E6 E8 5 6 7 8 EName 7 5 6 8 5Smith Mitchell Robson 8Blake Jones 2 4 6 8 M-S 6 2 4 8 2S M D 8M W 2 4 6 8 Sal 6 2 4 8 212,500 21,000 32,500 854,000 68,000 EMPLOYEE 5 6 7 8 RegNo 7 5 6 8 5 K123 ABC W811 STA JON 1 8V771 PQ 2 Owner 2 E3 E5 E1 E6 Type Corsa 1.3 Starlet GLi Jaguar XK Volvo S80 CAR CAR[Owner] is a subset of EMPLOYEE[ENo] E3 E6 E5 E1
3
Purpose of Referential Integrity To ensure that two different relations are consistent with each other. Example : Consider the CAR relation. We know that it does not make sense for an owner in CAR not to be an employee in EMPLOYEE. So every CAR ‘Owner’ attribute value must appear in the EMPLOYEE ‘ENo’ attribute. So the set of CAR ‘Owner’ attribute values must be a subset of the EMPLOYEE ‘ENo’ values. l Consistency between a DB’s relations is often required.
4
Example of Referential Integrity (2) 1 2 3 4 ENo 3 1 2 4 1E3 E5 E1 4E6 E8 5 6 7 8 EName 7 5 6 8 5Smith Mitchell Robson 8Blake Jones 2 4 6 8 M-S 6 2 4 8 2S M D 8M W 2 4 6 8 Sal 6 2 4 8 212,500 21,000 32,500 854,000 68,000 EMPLOYEE 5 6 7 8 RegNo 7 5 6 8 5 K123 ABC W811 STA JON 1 8V771 PQ 2 Owner 2 E3 E5 E1 E6 Type Corsa 1.3 Starlet GLi Jaguar XK Volvo S80 CAR 1 2 3 4 3 1 2 4 1E3 E5 E1 4E6 E8 Referenced Attribute. 22 E3 E5 E1 E6 Referencing Attribute.
5
Definition of Referential Integrity l The values in a referencing attribute must be the same set or a (proper) subset of the values in the referenced attribute. l Thus both attributes must be drawn from the same underlying data type. Referential integrity can be generalised to apply between two corresponding sets of attributes, where the sets may contain more than one attribute. Hence the full definition is :- l The set of n-tuples in the referencing set of n-attributes must be the same set or a (proper) subset of the n-tuples in the referenced set of n-attributes. l Thus both sets of n-attributes must be based on the same set of underlying data types.
6
Example of Referential Integrity (3) ENo 1 2 3 4 3 1 2 4 1E3 E5 E1 4E6 E8 5 6 7 8 EName 7 5 6 8 5Smith Mitchell Robson 8Blake Jones 2 4 6 8 M-S 6 2 4 8 2S M D 8M W 2 4 6 8 Sal 6 2 4 8 212,500 21,000 32,500 854,000 68,000 EMPLOYEE 5 6 7 8 RegNo 7 5 6 8 5 K123 ABC W811 STA JON 1 8V771 PQ Owner 22 E3 E5 E1 E6 Type Corsa 1.3 Starlet GLi Jaguar XK Volvo S80 CAR 22 E3 E5 E1 E6 Foreign Key 1 2 3 4 3 1 2 4 1E3 E5 E1 4E6 E8 This is a Primary / Candidate Key.
7
Foreign Keys l The referencing (set of) attribute(s) is called a Foreign Key. l The Foreign Key gets its name because traditionally the referenced (set of) attribute(s) is always a candidate key. l In SQL, the referenced (set of) attribute(s) must be a primary or alternate key. However this is not logically necessary, and in principle a Foreign Key may reference any (set of) attribute(s) with the same underlying data type(s).
8
SHIPMENT 5 6 7 8 PNo 7 5 6 8 5 P1 P2 8P3 Qty 22 5 10 12 7 SNo S1 S2 SNo Example - Foreign Key within a Candidate Key :- Key Overlap l A foreign key can occur within a candidate key, or overlap with it. SNo PNo S1 S2 5 6 7 8 7 5 6 8 5 P1 P2 8P3 Candidate Key SUPPLIER SNo S1 S2 S3 Details 22 ……………….. …………….. ……………….….. S1 S2 S3 Foreign Key S1 S2
9
Self-Referential Integrity l A foreign key can reference an attribute(s) in the same relation as itself. Example - A manager must also be an employee :- SUPERVISE MgerEmp E1 E2 E3 E4 E5 E6 E7 E1 E2 E3 E1 E2 E3 Foreign Key Candidate Key
10
Further Considerations l The link between foreign and candidate keys is asymmetric. It is the foreign key that is dependent on the candidate key for its values, not vice versa. l Note that a foreign key can, and often does, have values that are replicated in more than one tuple, maybe in many tuples. l The set of foreign key values (ignoring replicas) must be a subset of the candidate key’s set of values. It is useful to consider whether the foreign key set can or should be equal to the candidate key set, or be a proper subset of them, or be allowed to vary between these two possibilities. l Is it essential that the foreign key always have a candidate key value, or is it permissible for it to be missing ?
11
SQL Referential Integrity l Foreign Keys are allowed to be NULLs (unless additionally specified not to). If NULLs are allowed, then a Foreign Key must contain either a Primary/Alternate Key value or be null. l Referential integrity constraints can be named in the same way as primary/alternate key constraints. l A single attribute can be made a foreign key in the same sub- statement in which it is assigned its data type, OR A foreign key can be assigned to one or more attributes in a separate sub-statement at the end of a Create Table statement.
12
SQL Foreign Key Assignment l Assign a single attribute to be a foreign key in the same sub- statement in which it is assigned its data type. After the data type, append References TABLENAME ( AttributeName ) l Assign one or more attributes to a foreign key in a separate sub-statement at the end of a Create Table statement. Foreign Key ( AttributeName(s) ) References TABLENAME ( AttributeName(s) ) Keyword Insert actual names of table & attribute. Two possibilities : optional
13
Examples of an SQL Foreign Key (1) l Give the relation CAR a foreign key, attribute Owner, referencing attribute ENo of EMPLOYEE. Create Table CAR ( RegNoChar(9) Primary Key, TypeVarchar2(24), OwnerChar(2) References EMPLOYEE( ENo ) ) ; Create Table CAR ( RegNoChar(9) Primary Key, TypeVarchar2(24), OwnerChar(2), Foreign Key(Owner) References EMPLOYEE( ENo ) ); Two equivalent versions, with no user-assigned constraint name.
14
Create Table SHIPMENT ( PNoChar(2), SNoChar(2), QtyInteger, Constraint CAND_KEY Primary Key (Pno, SNo), Constraint FOR_KEY Foreign Key ( SNo ) References SUPPLIER (SNo ) ); Examples of an SQL Foreign Key (2) Create Table SHIPMENT ( PNoChar(2), SNoChar(2) Constraint FOR_KEY References SUPPLIER (SNo ), QtyInteger, Constraint CAND_KEY Primary Key (Pno, SNo) ); Two equivalent versions, with user-assigned constraint names.
15
Example of an SQL Foreign Key (3) l Ensure that the first row entered is the manager that manages him/herself ! (I.e. the root of the hierarchical tree). l This is because self-referentiality applies. It prevents any row being put into the table whose employee does not have a manager. l Self-referentiality will generally cause analogous problems, whatever its nature. Create Table SUPERVISE ( MgerChar(2), EmpChar(2), Constraint CAND_KEY Primary Key (Emp), Constraint FOR_KEY Foreign Key ( Mger ) References SUPERVISE ( Emp ) );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.