SQL DDL and DML/Add a constraint

Attach a foreign key or check constraint after table creation.

Section: CREATE, ALTER, and DROP

Add a constraint

sql
sql
ALTER TABLE orders
ADD CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers(customer_id);
Explanation

Constraints help maintain relational integrity.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing CREATE, ALTER, and DROP
Add a column
Modify an existing table by adding a new column.
OpenIn sheetsqlsame section
Create table
Define a new table with columns and constraints.
OpenIn sheetsqlsame section
Drop table
Remove a table definition and its data.
OpenIn sheetsqlsame section
Truncate table
Remove all rows quickly without dropping the table.
OpenIn sheetsqlsame section
Define primary key
Mark a column as the main row identifier.
OpenIn sheetsql1 tag match
Define unique constraint
Ensure a column or column set stays unique.
OpenIn sheetsql1 tag match