You are on page 1of 15

Types Of Constraints

In DBMS
Constraints
Constraints play a pivotal role in upholding the integrity and dependability of stored data. They
establish rules and conditions that govern the data within a database, ensuring precision,
consistency, and compliance with business rules.
What Are Constraints In DBMS?
Constraints in DBMS (Database Management Systems) encompass rules or conditions applied to
database data, ensuring data integrity, consistency, and adherence to business rules. They define
limitations and requirements that data must meet, preventing the entry of invalid or inconsistent
data. Constraints serve as pre-established rules governing the behavior and relationships of data
in a database, contributing to the maintenance of accuracy and reliability.

The purpose of constraints is to enforce data quality and thwart data inconsistencies, thereby
bolstering the overall data integrity and reliability of the database. Constraints set boundaries for
data values, relationships between entities, uniqueness requirements, and more. Through
constraint enforcement, DBMS ensures data conforms to predefined standards and business rules,
fortifying the database’s robustness and reliability.
Types Of Constraints In DBMS

Within relational databases, there are primarily five types of constraints, commonly referred to as
relational constraints. These include:
1.Domain Constraints
2.Key Constraints
3.Entity Integrity Constraints
4.Referential Integrity Constraints
5.Tuple Uniqueness Constraints
Domain Constraints
•Define the valid values that an attribute (column) can hold.
•Specify the data type (e.g., integer, string, date) and any additional restrictions (e.g., range of values,
allowed patterns).

Types of Domain Constraints


1.Check Constraint: A check constraint in a Database Management System (DBMS) is a way to enforce
certain conditions on the values that are stored in a database. It is a rule or condition that is specified at the
time of table creation or alteration to restrict the values that can be inserted or updated in a column.
Here’s a basic syntax for creating a check constraint in a table using SQL (Structured Query Language):
CREATE TABLE TableName (
Column1 DataType,
Column2 DataType,
-- Other columns
CONSTRAINT ConstraintName CHECK (condition)
);
In the above syntax, the condition is the expression or condition that must be satisfied for the data in the
specified column(s).
Example: Here’s an example to illustrate the concept. Let’s say we have a table for storing
employee information, and we want to ensure that his Salary is always greater than zero and
the Department is one of the specified values.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Salary DECIMAL(10, 2) CHECK (Salary > 0),
Department VARCHAR(50) CHECK (Department IN ('HR', 'IT', 'Finance'))
);

With this check constraint, any attempt to insert or update a record in the Employee table with
a Salary less than 0 or a Department other than HR, IT, or Finance will result in a constraint
violation error.
2. Not Null Constraint: The NOT NULL constraint in a Database Management System (DBMS) is used to
ensure that a column in a table cannot contain any NULL values. NULL is a special marker used in
databases to indicate that a data value does not exist in the database. The NOT NULL constraint ensures that
a column always has a value, and it cannot be left empty.
Here’s the basic syntax for creating a NOT NULL constraint while defining a table using SQL (Structured
Query Language):
CREATE TABLE TableName (
Column1 DataType NOT NULL,
Column2 DataType,
-- Other columns
);

In this syntax:
•TableName is the name of the table.
•Column 1, Column 2, and so on are the columns in the table.
•DataType is the data type of the column.
•The NOT NULL constraint is used after the data type to indicate that the column cannot contain NULL
values.
Example: Here’s a simple example using an “Employees” table where the “FirstName” column
cannot be NULL:
CREATE TABLE Employees (
EmployeeID INT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50),
-- Other columns
);

