The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
SQL Constraints are rules applied to table columns to
control what data can be stored in a database. They help maintain
data accuracy, consistency, and integrity.
What are SQL Constraints?
Constraints act like validation rules at the database level.
They ensure that only valid data is inserted, updated, or deleted.
Why Constraints are Important
Prevent invalid data entry
Maintain data integrity
Enforce business rules
Improve data consistency across tables
Types of SQL Constraints
1. NOT NULL
Ensures that a column cannot have NULL (empty) values
CREATE TABLE Users (
Id INT,
Name VARCHAR(100) NOT NULL
);
CREATE TABLE Employees (
Age INT CHECK (Age >= 18)
);
6. DEFAULT
Sets a default value if none is provided
CREATE TABLE Users (
Status VARCHAR(20) DEFAULT 'Active'
);
Example with Multiple Constraints
CREATE TABLE Students (
Id INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Email VARCHAR(100) UNIQUE,
Age INT CHECK (Age >= 18),
Status VARCHAR(20) DEFAULT 'Active'
);
Table-Level vs Column-Level Constraints
Column-Level → Applied directly to a column
Table-Level → Applied to the whole table (useful for composite keys)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
SQL Constraints are rules applied to table columns to control what data can be stored in a database. They help maintain data accuracy, consistency, and integrity.
What are SQL Constraints?
Why Constraints are Important
Types of SQL Constraints
1. NOT NULL
Ensures that a column cannot have NULL (empty) values
2. UNIQUE
Ensures all values in a column are different
3. PRIMARY KEY
4. FOREIGN KEY
Creates a relationship between two tables
5. CHECK
Ensures values meet a specific condition
6. DEFAULT
Sets a default value if none is provided
Example with Multiple Constraints
Table-Level vs Column-Level Constraints
Advantages
In One Line
SQL Constraints are rules that enforce valid and consistent data in a database.