0% found this document useful (0 votes)
14 views

Complex Integrity Constraints in SQL

The document discusses complex integrity constraints in SQL, including CHECK constraints, composite primary and foreign keys, and the use of triggers to maintain data integrity. It highlights the importance of active databases that utilize event-driven functionalities and the issues caused by data redundancy, along with normalization techniques to prevent it. Additionally, it explains the process of decomposition in DBMS and the normalization process to eliminate redundancy and ensure data integrity.

Uploaded by

lagishettisuresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Complex Integrity Constraints in SQL

The document discusses complex integrity constraints in SQL, including CHECK constraints, composite primary and foreign keys, and the use of triggers to maintain data integrity. It highlights the importance of active databases that utilize event-driven functionalities and the issues caused by data redundancy, along with normalization techniques to prevent it. Additionally, it explains the process of decomposition in DBMS and the normalization process to eliminate redundancy and ensure data integrity.

Uploaded by

lagishettisuresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Complex Integrity Constraints in SQL

Integrity constraints in SQL are rules that help ensure the accuracy and
reliability of data in the database. They ensure that certain conditions are
met when data is inserted, updated, or deleted. While primary key, unique,
and foreign key constraints are commonly discussed and used, SQL allows
for more complex constraints through the use of CHECK and custom
triggers. Here are some examples of complex integrity constraints:

1. Using CHECK Constraints:


Ensuring a range: You might want a column to only have values within a
certain range.
Example
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Age INT CHECK (Age >= 18 AND Age <= 30)
);

Pattern matching: Ensure data in a column matches a particular format.

CREATE TABLE Students (


ID INT PRIMARY KEY,
Email VARCHAR(255) CHECK (Email LIKE '%@%.%')
);

2. Composite Primary and Foreign Keys

These are cases where the uniqueness or referential integrity constraint is


applied over more than one column.

CREATE TABLE OrderDetails (


OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
3. Using TRIGGERS
A trigger is a procedural code in a database that automatically executes in
response to certain events on a particular table or view. Essentially, triggers
are special types of stored procedures that run automatically when an
INSERT, UPDATE, or DELETE operation occurs.
A trigger is a predefined action that the database automatically executes in
response to certain events on a particular table or view. Triggers are
typically used to maintain the integrity of the data, automate data-related
tasks, and extend the database functionalities.
There are various types of triggers based on when they are executed:

BEFORE: Trigger is executed before the triggering event.


AFTER: Trigger is executed after the triggering event.
INSTEAD OF: Trigger is used to override the triggering event, primarily for
views.

They can also be categorized by the triggering event:

INSERT: Trigger is executed when a new row is inserted.


UPDATE: Trigger is executed when a row is updated.
DELETE: Trigger is executed when a row is deleted.

Syntax:
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
trigger_body;

trigger_name: Name of the trigger.


trigger_time: BEFORE, AFTER, or INSTEAD OF.
trigger_event: INSERT, UPDATE, or DELETE.
table_name: The name of the table associated with the trigger.
trigger_body: The set of SQL statements to be executed.

Suppose we have an `Employees` table and we want to maintain an `AuditLog` table


that keeps a record of salary changes for employees.
Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Salary DECIMAL(10, 2)
);

AuditLog Table
CREATE TABLE AuditLog (
LogID INT AUTO_INCREMENT PRIMARY KEY,
EmployeeID INT,
OldSalary DECIMAL(10, 2),
NewSalary DECIMAL(10, 2),
ChangeDate DATETIME
);

Now, let's create a trigger that automatically inserts a record into the `AuditLog` table
whenever there's an update to the `Salary` column in the `Employees` table.

Trigger
mysql> DELIMITER //
mysql> CREATE TRIGGER AfterSalaryUpdate
AFTER UPDATE ON Employees
FOR EACH ROW
BEGIN
IF OLD.Salary != NEW.Salary THEN
INSERT INTO AuditLog (EmployeeID, OldSalary, NewSalary,
ChangeDate)
VALUES (OLD.EmployeeID, OLD.Salary, NEW.Salary, NOW());
END IF;
END;
//
mysql> DELIMITER ;

- The trigger is named `AfterSalaryUpdate`.


- It activates `AFTER` an `UPDATE` on the `Employees` table.
- It compares the old and new salary values. If there's a change
(`OLD.Salary != NEW.Salary`),
it inserts a new record into the `AuditLog` table with the details of the
change and the current date and time (`NOW()`).
Active Databases
An active database is a database that uses triggers and other event-driven functionalities. The term
"active" signifies that the DBMS reacts automatically to changes in data and predefined events.
Triggers are a primary mechanism that makes a database "active."

