SQL Constraints & Triggers May 10 th, 2002. Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.

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.
CSE 544 Constraints Lecture #3 Friday, January 13, 2011 Dan Suciu , Winter
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
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),
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
CMSC424: Database Design Instructor: Amol Deshpande
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
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.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 8.
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 Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
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.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
1 The Relational Model Instructor: Mohamed Eltabakh
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
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.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
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.
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.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
Deductive Databases General idea: some relations are stored (extensional), others are defined by datalog queries (intensional). Many research projects.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
1 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
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.
1 Introduction to Database Systems, CS420 SQL Constraints.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity Constraints Domain Constraints Referential Integrity Assertions Triggers.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
Chapter 6: Integrity (and Security)
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
SQL: Constraints and Triggers
Referential Integrity
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
The Relational Model Relational Data Model
Referential Integrity
SQL – Constraints & Triggers
CPSC-608 Database Systems
Presentation transcript:

SQL Constraints & Triggers May 10 th, 2002

Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL context –primary key –foreign key

Constraints & Triggers Constraints –model additional important aspects of real world Triggers –specify actions we want our database to execute whenever certain conditions are met

Where do they appear? Requirements analysis Conceptual design: use ER, ODL, … –constraints: keys, referential integrity, single-value, domain, … Logical schema design & refinement: –translate from ER/ODL to relational tables –use FD & MVD to refine/decompose tables Schema specification: use SQL –specify key, primary key, foreign key, … –create triggers Database modification –verify that constraints hold, activate triggers

Why are they important? Maintain integrity of a database –e.g., account balance always > 0 –relieve the mundane work from application writer Allow databases to be more “intelligent” –do more on our behalf –e.g., flight status changed to “cancelled” automatically notify all booked customers

SQL: Constraints and Triggers Chapter 6 Ullman and Widom Chapter 7 in the Complete Book Certain properties we’d like our database to hold Modification of the database may break these properties Build handlers into the database definition Key constraints Referential integrity constraints.

Declaring a Primary Keys in SQL CREATE TABLE MovieStar ( name CHAR(30) PRIMARY KEY, address VARCHAR(255), gender CHAR(1)); OR: CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1) PRIMARY KEY (name));

Primary Keys with Multiple Attributes CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1), PRIMARY KEY (name, address));

Other Keys CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), phone CHAR(10) UNIQUE, gender CHAR(1), petName CHAR(50), PRIMARY KEY (name), UNIQUE (gender, petName));

PRIMARY KEY vs. UNIQUE Can have multiple UNIQUEs A foreign key can only reference a primary key Primary key can influence the physical layout of the relation

Foreign Key Constraints CREATE TABLE ActedIn ( Name CHAR(30) PRIMARY KEY, MovieName CHAR(30) REFERENCES Movies(MovieName), Year INT);

Foreign Key Constraints OR CREATE TABLE ActedIn ( Name CHAR(30) PRIMARY KEY, MovieName CHAR(30), Year INT, FOREIGN KEY MovieName REFERENCES Movies(MovieName) MovieName must be a PRIMARY KEY

How do we Maintain them? Given a change to DB, there are several possible violations: –Insert new tuple with bogus foreign key value –Update a tuple to a bogus foreign key value –Delete a tuple in the referenced table with the referenced foreign key value –Update a tuple in the referenced table that changes the referenced foreign key value

How to Maintain? Recall, ActedIn has FK MovieName... Movies(MovieName, year) (Fatal Attraction, 1987) ActedIn(ActorName, MovieName) (Michael Douglas, Fatal Attraction) insert: (Rick Moranis, Strange Brew)

How to Maintain? Policies for handling the change… –Reject the update (default) –Cascade (example: cascading deletes) –Set NULL Can set update and delete actions independently in CREATE TABLE MovieName CHAR(30) REFERENCES Movies(MovieName)) ON DELETE SET NULL ON UPDATE CASCADE