Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.

Similar presentations


Presentation on theme: "CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009."— Presentation transcript:

1 CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009

2 Administrative Assignment 2 Due today Assignment 3 is assigned and due next Wednesday Quiz 2 is scheduled on Friday, Qct. 5 th. Project will be assigned next Wednesday. Choose your partner 1/17/2016Jinze Liu @ University of Kentucky2

3 1/17/2016Jinze Liu @ University of Kentucky3 Summary of SQL features covered so far SELECT - FROM - WHERE statements Ordering Set and bag operations Aggregation and grouping Nested Queries  Next: NULL ’s, outerjoins, data modification, constraints, …

4 1/17/2016Jinze Liu @ University of Kentucky4 Incomplete information Example: Student (SID, name, age, GPA) Value unknown We do not know Nelson’s age Value not applicable Nelson has not taken any classes yet; what is his GPA?

5 1/17/2016Jinze Liu @ University of Kentucky5 Solution 1 A dedicated special value for each domain (type) GPA cannot be –1, so use –1 as a special value to indicate a missing or invalid GPA Leads to incorrect answers if not careful SELECT AVG(GPA) FROM Student; Complicates applications SELECT AVG(GPA) FROM Student WHERE GPA <> -1; Remember the Y2K bug? “00” was used as a missing or invalid year value

6 1/17/2016Jinze Liu @ University of Kentucky6 Solution 2 A valid-bit for every column Student (SID,name, name_is_valid, age, age_is_valid, GPA, GPA_is_valid) Complicates schema and queries SELECT AVG(GPA) FROM Student WHERE GPA_is_valid;

7 1/17/2016Jinze Liu @ University of Kentucky7 Solution 3? Decompose the table; missing row = missing value StudentName (SID, name) StudentAge (SID, age) StudentGPA (SID, GPA) StudentID (SID) Conceptually the cleanest solution Still complicates schema and queries How to get all information about a student in a table? Would natural join work?

8 1/17/2016Jinze Liu @ University of Kentucky8 SQL’s solution A special value NULL For every domain Special rules for dealing with NULL ’s Example: Student (SID, name, age, GPA) h 789, “Nelson”, NULL, NULL i

9 1/17/2016Jinze Liu @ University of Kentucky9 Three-valued logic TRUE = 1, FALSE = 0, UNKNOWN = 0.5 x AND y = min(x, y) x OR y = max(x, y) NOT x = 1 – x WHERE and HAVING clauses only select rows for output if the condition evaluates to TRUE UNKNOWN is not enough ANDTrueFalseNULL True FalseUNK False NULLUNKFalseUNK AND ORTrueFalseNULL True FalseTRUE False UNK NULLTRUEUNK OR

10 1/17/2016Jinze Liu @ University of Kentucky10 Computing with NULL’s (Arithmetic operation) when we operate on a NULL and another value (including another NULL ) using +, –, etc., the result is NULL Aggregate functions ignore NULL, except COUNT(*) (since it counts rows) When we compare a NULL with another value (including another NULL ) using =, >, etc., the result is UNKNOWN

11 1/17/2016Jinze Liu @ University of Kentucky11 Unfortunate consequences SELECT AVG(GPA) FROM Student 3.4 SELECT SUM(GPA)/COUNT(*) FROM Student; 2.72 SELECT * FROM Student; SELECT * FROM Student WHERE GPA = GPA Not equivalent sidnameagegpa 1234John Smith213.5 1123Mary Carter193.8 1011Bob Lee22NULL 1204Susan Wong223.4 1306Kevin Kim182.9

12 1/17/2016Jinze Liu @ University of Kentucky12 Another problem Example: Who has NULL GPA values? SELECT * FROM Student WHERE GPA = NULL; Does not work; never returns anything (SELECT * FROM Student) EXCEPT ALL (SELECT * FROM Student WHERE GPA = GPA) Works, but ugly Introduced built-in predicates IS NULL and IS NOT NULL SELECT * FROM Student WHERE GPA IS NULL;

13 1/17/2016Jinze Liu @ University of Kentucky13 Outerjoin motivation Example: a master class list SELECT c.CID, s.SID FROM Enroll e, Student s, Course c WHERE e.SID = s.SID and c.CID = e.CID; What if a student take no classes For these students, CID column should be NULL What if a course with no student enrolled yet? For these courses, SID should be NULL