Key Features of Active Databases

1. Event-Condition-Action (ECA) Rule: This is the foundational concept of active databases. When a
specific event occurs, the database checks a particular condition, and if that condition is met, an action
is executed.
2. Reactive Behavior: The database can react to changes without external applications or users having
to intervene, thanks to the ECA rules.
3. Flexibility: Active databases provide more flexibility in data management and ensure better data
integrity and security.

Why are Active Databases Important?

 Integrity Maintenance: Active databases can enforce more complex business rules that can't be
enforced using standard integrity constraints.
 Automation: They can automate certain tasks, reducing manual interventions.
 Alerts: They can notify users or applications when specific conditions are met.
:Data Redundancy:

Data Redundancy in a database occurs when the same data is stored in multiple places.
Redundancy can cause various problems such as data inconsistencies, higher storage
requirements, and slower data retrieval.

Problems Caused Due to Redundancy :

 Data Inconsistency: Redundancy can lead to data inconsistencies, where the same
data is stored in multiple locations, and changes to one copy of the data are not
reflected in the other copies. This can result in incorrect data being used in decision-
making processes and can lead to errors and inconsistencies in the data.

 Storage Requirements: Redundancy increases the storage requirements of a


database. If the same data is stored in multiple places, more storage space is
required to store the data. This can lead to higher costs and slower data retrieval.

 Update Anomalies: Redundancy can lead to update anomalies, where changes


made to one copy of the data are not reflected in the other copies. This can result in
incorrect data being used in decision-making processes and can lead to errors and
inconsistencies in the data.

 Performance Issues: Redundancy can also lead to performance issues, as the


database must spend more time updating multiple copies of the same data. This can
lead to slower data retrieval and slower overall performance of the database.

 Security Issues: Redundancy can also create security issues, as multiple copies of
the same data can be accessed and manipulated by unauthorized users. This can
lead to data breaches and compromise the confidentiality, integrity, and availability of
the data.

 Data Duplication: Redundancy can lead to data duplication, where the same data is
stored in multiple locations, resulting in wasted storage space and increased
maintenance complexity. This can also lead to confusion and errors, as different
copies of the data may have different values or be out of sync.

 Data Integrity: Redundancy can also compromise data integrity, as changes made
to one copy of the data may not be reflected in the other copies. This can result in
inconsistencies and errors and can make it difficult to ensure that the data is accurate
and up-to-date.

 Usability Issues: Redundancy can also create usability issues, as users may have
difficulty accessing the correct version of the data or may be confused by
inconsistencies and errors. This can lead to frustration and decreased productivity, as
users spend more time searching for the correct data or correcting errors.
To prevent redundancy in a database , normalization techniques
can be used. Normalization is the process of organizing data in a database
to eliminate redundancy and improve data integrity. Normalization involves
breaking down a larger table into smaller tables and establishing
relationships between them. This reduces redundancy and makes the
database more efficient and reliable.

Decomposition In DBMS

When we divide a table into multiple tables or divide a relation into


multiple relations, then this process is termed Decomposition in
DBMS.

We perform decomposition in DBMS when we want to process a


particular data set. It is performed in a database management
system when we need to ensure consistency and remove
anomalies and duplicate data present in the database. When we
perform decomposition in DBMS, we must try to ensure that no
information or data is lost.

Types of Decomposition
There are two types of Decomposition:
 Lossless Decomposition

 Lossy Decomposition
Normalization Process in DBMS
Database Normalization is any systematic process of organizing a database schema
such that no data redundancy occurs and there is least or no anomaly while
performing any update operation on data. In other words, it means dividing a large
table into smaller pieces such that data redundancy should be eliminated. The
normalizing procedure depends on the functional dependencies among the attributes
inside a table and uses several normal forms to guide the design process.

Consider the table 1 shown below:


Full Name Institute Courses Result

Naveen Kumar IIT Delhi DBMS, OS Pass

Utkarsh Tiwari IIT Bombay CN. COA Fail

Utkarsh Tiwari IIT Kanpur OS Fail


Now, we are re-structuring the table according to the 1st Normal Form.
Rules of 1st Normal Form
 Each table should contain a single value.
 Each record needs to be unique.

Full Name Institute Subject Result

Naveen Kumar IIT Delhi DBMS Pass

Naveen Kumar IIT Delhi OS Pass

Utkarsh Tiwari IIT Bombay CN Fail

Utkarsh Tiwari IIT Bombay COA Fail

Utkarsh Tiwari IIT Kanpur OS Fail

You might also like