CS2102 Database Systems Null Values & Truth Table: Aggregate Functions: MIN, MAX, ER Diagrams Suppose S → T, to check
, ER Diagrams Suppose S → T, to check FD:
Integrity Constraints: - Unknown and/or/not is unknown AVG, SUM, COUNT - Rectangle for Entities CHECK ( NOT EXISTS ( SELECT 1 Key Constraints - False and _ is false, not is true Note that: COUNT(A) returns 0 and - Lozenge for Relationships FROM R r1, R r2 WHERE r1.S = r2.S Foreign Key Constraints - True or _ is true, not is false COUNT(*) returns n for n NULL, Weak Entities AND r1.T <> r2.T)) Other General Constraints - Op on NULL is unknown other functions return NULL - Attribute only unique within the - Best implemented as PK or Unique Superkey: subset of attributes that - Need to use IS NULL SELECT pizza, rname FROM Sells scope of a relationship Trivial FD: X→Y, Y ⸦ X; {A,B}→{A} uniquely identifies the tuple - Need to use IS DISTINCT WHERE price = (SELECT MAX(price) - primary key: primary key of Non-trivial FD: X→Y, Y X; (unique not null). Candidate Key SQL Samples FROM Sells) dominant entities + own PK Completely Non-trivial FD: X→Y, is a superkey in which no proper CREATE TABLE students ( ORDER BY area ASC, price DESC Participation Constraints Y /⊂ X, X Y = subset is a superkey (minimal studentId integer primary key, LIMIT 20 - (1,2), (0, 5), (1,n), (0,n) Armstrong’s Axioms subset). name varchar(100) not null, GROUP BY A Clause: - Interpret as 1 of entity can be Let X, Y, Z be subsets of Relation R Foreign Key Constraint: Foreign birthdate date, 1. Partition the tuples in table into mapped to (0,n) of other entity Reflexivity: If Y ⸦ X, then X→Y; keys must be (1) appear as primary dept varchar(20) default ‘CS’, groups based on A - Weak entities have (1,1) Augmentation: If X→Y, then key in referenced table or (2) null rating numeric(3,1), 2. Compute SELECT for each group XZ→YZ Relational Algebra # 3 digits, 1 decimal place 3. Output one tuple for each group Transitivity: If X→Y & Y→Z, then X→Z c(R) Selection: selects tuples from CHECK(rating > 8.0), GROUP BY Properties, for each Other useful logical relations: R that satisfies condition c. FOREIGN KEY (name) column A in relation R in SELECT, 1 Weak Augmentation: If X→Y, then e.g. price<20(Sells) REFERENCES table(tname) of the following must hold: XZ→Y l(R) Projection: projects attributes ); 1. A appears in the GROUP BY Relationships have primary key: Pseudo-transitivity: If X→Y & A→B & given by l from Relation R, SELECT DISTINCT s1.rname, s2.rname 2. A appears in an aggregated sum of PK of entities A⸦Y, then X→B duplicates are removed in FROM Sells s1, Sells S2 WHERE function in SELECT (min(A)) Exception 1: (0,1) mapping Check Super-key of R(A,B,C,D): projections s1.rname < s2.rname AND s1.pizza = 3. Primary Key of R appears in the - Relationship have (0,1) entity’s - Suppose we check {A,B}, check if e.g. rname, pizza (Sells) s2.pizza GROUP BY clause PK as PK {A,B}→{A,B,C,D} S(X,Y,Z)(R) Renaming: renames Set Operations: UNION, INTERSECT, HAVING Properties: for each column Exception 2: (1,1) mapping Check candidate key of R(A,B,C,D): R(A,B,C) to S(X,Y,Z) EXCEPT, if ALL, preserves duplicate A in relation R in HAVING, 1 of the - merge (1,1) entity and - Suppose we check {A,B}, check that e.g. shops(sname, region)(Restaurants) records following must hold: relationship table neither {A} nor {B} are candidate key NATURAL JOIN: Join on common 1. A appears in the GROUP BY Cross-Product: Consider Exception 3: Weak Entities Closure of a set of FD (F+) attributes 2. A appears in an aggregated - merge weak entity with R(A,B,C) S(X,Y), get all combi - All the FD that F entails, can be found INNER JOIN ON _: Join on specified functions in HAVING relationship table by applying Armstrong Axioms in all of (A,B,C,X,Y) attributes 3. Primary Key of R appears in the Functional Dependencies possible ways until nothing new comes Set Operations (requires LEFT OUTER JOIN ON _: Join on GROUP BY clause Analysed because tables have - 2 sets of FD are equivalent iff F+ == G+ compatibility: same # of attributes, preserving left attributes Order of Evaluation of Queries Redundant Storage - Attribute Closure: a set S of attributes, attributes have same domain) FULL OUTER JOIN ON _: Join on 1. compute cross-product in FROM Update Anomaly we call the closure of S, S+, the maximum Union: R S preserving both attributes 2. select tuples that fulfil WHERE Deletion Anomaly set of attributes s.t. S → S+ Intersection: R S e.g. Find distinct customers who like 3. partition with GROUP BY Set-Difference: R – S Insertion Anomaly e.g. F = {A→C, B→D}, then A+ = {A,C} some pizza by restaurant A 4. select groups that fulfil HAVING Definition of FD Algo: X(0) =X; X(i+1) := X(i) A, where A e.g. Find customer pairs (C1, C2) SELECT distinct cname FROM Likes L 5. generate output in SELECT - For a relation R, a FD from S of R is the union of the sets Z of attributes s.t. s.t. they like some common pizza WHERE EXISTS (SELECT 1 FROM 6. remove duplicates and C1 < C2 to T of R exists iff: Y→Z in F, and F ⸦ X(i); end: X(i+1) = X(i) Sells S WHERE S.rname = A AND 7. sort based on ORDER BY For every instance of R, if 2 Minimal Set of Dependencies: R = (pizza=pizza2)(cname<cname2) (Likes S.pizza = L.pizza); 8. truncate based on LIMIT tuples agree on the values of 1. Every RHS is a single attribute Likes2( cname2, pizza2)( Likes)) Subqueries: IN, ANY, ALL Conditional Expressions: CASE Data Definition Language (DDL): attributes in S, then they 2. For no FD X→A in F and proper SELECT * FROM Sells WHERE price > SELECT name, CASE agree on the values of subset Z of X is F – CRUD on Schemas ANY/ALL (SELECT price from Sells WHEN marks >= 70 THEN ‘A’, attributes of T. S uniquely Data Manipulation Language {X→A}{Z→A} is equivalent to F WHERE rname = A) WHEN marks >= 60 THEN ‘B’, identifies T. DML: 3. For no FD X→A in F is the set F – UNIQUE: Returns false if the result WHEN marks >= 50 THEN ‘C’, Write: S → T CRUD on Data {X→A} equivalent to F subquery contains at least 2 distinct ELSE ‘D’ FD are integrity constraints Minimal Cover: F is minimal & F is records t1 and t2 s.t. t1 = t2 END AS grade and can be checked equivalent to G