Download presentation
Presentation is loading. Please wait.
1
SQL: Structured Query Language (‘Sequel’)
Chapter 5 (cont.) The slides for this text are organized into chapters. This lecture covers Chapter 5. This is one of the most important chapters in any discussion of database systems. Students must acquire a solid grasp of SQL. In particular, learning how to write queries in SQL is important, and comes only with practice. The slides present the concepts through examples. The chapter contains several additional examples with in-depth explanations; assign these as additional readings. The exercises contain numerous further examples, and come with supporting online material. If you need additional time to cover this material, consider abbreviating the earlier discussion of algebra and calculus, and reinforcing the same concepts in the context of SQL. Note that some new SQL:1999 features for the HAVING clause are covered in these slides. (This material is not covered in the 2nd edition.) Also, material on cursors and other programmatic aspects of SQL has been moved to Chapter 6, following the revisions in the 3rd edition.
2
Query Language Constraints Triggers
SQL Query Language Constraints Triggers The slides for this text are organized into chapters. This lecture covers Chapter 5. This is one of the most important chapters in any discussion of database systems. Students must acquire a solid grasp of SQL. In particular, learning how to write queries in SQL is important, and comes only with practice. The slides present the concepts through examples. The chapter contains several additional examples with in-depth explanations; assign these as additional readings. The exercises contain numerous further examples, and come with supporting online material. If you need additional time to cover this material, consider abbreviating the earlier discussion of algebra and calculus, and reinforcing the same concepts in the context of SQL. Note that some new SQL:1999 features for the HAVING clause are covered in these slides. (This material is not covered in the 2nd edition.) Also, material on cursors and other programmatic aspects of SQL has been moved to Chapter 6, following the revisions in the 3rd edition.
3
Integrity Constraints (Review)
An IC describes conditions that every legal instance of a relation must satisfy. Inserts/deletes/updates that violate ICs are disallowed. Can be used to : ensure application semantics (e.g., sid is a key), or prevent inconsistencies (e.g., sname has to be a string, age must be < 200) Types of IC’s: Fundamental: Domain constraints, primary key constraints, foreign key constraints General constraints : Check Constraints, Table Constraints and Assertions. 5
4
Check or Table Constraints
CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 ) Can use queries to express constraint. 8
5
Table Constraints ( sname CHAR(10), bid INTEGER, day DATE,
CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT noInterlakeRes CHECK (`Interlake’ <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) 8
6
Assertions ( Constraints over Multiple Relations)
CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Number of boats plus number of sailors is < 100 Awkward ! Wrong? Associated with Sailors. If Sailors is empty, the number of Boats tuples can be anything! 9
7
Assertions ( Constraints over Multiple Relations)
CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Number of boats plus number of sailors is < 100 ASSERTION is the right solution; not associated with either table. CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) 9
8
Triggers (active database)
Trigger: procedure that starts automatically if specified changes occur to the DBMS Three parts: Event (activates the trigger) Condition (tests whether the triggers should run) Action (what happens if the trigger runs) A "daemon" that monitors a database Row-level trigger & statement-level trigger
9
Triggers: Example (SQL:1999)
CREATE TRIGGER youngSailorUpdate AFTER INSERT ON SAILORS REFERENCING NEW TABLE NewSailors FOR EACH STATEMENT INSERT INTO YoungSailors(sid, name, age, rating) SELECT sid, name, age, rating FROM NewSailors N WHERE N.age <= 18
10
Design of Active Consistent Data
Triggers are flexible and powerful But can be hard to understand …… Several triggers (order?) Chain triggers (When to stop?) Recursive triggers Constraints IC achieves same goal More opportunity for optimization Not restricted into insert/delete/update
11
Summary SQL allows specification of rich integrity constraints
Triggers respond to changes in the database
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.