ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
IC and Triggers in SQL. Find age of the youngest sailor with age
SQL Constraints and Triggers
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
ICS 421 Spring 2010 Indexing (1) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 02/18/20101Lipyeow Lim.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
The Relational Model These slides are based on the slides of your text book.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
SQL Server 7.0 Maintaining Referential Integrity.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 SQL: Constraints and Triggers Chapter 5,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
The Relational Model1 ER-to-Relational Mapping and Views.
ICS 321 Fall 2009 The Relational Model (ii) Asst. Prof. Lipyeow Lim Information and Computer Science Department University of Hawaii at Manoa 9/10/20091Lipyeow.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
SQL Integrity Constraints. 421B: Database Systems - Integrity Constraints 2 Integrity Constraints (Review) q An IC describes conditions that every legal.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Assertions and Triggers in SQL
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
SQL – Part 2.
COP Introduction to Database Structures
Constraints and Triggers
SQL: Constraints and Triggers
SQL: Structured Query Language (‘Sequel’)
2018, Fall Pusan National University Ki-Joune Li
The Relational Model Relational Data Model
Views 1.
SQL – Constraints & Triggers
Presentation transcript:

ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/07/20111Lipyeow Lim -- University of Hawaii at Manoa

PK and FK Constraints 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa2 CREATE TABLE Studio ( name CHAR(30) NOT NULL PRIMARY KEY, address VARCHAR(255), presC# INT REFERENCES MovieExec(cert#) ) CREATE TABLE Studio ( name CHAR(30) NOT NULL, address VARCHAR(255), presC# INT, PRIMARY KEY(name), FOREIGN KEY(presC#) REFERENCES MovieExec(cert#) ) Cert# must be declared with PRIMARY KEY or UNIQUE constraint

Maintaining Referential Integrity INSERT INTO studio VALUES (...) UPDATE studio SET presC#=?... DELETE FROM MovieExec WHERE... UPDATE MovieExec SET cert#=?... 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa3 CREATE TABLE Studio ( name CHAR(30) NOT NULL PRIMARY KEY, address VARCHAR(255), presC# INT REFERENCES MovieExec(cert#) ) If new presC# value does not exist in MovieExec, reject! If deleted cert# values are used in studio, reject! If old cert# values are used in studio, reject!

Other Options for Referential Integrity CASCADE : changes to referenced attributes are mimicked at FK. SET NULL : changes to referenced attributes makes affected FK null DEFERABLE : checking can wait till end of transaction – INITIALLY DEFERRED : defer checking – INITIALLY IMMEDIATE : check immediately 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa4 CREATE TABLE Studio ( name CHAR(30) NOT NULL PRIMARY KEY, address VARCHAR(255), presC# INT REFERENCES MovieExec(cert#) ON DELETE SET NULL ON UPDATE CASCADE )

Check Constraints Attribute, tuple-based, multi-table Syntax: CHECK conditional-expression 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa5 CREATE TABLE Studio ( name CHAR(30) NOT NULL PRIMARY KEY, address VARCHAR(255), presC# INT REFERENCES MovieExec(cert#) CHECK ( presC# >= ) ) CREATE TABLE MovieStar ( name CHAR(30) NOT NULL PRIMARY KEY, address VARCHAR(255), gender CHAR(1), birthdate DATE, CHECK ( gender = ‘F’ OR name NOT LIKE ‘Ms.%’ ) )

Naming Constraints Constraints can be named, so that you can refer to them in alter table statements 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa6 CREATE TABLE Studio ( name CHAR(30) CONSTRAINT nameiskey PRIMARY KEY, address VARCHAR(255), presC# INT REFERENCES MovieExec(cert#) CONSTRAINT sixdigit CHECK ( presC# >= ) ) ALTER TABLE Studio DROP CONSTRAINT nameiskey; ALTER TABLE Studio ADD CONSTRAINT nameiskey PRIMARY KEY(name) ;

Constraints over Multiple Tables Example: number of boats + number of sailors < /07/2011Lipyeow Lim -- University of Hawaii at Manoa7 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 ) When is the constraint enforced ? What happens if the sailors table is empty ? Think of a case when the constraint is violated but the system never catches it.

CREATE ASSERTION Allows constraints that are not associated with any table. Evaluated whenever tables in the condition are updated 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa8 CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 )

Triggers 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) 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa9

Example of a Trigger Why is “NewSailors” needed ? What is the difference between a constraint and a trigger ? 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa10 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

Another Example of a Trigger Create a trigger that will cause an error when an update occurs that would result in a salary increase greater than ten percent of the current salary. 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa11 CREATE TRIGGER RAISE_LIMIT AFTER UPDATE OF SALARY ON EMPLOYEE REFERENCING NEW AS N OLD AS O FOR EACH ROW WHEN (N.SALARY > 1.1 * O.SALARY) SIGNAL SQLSTATE '75000' SET MESSAGE_TEXT='Salary increase>10%'

Views A view is just a relation, but we store a definition, rather than a set of tuples. Views can be dropped using the DROP VIEW command. What if table that the view is dependent on is dropped ? DROP TABLE command has options to let the user specify this. CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21 11/07/201112Lipyeow Lim -- University of Hawaii at Manoa

Querying Views 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa13 CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21 SELECT name FROM YoungActiveStudents WHERE grade = ‘A’ SELECT name FROM (SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21) WHERE grade = ‘A’ Query views as with any table Conceptually, you can think of rewriting using a subquery

Updateable Views In general views are not updateable. Why? A view on R is updateable when – WHERE : must not involve R in a subquery – FROM : only one occurrence of R and no joins. – SELECT : include enough attributes to fill out other attributes in R 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa14 CREATE VIEW ParamountMovies AS SELECT title, year FROM movies WHERE studioName=‘Paramount’ INSERT INTO ParamountMovies VALUES (‘Star Trek’, 1979) INSERT INTO Movies ( title, year ) VALUES (‘Star Trek’, 1979) SELECT * FROM ParamountMovies

Indexes in SQL 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa15 SELECT * FROM Movies WHERE studioName=‘Disney’ AND year=1990 TitleYearLengthGenrestudio Name produc erC# ,000 rows 200 movies are made in 1990 An index on attribute A is a data structure that makes it efficient to find those tuples that have a fixed value for attribute A

Creating Indexes Clustered Index : an index on an attribute that the tuples are sorted in. If a primary key is specified in the CREATE TABLE statement, an (unclustered) index is automatically created for the PK. To create a clustered PK index: – Create table without PK constraint – Create index on PK with cluster option – Alter table to add PK constraint To get rid of unused indexes: DROP INDEX myIdx; 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa16 CREATE INDEX myIdx ON mytable(col1, col3) CREATE UNIQUE INDEX myUniqIdx ON mytable(col2, col5) CREATE INDEX myIdx ON mytable(col1, col3) CLUSTER

Materialized Views Views can be “materialized” for efficiency Updating the materialized view (materialized query table in DB2) : incremental or batch 11/07/2011Lipyeow Lim -- University of Hawaii at Manoa17 CREATE VIEW ParamountMovies AS SELECT title, year FROM movies WHERE studioName=‘Paramount’ CREATE TABLE ParamountMovies AS (SELECT title, year FROM movies WHERE studioName=‘Paramount’) SELECT title FROM movies WHERE studioName=‘Paramount’ AND year=1990) Queries on base relation may be able to exploit materialized views!