---> There have 1 or more employee manges 1 department
---> Each department have 1 or more employee
---> One or more employee work for one department
---> One department have many employee
Unique identifier
Only ONE
Can't be NULL value
Unique identifier
Can't be NULL value
Candidate Key without Primary Key
A Key can connect one table to another table
Table
Person | ||
---|---|---|
ID | Name | Address |
Bruckets
Person(ID, Name, Address)
(Warning: Need to include both relation and entity ! ! !)
-- Use DISTINCT
SELECT DISTINCT Customer.cname, city FROM Borrow, Customer
WHERE Borrow.cname = Customer.cname AND bname = “Sai Kong”;
-- Use LIKE
SELECT cname FROM Customer
WHERE street LIKE “%Main%”;
-- Solution 1:
SELECT DISTINCT T.cname FROM Deposit T
WHERE T.cname != “Jones” AND T.bname IN (
SELECT S.bname FROM Deposit S
WHERE S.cname = “Jones”
);
-- Solution 2:
SELECT DISTINCT T.cname FROM Deposit S, Deposit T
WHERE S.cname = “Jones” AND S.bname = T.bname AND T.cname != S.cname;
-- Solution 1:
SELECT X.bname FROM Branch X
WHERE NOT EXISTS (
SELECT * FROM Branch Y
WHERE Y.b-city = “New Territory” AND Y.assets >= X.assets
);
-- Solution 2:
SELECT bname FROM Branch
WHERE assets > ALL (
SELECT assets FROM Branch
WHERE b-city = “New Territory”
);
-- Solution 1:
SELECT bname FROM Branch
WHERE assets > SOME (
SELECT assets FROM Branch
WHERE b-city = “Kowloon”
);
-- Solution 2:
SELECT X.bname FROM Branch X, Branch Y
WHERE X.assets > Y.assets AND Y.b-city= “Kowloon”;
SELECT DISTINCT S.cname FROM Deposit S
WHERE (
SELECT T.bname FROM Deposit T
WHERE S.cname = T.cname
) CONTAINS (
SELECT bname FROM Branch
WHERE b_city = “Kowloon”
);
SELECT C.cname FROM Customer C
WHERE EXISTS (
SELECT * FROM Deposit D
WHERE D.cname = C.cname AND D.bname = “Central”
) AND NOT EXISTS (
SELECT * FROM Borrow B
WHERE B.cname = C.cname AND B.bname = “Central”
);
SELECT DISTINCT S.cname FROM Deposit S
WHERE NOT EXISTS (
(
SELECT bname FROM Branch
WHERE b_city = “Kowloon”
) MINUS (
SELECT T.bname FROM Deposit T
WHERE S.cname = T.cname
)
);
-- ORDER BY
SELECT DISTINCT cname FROM Borrow
WHERE b_city = “Kowloon”
ORDER BY cname;
-- ORDER BY ... DESC | ORDER BY ... ASC
SELECT * FROM Borrow
ORDER BY amount DESC, loan_no ASC;
xxxxxxxxxx
-- Table creation
CREATE TABLE table_name (
A1, D1,
A2, D2, ...
Ak, Dk
);
-- Table deletion
DROP TABLE table_name;
-- Row/Tuple deletion
DELETE table_name;
-- Table update
ALTER TABLE table_name
ADD Aj, Dj
A modification is permitted through a view ONLY IF the view is defined in terms of ONE base/physical relation !!!
xxxxxxxxxx
-- Example 1:
CREATE VIEW loan_info AS
SELECT bname, loan_no, cname FROM Borrow;
-- Example 2:
CREATE VIEW branch-city AS
SELECT bname, city FROM Borrow, Customer
WHERE Borrow.cname = Customer.cname;
Deletion
xxxxxxxxxx
-- Syntax
DELETE r
WHERE P
-- Example 1:
DELETE Deposit
WHERE b-name IN (
SELECT b-name FROM Branch
WHERE b-city = “Laguna”
);
-- Example 2:
DELETE Deposit
WHERE balance < (
SELECT AVG(balance) FROM Deposit
);
Insertion
xxxxxxxxxx
-- Syntax
INSERT INTO r VALUES (v1, v2, …, vn);
-- or
INSERT INTO r
SELECT clmn_name1, clmn_name2, …, clmn_namek FROM s1, s2, …
WHERE ...
-- Example 1:
INSERT INTO Deposit
SELECT b-name, loan_no, c-name, 0 FROM Borrow
WHERE b-name = “ClearWaterBay”
-- Example 2:
INSERT INTO Deposit
SELECT * FROM Deposit
Update
xxxxxxxxxx
-- Syntax
UPDATE r
SET clmn1 = expression
WHERE ...
-- Example 1:
UPDATE Deposit
SET balance = balance * 1.05
-- Example 2:
UPDATE Deposit
SET balance = balance * 1.06 WHERE balance > 10000
UPDATE Deposit
SET balance = balance * 1.05 WHERE balance <= 10000
xxxxxxxxxx
SELECT c_name FROM Deposit
WHERE balance IS NULL; -- WHERE balance IS NOT NULL;
xxxxxxxxxx
-- Functions: avg, sum, min, count, max
-- Example 1:
SELECT b-name, AVG(balance) FROM Deposit
GROUP BY b-name
HAVING AVG(balance) > 12000;
-- Example 2:
SELECT AVG(balance) FROM Deposit, Customer
WHERE Deposit.c_name = Customer.c_name AND city = “Laguna”
GROUP BY Deposit.c_name HAVING COUNT(DISTINCT acct_no) >= 3;
xxxxxxxxxx
SELECT c_name FROM Deposit
WHERE balance IS NULL;
-- Find the person name which have null balance in the their deposit.
SELECT X.bname FROM Branch X, Branch Y
WHERE X.assets > Y.assets AND Y.b_city= “Kowloon”;
-- Find which brank assets is more than every each Kowloon's branch assets.
Form of the operation: σ c(R)
Example:
xxxxxxxxxx
σ topic = "Database" (Tutorials)
-- Selects tuples from Tutorials where topic = ‘Database’.
σ sales > 50000 (Customers)
-- Selects tuples from Customers where sales more than 50000.
σ DNO = 4(EMPLOYEE)
-- Selects tuples from EMPLOYEE who department number is 4.
π A1, A2, An (r)
Example:
UNION: R1 U R2
INTERSECTION: R1 ∩ R2
SET DIFFERENCE: R1 - R2
CARTESIAN PRODUCT: R1 X R2
Example:
xxxxxxxxxx
-- Combine each female EMPLOYEE tuple with the DEPENDENTS tuple to get her dependent(s).
FemEMPNAMES ← π FNAME,LNAME,SSN (σ Sex=F (EMPLOYEE))
EMPDEPENDENTS← FemEMPNAMES X DEPENDENTS
ActualDEPENDENTS ← σ SSN=ESSN (EMPDEPENDENTS)
Result ← π FNAME,LNAME,DEPENDENT_NAME (ActualDEPENDENTS)
Theta Join
Equi Join
xxxxxxxxxx
EMPLOYEE ⋈EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT
Natural Join
xxxxxxxxxx
EMPLOYEE ⋈ DEPARTMENT
xxxxxxxxxx
-- SQL
SELECT a.name, b.capital FROM countries AS a, countries AS b
WHERE a.name = b.capital
-- Relational Algebra
π a.name, b.capital
σ a.name = b.capital
(ρ a countries × ρ b countries)
1st Normal Form (1NF)
2nd Normal Form (2NF)
3rd Normal Form (3NF)
BCNF
BCNF is stronger than 3NF
BCNF is useful when a relation has:
BCNF reduces to 3NF if the above conditions do not apply
Unordered Files (Pile Files)
Ordered Filles (Sequential Files)
BFR = Block Size / Record Size
number of index blocks b = r / (Block Size / Record Size)
Avg Linear Search = (b/2)= 10000/2= 5000 block accesses
Block Access:
Binary search needs log2b = log2938= 10 block accesses +1 get file block = 11 block accesses
Primary Indexing (Index table is created using primary keys)
Secondary Indexing (Index table is created using candidate keys)
(There can be many secondary indexes for the same data file !!!)
Clustered Indexing (Index table is created using non-key values)
B Tree | B+ Tree | |
---|---|---|
Difference | Pointers to data records exist at all levels of the tree | All pointers to data records exists at the leaf-level nodes |
Example | ||
Insertion | N/A | |
Deletion | N/A |
Atomicity (Commit & Abort)
Consistency
Isolation
Durability
Recovery manager
Use: undo & redo
Track:
Example:
Label | Transaction |
---|---|
T1 | w1(x), r1(y), w1(y) |
T2 | r2(x), r2(y), w2(y) |
--> | w1(x), r2(x), r1(y), w1(y), r2(y), w2(y) |
Serial Schedule
Non-serial Schedule
Serializable Schedule
“Recoverable” Schedule (Cascading Rollback)
Failure of one transaction causes several other dependent transactions to rollback or abort
Example:
“Cascadeless” Schedule (only committed read operations & uncommitted write operations)
In a schedule, a transaction is not allowed to read a data item until the last transaction that has written it is committed or aborted
Example:
“Strict” Schedules (only committed read and write operations)
A transaction is neither allowed to read nor write a data item until the last transaction that has written it is committed or aborted
Example:
Conflict Serializable Schedule
Locking is an operation which secures permission
Unlocking is an operation which removes these permissions from the data item
Lock and Unlock are Atomic operations
Essential components
Two Lock Modes
Shared (read) - shared lock (X)
Exclusive (write) - write lock (X)
Lock Manager
Lock Table