14 1/17/2016Jinze Liu @ University of Kentucky14 Outerjoin SELECT * FROM R FULL OUTER JOIN S ON p; A full outer join between R and S (denoted R ! S) includes all rows in the result of R ! p S, plus “Dangling” R rows (those that do not join with any S rows) padded with NULL ’s for S’s columns “Dangling” S rows (those that do not join with any R rows) padded with NULL ’s for R’s columns

15 1/17/2016Jinze Liu @ University of Kentucky15 Outerjoin (II) SELECT * FROM R LEFT OUTER JOIN S ON p;; SELECT * FROM R RIGHT OUTER JOIN S ON p; A left outer join (R ! S) includes rows in R ! S plus dangling R rows padded with NULL ’s A right outer join (R ! S) includes rows in R ! S plus dangling S rows padded with NULL ’s

16 1/17/2016Jinze Liu @ University of Kentucky16 Outerjoin examples Department Employee DidMidDname 41123Research 51234Finance 61312HR EidName 1123John Smith 1234Mary Carter 1311Bob Lee SELECT * FROM Employee LEFT OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance 1311Bob LeeNULL SELECT * FROM Employee RIGHT OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance NULL 61312HR SELECT * FROM Employee FULL OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance 1311Bob LeeNULL 61312HR

17 1/17/2016Jinze Liu @ University of Kentucky17 Summary of SQL features covered so far SELECT - FROM - WHERE statements Set and bag operations Ordering Aggregation and grouping Table expressions, subqueries NULL ’s and outerjoins  Next: data modification statements, constraints

18 Relational Query Languages Two sublanguages: DDL – Data Definition Language Define and modify schema DML – Data Manipulation Language Queries can be written intuitively. Insert, delete, update rows in a table DBMS is responsible for efficient evaluation. The key: precise semantics for relational queries. Optimizer can re-order operations, without affecting query answer. Choices driven by “cost model” 1/17/201618Jinze Liu @ University of Kentucky

19 1/17/2016Jinze Liu @ University of Kentucky19 Constraints in Table Declaration Restrictions on allowable data in a database In addition to the simple structure and type restrictions imposed by the table definitions Declared as part of the schema Enforced by the DBMS Why use constraints? Protect data integrity (catch errors) Tell the DBMS about the data (so it can optimize better)

20 1/17/2016Jinze Liu @ University of Kentucky20 Types of SQL constraints Key constraint NOT NULL Referential integrity (foreign key)

21 1/17/2016Jinze Liu @ University of Kentucky21 Key declaration At most one PRIMARY KEY per table Typically implies a primary index Rows are stored inside the index, typically sorted by the primary key value ) best speedup for queries Any number of UNIQUE keys per table Typically implies a secondary index To speedup queries

22 1/17/2016Jinze Liu @ University of Kentucky22 NOT NULL Indicates that the value of an attribute can not be NULL

23 1/17/2016Jinze Liu @ University of Kentucky23 Examples CREATE TABLE Student (SID INTEGER NOT NULL PRIMARY KEY, name VARCHAR(30) NOT NULL, email VARCHAR(30) UNIQUE, age INTEGER, GPA FLOAT); CREATE TABLE Course (CID CHAR(10) NOT NULL PRIMARY KEY, title VARCHAR(100) NOT NULL); CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID)); Used for query processing This form is required for multi-attribute keys

24 1/17/2016Jinze Liu @ University of Kentucky24 Referential integrity example Enroll.SID references Student.SID If an SID appears in Enroll, it must appear in Student Enroll.CID references Course.CID If a CID appears in Enroll, it must appear in Course  That is, no “dangling pointers”

25 1/17/2016Jinze Liu @ University of Kentucky25 Referential integrity in SQL Referenced column(s) must be PRIMARY KEY Referencing column(s) form a FOREIGN KEY Example CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID), FOREIGN KEY CID REFERENCES Course(CID) FOREIGN KEY SID REFERENCES Student(SID));

26 1/17/2016Jinze Liu @ University of Kentucky26 Enforcing referential integrity Example: Enroll.SID references Student.SID Insert or update an Enroll row so it refers to a non- existent SID Reject Delete or update a Student row whose SID is referenced by some Enroll row Reject Cascade: ripple changes to all referring rows Set NULL : set all references to NULL Set DEFAULT: use the default value of the attribute All four options can be specified in SQL

27 1/17/2016Jinze Liu @ University of Kentucky27 SQL Example CREATE TABLE Enroll (SID INTEGER NOT NULL, CID CHAR(10) NOT NULL, PRIMARY KEY(SID, CID), FOREIGN KEY CID REFERENCES Course(CID) CASCADE ON DELETE FOREIGN KEY SID REFERENCES Student(SID) CASCADE ON DELETE); Delete all student enrollments for a canceled class Delete all student enrollments for a dropped student

