Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems (CS 564)

Similar presentations


Presentation on theme: "Database Management Systems (CS 564)"— Presentation transcript:

1 Database Management Systems (CS 564)
Fall 2017 Lecture 5

2 “It’s so easy marketers can learn it!”
SQL: Part 2 “It’s so easy marketers can learn it!” CS 564 (Fall'17)

3 Recap DDL: DML CREATE TABLE INSERT DELETE UPDATE
PRIMARY KEY, FOREIGN KEY, NOT NULL, ON DELETE (NO ACTION, CASCADE, SET DEFAULT/NULL) DML INSERT DELETE UPDATE CS 564 (Fall'17)

4 Recap: Basic SELECT SELECT * Arithmetic expressions LIKE ORDER BY
SELECT [DISTINCT] target-list FROM relation-list [WHERE condition]; SELECT * Arithmetic expressions LIKE ORDER BY LIMIT CS 564 (Fall'17)

5 Recap: Multi-relation Queries
General form Natural language semantics Start with the Cartesian product R1×R2×…×Rn Apply the selection conditions from the WHERE clause Project the results onto a1,a2,…,ak SELECT a1, a2, …, ak FROM R1 AS x1, R2 AS x2, …, Rn AS xn WHERE <conditions>; CS 564 (Fall'17)

6 Review Example User(UID: string, Name: string, Age: int)
Event(EID: string, Name: string, Location: string, StartDT: DateTime, EndDT: DateTime, Description: string, CreatorUID: string, CreateDT: DateTime) ParticipateIn(EID: string, UID: string, RSVPDT: DateTime) What are the Names of all Users who have participated in some Super Bowl Event? SELECT U.Name FROM User U, Event E, ParticipateIn P WHERE U.UID = P.UID AND E.EID = P.EID AND (E.Name LIKE ‘%SuperBowl%’ OR E.Name LIKE ‘%Super Bowl%’); CS 564 (Fall'17)

7 Set (and Bag) Operations
Student UNION SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 PHYS 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Class = 21; Union-compatible UNION SELECT Name FROM Student WHERE Major = ‘CS’; Name Brown Moreno Bakhtiari Set semantics CS 564 (Fall'17)

8 Set (and Bag) Operations (Cont.)
Student UNION ALL SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 PHYS 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Class = 21; UNION ALL SELECT Name FROM Student WHERE Major = ‘CS’; Name Moreno Bakhtiari Brown Bag semantics CS 564 (Fall'17)

9 Set (and Bag) Operations (Cont.)
Student INTERSECT SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 PHYS 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Class = 21; INTERSECT SELECT Name FROM Student WHERE Major = ‘CS’; Name Bakhtiari CS 564 (Fall'17)

10 Set (and Bag) Operations (Cont.)
Student EXCEPT (set minus) SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 PHYS 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Class = 21; EXCEPT SELECT Name FROM Student WHERE Major = ‘CS’; Name Moreno CS 564 (Fall'17)

11 Nested Queries Course CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What are the Names of all Courses which are in the same Department as “Database Management Systems”? SELECT Name FROM Course WHERE Department = (SELECT Department WHERE Name = ‘Database Management Systems’); CS 564 (Fall'17)

12 Nested Queries (Cont.) A parenthesized SELECT-FROM- WHERE statement (subquery) used in SELECT clauses FROM clauses WHERE clauses Easier to write certain types of queries using nested queries SELECT CID, Name FROM (SELECT * FROM Course WHERE Department = ‘CS’) WHERE Name LIKE ‘%Management%’; CS 564 (Fall'17)

13 Unnesting Rewriting a nested query without subqueries such that the rewritten version is equivalent to the original query; i.e. they return the same data for any database instance Query optimizers can use unnesting to speed-up the execution of certain queries CS 564 (Fall'17)

14 Unnesting (Cont.) SELECT CID, Name FROM (SELECT * FROM Course
WHERE Department = ‘CS’) WHERE Name LIKE ‘%Management%’; Can you unnest this query? SELECT CID, Name FROM Department WHERE Department = ‘CS’ AND Name LIKE ‘%Management%’; CS 564 (Fall'17)

15 Unnesting (Cont.) SELECT D.Name FROM Department AS D WHERE D.DID = (
SELECT Major FROM Student WHERE SID = 17); Can you unnest this query? SELECT D.Name FROM Department AS D, Student AS S WHERE D.DID = S.Major AND S.SID = 17; CS 564 (Fall'17)

16 Set Comparison Operators in Nested Queries: IN
User(UID, Name, Age) Event(EID, Name, Location, StartDT, EndDT, Description, CreatorUID, CreateDT) What are the Names of all Events whose creators are under 18? SELECT E.Name FROM Event AS E WHERE E.CreatorID IN ( SELECT UID FROM User WHERE Age < 18); CS 564 (Fall'17)

17 Set Comparison Operators in Nested Queries: NOT IN
User(UID, Name, Age) Event(EID, Name, Location, StartDT, EndDT, Description, CreatorUID, CreateDT) What are the Names of all Events whose creators are NOT under 18? SELECT E.Name FROM Event AS E WHERE E.CreatorID NOT IN ( SELECT UID FROM User WHERE Age < 18); Q: Not the same as using IN and Age >= 18. Why? A: NULLs! CS 564 (Fall'17)

18 Set Comparison Operators in Nested Queries: EXISTS
Course(CID, Name, Credits, Department) Section(SecID, CID, Semester, Year, Instructor) What are the Names of all Courses which have at least one Section offered in 2010? SELECT C.Name FROM Course AS C WHERE EXISTS ( SELECT * FROM Section AS S WHERE S.Year = ‘2010’ AND C.CID = S.CID); Correlated subqueries CS 564 (Fall'17)

19 Set Comparison Operators in Nested Queries: NOT EXISTS
Course(CID, Name, Credits, Department) Section(SecID, CID, Semester, Year, Instructor) What are the Names of all Courses which have no Sections offered in 2010? SELECT C.Name FROM Course AS C WHERE NOT EXISTS ( SELECT * FROM Section AS S WHERE S.Year = ‘2010’ AND C.CID = S.CID); CS 564 (Fall'17)

20 Set Comparison Operators in Nested Queries: UNIQUE
Course(CID, Name, Credits, Department) Section(SecID, CID, Semester, Year, Instructor) What are the Names of all Courses which have at most one Section offered each year? Returns TRUE if there are no duplicates in the results of the subquery (empty results set yields true) SELECT C.Name FROM Course AS C WHERE UNIQUE ( SELECT Year FROM Section AS S WHERE C.CID = S.CID); CS 564 (Fall'17)

21 Set Comparison Operators in Nested Queries: NOT UNIQUE
Course(CID, Name, Credits, Department) Section(SecID, CID, Semester, Year, Instructor) What are the Names of all Courses which have more than one Section offered in at least one year? SELECT C.Name FROM Course AS C WHERE NOT UNIQUE ( SELECT Year FROM Section AS S WHERE C.CID = S.CID); CS 564 (Fall'17)

22 Set Comparison Operators in Nested Queries: ANY
Course(CID, Name, Credits, Department) Section(SecID, CID, Semester, Year, Instructor) What are the Names of all Courses some Section of which has been offered after some Course offered by the late Jim Gray? SELECT C.Name FROM Course AS C, Section AS S1 WHERE C.CID = S1.CID AND S1.Year > ANY ( SELECT S2.Year FROM Section AS S2 WHERE S2.Instructor = ‘Jim Gray’); CS 564 (Fall'17)

23 Set Comparison Operators in Nested Queries: ALL
Course(CID, Name, Credits, Department) Section(SecID, CID, Semester, Year, Instructor) What are the Names of all Courses some Section of which has been offered before any Courses offered by the late Jim Gray? SELECT C.Name FROM Course AS C, Section AS S1 WHERE C.CID = S1.CID AND S1.Year < ALL ( SELECT S2.Year FROM Section AS S2 WHERE S2.Instructor = ‘Jim Gray’); CS 564 (Fall'17)

24 Recap Multi-relation queries Aliases Set and bag operations
Basis SELECT SELECT *, arithmetic expressions, LIKE, ORDER BY, LIMIT Multi-relation queries Aliases Set and bag operations Nested queries and unnesting Set comparison operators (NOT) IN, (NOT) EXISTS, (NOT) UNIQUE, op ANY, op ALL op ∈ {=, <>, >, >=, <, <=} CS 564 (Fall'17)

25 Rewriting INTERSET Using IN
SELECT Name FROM Student WHERE Class = 21 INTERSECT WHERE Major = ‘CS’; SELECT Name FROM Student WHERE Class = 21 AND SID IN ( SELECT SID WHERE Major = ‘CS’); CS 564 (Fall'17)

26 Rewriting EXCEPT Using NOT IN
SELECT Name FROM Student WHERE Class = 21 EXCEPT WHERE Major = ‘CS’; SELECT Name FROM Student WHERE Class = 21 AND SID NOT IN ( SELECT SID WHERE Major = ‘CS’); CS 564 (Fall'17)

27 Aggregates Getting into the realm of data analytics
Applied to a column to generate aggregated values SUM, AVG, COUNT, MIN, MAX CS 564 (Fall'17)

28 COUNT SELECT COUNT(*) FROM Course WHERE Department = ‘CS’; Course
CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management How many Courses does CS department offer? SELECT COUNT(*) FROM Course WHERE Department = ‘CS’; COUNT(*) 3 CS 564 (Fall'17)

29 COUNT DISTINCT SELECT COUNT(DISTINCT Credits) FROM Course
CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management How many different Credits CS department Courses have? SELECT COUNT(DISTINCT Credits) FROM Course WHERE Department = ‘CS’; COUNT(DISTINCT Credits) 2 CS 564 (Fall'17)

30 SUM SELECT SUM(Credits) FROM Course WHERE Department = ‘CS’; Course
CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What is the total number of Credits for CS department Courses? SELECT SUM(Credits) FROM Course WHERE Department = ‘CS’; SUM(Credits) 10 CS 564 (Fall'17)

31 AVG SELECT AVG(Credits) FROM Course WHERE Department = ‘CS’; Course
CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What is the average number of Credits for CS department Courses? SELECT AVG(Credits) FROM Course WHERE Department = ‘CS’; AVG(Credits) 3.33 CS 564 (Fall'17)

32 MAX and MIN Column renaming SELECT MAX(Credits) AS MaxCreds,
Course CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What are the max and min number of Credits for CS department Courses? SELECT MAX(Credits) AS MaxCreds, MIN(Credits) AS MinCreds FROM Course WHERE Department = ‘CS’; MaxCreds MinCreds 4 3 Column renaming CS 564 (Fall'17)

33 Aggregates (Cont.) SELECT CID, MAX(Credits) AS MaxCreds FROM Course
Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management SELECT CID, MAX(Credits) AS MaxCreds FROM Course WHERE Department = ‘CS’; Q: Is this a valid query? A: No. Can’t use attributes in the target-list as such. CS 564 (Fall'17)

34 Aggregates (Cont.) SELECT C1.Name FROM Course AS C1
CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What are the Names of all Courses whose number of Credits is greater than the overall average number of Credits across all Departments? SELECT C1.Name FROM Course AS C1 WHERE C1.Credits > ( SELECT AVG(C2.Credits) FROM Course AS C2 ); Name Discrete Mathematics Intro to Data Structures CS 564 (Fall'17)

35 Aggregates on Groups How about computing average number of Credits for Courses in each Department separately? Write an aggregation query for each Department, or … Use GROUP BY clause CS 564 (Fall'17)

36 GROUP BY Course CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What are the average number of Credits for Courses in each Department ? SELECT Department, AVG(Credits) AS AvgCreds FROM Course GROUP BY Department; Department AvgCreds MATH 4 CS 3.33 CS 564 (Fall'17)

37 GROUP BY (Cont.) If any aggregation is used, then each element of the SELECT list must be either aggregated, or an attribute on the GROUP BY list What if we are only interested in retrieving groups satisfying a specific condition; e.g. all the Departments with average number of Credits above 3.5? Use HAVING clause CS 564 (Fall'17)

38 GROUP BY … HAVING SELECT Department, AVG(Credits) AS AvgCreds
Course CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What are all the Departments with average number of Credits above 3.5?? SELECT Department, AVG(Credits) AS AvgCreds FROM Course GROUP BY Department HAVING AvgCreds > 3.5; Department AvgCreds MATH 4 CS 564 (Fall'17)

39 GROUP BY … HAVING (Cont.)
Course CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management What are the average number of Credits for Courses in each Department offering more than one Course? SELECT Department, AVG(Credits) AS AvgCreds FROM Course GROUP BY Department HAVING COUNT(*) > 1; Department AvgCreds CS 3.33 CS 564 (Fall'17)

40 Recap: Aggregates SELECT [DISTINCT] target-list FROM relation-list [WHERE condition] GROUP BY grouping-list HAVING group-condition; target-list may contain from grouping- list and/or any aggregates, but no other attributes condition may contain conditions on any attributes in relation-list group-condition may contain conditions on aggregate expressions CS 564 (Fall'17)

41 Recap: Aggregates (Cont.)
SELECT [DISTINCT] target-list FROM relation-list [WHERE condition] GROUP BY grouping-list HAVING group-condition; Conceptual evaluation steps Compute the FROM relation-list [WHERE condition] part, obtain a table with all attributes in relation-list Group by the attributes in grouping-list Compute the aggregates in group-condition and keep only groups satisfying the conditions Compute aggregates in target-list and return the result CS 564 (Fall'17)

42 NULL: The Hairy Beast (Revisited)
A special value which signifies one of the following: Nonexistent value e.g. a Student has not declared a Major Missing value e.g. a Student has not entered their Height Not applicable e.g. a single-family Home does not have an AptNo NULL does not mean 0, “” or NaN. NULL ≠ NULL 824 Fernando 19 NULL CS 564 (Fall'17)

43 NULL and Simple Predicates
Student What are the Names of all Students who have declared a CS Major? SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 NULL 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Major = ‘CS’; NULL values “fail” to satisfy the condition due to ambiguity. Name Brown Bakhtiari CS 564 (Fall'17)

44 NULL and Complicated Predicates
Student What are the Names of all Students who have declared a CS Major or are in Class 21? SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 NULL 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Major = ‘CS’ OR Class = 21; Q: What would the result set look like? CS 564 (Fall'17)

45 Binary Logic Familiar, two-valued logic OR TRUE FALSE AND TRUE FALSE
NOT TRUE FALSE CS 564 (Fall'17)

46 Ternary Logic Predicates including NULLs would be evaluated to UNKNOWN. OR TRUE FALSE UNKNOWN AND TRUE FALSE UNKNOWN NOT TRUE FALSE UNKNOWN CS 564 (Fall'17)

47 NULL and Complicated Predicates
Student What are the Names of all Students who have declared a CS Major or are in Class 21? SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 NULL 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Major = ‘CS’ OR Class = 21; Name Brown Moreno Bakhtiari CS 564 (Fall'17)

48 NULL and Complicated Predicates
Student SID Name Class Major 17 Smith 22 MATH 8 Brown 24 CS 5 Moreno 21 NULL 23 Boll BIOL 7 Bakhtiari SELECT Name FROM Student WHERE Major = ‘CS’ OR Major <> ‘CS’; Name Smith Brown Boll Bakhtiari CS 564 (Fall'17)

49 Recap Multi-relation queries Aliases Set and bag operations
Basis SELECT SELECT *, arithmetic expressions, LIKE, ORDER BY, LIMIT Multi-relation queries Aliases Set and bag operations Nested queries and unnesting Set comparison operators (NOT) IN, (NOT) EXISTS, (NOT) UNIQUE, op ANY, op ALL (op ∈ {=, <>, >, >=, <, <=}) Aggregates SUM, AVG, COUNT, MIN, MAX GROUP BY … HAVING NULL and ternary logic CS 564 (Fall'17)

50 More DDL Delete a table Modify a table Add a new column
Delete a column DROP TABLE User; ALTER TABLE User ADD COLUMN Weight INT CHECK (Weight < 1000); ALTER TABLE User DROP COLUMN Weight; CS 564 (Fall'17)

51 More DDL: Constraints Declare (alternate) key constraints
CREATE TABLE User ( UID CHAR(20), Name CHAR(50), Age INTEGER, SSN INTEGER, PRIMARY KEY (UID), UNIQUE (SSN)); CS 564 (Fall'17)

52 More DDL: Constraints (Cont.)
Integrity constraints CREATE TABLE User ( UID CHAR(20), Name CHAR(50), Age INTEGER, SSN INTEGER CHECK (SSN < ), PRIMARY KEY (UID), UNIQUE (SSN); A CHECK is checked (!) when a value for the attribute is inserted or updated. CS 564 (Fall'17)

53 More DDL: Constraints (Cont.)
Integrity constraints (cont.) CREATE TABLE User ( UID CHAR(20), Name CHAR(50), Age INTEGER, SSN INTEGER, PRIMARY KEY (UID), UNIQUE (SSN), CHECK (Age >= 0 AND Age <= 150); CS 564 (Fall'17)

54 More DDL: Constraints (Cont.)
Named integrity constraints CREATE TABLE Event ( EID CHAR(20), Name CHAR(50), Location CHAR(50), StartDT DATE, EndDT DATE, Description CHAR(100), CreatorUID CHAR(20) NOT NULL, CreateDT DATE, PRIMARY KEY (EID), FOREIGN KEY (CreatorUID) REFERENCES User(UID) CONSTRAINT CreatorAgeConstr CHECK (18 <= (SELECT U.Age FROM User AS U WHERE U.UID = Event.CreatorUID)); CS 564 (Fall'17)

55 Assertions Another mechanism to declare constraints which span multiple relations In principle, checked every time any relation in database is modified CREATE ASSERTION MoreStudsThanProfs CHECK ( (SELECT COUNT(*) FROM Professor) <= (SELECT COUNT(*) FROM Student) ); CS 564 (Fall'17)

56 Assertions (Cont.) In reality, DBMSs are more clever and check only if the assertion can be violated e.g. the above assertions does not need to be checked if we only change the age of a Student However, it is not trivial to find out only the assertions that could possibly have been violated upon every update to the database CS 564 (Fall'17)

57 Triggers A more general and comprehensive tool to enforce integrity constraints and more! Follows a Event-Condition-Action model Event: what activates the trigger Condition: when the trigger should be executed Action: how the trigger operates CS 564 (Fall'17)

58 Example Trigger CREATE TRIGGER AddAgeTrig BEFORE INSERT ON Student
FOR EACH ROW BEGIN SELECT RAISE(ABORT , ’Age lt average') WHERE NEW.Age < ( SELECT AVG(Age) FROM Student ); END Don’t insert a Student whose Age is less than the average Age of current Students. CS 564 (Fall'17)

59 Trigger General Syntax
CREATE [OR REPLACE] TRIGGER <Trigger name> {BEFORE | AFTER | INSTEAD OF} {INSERT | DELETE | UPDATE [OF <Columns>]} ON <Table name> [REFERENCING {OLD | NEW} {ROW | TABLE} AS <Reference name>] [FOR EACH {ROW | STATEMENT}] [WHEN (search condition)] <SQL statement> | BEGIN [ATOMIC] <SQL statements> END CS 564 (Fall'17)

60 Triggers (Cont.) “When” of the event “What” of the event
BEFORE, AFTER, INSTEAD OF “What” of the event INSERT, DELETE, UPDATE, UPDATE OF <attribute> Action granularity FOR EACH ROW, FOR EACH STATEMENT Referring to modified values NEW.<attribute>: the new value before the event (INSERT and UPDATE) OLD.<attribute>: the old value before the event (DELETE and UPDATE) NEW and OLD can be temporary tables containing all modified tuples CS 564 (Fall'17)

61 Another Example Trigger
CREATE TRIGGER CreateCourseForSectionTrig AFTER INSERT ON Section REFERENCING NEW ROW AS n FOR EACH ROW WHEN (NOT EXISTS ( SELECT * FROM COURSE AS C WHERE C.CID = n.CID)) BEGIN INSERT INTO Course VALUES (n.CID, ‘TBD’, 3, ‘CS’); END CS 564 (Fall'17)

62 Triggers vs. Constraints
Both provide to enforce integrity and consistency of the database Constraints are declarative, whereas triggers are “operational” Having many interrelated triggers can cause unexpected or unwanted behaviors, whereas constraints are easier to understand/reason about Triggers are more powerful and expressive Complex integrity constraints (e.g., enforce credit limits) Form auto-completing Generating logs for specific auditing/security reasons CS 564 (Fall'17)

63 View A ”virtual table” that is defined based on the contents of other tables (base tables) and views Useful to avoid rewriting, sometimes complex, queries Provides logical data independence Another layer of isolation between end user and database internals CS 564 (Fall'17)

64 CREATE VIEW CREATE VIEW CourseInfo AS SELECT C.CID, C.Name, C.Credits,
MAX(S.Year) AS YearLastOffered FROM Course AS C, Section AS S WHERE C.CID = S.SID GROUP BY C.CID, C.Name, C.Credits; CS 564 (Fall'17)

65 Example View CourseInfo Section Course CID Name Credits
Semester Year Instructor 30098 MATH240 Fall 2017 Euclid 40026 CS367 2016 Dijkstra 1005 Spring 2004 Gauss 30451 CS764 Patel 20006 CS564 2001 Codd CID Name Credits Department CS564 Database Management Systems 3 CS MATH240 Discrete Mathematics 4 MATH CS367 Intro to Data Structures CS764 Adv. Database Management CourseInfo CID Name Credits YearLastOffered CS564 Database Management Systems 3 2001 MATH240 Discrete Mathematics 4 2017 CS367 Intro to Data Structures 2016 CS764 Adv. Database Management CS 564 (Fall'17)

66 Example View (Cont.) Logical Data Independence CourseInfo Section
SecID CID Semester Year Instructor 30098 MATH240 Fall 2017 Euclid 40026 CS367 2016 Dijkstra 1005 Spring 2004 Gauss 30451 CS764 Patel 20006 CS564 2001 Codd CID Name Credits Department Textbook CS564 Database Management Systems 3 CS Cow book MATH240 Discrete Mathematics 4 MATH Casino book CS367 Intro to Data Structures Tree book CS764 Adv. Database Management Complete book Logical Data Independence CourseInfo CID Name Credits YearLastOffered CS564 Database Management Systems 3 2001 MATH240 Discrete Mathematics 4 2017 CS367 Intro to Data Structures 2016 CS764 Adv. Database Management CS 564 (Fall'17)

67 Query A View SELECT * FROM CourseInfo; DBMS Query Rewriting SELECT *
FROM (SELECT C.CID, C.Name, C.Credits, MAX(S.Year) AS YearLastOffered FROM Course AS C, Section AS S WHERE C.CID = S.SID GROUP BY C.CID, C.Name, C.Credits); CS 564 (Fall'17)

68 Update A View A bit trickier since a view does not really exist
Possible solution: using triggers! Still tricky since multiple underlying base tables might need to change CS 564 (Fall'17)

69 Update A View (Cont.) CREATE TRIGGER ClassInfoInsert
CREATE VIEW ClassInfo AS SELECT C.CID, C.Name, C.Credits, S.Semester, S.Year FROM Course AS C, Section AS S WHERE C.CID = S.SID; CREATE TRIGGER ClassInfoInsert INSTEAD OF INSERT ON ClassInfo FOR EACH ROW BEGIN INSERT INTO Course VALUES (NEW.CID, NEW.Name, NEW.Credits, ‘CS’); INSERT INTO Section VALUES ( (SELECT MAX(S.SID) FROM Section AS S) + 1, NEW.CID, NEW.Semester, NEW.Year, ‘TBA’ ); END; CS 564 (Fall'17)

70 Recap Set and bag operations Nested queries and unnesting
Basis SELECT, multi-relation queries and aliases Set and bag operations Nested queries and unnesting Set comparison operators Aggregates NULL and ternary logic Integrity Constraints CHECK, CONSTRAINT, ASSERTION, TRIGGER Views CS 564 (Fall'17)

71 Schema Refinement: Escaping Data Traps
Next Up Schema Refinement: Escaping Data Traps Questions? CS 564 (Fall'17)


Download ppt "Database Management Systems (CS 564)"

Similar presentations


Ads by Google