0% found this document useful (0 votes)
19 views

Foreign Key

Uploaded by

rajputajay0821
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Foreign Key

Uploaded by

rajputajay0821
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

DATABASE

MANAGEMENT
SYSTEM
Contents

Foreign Key

E-R Model to Relational Model

Index

Control Functions
CONSTRAINTS- FOREIGN KEY
• A FOREIGN KEY in MySQL creates a link between two tables by one specific
column of both tables. The specified column in one table must be a PRIMARY
KEY and referred by the column of another table known as FOREIGN KEY.
 CREATE TABLE Persons (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Age int,
PRIMARY KEY (ID)
);
 CREATE TABLE Orders (
OrderID int NOT NULL,
Order_Date Date NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons
(ID));
mysql> select * from persons;
+----+-------+------+
| ID | Name | Age |
+----+-------+------+
| 1 | Ram | 21 |
| 2 | Riya | 22 |
| 3 | Priya | 23 |
+----+-------+------+
3 rows in set (0.00 sec)

mysql> select * from orders;


+---------+------------+----------+
| OrderID | Order_Date | PersonID |
+---------+------------+----------+
| 103 | 2024-01-01 | 1|
| 104 | 2024-01-01 | 1|
| 105 | 2024-01-03 | 2|
+---------+------------+----------+
3 rows in set (0.00 sec)
CLAUSES FOR FOREIGN KEY

• CASCADE: Delete or update the row from the parent table and automatically
delete or update the matching rows in the child table. Both ON DELETE
CASCADE and ON UPDATE CASCADE are supported.

• Set Null- Delete or update the row from the parent table and automatically set
null in the matching rows in the child table.

• RESTRICT: Rejects the delete or update operation for the parent table.
 CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL);

 CREATE TABLE rooms (


room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no) REFERENCES buildings
(building_no) ON DELETE CASCADE );
 INSERT INTO
buildings(building_name,address)
VALUES('ACME Headquaters','3950 North 1st
Street CA 95134'), ('ACME Sales','5000
North 1st Street CA 95134’);

 INSERT INTO rooms(room_name,building_no)


VALUES('Amazon',1), ('War Room',1),
('Office of CEO',1), ('Marketing',2),
('Showroom',2);
mysql> select * from buildings;
+-------------+------------------+--------------------------------+
| building_no | building_name | address |
+-------------+------------------+--------------------------------+
| 1 | ACME Headquaters | 3950 North 1st Street CA 95134 |
| 2 | ACME Sales | 5000 North 1st Street CA 95134 |
+-------------+------------------+--------------------------------+
2 rows in set (0.00 sec)

mysql> select * from rooms;


+---------+---------------+-------------+
| room_no | room_name | building_no |
+---------+---------------+-------------+
| 1 | Amazon | 1|
| 2 | War Room | 1|
| 3 | Office of CEO | 1|
| 4 | Marketing | 2|
| 5 | Showroom | 2|
+---------+---------------+-------------+
5 rows in set (0.00 sec)
 CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL);

 CREATE TABLE rooms (


room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no) REFERENCES buildings
(building_no) ON Update CASCADE );
 INSERT INTO
buildings(building_name,address)
VALUES('ACME Headquaters','3950 North 1st
Street CA 9589'), ('ACME Sales','5000
North 1st Street CA 9589’);

 INSERT INTO rooms(room_name,building_no)


VALUES('Amazon',1), ('War Room',1),
('Office of CEO',1), ('Marketing',2),
('Showroom',2);
mysql> select * from buildings;
+-------------+------------------+-------------------------------+
| building_no | building_name | address |
+-------------+------------------+-------------------------------+
| 1 | ACME Headquaters | 3950 North 1st Street CA 9589 |
| 2 | ACME Sales | 5000 North 1st Street CA 9589 |
+-------------+------------------+-------------------------------+
2 rows in set (0.00 sec)

mysql> select * from rooms;


+---------+---------------+-------------+
| room_no | room_name | building_no |
+---------+---------------+-------------+
| 1 | Amazon | 1|
| 2 | War Room | 1|
| 3 | Office of CEO | 1|
| 4 | Marketing | 2|
| 5 | Showroom | 2|
+---------+---------------+-------------+
5 rows in set (0.00 sec)
 CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL);

 CREATE TABLE rooms (


room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no) REFERENCES buildings
(building_no) ON Update CASCADE on DELETE cascade );
 INSERT INTO
buildings(building_name,address)
VALUES('ACME Headquaters','3950 North 1st
Street CA 95134'), ('ACME Sales','5000
North 1st Street CA 95134’);

 INSERT INTO rooms(room_name,building_no)


VALUES('Amazon',1), ('War Room',1),
('Office of CEO',1), ('Marketing',2),
('Showroom',2);
CREATE TABLE Persons ( ID INT
NOT NULL AUTO_INCREMENT,
Name varchar(50) NOT NULL,
City varchar(50) NOT NULL,
PRIMARY KEY (ID) );

CREATE TABLE Contacts (ID INT Primary key,


Person_Id INT,
Info varchar(50) NOT NULL,
Type varchar(50) NOT NULL,
FOREIGN KEY (Person_Id)
REFERENCES Persons(ID)
ON DELETE SET NULL
ON UPDATE SET NULL );
FOREIGN KEY USING ALTER COMMAND
 ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Per
sons(ID);
Slide
7- 17
ER-TO-RELATIONAL MAPPING ALGORITHM
 Step 1: Mapping of Regular Entity Types.
 For each regular (strong) entity type E in the ER schema, create a relation R
that includes all the simple attributes of E.
 Choose one of the key attributes of E as the primary key for R.
 If the chosen key of E is composite, the set of simple attributes that form it will
