Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Term Definition

Key Minimal set of attributes that uniquely identifies the entity Armstrong Axioms
Superkey A subset of attributes that uniquely identifies its tuples DML – Data Definition Language Reflexivity: if Y ⊆ X , then X → Y
Candidate Relation could have multiple keys called candidate keys INSERT INTO… INSERT INTO name (relation_attributes)
Augmentation: if X → Y , then XZ → YZ
Key VALUES… VALUES (‘value1’, ‘value2’, …)
Primary Key One of the candidate key chosen DELETE FROM DELETE FROM name [WHERE…] Transitivity: if X → Y ∧Y → Z , then X → Z
Cardinality UPDATE… SET... UPDATE name SET attribute = value [WHERE …] Union: if X → Y ∧X → Z ,then X → YZ
Degree ORDER BY name DESC OR name ASC Decomposition: if X → YZ , then X → Y and X → Z
SELECT… AS… SELECT name AS newName Rename
Entity-Relationship Model GROUP BY Usually models the ‘each’ noun e.g. each director, in the qn
Weak Entities and is similar to what you are SELECT-ing
Relationship must exist and be unique for each entity in HAVING SELECT a FROM Acts a GROUP BY a.Actor Functional Dependency
the set. Weak entities can only be defined for a HAVING COUNT (*) > ( SELECT COUNT (m.title)
participation constrained by (1,1) cardinality FROM Movies m WHERE qualifier )
Define X →Y Trivial FD: sid, sname→ sid

