What are the Constraints in SQL Server?
In SQL Server, constraints are used to enforce rules and maintain data integrity within the database. Let's explore each constraint type with an example:
1-Primary Key Constraint:
This constraint ensures that a column or a combination of columns uniquely identifies each row in a table. It enforces uniqueness and non-nullability.
Example:
Consider a table called "Employees" with columns: EmployeeID (primary key), FirstName, LastName, and Email.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100)
);
In this example, the "EmployeeID" column is defined as the primary key, guaranteeing that each value in the column is unique and not null.
2-Foreign Key Constraint:
This constraint establishes a relationship between two tables based on the values of a column(s). It ensures referential integrity.
Example:
Let's assume we have two tables, "Employees" and "Departments." The "Employees" table has a foreign key column "DepartmentID" referencing the primary key column "DepartmentID" in the "Departments" table.
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);
In this example, the foreign key constraint ensures that the values in the "DepartmentID" column of the "Employees" table exist in the "DepartmentID" column of the "Departments" table, maintaining the referential integrity.
3-Unique Constraint:
This constraint ensures that the values in a column(s) are unique within a table.
Example:
Consider a table called "Students" with columns: StudentID (unique), FirstName, LastName, and Email.
CREATE TABLE Students (
StudentID INT UNIQUE,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100)
);
In this example, the "StudentID" column is defined as unique, ensuring that each value in the column is distinct.
4-Check Constraint:
This constraint defines a condition that each row must satisfy. It restricts the values that can be inserted or updated in a column(s) based on the specified condition.
Example:
Suppose we have a table called "Products" with columns: ProductID, ProductName, Quantity, and Price. We want to ensure that the quantity is always greater than zero.
CREATE TABLE Products (
ProductID INT,
ProductName VARCHAR(50),
Quantity INT CHECK (Quantity > 0),
Price DECIMAL(10, 2)
);
In this example, the check constraint "CHECK (Quantity > 0)" ensures that the value in the "Quantity" column is always greater than zero.
5-Default Constraint:
This constraint assigns a default value to a column when no explicit value is specified during an insert operation.
Example:
Let's say we have a table called "Orders" with columns: OrderID, OrderDate (default to current date), and TotalAmount.
CREATE TABLE Orders (
OrderID INT,
OrderDate DATE DEFAULT GETDATE(),
TotalAmount DECIMAL(10, 2)
);
In this example, the "OrderDate" column is assigned a default value of the current date using the default constraint.
6-Not Null Constraint:
This constraint ensures that a column does not contain null values.
Consider a table called "Customers" with columns: CustomerID (not null), FirstName, LastName, and Email.
CREATE TABLE Customers (
CustomerID INT NOT NULL,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100)
);
In this example, the "CustomerID" column is defined as not null, which means it cannot contain null values. Whenever a new row is inserted into the "Customers" table, a non-null value must be provided for the "CustomerID" column. If an insert operation is attempted without specifying a value for the "CustomerID" column or with a null value, it will result in a constraint violation error. The not null constraint ensures that the "CustomerID" column always has a valid, non-null value for each row.