28 1/17/2016Jinze Liu @ University of Kentucky28 Database Modification Operations INSERT DELETE UPDATE

29 1/17/2016Jinze Liu @ University of Kentucky29 INSERT Insert one row INSERT INTO Enroll VALUES (456, ’CS405’); Student 456 takes CS405 Insert the result of a query INSERT INTO Enroll (SELECT SID, ’CS405’ FROM Student WHERE SID NOT IN (SELECT SID FROM Enroll WHERE CID = ’CS405’)); Force everybody to take CS405!

30 1/17/2016Jinze Liu @ University of Kentucky30 DELETE Delete everything DELETE FROM Enroll; Delete according to a WHERE condition Example: Student 456 drops CS405 DELETE FROM Enroll WHERE SID = 456 AND CID = ’CS405’; Example: Drop students from all CS classes with GPA lower than 1.0 DELETE FROM Enroll WHERE SID IN (SELECT SID FROM Student WHERE GPA < 1.0) AND CID LIKE ’CS%’;

31 1/17/2016Jinze Liu @ University of Kentucky31 UPDATE Example: Student 142 changes name to “Barney” UPDATE Student SET name = ’Barney’ WHERE SID = 142; Example: Let’s be “fair”? UPDATE Student SET GPA = (SELECT AVG(GPA) FROM Student); But update of every row causes average GPA to change! Average GPA is computed over the old Student table

32 1/17/2016Jinze Liu @ University of Kentucky32 “Active” data Constraint enforcement: When an operation violates a constraint, abort the operation or try to “fix” data Example: enforcing referential integrity constraints Generalize to arbitrary constraints? Data monitoring: When something happens to the data, automatically execute some action Example: When price rises above $20 per share, sell Example: When enrollment is at the limit and more students try to register, email the instructor

33 1/17/2016Jinze Liu @ University of Kentucky33 Triggers A trigger is an event-condition-action (ECA) rule When event occurs, test condition; if condition is satisfied, execute action Example: Event: whenever there comes a new student… Condition: with GPA higher than 3.0… Action: then make him/her take EECS 168!

34 1/17/2016Jinze Liu @ University of Kentucky34 Trigger example CREATE TRIGGER EECS168AutoRecruit AFTER INSERT ON Student REFERENCING NEW ROW AS newStudent FOR EACH ROW WHEN (newStudent.GPA > 3.0) INSERT INTO Enroll VALUES(newStudent.SID, ’EECS168’); Event Condition Action

35 1/17/2016Jinze Liu @ University of Kentucky35 Trigger options Possible events include: INSERT ON table DELETE ON table UPDATE [ OF column] ON table Granularity—trigger can be activated: FOR EACH ROW modified FOR EACH STATEMENT that performs modification Timing—action can be executed: AFTER or BEFORE the triggering event

36 1/17/2016Jinze Liu @ University of Kentucky36 Transition variables OLD ROW : the modified row before the triggering event NEW ROW : the modified row after the triggering event OLD TABLE : a hypothetical read-only table containing all modified rows before the triggering event NEW TABLE : a hypothetical table containing all modified rows after the triggering event  Not all of them make sense all the time, e.g. AFTER INSERT statement-level triggers Can use only NEW TABLE BEFORE DELETE row-level triggers Can use only OLD ROW etc.

37 1/17/2016Jinze Liu @ University of Kentucky37 Statement-level trigger example CREATE TRIGGER AutoRecruit AFTER INSERT ON Student REFERENCING NEW TABLE AS newStudents FOR EACH STATEMENT INSERT INTO Enroll (SELECT SID, ’EECS647’ FROM newStudents WHERE GPA > 3.0);

38 1/17/2016Jinze Liu @ University of Kentucky38 BEFORE trigger example Never give faculty more than 50% raise in one update CREATE TRIGGER NotTooGreedy BEFORE UPDATE OF salary ON Faculty REFERENCING OLD ROW AS o, NEW ROW AS n FOR EACH ROW WHEN (n.salary > 1.5 * o.salary) SET n.salary = 1.5 * o.salary;  BEFORE triggers are often used to “condition” data  Another option is to raise an error in the trigger body to abort the transaction that caused the trigger to fire

