Module 3 - Advance Database Concepts-Lesson 2
Module 3 - Advance Database Concepts-Lesson 2
Introduction
Hello students, welcome to Lesson 2 of Module 3 of Advance Database System!
As the complexity of data structures continues to evolve, databases have shifted
to the relational databases and multi-modal databases that are most often used
today. Now, we can relate various tables in a meaningful way using foreign keys.
In this module I will introduce you to foreign keys and show you how to use
them in SQL.
Activity
Using your internet take at least one screenshot or picture of an entity relation diagram showing a Foreign
Key Constraints.
Analysis
Foreign key is a column (or combination of columns) in a table that points to the primary key of another table.
FOREIGN KEY constraints enforce referential integrity, which essentially says that if column value A refers to column
value B, then column value B must exist. In simple words, foreign key ensures values in one table must be present in
another table.
• CASCADE: It is used when we delete or update any row from the parent table, the values of the matching
rows in the child table will be deleted or updated automatically.
• SET NULL: It is used when we delete or update any row from the parent table, the values of the foreign key
columns in the child table are set to NULL.
• RESTRICT: It is used when we delete or update any row from the parent table that has a matching row in
the reference(child) table, MySQL does not allow to delete or update rows in the parent table.
• NO ACTION: It is similar to RESTRICT. But it has one difference that it checks referential integrity after trying
to modify the table.
• SET DEFAULT: When a referenced foreign key is deleted or updated, the columns of all rows referencing
that key are set to the default value for that column.
Creating Table with Foreign Key Constraints
1. Create the following relations under IT223 database. BNO and SNO are primary keys.
Branch Staff
Bno Bname Sno Sname Bno
100 Panabo 1 Peter 100
14. Can you update Panabo branch number from 100 into 500?
If YES, what is the message?
15. Examine the STAFF and BRANCH relations what have you observed?
16. Create a backup of the database and send it together with your answers to exercise. Name your document
as “FK_urNameAndUrSet”