Col appearing in HAVING clause must either be in GROUP ∀ t1 , t2 ∈ R completely non-trivial: totally
do NOT share attributes
Hierarchies: BY clause or be an argument of an aggregation operator t 1 . X =t 2 . X ⟹ t 1 . Y =tNon-trivial
2 .Y
- Subclass and Superclass e.g. Person  Student Arithmetic WHERE assets * 1.7 < 17 OR SELECT (rating + 0.2) * 10
non-completely non-trivial:
- Inheritance: subclass inherits attributes of superclass Logical WHERE qualifier1 AND, OR, NOT share SOME attributes
- Specialization: subclass as its own attributes Connectors AND qualifier 2
LIKE WHERE title LIKE ‘W_%S’ Minimal Cover
Symbol ‘_’ stands for single arbitrary char Given a relation R(A,B,C,D,E,F). The following set F of FDs hold for this table.
Recursive Relationship Sets and Roles in Relationships
Symbol ‘%’ stands for 0 or more arbitrary char F={ AB →CD ,C → CE ,C → F , F → E , CDF → E , DFE →
UNION SELECT a FROM Acts a WHERE a.b > 0 Step 1: Decompose FDs
INTERSECT UNION / INTERSECT / EXCEPT F={ AB →C , AB→ D ,C →C ,C → E , C → F , F → E , CD
EXCEPT SELECT b FROM Acts b WHERE b.a > 0
Step 2: Eliminate redundant attributes from LHS of FDs: CHECK CLOSURE
The Relational Model Aggregation COUNT ([DISTINCT] A) Number of [unique] values in A col
If we replaced DFE → A with DF → A , then we get
operators COUNT ([DISTINCT] *)  Number of (unique) rows
No. of columns = Degree +¿={D , F , A , E }¿
No. of rows = Cardinality
SUM ([DISTINCT] A)  Sum of all (unique) values in A column DF
AVG ([DISTINCT] A)  Average of all (unique) values in A col
A subset of attributes relation A is a foreign key if it is the primary key relation B
SUM, AVG, MIN, MAX often appear in SELECT statement
F={ AB →C , AB→ D ,C → F , F → E , DF → A }
Options if tuple t in Courses is to be deleted: Step 3: Eliminate redundant FDs and check equivalence
- Disallow deletion if some rows in Enrolls refers to t Set ― v IN Q is true iff value v is in the set returned by Q ¿
Comparison RemoveC → E sinceC ={C , F , E }. Hence, we are left with:
- Delete all rows in Enrolls that refer to t ― v NOT IN Q is true iff value v is not in the set returned by Q
- For each row in Enrolls that refer to t, replace cid value with DEFAULT in WHERE
― EXISTS Q is true iff the result of Q is non-empty F={ AB →C , AB→ D ,C → F , F → E , CDF → E , DFE →
- For each row in Enrolls that refer to t, replace cid value with NULL, clause
If we removedCDF → E ,
+¿={C , D , F , E , A }¿
provided cid is not a primary key attribute in Enrolls
― NOT EXISTS Q is true iff the result of Q is empty CDF
― UNIQUE Q is true iff the result of Q has no duplicates +¿={C , D, F , E , A }¿
― v op ANY Q is true iff there exists some v ' in result of Q s.t. CDF .
Structured Query Language So, we can remove this f.d.
DDL – Data Definition Language v op v ' is true F={ AB →C , AB→ D ,C → F , F → E , DFE → A }
CREATE TABLE CREATE TABLE name ( Creates table ― v op ALL Q is true iff for each v ' in result of Q, v op v ' is
…) for name
Decompositions
true Decompositions have to be:
DEFAULT name CHAR(24) DEFAULT ‘some name’ Default value ― op ∈ { = , <> , < , <= , > , >=}
PRIMARY KEY PRIMARY KEY (name, name) - Lossless: When you ⨂ them back, the original is subset of the result
OR To be lossless, attributes common between two relations must
name CHAR(24) PRIMARY KEY Relational Algebra functionally determine all attributes in ONE of the two relations
REFERENCES FOREIGN KEY (attribute) Where to get σ c (R) Select tuples of relation R that satisfy condition c - Dependency preserving: { A → B , B → C , A →C } 
FOREIGN KEY REFERENCES Staff(name) the attribute
DROP TABLE DROP TABLE name Delete table π L (R) List attributes L of relation R { A → B , B →C }
Computing FD Projections
ALTER TABLE ALTER TABLE name ADD
attribute+domain
Edit columns
ρ( R ( N 1 → N 1 , …Rename.
' '
) , R) Can also do with ρ(R ' , R) For F XY
OR ALTER TABLE name DROP attribute R ∪ S / R ∩S / - Compute X
+¿= X . .¿
, we have X → X ..∩ XY
UNIQUE name CHAR(24) UNIQUE For Candidate
NOT NULL name CHAR(24) NOT NULL key
R−S - Compute Y +¿=Y .. ,¿ we have Y →Y ..∩ XY
CHECK age NUMERIC CHECK (age > < = value) Check value R 1 × R2 / R ⨂c S - So, F XY ={ X → xyz , Y → xyz }
CREATE VIEW CREATE VIEW NewName R/ S R/ S contains all A tuples s.t. for every B tuple in
AS (some query you want in NewName) - xyz is the common attribute of X .. ∩ XY
S there is a AB tuple in R
Normal Forms
3NF BCNF
1. Trivial 1. Trivial
2. LHS is a superkey 2. LHS is a superkey
3. RHS is a prime attribute (appear in at
least one key)
Decomposition into BCNF (only guarantees lossless)
- Let X → A be an FD in F that violates BCNF
- Decompose R into
R1= XAR2=R− A
- If R 1 or R2 is not in BCNF, decompose further
E.g.

Decomposition into 3NF (lossless and dependency preserving)


- Compute minimal cover of R
- Create schema for each FD in minimal cover
- Choose a key and create i+1th schema
- Remove redundant schema if one is a subset of another
F={ AC → E , E → D , A → B }, key is AC
E.g. Minimal cover of
Schema created: R1 ( A ,C , E ) , R2 ( E , D ) , R 3 ( A , B ) , R 4 ( A , C)
3NF decomposition is R 1 ( A ,C , E ) , R2 ( E , D ) , R 3 ( A , B )
SQL Important Tips
Find names of suppliers that supplies at least two parts and average cost of it - Any table with 2 attributes is in BCNF trivially
SELECT P.sname, AVG(P.cost)
FROM Part P - Check if 3NF
GROUP BY P.sname
1.Find keys
HAVING COUNT(*) >1
2.Find prime attributes
Find names of suppliers who supply at least 5 parts with price > 1000
SELECT S.name 3. Find non trivial, non-decomposed F.Ds
FROM Part P, Supplier S, PartSupp PS
WHERE P.price > 1000 AND P.partkey = PS.partkey AND PS.suppkey = S.suppkey 4. Check F.Ds are either LHS=superkey or RHS=Prime attribute
GROUP BY S.name
HAVING COUNT ( DISTINCT P.partkey) > 4

Find Dept_no where the avg salary of emp in that dept is > the avg emp Lossless join: A decomposition is lossless if common attributes b/w 2 tables must
SELECT E.dept_no be a key In alteleast one of the tables
FROM Employee E Dependency Preservation: If all originals F.Ds can be derived from new tables
GROUP BY E.dept_no REMEMBER TO CHECK FOR IMPLIED DEPENDENCIES!!!!!
HAVING AVG(E.salary) > ( SELECT AVG ( T1.salary) FROM Employee T1) Minimal cover reduandant attribute – TO check if C is redundant in {A}->B,
Check {A-C}+ contains B (on the original FDs). If yes then remove C.
Find names of required courses for ‘CS’ curriculum that ‘Smith’ did not take
SELECT C.course_name
FROM Couse C, Required R
WHERE R.curriculum = ‘CS’ AND R.CID = C.CID Relational Algebra
AND C.CID NOT IN ( SELECT T.CID Find sids of Suppliers who supply every red part
FROM Student S, Take T ( π sid , pid Catalog)/( π pid σ ¿ red Parts)
WHERE S.student_name = ‘Smith’ AND S.SID = T.SID)
Find sids of Suppliers who supply some red part
Find identifier of all students who never took the course 101 offered by Dept 11
SELECT S.SID π sid (Catalog ⊗ pid= pid ( σ ¿red Parts ) )
FROM Student S
WHERE NOT EXISTS ( SELECT * List names of suppliers who supply at least two parts
FROM Transcript T, Section SE
WHERE SE.dept_id = 11 AND SE.course_no = 101
ρ ( T 1 , Part ) ρ ( T 2 , Part )
AND S.SID = T.SID AND T.SEID = SE.SEID) π sname (σ pno≠ pno ∧ sname=sname (T 1× T 2 ) )
Find course number and dept_id of all course where no student ever got an ‘F’
List names of suppliers who supply ALL complex parts whose labor cost is > 100
SELECT C.course_no, D.dept_id
FROM Course C ρ ¿π sname , pno ( Part /T 1)
WHERE NOT EXISTS ( SELECT *
FROM Transcript T, Section S Find the employment numbers of pilots who can fly ALL MD planes
WHERE T.grade = ‘F’ AND T.SID = S.SID
AND C.course_no = S.course_no ) ρ ( B , π Mode l ( σ Maker =MD ( Plane )) ) ρ( A , Can Fly )
No

Find names of all students who are enrolled two classes at the same timing π Emp ( A)−π Emp (( π Emp (A )× B)− A)
No no no
SELECT DISTINCT S.name
FROM Student S
WHERE S.snum IN ( SELECT E.snum
FROM Enrolled E1, Enrolled E2, Class C1, Class C2
WHERE E1.enum = E2.enum AND E1.cname <> E2.cname
AND E1.cname = C1.cname AND E2.cname = C2.cname
AND C1.meets_at = C2.meets_at )

You might also like