MySQL IC
MySQL IC
2023
1 Integrity Constraints
2 Create Constraints
The Not Null constraint enforces a column to Not accept Null values.
e.g.
Create Table Product (
ProductID char(3) Primary Key,
ProductName varchar(30) Not Null,
Price float,
CategoryID char(3) Not Null,
);
• The Unique constraint ensures that all values in a column are different.
• Both the Unique and Primary Key constraints provide a guarantee for uniqueness
for a column or set of columns.
• A Primary Key constraint automatically has a Unique constraint.
e.g.
Create Table Product (
ProductID char(3) Primary Key,
ProductName varchar(30),
Price float,
CategoryID char(3),
Unique(ProductName)
);
• The Check constraint is used to limit the value range that can be placed in one or
multiple columns.
e.g.
Create Table Product (
ProductID char(3) Primary Key,
ProductName varchar(30),
Price float,
Quality int,
CategoryID char(3),
Check (Price > 0)
);
e.g.
Create Table Product (
ProductID char(3) Primary Key,
ProductName varchar(30),
Price float,
Quality int,
CategoryID char(3),
Constraint CKH_Price Check (Price > 0 And Quality >=5)
);
Drop Check:
Foreign key: In a relation, attributes (one or many) are called foreign key if they are
not a key in this relation, but a primary key in another relation.
Foreign key assert that a value appearing in one relation must also appear in the
primary key component(s) of another relation.
e.g.
Class(ClassID, Description)
C01
C02
Student(StudentID, Name, Address, Email, ClassID)
C01
C02
C02
C03
When foreign key constraint is declared, the system has to reject the violating
modification.
There are two policy for Update and Delete tuples in foreign key:
1. Default policy: Reject Violating Modifications.
2. Cascade policy: When changes to the referenced attribute(s) in R1 . There are
following change to the foreign key in R2 .
3. Null policy: When a modification to the referenced relation (R1 ) affects a
foreign-key value (in R2 ) is changed to NULL.
CREATE TABLE R2 (
Z datatype,
X datatype,
Foreign Key (X) References R1 (X)
[On Update reference_option ]
[On Delete reference_option ] ) ;
e.g.
Create Table OrderDetail (
OrderID char(3),
ProductID char(3),
Quantity int,
Primary Key (OrderID, ProductID),
Constraint FK_Order Foreign Key (OrderID) References Order(OrderID)
On Delete Set Null On Update Cascade,
Constraint FK_Prod Foreign Key (ProductID) References Products(ProductID)
On Delete Set Null On Update Cascade
);
Drop Foreign key:
Alter Table OrderDetail Drop Foreign Key FK1;
Nguyễn Văn Diêu 4. Foreign Key Constraint 21/31
General Algorithm
e.g.
DELIMITER $$
CREATE TRIGGER Price_check
BEFORE UPDATE
ON Product
FOR EACH ROW
BEGIN
IF NEW.Price < 0 THEN
SET NEW.Price = 0;
ELSEIF NEW.Price > 100 THEN
SET NEW.Price = 100;
END IF;
END;
$$
Nguyễn Văn Diêu 5. Integrity Constraints - General Algorithm 23/31
General Algorithm
e.g.
DELIMITER $$
CREATE TRIGGER GreaterThanZero
BEFORE INSERT
ON Product
FOR EACH ROW
BEGIN
IF NEW.Price <=0 THEN
SIGNAL SQLSTATE ’45000’
SET MESSAGE_TEXT =’Price must greater than zero’ ;
END IF ;
END ;
$$
e.g.
Create table R(A,B); A, B: integer;
Constraint: A >= B; Do not allow delete if A > 2*B.
Integrity Constraints
• Context: R
• Condition:
∀t ∈ ∀r
t.A ≥ t.B and not allow delete if A > 2 ∗ B
end;
• Influence table:
Insert Delete Update
R + + +(A/B)
delimiter $$
create trigger insert_R before insert on R
for each row
begin
if new.A < new.B then
signal sqlstate ’45000’
set message_text = ’Insert error: A < B’;
end if;
end;
$$
delimiter $$
create trigger delete_R before delete on R
for each row
begin
if old.A > 2 * old.B then
signal sqlstate ’45000’
set message_text = ’Delete error: A > 2*B’;
end if;
end;
$$
delimiter $$
create trigger update_R before update on R
for each row
begin
if new.A < new.B then
signal sqlstate ’45000’
set message_text = ’Update error: A < B’;
end if;
end;
$$
Exercises:
R1(A, B); R2(B, C); A, B, C : integer.
Constraint: A > C.
delimiter $$
create trigger insert_R1 before insert on R1
for each row
begin
if Exists (Select *
From R2
Where new.B = B and new.A <= C ) then
signal sqlstate ’45000’
set message_text = ’Insert R1 Error: A <= C’;
end if;
end;
$$