0% found this document useful (0 votes)
33 views12 pages

Lab 7 - Foreign Key Constraints and Subqueries BOUCHINA OTHMANE

Uploaded by

Boutchi TMN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Lab 7 - Foreign Key Constraints and Subqueries BOUCHINA OTHMANE

Uploaded by

Boutchi TMN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Faculty of Engineering Applied Sciences and Technology

LABORATORY MANUAL
BCS 306 - Database Management Systems

Spring 2024

Lab 7

Foreign Key Constraints and Subqueries


1. Lab Objectives:
 Gain a solid understanding of what foreign key constraints are in relational
databases.
 Recognize the role of foreign keys in establishing relationships between tables.
 Implementing Referential Integrity:
- Learn how foreign key constraints enforce referential integrity in a database.
- Understand the importance of maintaining consistency between tables
through foreign keys.
 Exploring ON DELETE and ON UPDATE Actions:
- Explore the various actions (CASCADE, RESTRICT, SET NULL, SET
DEFAULT, etc.) associated with ON DELETE and ON UPDATE for foreign
key constraints.
 Identify and differentiate between different types of subqueries, including scalar,
column, row, and table subqueries.

Syntax: (ALTER)
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (child_column)
REFERENCES parent_table(parent_column)
ON UPDATE <action>
ON DELETE <action>;

Syntax: (subqueries)

SELECT column1, column2, ...


FROM main_table
WHERE column_name operator (SELECT column_name FROM sub_table
WHERE condition);
2. ERD:

3. Exercises for queries: (to able to answer this, you must download and
import this Database)

3.1. Using Invoices DB, Update the payment_total in the table invoices for the client with
id ‘5’ by multiplying the total invoice by 0.5.

EVIDENCE #1
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.2. Using Invoices DB, Update the payment_total in the table invoices for the client
with name “Myworks” by multiplying the total invoice by 0.5.

EVIDENCE #2
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.3. Using Invoices DB, Update the payment_total for clients whose city is in CA or NY
by multiplying the total_invoice by 0.5 and Payment_date by the current date.

EVIDENCE #3
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

3.4. Using Invoices DB, Update the payment_date to be current date for the clients whose
payment_date is Null.

EVIDENCE #4
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.5. Using sql_store DB, Update the comments to be “ Gold customer” for the clients
whose Points >3000.

EVIDENCE #5
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.6. Using store DB, update the customer ID to 111 for a customer with the first_name
'Ines' in a store database, while ensuring referential integrity or foreign key
constraints.

EVIDENCE #6
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

3.7. Using store DB, update the customer ID to 999 for a customer with the first name
'Levy' and add 1000 points to their account in a store database, while ensuring
referential integrity and foreign key constraints

EVIDENCE #7
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.8. Use the ALTER TABLE statement to add a column named 'gender' with the data type
'varchar' after the 'last_name' column in the 'customers' table of a store database

EVIDENCE #8
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF THE
SCRIPT.
3.9. Use the ALTER TABLE statement to modify the 'orders' table, setting the foreign key
constraint to ON UPDATE CASCADE and ON DELETE CASCADE

EVIDENCE #9
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

3.10. Use the ALTER TABLE statement to modify the 'order_items' table, setting the
foreign key constraint to ON UPDATE CASCADE and ON DELETE CASCADE.

EVIDENCE #10
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.11. Using store DB, delete the customer with ID 7 while ensuring referential integrity and
foreign key constraints

EVIDENCE #11
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.
3.12. Using store DB, delete customers whose points are greater than 3000 while ensuring
referential integrity and foreign key constraints.

EVIDENCE #12
TAKE A SCREENSHOT OF THE CALL TO THE SCRIPT AND THE OUTPUT OF
THE SCRIPT.

You might also like