SQL: Structured Query Language Sequel - PowerPoint PPT Presentation

About This Presentation
Title:

SQL: Structured Query Language Sequel

Description:

CREATE ASSERTION smallClub. CHECK ( (SELECT COUNT (S.sid) FROM ... CREATE TRIGGER youngSailorUpdate. AFTER INSERT ON SAILORS. REFERENCING NEW TABLE NewSailors ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 12
Provided by: RaghuRamak241
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: SQL: Structured Query Language Sequel


1
SQL Structured Query Language(Sequel)
  • Chapter 5
  • (cont.)

2
SQL
  • Query Language
  • Constraints
  • Triggers

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 lt 200)
  • Types of ICs
  • Fundamental Domain constraints, primary
    key constraints, foreign key constraints
  • General constraints Check Constraints, Table
    Constraints and Assertions.

4
Check or Table Constraints
CREATE TABLE Sailors ( sid INTEGER, sname
CHAR(10), rating INTEGER, age REAL, PRIMARY
KEY (sid), CHECK ( rating gt 1 AND rating
lt 10 )
  • Can use queries to express constraint.

5
Table Constraints
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) lt 100 )
Number of boats plus number of sailors is lt 100
  • Awkward !
  • Wrong?
  • Associated with Sailors.
  • If Sailors is empty, the number of Boats tuples
    can be anything!

7
Assertions ( Constraints over Multiple Relations)
Number of boats plus number of sailors is lt 100
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) lt 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) lt 100 )
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 (SQL1999)
  • 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 lt 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
Write a Comment
User Comments (0)
About PowerShow.com