With this NOT NULL constraint, every record in the “Employees” table must have a non-null
value in the “FirstName” column. If an attempt is made to insert a record without specifying a
value for “FirstName” or updating an existing record to set “FirstName” to NULL, it will result in
a constraint violation error.
Key Constraints
A key constraint in a Database Management System (DBMS) refers to a set of rules applied to
one or more columns in a database table to ensure the uniqueness and integrity of data. Keys are
used to uniquely identify rows in a table, and they play a fundamental role in establishing
relationships between tables. There are several types of key constraints, each serving a specific
purpose:
1.Primary Key Constraint:
•A primary key is a column or a set of columns that uniquely identifies each row in a table.
•The primary key constraint ensures that the values in the specified columns are unique and not
NULL.
•There can be only one primary key in a table.
Example: Let’s consider a simple example of a primary key constraint in a relational database using a Students table:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
-- Other columns
);
In this example:
1.StudentID is the primary key for the Students table.
2.FirstName, LastName, Age, and other columns are attributes associated with each student.
Here’s how this primary key constraint works:
a. Inserting Data:
-- Inserting two students with unique StudentID values
INSERT INTO Students (StudentID, FirstName, LastName, Age)
VALUES (1, 'John', 'Doe', 20);

INSERT INTO Students (StudentID, FirstName, LastName, Age)


VALUES (2, 'Jane', 'Smith', 22);
This is valid because each StudentID is unique.
-- Trying to insert a student with a duplicate StudentID
INSERT INTO Students (StudentID, FirstName, LastName, Age)
VALUES (1, 'Bob', 'Johnson', 21);
This would result in a constraint violation error since the primary key must be unique, and ‘1’ is already used.
2. Unique Constraint:
•A unique constraint ensures that all values in a column or a set of columns are unique.
•Unlike the primary key, a unique constraint allows NULL values.
Example: Suppose we have a Products table where we want to ensure that each product has a unique product code:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductCode VARCHAR(20) UNIQUE,
ProductName VARCHAR(100),
Price DECIMAL(10, 2),
-- Other columns
);
In this example:
1.ProductID is the primary key that uniquely identifies each product.
2.ProductCode is a column with a unique key constraint.
The UNIQUE constraint on the ProductCode column ensures that each value in this column must be unique across all rows in the
Products table. It means that we cannot have two products with the same product code.
Here’s how this unique key constraint works:
a. Inserting Data:
-- Inserting two products with different product codes
INSERT INTO Products (ProductID, ProductCode, ProductName, Price)
VALUES (1, 'P001', 'Product A', 49.99);

INSERT INTO Products (ProductID, ProductCode, ProductName, Price)


VALUES (2, 'P002', 'Product B', 29.99);
This is valid because the product codes (‘P001’ and ‘P002’) are unique.
-- Trying to insert a product with a duplicate product code
INSERT INTO Products (ProductID, ProductCode, ProductName, Price)
VALUES (3, 'P001', 'Product C', 19.99);
This would result in a constraint violation error since ‘P001’ is already used as a product code.
•The foreign key constraint ensures referential integrity, meaning that relationships between tables are maintained, and it helps
prevent inconsistencies in the data. It’s a powerful tool for enforcing relationships between tables in a relational database.
Example: Let’s consider a simple example to illustrate the use of a foreign key constraint in a relational database. Suppose we
have two tables: Customers and Orders.
1.Customers Table:
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100) UNIQUE,
-- Other columns
);
2. Orders Table:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10, 2),
-- Other columns
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
In this example:
2.In the Customers table, CustomerID is the primary key that uniquely identifies each customer. It is also referenced by the
foreign key in the Orders table.
2.In the Orders table, OrderID is the primary key that uniquely identifies each order. The CustomerID column is a foreign key that
establishes a relationship with the Customers table.
3.The FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) statement in the Orders table specifies that the
CustomerID column in the Orders table is a foreign key, and it must refer to a valid CustomerID in the Customers table.
Here’s how this foreign key constraint works:
a. Inserting Data:
-- Insert a customer
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
VALUES (1, 'John', 'Doe', 'john.doe@example.com');

-- Insert an order referencing the customer


INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
VALUES (101, 1, '2024-01-06', 150.00);
This ensures that when you insert an order into the Orders table, the CustomerID must exist in the Customers table.
Entity Integrity Constraint
The Entity Integrity Constraint is essentially a subset of the Key constraint in a database. While the Key constraint ensures that
Primary Key attributes are unique and non-null, the Entity Integrity Constraint specifically emphasizes that no attribute of a
Primary Key should contain null values. This constraint highlights the perspective that allowing null values in Primary Key
attributes could lead to multiple null entries, violating the uniqueness requirement for each tuple in the Primary Key. Therefore, the
Entity Integrity Constraint reinforces the importance of non-null values within the Primary Key to maintain the uniqueness of each
record in a relational database.
Example: Let’s consider an example to illustrate entity constraints in a database. Assume we have a table named Employees with
the following structure:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
-- Other columns
);
Now, let’s insert some data to demonstrate the entity integrity constraints:
-- Valid data insertion
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (1, 'John', 'Doe', 'john.doe@example.com');

-- Invalid data insertion (violates NOT NULL constraint)


INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (2, NULL, 'Smith', 'smith@example.com');
-- This would result in an error since FirstName cannot be NULL

-- Invalid data insertion (violates PRIMARY KEY constraint)


INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (1, 'Jane', 'Doe', 'jane.doe@example.com');
-- This would result in an error since EmployeeID must be unique

-- Valid data insertion


INSERT INTO Employees (EmployeeID, FirstName, LastName, Email)
VALUES (2, 'Alice', 'Johnson', 'alice@example.com');
referencing attribute to be a subset of the referred attribute.
This ensures that records cannot be inserted in the referencing relation unless they exist in the referenced relation. Furthermore, any
record present in the referencing relation cannot be updated or deleted from the referenced relation, maintaining the accuracy and
coherence of the relational database.
Example: Let’s consider an example with two tables: Orders and Customers.
-- Customers table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100) UNIQUE,
-- Other columns
);

-- Orders table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10, 2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Now, let’s perform some operations to demonstrate referential integrity:
-- Valid data insertion
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
VALUES (1, 'John', 'Doe', 'john.doe@example.com');

INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)


VALUES (101, 1, '2024-01-06', 150.00);

-- Invalid data insertion (violates foreign key constraint)


INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
VALUES (102, 2, '2024-01-07', 100.00);
-- This would result in an error since CustomerID 2 does not exist in the Customers table

-- Valid data update


UPDATE Customers
SET FirstName = 'Jane'
WHERE CustomerID = 1;

-- Invalid data update (violates foreign key constraint)


UPDATE Customers
Tuple Uniqueness Constraint
In a database, a Tuple Uniqueness Constraint, also known as a Unique Constraint, ensures that no two tuples (rows) in a table have
the same combination of values in specified columns. This constraint helps maintain the uniqueness of data within a table, similar
to the UNIQUE constraint.
Example: Let’s consider an example using a Products table where we want to enforce a Tuple Uniqueness Constraint on the
combination of ProductCode and Category:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductCode VARCHAR(20) NOT NULL,
ProductName VARCHAR(100),
Category VARCHAR(50),
Price DECIMAL(10, 2),
-- Other columns
UNIQUE (ProductCode, Category)
);
The UNIQUE (ProductCode, Category) statement specifies a Tuple Uniqueness Constraint, indicating that the combination of
ProductCode and Category must be unique for each tuple in the Products table.
Now, let’s insert some data to demonstrate the Tuple Uniqueness Constraint:
-- Valid data insertion
INSERT INTO Products (ProductID, ProductCode, ProductName, Category, Price)
VALUES (1, 'P001', 'Product A', 'Electronics', 49.99);

INSERT INTO Products (ProductID, ProductCode, ProductName, Category, Price)


VALUES (2, 'P002', 'Product B', 'Clothing', 29.99);

-- Invalid data insertion (violates Tuple Uniqueness Constraint)


INSERT INTO Products (ProductID, ProductCode, ProductName, Category, Price)
VALUES (3, 'P001', 'Product C', 'Electronics', 19.99);
-- This would result in an error since the combination 'P001' and 'Electronics' is not unique

-- Valid data insertion


INSERT INTO Products (ProductID, ProductCode, ProductName, Category, Price)
VALUES (4, 'P003', 'Product D', 'Electronics', 79.99);
In this example, the Tuple Uniqueness Constraint ensures that the combination of ProductCode and Category must be

You might also like