What is fk in sql
Last updated: April 1, 2026
Key Facts
- A Foreign Key ensures referential integrity by preventing invalid relationships between tables
- FK values in a child table must exist as primary key values in the parent table
- Foreign Keys automatically prevent deletion of parent records that are still referenced
- Multiple Foreign Keys can reference different tables from the same table
- Setting ON DELETE CASCADE allows automatic deletion of child records when parent records are deleted
What is a Foreign Key?
A Foreign Key (FK) is a database constraint that creates a link between two tables. It references the primary key of another table, establishing a parent-child relationship. This ensures that data in one table matches with data in another table, maintaining database integrity.
How Foreign Keys Work
When you define a Foreign Key on a column, the database enforces that every value in that column must either be NULL or match an existing primary key value in the referenced table. For example, if a "Orders" table has a Foreign Key referencing the "Customers" table's primary key, every order must be linked to a valid customer.
Benefits of Using Foreign Keys
- Referential Integrity: Prevents orphaned records where a child record references a non-existent parent
- Data Consistency: Ensures relationships between tables remain valid
- Cascading Actions: Automatically handles updates or deletions across related tables
- Query Optimization: The database can optimize joins using foreign key relationships
Foreign Key Constraints
Foreign Keys support several constraint actions: RESTRICT prevents deletion of referenced rows, CASCADE automatically deletes child rows, SET NULL sets the foreign key to NULL when the parent is deleted, and NO ACTION is similar to RESTRICT but checked at transaction end. These actions determine how the database handles changes to parent records.
Creating Foreign Keys in SQL
You define a Foreign Key using the ALTER TABLE or CREATE TABLE statement. The syntax typically includes FOREIGN KEY (column_name) REFERENCES parent_table(primary_key). Most modern databases support Foreign Keys, including MySQL, PostgreSQL, SQL Server, and Oracle, making them a standard feature for maintaining database integrity.
Related Questions
What is the difference between primary key and foreign key?
A primary key uniquely identifies records within a table and cannot be NULL, while a foreign key references another table's primary key to create relationships. Primary keys enforce uniqueness; foreign keys enforce referential integrity.
What does ON DELETE CASCADE do?
ON DELETE CASCADE automatically deletes all child records when their parent record is deleted. This maintains referential integrity by ensuring no orphaned child records remain in the database.
Can a foreign key be NULL?
Yes, a foreign key column can be NULL unless explicitly defined as NOT NULL. A NULL foreign key means the record has no relationship to a parent record, which is valid in most database designs.
More What Is in Daily Life
Also in Daily Life
More "What Is" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Wikipedia - Foreign KeyCC-BY-SA-4.0
- PostgreSQL Documentation - ConstraintsCC-BY-SA-3.0