1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

PL/SQL.
1 Constraints, Triggers and Active Databases Chapter 9.
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.
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.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Database Systems More SQL Database Design -- More SQL1.
Triggers.
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Lecture #19 May 13 th, Agenda Trip Report End of constraints: triggers. Systems aspects of SQL – read chapter 8 in the book. Going under the lid!
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
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.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
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.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Assertions and triggers1. 2 Constraints Attribute-based CHECK constraints create table … ( postcode number(4) check (postcode > 0) ); Checked at update.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
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
COP-5725 Practice Exercises
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
Database Management COP4540, SCS, FIU Database Trigger.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Copyright © 2004 Pearson Education, Inc.. Chapter 24 Enhanced Data Models for Advanced Applications.
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Introduction to Database Systems, CS420
Remaining Topics in SQL to be covered…
Advanced SQL: Views & Triggers
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL – Constraints & Triggers
-Transactions in SQL -Constraints and Triggers
Assertions and Triggers
Presentation transcript:

1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184Notes 102 Assertions So far most constraints are within one table Assertions: constraints over a table as a whole or multiple tables. CREATE ASSERTION CHECK An assertion must always be true at transaction boundaries. –Any modification that causes it to become false is rejected. Similar to tables, assertions can be dropped by a DROP command.

ICS184Notes 103 Example Dept(dept#, mgr), Emp(name, salary), “Each manager must make more than $50000.” CREATE ASSERTION RichMGR CHECK (NOT EXISTS (SELECT * FROM dept, emp WHERE emp.name = dept.mgr AND emp.sal < 50000)); If someone inserts a manager whose salary is less than 50K, the insertion/update to dept table will be rejected. Furthermore, if a manager’s salary reduced to less than 50K, the corresponding update to emp table will also be rejected.

ICS184Notes 104 Example Dept(dept#, mgr), Emp(name, salary), “Each manager must make more than $50000.” CREATE ASSERTION RichMGR CHECK (NOT EXISTS (SELECT * FROM dept, emp WHERE emp.name = dept.mgr AND emp.sal < 50000)); When checked? Insert/updates on the two tables. E.g., insertion (‘tom’, 40000) to Emp will be rejected. Emp (ename, dno, sal) Dept(dno, dname, mgr)

ICS184Notes 105 Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute insert/update not if contains CHECK on the attribute subquery Tuple relation schema insert/update not if contains CHECK to the relation subquery Assertion database schema any change on always any relation mentioned

ICS184Notes 106 Giving Names to Constraints Goal: to be able to alter constraints. Add the keyword CONSTRAINT and then a name: CREATE TABLE Emp ( name CHAR(30), ssn int CONSTRAINT ssnIsKey PRIMARY KEY, age int CONSTRAINT rightage CHECK (age > 18 AND age < 100) ); Change constraints ALTER TABLE emp ADD CONSTRAINT ssnPositive CHECK (ssno > 0); ALTER TABLE emp DROP CONSTRAINT ssnIsKey; DROP ASSERTION assert1;

ICS184Notes 107 Triggers Motivation: enable database programmers to specify –when to check a constraint, –what exactly to do. A trigger has 3 parts: –An event (e.g., update to an attribute) –A condition (e.g., a query to check) –An action (deletion, update, insertion) –When the event happens, the system will check the constraint, and if satisfied, will perform the action. –Thus, also called “ECA” NOTE: triggers may cause cascading effects. Triggers not part of SQL2 but included in SQL3. DB vendors did not wait for standards with triggers!

ICS184Notes 108 Elements of Triggers (in SQL3) Timing of action execution: before, after, or instead of triggering event The action can refer to both the old and new state of the database. Update events may specify a particular column or set of columns. A condition is specified with a WHEN clause. The action can be performed either –once for every tuple (row-level), or –once for all the tuples that are changed by the database operation ( statement level)

ICS184Notes 109 Row-Level Triggers emp(name, sal,…) “No update can reduce employees’ salaries.” CREATE TRIGGER NoLowerSalary AFTER UPDATE OF sal ON emp REFERENCING OLD AS OldTuple NEW AS NewTuple WHEN (OldTuple.sal > NewTuple.sal) UPDATE Emp SET sal= OldTuple.sal WHERE name = NewTuple.name FOR EACH ROW;

ICS184Notes 1010 Statement-Level Triggers emp(dno…), dept(dept#, …) “Whenever we insert employees tuples, make sure that their dno’s exist in Dept.” CREATE TRIGGER deptExistTrig AFTER INSERT ON emp REFERENCING OLD_TABLE AS OldStuff NEW_TABLE AS NewStuff WHEN (EXISTS (SELECT * FROM NewStuff WHERE dno NOT IN (SELECT dept# FROM dept))) DELETE FROM emp WHERE dno NOT IN (SELECT dept# FROM dept));

ICS184Notes 1011 Index A data structure for efficient retrieval of tables Implementation could be different –E.g. Hash table Create an index: –Syntax: CREATE INDEX ON ( ); –Example: CREATE INDEX sal_index ON emp(sal); Drop an index –Syntax:DROP INDEX ; –Example: DROP INDEX sal_index;