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

Module 3 - Advance Database Concepts-Lesson 2

Thank you for the feedback. I'm an AI assistant created by Anthropic to be helpful, harmless, and honest. I don't have a name.

Uploaded by

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

Module 3 - Advance Database Concepts-Lesson 2

Thank you for the feedback. I'm an AI assistant created by Anthropic to be helpful, harmless, and honest. I don't have a name.

Uploaded by

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

IV.

The Lesson Structure

Module No. 1 Advance Database Concepts


Module No. and Title

Lesson No. 2: Foreign Key Constraints


Lesson No. and Title

At the end of this module, learners are expected to:


• Define and discuss the importance of foreign key constraints in a
database
Module Learning Outcomes
• Apply foreign key referential actions to the relation

Time Frame 5 Hours

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

Visit the url https://www.youtube.com/watch?v=rOyZtFo4qaU&t=85s and answer the following:

1. Why foreign key is important in creating a data model?


2. A foreign key imposes a specific kind of integrity to related tables. What is the name of this integrity?
3. What are the differences between primary keys and foreign keys?
4. Can we easily add any records to a child table if Foreign Key constraints is enforced?
Abstraction

What is Foreign Key in DBMS?

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.

Rules for FOREIGN KEY

• NULL is allowed in SQL Foreign key.


• The table being referenced is called the Parent Table
• The table with the Foreign Key in SQL is called Child Table.
• The SQL Foreign Key in child table references the primary key in the parent table.
• This parent-child relationship enforces the rule which is known as "Referential Integrity."

Foreign Key Actions

• 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

Sample of Created Foreign Key Constraints in Workbench


Application

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

200 Davao 2 John 200

2. Can you insert record (S3,Jean,300) into STAFF relation?


If YES, what is the message?

If NO, what is the message? Why it cannot be inserted?

3. Delete staff S3.


Is it successful? Yes/No

4. Create a FOREIGN KEY in the branch relation


ALTER TABLE staff ADD CONSTRAINT staff_branch
FOREIGN KEY(bno)
REFERENCES branch(bno)
ON DELETE CASCADE
ON UPDATE NO ACTION;

5. Can you insert record (S3,Jean,300) into STAFF relation?


If YES, what is the message?

If NO, what is the message? Why it cannot be inserted?

6. Can you update the Branch # of John to 300.


UPDATE staff SET bno = 300 WHERE sno = 2;
If YES, what is the message?

If NO, what is the message? Why it cannot be updated?

7. Insert record (300,Tagum) into BRANCH relation.


Is it successful? Yes/No

8. Can you insert record (S3,Jean,300) into STAFF relation?


If YES, what is the message?

If NO, what is the message? Why it cannot be inserted?

9. Delete Branch 100 in the BRANCH relation


Is it successful? Yes/No

10. What happen to the records under STAFF relation?


11. Drop the FOREIGN KEY of the STAFF relation
ALTER TABLE staff DROP FOREIGN KEY staff_branch

12. Create a new FOREIGN KEY in the STAFF relation


ALTER TABLE staff ADD CONSTRAINT staff_branch
FOREIGN KEY (bno)
REFERENCES branch(bno)
ON DELETE NO ACTION
ON UPDATE CASCADE

13. Can you delete Panabo branch?


If YES, what is the message?

If NO, what is the message? Why it cannot be deleted?

14. Can you update Panabo branch number from 100 into 500?
If YES, what is the message?

If NO, what is the message? Why it cannot be updated?

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”

for completing the 2nd lesson of the 3rd module in Advance


Database System

You might also like