foreign key
A foreign key links one table to another by referencing its primary key. Learn how foreign keys enforce referential integrity, with SQL syntax, composite key examples, and data warehousing use cases.
Key Takeaways
- Definition: A Foreign Key (FK) is a column that links two tables together by referencing the Primary Key of another table.
- Purpose: It maintains Referential Integrity, ensuring relationships between data remain consistent.
- DuckDB Context: DuckDB supports standard SQL foreign key constraints, which are useful for ensuring data quality in your warehouse, though they incur a small performance cost during write operations.
- Usage: Essential for joining dimensions and facts in Star Schema data modeling.
Foreign Key Definition
A foreign key is a column (or collection of columns) in a table that links to the primary key in another table. It is a critical component of relational database systems, used to establish and enforce a link between data in two tables. This link ensures referential integrity, meaning the database prevents you from having data in one table that points to non-existent data in another.
Foreign Key Syntax in DuckDB
In DuckDB and other databases, you define a foreign key as part of your table creation using the FOREIGN KEY constraint. This tells the database to enforce the relationship between tables by preventing invalid references. Here's an example:
Copy code
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
total DECIMAL(10,2)
);
CREATE TABLE order_items (
id INTEGER PRIMARY KEY,
order_id INTEGER,
product_name VARCHAR(100),
quantity INTEGER,
FOREIGN KEY (order_id) REFERENCES orders(id)
);
In this example, order_id in the order_items table is a foreign key that references the id column in the orders table. DuckDB will prevent you from:
- Inserting an order item with an
order_idthat doesn't exist in theorderstable - Deleting an order from
ordersif there are still items referencing it inorder_items
Primary Key vs. Foreign Key
Understanding the distinction between these two keys is fundamental to relational database design:
| Feature | Primary Key | Foreign Key |
|---|---|---|
| Uniqueness | Must be unique for every row. | Can accept duplicate values. |
| Nullability | Cannot be NULL. | Can be NULL (unless specified otherwise). |
| Function | Identifies the record itself. | Identifies a relationship to another table. |
| Count | Only one per table. | A table can have multiple foreign keys. |
Composite Foreign Keys
You can also create foreign keys that reference multiple columns, known as composite foreign keys:
Copy code
CREATE TABLE locations (
country_code CHAR(2),
region_code CHAR(2),
city VARCHAR(100),
PRIMARY KEY (country_code, region_code)
);
CREATE TABLE stores (
id INTEGER PRIMARY KEY,
country_code CHAR(2),
region_code CHAR(2),
address VARCHAR(200),
FOREIGN KEY (country_code, region_code)
REFERENCES locations(country_code, region_code)
);
Foreign Keys in Data Warehousing
In transactional databases (OLTP), foreign keys are strictly enforced to prevent errors. in Data Warehousing (OLAP) scenarios like those used with MotherDuck, foreign keys play a dual role:
- Data Quality: They prevent 'orphaned' records in your fact tables.
- Join Optimization: Defining foreign keys helps the query optimizer understand the relationships between tables, potentially speeding up JOIN operations in complex analytical queries.
Performance Considerations
Foreign keys automatically create an index on the referencing columns in DuckDB, which can help with query performance when joining tables. However, they also add overhead during data modifications since the database must check referential integrity with each insert or update.
While DuckDB indexes foreign keys for faster joins, enabling constraints does check every INSERT or UPDATE. For massive bulk loads in a data warehouse context, engineers sometimes temporarily disable constraints to maximize load speed, re-enabling them afterwards to check integrity.
Related terms
Referential integrity is the guarantee that a reference from one table to another — typically a foreign key — always points to a row that actually exists, preventing orphaned records.
primary key →A primary key uniquely identifies each database row. Learn SQL definitions, examples, UUID vs auto-increment best practices, and how to fix constraint errors.
Composite key →A composite key is a primary key made up of two or more columns whose combined values uniquely identify a row, used when no single column is unique on its own.
Surrogate key →A surrogate key is a system-generated identifier — typically a sequential integer or UUID — assigned to a row that has no business meaning of its own, used as the primary key in dimension and fact tables.
Natural key →A natural key is an identifier that comes from real-world business data — like an email address, SSN, or product SKU — as opposed to a surrogate key generated purely for database use.
Fact table →A fact table is the central table in a dimensional model that stores numeric measures (like revenue or quantity) alongside foreign keys linking to descriptive dimension tables.
FAQS
A primary key uniquely identifies each row within its own table and cannot be NULL. A foreign key is a column that references another table's primary key, creating a link and enforcing referential integrity. The primary key defines identity; the foreign key defines a relationship.
Yes. Unless the column is defined as NOT NULL, a foreign key can contain NULL values, meaning the row has no related record in the referenced table. DuckDB supports standard SQL foreign key constraints.
Yes, by default. DuckDB rejects inserts or updates that would reference a non-existent row in the parent table, guaranteeing referential integrity at a small performance cost during writes.

