- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL FOREIGN KEY
A FOREIGN KEY is a constraint that establishes a relationship between two tables by linking a column in one table to the PRIMARY KEY of another table. It ensures referential integrity by preventing actions that would break the link between tables.
Key Features of SQL FOREIGN KEY
- Ensures that values in the child table match existing values in the parent table.
- Prevents deletion of a referenced record unless explicitly handled.
- Helps maintain data integrity between related tables.
- Can reference a single column or multiple columns (composite foreign key).
SQL FOREIGN KEY Syntax
Creating a FOREIGN KEY in Table Creation
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);
- The
EmployeeIDcolumn in theOrderstable is a foreign key referencing theEmployeeIDcolumn in theEmployeestable.
Adding a FOREIGN KEY to an Existing Table
ALTER TABLE Orders
ADD CONSTRAINT FK_Employee
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID);
Removing a FOREIGN KEY
ALTER TABLE Orders
DROP CONSTRAINT FK_Employee;
ON DELETE and ON UPDATE Actions
You can define how a FOREIGN KEY behaves when the referenced data changes.
Cascade Delete (Deletes related records)
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
ON DELETE CASCADE
);
- If an employee is deleted from
Employees, all their related orders inOrdersare also deleted.
Set NULL on Delete (Keeps records but sets foreign key to NULL)
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
ON DELETE SET NULL
);
- If an employee is deleted, the
EmployeeIDfield inOrdersis set toNULL.
Summary
- A
FOREIGN KEYlinks two tables by referencing thePRIMARY KEYof another table. - Ensures data integrity by preventing orphaned records.
- Supports
CASCADEandSET NULLactions for deletion and updates. - Can be added or removed using
ALTER TABLE.