Download presentation
1
CMPT 258 Database Systems Final Exam Review
2
Final Exam Final exam Cheat sheet Tuesday Dec 15, 11 – 1 RLC 105
Both sides of an 8x11 paper
3
Coverage ER diagram Database design Relational algebra SQL queries
Create Table Update, delete, etc. Relational algebra SQL queries Normal forms
4
Best ways to study Lecture notes Homework problems 2 Tests
Review the examples we did in class Homework problems 2 Tests
5
Key Constraints 1:1 1:N N:1 M:N
Many-to-Many 1-to-1 1-to Many Many-to-1 Consider Works_In: An employee can work in many departments; a dept can have many employees. 1:1 1:N N:1 M:N did dname budget Departments lot since name Works_In Employees ssn M N
6
Session #, Speaker Name 08/30/11 Key Constraints The constraint that each department has at most one manager is an example of a key constraint, and it implies that each Departments entity appear in at most one Manages relationship. Arrow states that given a Department entity, we can uniquely determine the manages relationship in which it appears. dname budget did since lot name ssn Manages Employees Departments Many-to-Many 1-to-1 1-to Many Many-to-1 Session #, Speaker Name 08/30/11 6 6
7
Participation Constraints
Does every department have a manager? If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial). Every Departments entity must appear in an instance of the Manages relationship. lot name dname budget did since Manages Departments Employees ssn Works_In
8
Weak Entities A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Partial key: the set of attributes of a weak entity set that uniquely identify a weak entity for a given owner entity lot name age pname Dependents Employees ssn Policy cost 1 N
9
Weak Entities Owner entity set and weak entity set must participate in a one-to-one or one-to-many relationship set (one owner, one or more weak entities). Weak entity set must have total participation in this identifying relationship set. identifying owner, strong entity lot name age pname Dependents Employees ssn Policy cost
10
Aggregation Aggregation vs. ternary relationship: why not make Sponsors a ternary relationship? There are really two distinct relationships (monitors and sponsors), each with attribute of its own. Monitors is a distinct relationship, with a descriptive attribute. budget did pid started_on pbudget dname until Departments Projects Sponsors Employees Monitors lot name ssn since
11
Creating Tables in SQL Creates the Students relation.
4/26/2017 Creating Tables in SQL Creates the Students relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2)) Session #, Speaker Name 15
12
Altering Tables ALTER TABLE tbl_name ADD COLUMN col_name domain DROP COLUMN col_name e.g., ALTER TABLE Students ADD COLUMN firstYear integer The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field.
13
DML: updating data within a table
UPDATE table_name SET column_name1 = value1, column_name2 = value2, [WHERE condition]
14
DML: Deleting Tuples Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students WHERE name = ‘Smith’ What if nothing satisfies the condition?
15
Destroying Tables DROP TABLE tbl_name;
e.g., DROP TABLE Students; e.g., DROP TABLE Enrolled; Destroys the relation Students. The schema information and the tuples are deleted.
16
4/26/2017 Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR(10), cid CHAR(30), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students(sid) ); Enrolled Students Session #, Speaker Name 7
17
Referential Integrity in SQL
support all 4 options on deletes and updates. Default is NO ACTION delete/update is rejected CASCADE also delete all tuples that refer to deleted tuple SET DEFAULT Set E.sid to S.sid of the “default” student if a student is deleted from the Student relation SET NULL Sets foreign key value of referencing tuple to null
18
Referential Integrity in SQL
CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students(sid) ON DELETE NO ACTION ON UPDATE CASCADE );
19
Translating ER Diagrams with Key Constraints
4/26/2017 Translating ER Diagrams with Key Constraints CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) Since each department has a unique manager, we could instead combine Manages and Departments. dname budget did since lot name ssn Manages Employees Departments Session #, Speaker Name 7
20
Review: Participation Constraints
4/26/2017 Review: Participation Constraints Does every department have a manager? If so, this is a participation constraint Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value!) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) lot name dname budget did since Manages Departments Employees ssn Session #, Speaker Name 8
21
Translating Weak Entity Sets
4/26/2017 Translating Weak Entity Sets Weak entity set and identifying relationship set are translated into a single table. When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees (ssn) ON DELETE CASCADE) lot name age pname Dependents Employees ssn Policy cost Session #, Speaker Name 11
22
E/R to Relations E/R diagram E Relational schema, e.g.
account=(bname, acct_no, bal) E E = ( a1, …, an ) a1 … an R1 E2 E1 R1= ( a1, b1, c1, …, ck ) a1 …. an c1 …. ck b1 …. bm
23
More on relationships What about: Could have :
put b1 as the key for R1, it is also the key for E2=(b1, …., bn) Usual strategy: ignore R1 Add a1, c1, …., ck to E2 instead, i.e. E2=(b1, …., bn, a1, c1, …, ck) R1 E2 E1 a1 …. an c1 …. ck b1 …. bm R1= ( a1, b1, c1, …, ck )
24
More ? ? a1 …. an c1 …. ck b1 …. bm Treat as N:1 or 1:N
E2 = ( b1, …, bm ) R1 R1 = ( a1, b1, c1 …, ck ) E1 = ( a1, …, an ) R1 E2 = ( b1, …, bm , a1, c1, …, ck) E1 = ( a1, …, an , b1, c1, …, ck) R1 E2 = ( b1, …, bm ,) R1 Treat as N:1 or 1:N Foreign key also needs to be marked as a candidate key
25
E/R to Relational Weak entity sets a1 …. an b1 …. bm
E1 = ( a1, …, an ) IR E2 E1 E2 = (a1, b1, …, bm ) a1 …. an b1 …. bm
26
E/R to Relational Aggregation E1, R1, E2, E3 as before
R2 = (c1, a1, b1, d1, …, dj) a1 …. an b1 …. bm d1 … dj R2 E3 c1 …. ck
27
Relational Algebra: 5 Basic Operations
Selection ( s ) Selects a subset of rows from relation (horizontal). Projection ( p ) Retains only wanted columns from relation (vertical). Cross-product ( ) Allows us to combine two relations. Set-difference ( — ) Tuples in r1, but not in r2. Union ( ) Tuples in r1 and/or in r2. Since each operation returns a relation, operations can be composed!
28
Projection S2
29
Selection () Selects rows that satisfy selection condition.
Result is a relation. Schema of result is same as that of the input relation.
30
Union S1 S2
31
Set Difference Set-difference ( r1—r2 ): Tuples in r1, but not in r2.
S2 – S1 S2
32
Cross Product Example R1 S1 S1 X R1 =
33
Intersection S1 S2
34
Natural Join Example R1 S1 S R1 =
35
Compound Operator: Division
Useful for expressing “for all” queries like: Find sids of sailors who have reserved all boats. For A/B attributes of B are subset of attrs of A. May need to “project” to make this happen. Reserves Boats
36
Examples of Division A/B
37
See lecture notes for more examples!
38
Conceptual Evaluation Strategy
4/26/2017 Conceptual Evaluation Strategy SELECT [DISTINCT] target-list FROM relation-list WHERE qualification Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the cross-product of relation-list. Discard resulting tuples if they fail qualifications. Delete attributes that are not in target-list. If DISTINCT is specified, eliminate duplicate rows. Session #, Speaker Name
39
Expressions and Strings
4/26/2017 Expressions and Strings Find ages of sailors whose names begin and end with B and contain at least three characters. LIKE is used for string matching. ‘_’ stands for any one character and ‘%’ stands for 0 or more arbitrary characters (including blanks). ‘_AB%’ Comparison operators (=, <, >, etc.) can be used for string comparison Session #, Speaker Name
40
Find sid’s of sailors who’ve reserved a red or a green boat
4/26/2017 Find sid’s of sailors who’ve reserved a red or a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’) UNION: Can be used to compute the union of any two union- compatible sets of tuples (which are themselves the result of SQL queries). If we replace OR by AND in the first version, what do we get? SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ UNION AND B.color=‘green’ Session #, Speaker Name
41
Nested Queries Find names of sailors who’ve reserved boat #103:
4/26/2017 Nested Queries Find names of sailors who’ve reserved boat #103: SELECT sname FROM Sailors WHERE sid IN (SELECT sid FROM Reserves WHERE bid=103) Understand semantics of nested queries For each Sailors tuple, check the qualification by computing the subquery. Session #, Speaker Name
42
Nested Queries Find names of sailors who’ve NOT reserved boat #103:
4/26/2017 Nested Queries Find names of sailors who’ve NOT reserved boat #103: SELECT sname FROM Sailors WHERE sid NOT IN (SELECT sid FROM Reserves WHERE bid=103) A very powerful feature of SQL: a WHERE clause can itself contain an SQL query! Session #, Speaker Name
43
Nested Queries with Correlation
4/26/2017 Nested Queries with Correlation Q: Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Reserves Sailors Session #, Speaker Name
44
Nested Queries with Correlation
4/26/2017 Nested Queries with Correlation Q: Find names of sailors who’ve not reserved boat #103: SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) EXISTS is another set comparison operator, like IN. NOT EXISTS Reserves Sailors Session #, Speaker Name
45
Division in SQL Q: Find names of sailors who’ve reserved all boats.
4/26/2017 Division in SQL Q: Find names of sailors who’ve reserved all boats. SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT bid FROM Boats WHERE bid NOT IN (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) For each sailor we check that there is no boat that has not been reserved by this sailor Session #, Speaker Name
46
Aggregate Operators Significant extension of relational algebra.
4/26/2017 Aggregate Operators Significant extension of relational algebra. COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) single column Q: Find the average age of all sailors SELECT AVG (age) FROM Sailors Q: Find the average age of sailors with a rating of 10 Session #, Speaker Name
47
Queries with GROUP BY and HAVING
4/26/2017 Queries with GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., MIN (S.age)). The attribute list (i) must be a subset of grouping- list. Session #, Speaker Name
48
Important! Only columns that appear in the GROUP BY clause can appear in the HAVING clause, unless they appear with an aggregate operator (COUNT, AVG, MIN, etc.)
49
4/26/2017 Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors Sailors instance: SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Session #, Speaker Name
50
See lecture notes for more examples!
51
Attribute Closure Compute attribute closure of X (denoted X+).
X+ = Set of all attributes A such that X A X+ := X Repeat until no change: { if there is an fd U V in F such that U is in X+, then add V to X+ } Approach can also be used to find the keys of a relation.
52
Attribute Closure (example)
R = {A, B, C, D, E} F = { B CD, D E, B A, E C, AD B } Can B infer E ? B+ = B B+ = BCD B+ = BCDA B+ = BCDAE … Yes! and B is a key for R too! Is D a key for R? D+ = D D+ = DE D+ = DEC … Nope!
53
Normal Forms Four most commonly used normal forms are
First (1NF) normal forms Second (2NF) normal forms Third (3NF) normal forms Boyce-Codd (BCNF) normal forms Based on functional dependencies among the attributes of a relation.
54
Second Normal Forms (2NF)
Table is in 2NF when it is in 1NF and contains no partial dependencies. Partial dependency: there is a functional dependence in which a non-key field is dependent on a subset of a candidate key.
55
Third Normal Forms (3NF)
No non-key field depends on another. All non-key fields depend only on a candidate key. Table is in 3NF when it is in 2NF and contains no transitive dependencies Transitive dependency: there are functional dependencies such that X → Y, Y → Z, and X is the primary key
56
Third Normal Forms (3NF)
Table is in 3NF when it is in 2NF and contains no transitive dependencies
57
Boyce-Codd Normal Form (BCNF)
Reln R with FDs F is in BCNF if, for all X A in A X (called a trivial FD), or X contains a key for R. In other words, R is in BCNF if the only non-trivial FDs that hold over R are key constraints. Every determinant in table is a key. Has same characteristics as primary key, but for some reason, not chosen to be primary key 23
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.