39 1/17/2016Jinze Liu @ University of Kentucky39 Statement- vs. row-level triggers Why are both needed? Certain triggers are only possible at statement level If the average GPA of students inserted by this statement exceeds 3.0, do … Simple row-level triggers are easier to implement and may be more efficient Statement-level triggers require significant amount of state to be maintained in OLD TABLE and NEW TABLE However, a row-level trigger does get fired for each row, so complex row-level triggers may be inefficient for statements that generate lots of modifications

40 1/17/2016Jinze Liu @ University of Kentucky40 Another statement-level trigger Give faculty a raise if GPA’s in one update statement are all increasing CREATE TRIGGER AutoRaise AFTER UPDATE OF GPA ON Student REFERENCING OLD TABLE AS o, NEW TABLE AS n FOR EACH STATEMENT WHEN (NOT EXISTS(SELECT * FROM o, n WHERE o.SID = n.SID AND o.GPA >= n.GPA)) UPDATE Faculty SET salary = salary + 1000;  A row-level trigger would be difficult to write in this case

41 1/17/2016Jinze Liu @ University of Kentucky41 System issues Recursive firing of triggers Action of one trigger causes another trigger to fire Can get into an infinite loop Some DBMS restrict trigger actions Most DBMS set a maximum level of recursion (16 in DB2) Interaction with constraints (very tricky to get right!) When do we check if a triggering event violates constraints? After a BEFORE trigger (so the trigger can fix a potential violation) Before an AFTER trigger AFTER triggers also see the effects of, say, cascaded deletes caused by referential integrity constraint violations

42 1/17/2016Jinze Liu @ University of Kentucky42 Views A view is like a “virtual” table Defined by a query, which describes how to compute the view contents on the fly DBMS stores the view definition query instead of view contents Can be used in queries just like a regular table

43 1/17/2016Jinze Liu @ University of Kentucky43 Creating and dropping views Example: EECS647roster CREATE VIEW EECS647Roster AS SELECT SID, name, age, GPA FROM Student WHERE SID IN (SELECT SID FROM Enroll WHERE CID = ’EECS647’); To drop a view DROP VIEW view_name ; Called “base tables”

44 1/17/2016Jinze Liu @ University of Kentucky44 Using views in queries Example: find the average GPA of EECS647 students SELECT AVG(GPA) FROM EECS647Roster; To process the query, replace the reference to the view by its definition SELECT AVG(GPA) FROM (SELECT SID, name, age, GPA FROM Student WHERE SID IN (SELECT SID FROM Enroll WHERE CID = ’EECS647’));

45 1/17/2016Jinze Liu @ University of Kentucky45 Why use views? To hide data from users To hide complexity from users Logical data independence If applications deal with views, we can change the underlying schema without affecting applications Recall physical data independence: change the physical organization of data without affecting applications To provide a uniform interface for different implementations or sources  Real database applications use tons of views

46 1/17/2016Jinze Liu @ University of Kentucky46 Modifying views Does not seem to make sense since views are virtual But does make sense if that is how users see the database Goal: modify the base tables such that the modification would appear to have been accomplished on the view Be careful! There may be one way to modify There may be many ways to modify There may be no way to modify

47 1/17/2016Jinze Liu @ University of Kentucky47 A simple case CREATE VIEW StudentGPA AS SELECT SID, GPA FROM Student; DELETE FROM StudentGPA WHERE SID = 123; translates to: DELETE FROM Student WHERE SID = 123;

48 1/17/2016Jinze Liu @ University of Kentucky48 An impossible case CREATE VIEW HighGPAStudent AS SELECT SID, GPA FROM Student WHERE GPA > 3.7; INSERT INTO HighGPAStudent VALUES(987, 2.5); No matter what you do on Student, the inserted row will not be in HighGPAStudent

49 1/17/2016Jinze Liu @ University of Kentucky49 A case with too many possibilities CREATE VIEW AverageGPA(GPA) AS SELECT AVG(GPA) FROM Student; Note that you can rename columns in view definition UPDATE AverageGPA SET GPA = 2.5; Set everybody’s GPA to 2.5? Adjust everybody’s GPA by the same amount? Just lower Lisa’s GPA?

50 1/17/2016Jinze Liu @ University of Kentucky50 SQL92 updateable views More or less just single-table selection queries No join No aggregation No subqueries Arguably somewhat restrictive Still might get it wrong in some cases See the slide titled “An impossible case” Adding WITH CHECK OPTION to the end of the view definition will make DBMS reject such modifications

51 1/17/2016Jinze Liu @ University of Kentucky51 Summary of SQL features covered so far Query Modification Constraints Triggers Views  Next: Indexes, transactions


Download ppt "CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009."

Similar presentations


Ads by Google