MODULO SQL & Database Design Master SeriesLAB

Constraints: The Guardians of Data Integrity

Introduction to Constraints

Constraints are the rules that govern the data in a database table. They ensure data integrity by enforcing specific conditions on the data, preventing invalid or inconsistent data from being entered. In this masterclass, we will delve into the world of constraints, exploring the different types, their uses, and examples.

Types of Constraints

There are five primary types of constraints in SQL:

  1. NOT NULL Constraint
  2. UNIQUE Constraint
  3. CHECK Constraint
  4. DEFAULT Constraint
  5. FOREIGN KEY Constraint

1. NOT NULL Constraint

The NOT NULL constraint ensures that a column cannot contain null values. It is used to specify that a column must always have a value.

Example:

CREATE TABLE Employees (
    EmployeeID INT NOT NULL,
    Name VARCHAR(255),
    Age INT
);

In this example, the EmployeeID column cannot be null.

2. UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are unique. It is used to prevent duplicate values in a column.

Example:

CREATE TABLE Employees (
    EmployeeID INT UNIQUE,
    Name VARCHAR(255),
    Age INT
);

In this example, the EmployeeID column must have unique values.

3. CHECK Constraint

The CHECK constraint ensures that all values in a column satisfy a specific condition. It is used to limit the range of values that can be entered in a column.

Example:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(255),
    Age INT CHECK (Age >= 18)
);

In this example, the Age column must have values greater than or equal to 18.

4. DEFAULT Constraint

The DEFAULT constraint provides a default value for a column when no value is specified. It is used to ensure that a column always has a value, even if no value is provided.

Example:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(255),
    Country VARCHAR(255) DEFAULT 'USA'
);

In this example, the Country column will have a default value of 'USA' if no value is specified.

5. FOREIGN KEY Constraint

The FOREIGN KEY constraint ensures that the values in a column match the values in another table. It is used to link two tables together.

Example:

CREATE TABLE Departments (
    DepartmentID INT PRIMARY KEY,
    DepartmentName VARCHAR(255)
);

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(255),
    DepartmentID INT,
    FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

In this example, the DepartmentID column in the Employees table must match the DepartmentID column in the Departments table.

Composite Constraints

Constraints can also be applied to multiple columns. This is known as a composite constraint.

Example:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(255),
    Age INT,
    UNIQUE (EmployeeID, Name)
);

In this example, the combination of EmployeeID and Name must be unique.

Constraint Naming

Constraints can be named for easier identification and management.

Example:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(255),
    Age INT,
    CONSTRAINT chk_Age CHECK (Age >= 18)
);

In this example, the CHECK constraint is named chk_Age.

Altering Constraints

Constraints can be added or dropped from an existing table using the ALTER TABLE statement.

Example:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(255),
    Age INT
);

ALTER TABLE Employees
ADD CONSTRAINT chk_Age CHECK (Age >= 18);

In this example, the CHECK constraint is added to the Employees table.

Dropping Constraints

Constraints can be dropped from a table using the ALTER TABLE statement.

Example:

CREATE TABLE Employees (
    EmployeeID INT,
    Name VARCHAR(255),
    Age INT,
    CONSTRAINT chk_Age CHECK (Age >= 18)
);

ALTER TABLE Employees
DROP CONSTRAINT chk_Age;

In this example, the CHECK constraint is dropped from the Employees table.

Conclusion

Constraints are an essential part of database design, ensuring data integrity and consistency. Understanding the different types of constraints and how to apply them is crucial for effective database management.

⚔️ YOUR MISSION

Mission:

Create a database schema for a simple e-commerce application. The schema should include the following tables:

  • Customers: CustomerID (primary key), Name, Email, Address
  • Orders: OrderID (primary key), CustomerID (foreign key), OrderDate, Total
  • Products: ProductID (primary key), ProductName, Price
  • OrderDetails: OrderID (foreign key), ProductID (foreign key), Quantity

Apply the following constraints:

  • CustomerID and Email must be unique in the Customers table.
  • OrderID must be unique in the Orders table.
  • ProductID must be unique in the Products table.
  • The combination of OrderID and ProductID must be unique in the OrderDetails table.
  • The Total column in the Orders table must always be greater than or equal to 0.
  • The Quantity column in the OrderDetails table must always be greater than or equal to 1.
SQL practice editor
Read the exercise prompt
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    Name VARCHAR(255),
    Email VARCHAR(255) UNIQUE,
    Address VARCHAR(255)
);

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    Total DECIMAL(10, 2) CHECK (Total >= 0),
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(255),
    Price DECIMAL(10, 2)
);

CREATE TABLE OrderDetails (
    OrderID INT,
    ProductID INT,
    Quantity INT CHECK (Quantity >= 1),
    PRIMARY KEY (OrderID, ProductID),
    FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
    FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
WAITING FOR SIGNAL...
BACKNEXT STAGE