SQL > Constraint

You can place constraints to limit the type of data that can go into a table. Such constraints can be specified when the table when the table is first created via the CREATE TABLE statement, or after the table is already created via the ALTER TABLE statement.

SQL constraints are rules that enforce data integrity by restricting the type and format of data stored in a table column. They can be defined at table creation or added later via ALTER TABLE.

Common types of constraints include the following:

Each constraint is discussed in the following sections.

Frequently Asked Questions

What are SQL constraints?
SQL constraints are rules applied to table columns that limit the type or range of data that can be stored. They ensure data accuracy and integrity. Common constraints include NOT NULL, DEFAULT, UNIQUE, CHECK, PRIMARY KEY, and FOREIGN KEY. They can be added when a table is created or via ALTER TABLE afterward.
What is the difference between PRIMARY KEY and UNIQUE constraints?
A PRIMARY KEY uniquely identifies each row and does not permit NULL values — a table can have only one primary key. A UNIQUE constraint also enforces uniqueness but allows one NULL value, and a table can have multiple UNIQUE constraints on different columns.
What is a FOREIGN KEY constraint in SQL?
A FOREIGN KEY constraint maintains referential integrity between two tables by requiring that a column's value matches a value in the referenced table's primary key. This prevents orphaned records and ensures related data remains consistent.
When should I use a CHECK constraint?
Use a CHECK constraint to validate that column values meet specific criteria — for example, ensuring a salary column is greater than zero, or a status column only contains allowed values. CHECK constraints run on every INSERT and UPDATE operation.

Next: SQL NOT NULL

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact