Welcome to our SQL Constraints Tutorial! In this lesson, we'll dive deep into understanding the role of constraints in databases, and learn about different types of SQL constraints. By the end, you'll be able to create robust and reliable databases for your projects. 🎯
Constraints play a crucial role in maintaining the integrity of your database. They ensure that the data entering your database adheres to specific rules, enhancing the overall quality and reliability of your data.
A primary key is a column or a set of columns in a table that uniquely identify each row. It is used to enforce the uniqueness of the data in the column or columns specified.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);In the example above, EmployeeID is the primary key column, ensuring that each employee in the table is uniquely identified.
A unique constraint guarantees that the values in a column or a set of columns are unique, but unlike the primary key, it can allow null values.
Example:
CREATE TABLE Employees (
EmployeeID INT,
EmployeeEmail VARCHAR(100) UNIQUE,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);In this example, EmployeeEmail is a unique column, ensuring that no two employees have the same email address.
A foreign key is a field in one table that refers to the primary key of another table. It ensures referential integrity by preventing the deletion or modification of data that would violate the foreign key's relationship with the primary key.
Example:
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
DepartmentID INT,
DepartmentID FOREIGN KEY REFERENCES Departments(DepartmentID)
);In this example, DepartmentID is a foreign key that references the DepartmentID in the Departments table.
A check constraint is used to verify that data in a column adheres to a specified condition.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Age INT CHECK (Age >= 18 AND Age <= 65),
FirstName VARCHAR(50),
LastName VARCHAR(50)
);In this example, the Age column must have a value between 18 and 65.
A default constraint assigns a default value to a column when no value is provided during an insert operation.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
HireDate DATE DEFAULT CURRENT_DATE,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);In this example, if no HireDate is provided during an insert, the current date will be used.
You can create constraints when creating a table, or you can alter an existing table to add constraints.
ALTER TABLE Employees ADD CONSTRAINT UK_EmployeeEmail UNIQUE (EmployeeEmail);The above SQL statement adds a unique constraint to the EmployeeEmail column in the Employees table.
What is the purpose of a primary key constraint?
What is the difference between a primary key and a unique constraint?
That's it for this lesson! In the next lesson, we'll dive deeper into each constraint type and see how to work with them in real-world scenarios. Happy learning! 🎯 🎉