A professional SQL project demonstrating the implementation of relational database concepts, specifically focusing on Foreign Key Constraints and the ON DELETE CASCADE behavior.
The primary goal of this task is to create a simple two-table relational structure representing departments and employees. It illustrates how data integrity is maintained across related tables and how automated cleanup works when parent records are deleted.
- Database Management System: SQL (MySQL / PostgreSQL / SQL Server compatible)
- Core Concepts: Relational Database Design, Data Integrity
- Advanced Techniques: Foreign Key Constraints (
FOREIGN KEY,REFERENCES), Cascading Actions (ON DELETE CASCADE)
| Column | Data Type | Constraint / Description |
|---|---|---|
department_id |
INT |
Primary Key |
department_name |
VARCHAR(50) |
Name of the Department, Not Null |
| Column | Data Type | Constraint / Description |
|---|---|---|
employee_id |
INT |
Primary Key |
employee_name |
VARCHAR(50) |
Full Name of Employee, Not Null |
department_id |
INT |
Foreign Key referencing departments.department_id |
Key operations demonstrated in the script:
- Schema Creation & Relations:
- Creation of the
task7database. - Creating
departmentsandemployeestables with a primary key / foreign key relationship. - Enforcing
ON DELETE CASCADEto automatically remove child records.
- Creation of the
- Data Insertion (Valid & Invalid):
- Inserting valid department and employee records.
- Attempting an invalid insert (assigning an employee to a non-existent department) to demonstrate foreign key constraint errors.
- Cascading Deletion:
- Deleting a department record.
- Verifying the automatic deletion of all employees associated with the deleted department without requiring manual intervention.
- Initialize: Execute the script to create the
task7database and both tables. - Populate Data: Run the
INSERTstatements to load initial departments and employees. - Test Constraints: Execute the invalid
INSERTstatement to observe the foreign key constraint failure. - Demonstrate Cascade: Run the
DELETEstatement on a department and useSELECTto verify that associated employees have been automatically removed.
Important
The ON DELETE CASCADE option is powerful for maintaining clean data, but must be used carefully to prevent unintended mass data deletion when parent records are removed.
Developed for Elevate Lab Internship Program - SQL Practice and Interview Preparation.