Complex Integrity Constraints in SQL
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:
Syntax:
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
trigger_body;
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 ;
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.
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.
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.
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
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.