Professional Documents
Culture Documents
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).
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);
-- 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');