Skip to main content
Log inGet a demo
← SQL Dictionary
Constraints

SQL FOREIGN KEY

What is SQL FOREIGN KEY?

A FOREIGN KEY constraint in SQL is a database constraint that defines a relationship between two tables. It ensures that values in a specific column or set of columns (the foreign key) in one table are linked to the values in another table (the primary key). This constraint maintains referential integrity, which means that data remains consistent and accurate across related tables.

When you would use it

You would use a FOREIGN KEY constraint in SQL when you need to:

  1. Establish Relationships: Define relationships between tables to enforce referential integrity and data consistency in a relational database.

  2. Implement Data Integrity: Ensure that the values in a foreign key column match the values in the primary key column of a related table, preventing orphaned records.

  3. Enforce Business Rules: Implement business rules that require certain relationships between entities, such as associating orders with customers or products with categories.

  4. Optimize Queries: Improve query performance by enabling efficient joins between related tables, making it easier to retrieve and analyze data.

Syntax

The syntax for defining a FOREIGN KEY constraint in SQL is as follows:

CREATE TABLE table_name1 (
    column1 data_type,
    column2 data_type,
    -- ...
    CONSTRAINT fk_name
    FOREIGN KEY (column_name)
    REFERENCES table_name2 (referenced_column)
);
  • table_name1: The name of the table in which you're defining the FOREIGN KEY constraint.
  • column1, column2, ...: The columns in the table.
  • fk_name: An optional name for the FOREIGN KEY constraint.
  • column_name: The column in table_name1 to which the FOREIGN KEY constraint is applied.
  • table_name2: The name of the related table.
  • referenced_column: The column in table_name2 that acts as the primary key.

Parameter values

  • table_name1: The name of the table in which you're defining the FOREIGN KEY constraint.
  • column1, column2, ...: The columns within table_name1 that may or may not have the FOREIGN KEY constraint.
  • fk_name: An optional name for the FOREIGN KEY constraint, which can be used to reference the constraint.
  • column_name: The column in table_name1 to which you're applying the FOREIGN KEY constraint.
  • table_name2: The name of the related table that contains the primary key.
  • referenced_column: The column in table_name2 that serves as the primary key, to which the FOREIGN KEY in table_name1 refers.

Example query

Here's an example SQL query that creates a "orders" table with a FOREIGN KEY constraint that links the "customer_id" column to the "customers" table's "customer_id" primary key:

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    -- Other columns
    CONSTRAINT fk_customer
    FOREIGN KEY (customer_id)
    REFERENCES customers (customer_id)
);

Example table response

The "orders" table is created with a FOREIGN KEY constraint linking the "customer_id" column to the "customers" table. The table might contain data like this:

order_idcustomer_idorder_date
11012023-10-19
21022023-10-20
31032023-10-21

Use cases

  1. Establish Relationships: To define relationships between tables, enforce referential integrity, and ensure that data remains consistent and accurate across related tables.

  2. Implement Data Integrity: To prevent the creation of orphaned records by ensuring that values in the foreign key column match values in the primary key column of a related table.

  3. Enforce Business Rules: To implement business rules that require certain relationships between entities, such as associating orders with customers or products with categories.

  4. Optimize Queries: To improve query performance by enabling efficient joins between related tables, making it easier to retrieve and analyze data.

SQL Languages Availability

The concept of a FOREIGN KEY constraint is available in most SQL-based relational database management systems (RDBMS), including but not limited to:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • IBM Db2
  • SQLite

While the core concept of FOREIGN KEY constraints is universal, the specific implementation and syntax may vary between different database systems. Always consult your specific RDBMS documentation for precise details on using FOREIGN KEY constraints.


Keep reading

SQL PRIMARY KEY

Read article

SQL CHECK

Read article

SQL DEFAULT

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.