Chapter 6
Chapter 6
Chapter 6
▪ Integrity constraints
▪ Structure Query Language
▪ DDL
▪ DML
▪ DCL (self studying)
▪ Sub query
Getting Relational
User High-Level Database Relational
Requiremen Design Schema DBMS
t Design
COMPANY Database
x y x AND y x OR y NOT x
TRUE TRUE TRUE TRUE FALSE
TRUE UNKNOW UNKNOW TRUE FALSE
N N
TRUE FALSE FALSE TRUE FALSE
UNKNOW TRUE UNKNOW TRUE UNKNOW
N N N
UNKNOW UNKNOW UNKNOW UNKNOW UNKNOW
N N N N N
UNKNOW FALSE FALSE UNKNOW UNKNOW
N N N
FALSE TRUE FALSE TRUE TRUE
FALSE UNKNOW FALSE UNKNOW TRUE
N THE DATABASE LANGUAGE SQL N 13
The Truth-Value Unknown
SQL conditions in Where clause produce three
truth values: True, False, and Unknown
Those tuples which condition has the value True
become part of the answer
Those tuples which condition has the value False
or Unknown are excluded from the answer
▪Add/remove constraints
ALTER TABLE tablename
ADD CONSTRAINT constraintName PRIMARY KEY
(<attribute list>);
SELECT L
FROM R πL(σC(R))
WHERE C
31
Ordering the Output
Example 6:
◦ Listing all employee by department number
ascreasingly, then by salary descreasingly
Example 10.1
◦ Find all those employees whose name is begun
by ‘H’ or salary exceed 80000
Example 10.2
◦ Find all those normal employees, that is who do
not supervise any other employees
Example 10.3
◦ Find all employees who work on projectB and
projectC
Example 11:
◦ Find the employees of Phòng Phần mềm trong
nước department
Example 11:
◦ Find the employees of Phòng Phần mềm trong
nước department
Example 11:
◦ Find the employees of Phòng Phần mềm trong
nước department
Example 12:
◦ Find the dependents of all employees of
department number 1
A B
A 2000 100 VN
B 2020 120 Disney
C 1990 90 GMC
D 2021 95 FOX
E 2000 110 FOX
1 A 2000
2 A 2001
3 B 2021
4 D 2021
◦ Find all studio names whose films are released the same year of production.
SELECT A.studioName
FROM Movies A
WHERE exists
(SELECT B.*
FROM Tickets B
WHERE A.title =B.title and A.year=B.ReleasedDate)
R UNION ALL S;
R INTERSECT ALL S;
R EXCEPT ALL S;
A B
NULL NULL