Data Constraints
Data constraints in SQL are used to specify rules for the data in a table. Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table.
Types of SQL Data Constraints
-
NOT NULL Constraint: Ensures that a column cannot have a NULL value.
For Example:
CREATE TABLE Students (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Age int
); -
UNIQUE Constraint: Ensures that all values in a column are different.
For Example:
CREATE TABLE Students (
ID int NOT NULL UNIQUE,
Name varchar(255) NOT NULL,
Age int
); -
PRIMARY KEY Constraint: Uniquely identifies each record in a database table. Primary keys must contain UNIQUE values. Exactly the same as the UNIQUE constraint but there can be many unique constraints in a table, but only one PRIMARY KEY constraint per table.
For Example:
CREATE TABLE Students (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Age int,
PRIMARY KEY (ID)
); -
FOREIGN KEY Constraint: Prevents actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
For Example:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
ID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (ID) REFERENCES Students(ID)
); -
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfies certain conditions.
For Example:
CREATE TABLE Students (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Age int,
CHECK (Age>=18)
); -
DEFAULT Constraint: Provides a default value for a column when none is specified.
For Example:
CREATE TABLE Students (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Age int,
City varchar(255) DEFAULT 'Unknown'
); -
INDEX Constraint: Used to create and retrieve data from the database very quickly.
For Example:
CREATE INDEX idx_name
ON Students (Name);Note: Indexes are not a part of the SQL standard and are not supported by all databases.