together form the primary key of R.
 Example: We create the relations EMPLOYEE, DEPARTMENT, and
PROJECT in the relational schema corresponding to the regular entities in
the ER diagram.
 SSN, DNUMBER, and PNUMBER are the primary keys for the relations
EMPLOYEE, DEPARTMENT, and PROJECT as shown.
ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

 Step 2: Mapping of Weak Entity Types


 For each weak entity type W in the ER schema with owner entity type
E, create a relation R & include all simple attributes (or simple
components of composite attributes) of W as attributes of R.
 Also, include as foreign key attributes of R the primary key
attribute(s) of the relation(s) that correspond to the owner entity
type(s).
 The primary key of R is the combination of the primary key(s) of the
owner(s) and the partial key of the weak entity type W, if any.
 Example: Create the relation DEPENDENT in this step to
correspond to the weak entity type DEPENDENT.
 Include the primary key SSN of the EMPLOYEE relation as a foreign
key attribute of DEPENDENT (renamed to ESSN).
 The primary key of the DEPENDENT relation is the combination
{ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is
the partial key of DEPENDENT.
ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)
 Step 3: Mapping of Binary 1:1 Relation Types
 For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that
correspond to the entity types participating in R.
 There are three possible approaches:
1. Foreign Key approach: Choose one of the relations-say S-and include a foreign key in S the
primary key of T. It is better to choose an entity type with total participation in R in the role of S.

 Example: 1:1 relation MANAGES is mapped by choosing the participating entity type DEPARTMENT
to serve in the role of S, because its participation in the MANAGES relationship type is total.

2. Merged relation option: An alternate mapping of a 1:1 relationship type is possible by merging
the two entity types and the relationship into a single relation.

3. Cross-reference or relationship relation option: The third alternative is to set up a third relation
R for the purpose of cross-referencing the primary keys of the two relations S and T representing
the entity types.
Slide
7- 22
Student Enroll Course

Rollno Others Rollno Course CourseC Others


Code ode
S1 -
S1 C1
S2 - C1 -
S2 C2
S3 - C2 -
S3 C3
S4 - C3 -
S4 C4
C4 -
ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)
 Step 4: Mapping of Binary 1:N Relationship Types.
 For each regular binary 1:N relationship type R, identify the relation S that
represent the participating entity type at the N-side of the relationship type.
 Include as foreign key in S the primary key of the relation T that represents the
other entity type participating in R.
 Include any simple attributes of the 1:N relation type as attributes of S.
 Example: 1:N relationship types WORKS_FOR, CONTROLS, and
SUPERVISION in the figure.
 For WORKS_FOR we include the primary key DNUMBER of the
DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it
DNO.
TABLE FOR EMPLOYEE
CREATE TABLE Employees (
-> SSN INT PRIMARY KEY, Bday date,Fname varchar(20),Mname
varchar(20),Lname varchar(20),
-> sex char, Address varchar(45),salary decimal(10,3),F_SSN int, foreign
key(F_SSN) references Employees(SSN));
Student Enrolls Elective Course

S_id Others S_id E_id E_id Others


S1 - S1 E1
E1 -
S2 - S2 E2
E2 -
S3 - S3 E1
S4 - S4 E1
ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

 Step 5: Mapping of Binary M:N Relationship Types.


 For each regular binary M:N relationship type R, create a new
relation S to represent R.
 Include as foreign key attributes in S the primary keys of the relations
that represent the participating entity types; their combination will form
the primary key of S.
 Also include any simple attributes of the M:N relationship type (or
simple components of composite attributes) as attributes of S.
 Example: The M:N relationship type WORKS_ON from the ER
diagram is mapped by creating a relation WORKS_ON in the
relational database schema.
 The primary keys of the PROJECT and EMPLOYEE relations are
included as foreign keys in WORKS_ON and renamed PNO and ESSN,
respectively.
 Attribute HOURS in WORKS_ON represents the HOURS attribute of
the relation type. The primary key of the WORKS_ON relation is the
combination of the foreign key attributes {ESSN, PNO}.
Student Enrolls Compulsory Course

S_id Others S_id C_id C_id Others


S1 - S1 C1
C1 -
S2 - S1 C2
C2 -
S3 - S3 C1
C3
S4 - S4 C3
C4
S4 C2
S3 C3
ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

 Step 6: Mapping of Multivalued attributes.


 For each multivalued attribute A, create a new relation R.
 This relation R will include an attribute corresponding to A, plus the
primary key attribute K-as a foreign key in R-of the relation that
represents the entity type of relationship type that has A as an attribute.
 The primary key of R is the combination of A and K. If the multivalued
attribute is composite, we include its simple components.
 Example: The relation DEPT_LOCATIONS is created.
 The attribute DLOCATION represents the multivalued attribute
LOCATIONS of DEPARTMENT, while DNUMBER-as foreign key-
represents the primary key of the DEPARTMENT relation.
 The primary key of R is the combination of {DNUMBER,
DLOCATION}.
ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

 Step 7: Mapping of N-ary Relationship Types.


 For each n-ary relationship type R, where n>2, create a new
relationship S to represent R.
 Include as foreign key attributes in S the primary keys of the
relations that represent the participating entity types.
 Also include any simple attributes of the n-ary relationship type
(or simple components of composite attributes) as attributes of
S.
 Example: The relationship type SUPPY in the ER on the next
slide.
 This can be mapped to the relation SUPPLY shown in the relational
schema, whose primary key is the combination of the three foreign
keys {SNAME, PARTNO, PROJNAME}
TERNARY RELATIONSHIP TYPES. (A) THE SUPPLY RELATIONSHIP.
FIGURE 7.3
MAPPING THE N-ARY RELATIONSHIP TYPE SUPPLY FROM
FIGURE 4.11A.
THANKS

You might also like