Attribute Constraints in SQL
1. NOT NULL Constraint
Ensures that a column cannot have NULL values.
Example:
Sql> CREATE TABLE Employees (EmployeeID INT NOT NULL, Name VARCHAR(50) NOT
NULL, Department VARCHAR(50));
Sql> INSERT INTO Employees (EmployeeID, Name, Department) VALUES (1, 'Alice', 'HR'); --
Valid
Sql> INSERT INTO Employees (EmployeeID, Department) VALUES (2, 'IT'); -- Error (Name
cannot be NULL)
2. UNIQUE Constraint
Ensures that all values in a column are unique.
Example:
Sql> CREATE TABLE Products ( ProductID INT UNIQUE, ProductName VARCHAR(100), Price
DECIMAL(10, 2) );
INSERT INTO Products (ProductID, ProductName, Price) VALUES (1, 'Laptop', 45000.00); --
Valid
INSERT INTO Products (ProductID, ProductName, Price) VALUES (1, 'Phone', 20000.00); --
Error (Duplicate ProductID)
3. PRIMARY KEY Constraint
A combination of NOT NULL and UNIQUE. Ensures each row is uniquely identified.
Example:
Sql> CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100), Age
INT );
Sql> INSERT INTO Students (StudentID, Name, Age) VALUES (101, 'John', 20); -- Valid
Sql> INSERT INTO Students (StudentID, Name, Age) VALUES (101, 'Jane', 22); -- Error
(Duplicate StudentID)
4. FOREIGN KEY Constraint
Used to establish a relationship between two tables.
Example:
Sql> CREATE TABLE Departments (DepartmentID INT PRIMARY KEY, DepartmentName
VARCHAR(100));
Sql> CREATE TABLE Employees (EmployeeID INT PRIMARY KEY, Name VARCHAR(100),
DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES
Departments(DepartmentID));
Sql> INSERT INTO Departments (DepartmentID, DepartmentName) VALUES (1, 'HR'); -- Valid
Sql> INSERT INTO Employees (EmployeeID, Name, DepartmentID) VALUES (101, 'Alice', 1); -- Valid
Sql> INSERT INTO Employees (EmployeeID, Name, DepartmentID) VALUES (102, 'Bob', 2); -- Error
(DepartmentID 2 does not exist)
5. DEFAULT Constraint
Sets a default value for a column when no value is specified.
Example:
Sql> CREATE TABLE Orders ( OrderID INT PRIMARY KEY, OrderDate DATE DEFAULT
CURRENT_DATE, Quantity INT DEFAULT 1);
Sql> INSERT INTO Orders (OrderID) VALUES (1); -- OrderDate = CURRENT_DATE, Quantity = 1
INSERT INTO Orders (OrderID, Quantity) VALUES (2, 5); -- OrderDate = CURRENT_DATE
6. CHECK Constraint
Ensures that values in a column satisfy a specific condition.
Example:
Sql> CREATE TABLE Accounts (AccountID INT PRIMARY KEY, Balance DECIMAL(10, 2)
CHECK (Balance >= 0) );
Sql> INSERT INTO Accounts (AccountID, Balance) VALUES (101, 500.00); -- Valid
Sql> INSERT INTO Accounts (AccountID, Balance) VALUES (102, -50.00); -- Error (Balance
cannot be negative)
7. AUTO_INCREMENT (MySQL Specific)
Automatically generates a unique value for the column.
Example:
Sql> CREATE TABLE Customers ( CustomerID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100));
Sql> INSERT INTO Customers (Name) VALUES ('Alice'); -- CustomerID = 1
Sql> INSERT INTO Customers (Name) VALUES ('Bob'); -- CustomerID = 2
Combined Practice Scenario
Create Tables and Work with All Constraints:
Sql> CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(100) NOT
NULL );
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL,
DepartmentID INT, Salary DECIMAL(10, 2) DEFAULT 30000.00, JoinDate DATE DEFAULT
CURRENT_DATE, FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID),
CHECK (Salary > 0) );
-- Insert Practice Data
INSERT INTO Departments (DepartmentID, DepartmentName) VALUES (1, 'HR'), (2, 'IT');
INSERT INTO Employees (EmployeeID, Name, DepartmentID, Salary) VALUES (101, 'Alice', 1, 50000.00); -- Valid
INSERT INTO Employees (EmployeeID, Name, DepartmentID) VALUES (102, 'Bob', 2); -- Uses default Salary
INSERT INTO Employees (EmployeeID, Name, Salary) VALUES (103, 'Charlie', -1000.00); -- Error (Salary cannot
be negative)