Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

Similar documents
SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

The Relational Model. Suan Lee

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

The Relational Model of Data (ii)

The Relational Data Model. Data Model

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

CS W Introduction to Databases Spring Computer Science Department Columbia University

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Integrity Constraints (Reminder)

Constraints. Local and Global Constraints Triggers

Lecture 08. Spring 2018 Borough of Manhattan Community College

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

The Relational Model. Week 2

Relational data model

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Introduction to Database Systems CSE 414

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

SQL DATA DEFINITION LANGUAGE

Constraints and Triggers

SQL DATA DEFINITION LANGUAGE

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

CS425 Midterm Exam Summer C 2012

The Relational Model 2. Week 3

Fundamentals, Design, and Implementation, 9/e Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M.

Database Management Systems. Chapter 3 Part 1

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

SQL Server. Lecture3 Cascading referential integrity constraint

SQL DATA DEFINITION LANGUAGE

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Database Systems ( 資料庫系統 )

The Relational Model. Chapter 3

Basic operators: selection, projection, cross product, union, difference,

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

The Relational Model

Database Applications (15-415)

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

EECS 647: Introduction to Database Systems

CSC 261/461 Database Systems Lecture 6. Fall 2017

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

From E/R Diagrams to Relations

Announcements (September 21) SQL: Part III. Triggers. Active data. Trigger options. Trigger example

Chapter 7: Constraints and Triggers. Foreign Keys Local and Global Constraints Triggers

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

CSC 742 Database Management Systems

CMPT 354: Database System I. Lecture 3. SQL Basics

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

CS 2451 Database Systems: Relational Data Model

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Introduction to Data Management CSE 344

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

SQL: Data Definition Language

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

EECS 647: Introduction to Database Systems

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

CMPT 354 Views and Indexes. Spring 2012 Instructor: Hassan Khosravi

Introduction to Data Management CSE 344

Database Systems. Course Administration

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

MASTER-DETAIL FORMS. In this Chapter, you will learn about: Master-Detail Forms Page 108

Integrity and Security

Keys, SQL, and Views CMPSCI 645

CSEN 501 CSEN501 - Databases I

The Relational Model Constraints and SQL DDL

CSE 344 MAY 14 TH ENTITIES

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

DATABASE DESIGN - 1DL400

Introduction to Information Systems

Introduction to Data Management CSE 344

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

NULL. The special value NULL could mean: Unknown Unavailable Not Applicable

ER to Relational Model. Professor Jessica Lin

COSC344 Database Theory and Applications. Lecture 11 Triggers

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Chapter 1 SQL and Data

L3 The Relational Model. Eugene Wu Fall 2018

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

CSE 344 AUGUST 1 ST ENTITIES

Chapter 8 INTEGRITY 1

Transcription:

Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi

SQL: Constraints and Triggers Certain properties we d like our database to hold Modification of the database may break these properties Data entry may have errors Build handlers into the database definition 7.2

SQL: Constraints and Triggers (integrity) constraints (static) Constrain allowable database states Triggers (Dynamic) Monitor database changes Check conditions and initiate conditions 7.3

(integrity) Constraints Impose restrictions on allowable data beyond those imposed by structure and types Examples on university database 0 < gpa < 4.0 Enrollment < 50,000 Decision attribute: y or n Major = CS decision = null sizehs < 200 not addmitted enr > 5000 Why use integrity constraints Data-entry error (insert) gpa in range Correctness criteria (update) Enforce consistency Referenced tuples Tell system about your data 7.4

Classification of Integrity Constraints Non- null Keys Uniqueness Referential integrities (foreign key) Attribute-based Constraining values in attributes Tuples-based How values in different tuples should correlate General assertions 7.5

Declaration and enforcing Constraints Declaration With original schema when tables are declared Once you have a running database Enforcement Check after every dangerous modification Changing major we don t need to check the gpa constraint Deferred constraint checking We may do some modifications that would raise errors But after we have done all the modifications it should be ok Check once some modifications are done (transaction) 7.6

Triggers Event-Condition-Action rules When event occurs, check condition, if true, then do action Example Enrolllment > 75000 reject all applications If application with gpa > 3.95 accept automatically Update sizehs to be > 7000 change to wrong and raise error Why use triggers? Move codes from application to DBMS Enforce constraints Some of the assertions and checks are not implemented in some DBMS Triggers could not only detect the problem, they can also solve it 7.7

Non-null constraints Defining that a specific attribute in a specific table can not take the value of null Create table A(A 1 int, A 2 int not null, A 3 text) Examples 7.8

Key Constraints The primary key of the tables has to be unique Create table A (A 1 int primary key, A 2 int, A 3 text) Create table A (A 1 int, A 2 int, A 3 text) primary key A 1, A 2 )) You can also define other combination of attributes to be unique ( without declaring them as key) Create table A (A 1 int primary key, A 2 int, A 3 text unique) Create table A (A 1 int primary key, A 2 int, A 3 text, unique(a 1, A 2 )) Examples 7.9

Attribute base check constraints Constraints on the attributes to have specific ranges or types Create table A (A 1 int, A 2 int, A 3 text, check (A 2 >0)) Examples 7.10

Assertions Assertions are very strong they are checks done over all tables at the same time. (assertions are generally not implemented in current dbms) Create assertion name check () Example 7.11

Referential integrity Referential integrity is integrity of references. No dangling pointers What would referential integrity from S.sid to A.sid mean? 7.12

Example 7.13

Example 7.14

Referential integrity 7.15

Referential integrity 7.16

7.17

7.18

Restrict 7.19

Set Null 7.20

Cascade 7.21

Update 7.22

Referential Integrity Examples 7.23

Triggers Event-Condition-Action rules When event occurs, check condition, if true, then do action Example Enrolllment > 75000 reject all applications If application with gpa > 3.95 accept automatically Update sizehs to be > 7000 change to wrong and raise error 7.24

Triggers 7.25

Triggers Using Each Row 7.26

Triggers using table statement 7.27

Tricky issues with Triggers Row-Level vs. Statement-level Which one do you use? Multiple triggers activated at the same time For example a delete command triggers multiple triggers Which one do you run first? Chaining trigger action Self triggering Cycles Trigger A Trigger B Trigger C Trigger A Nested triggers Implementations are significantly different in different systems Tricky example 7.28

Trigger Examples For each student with GPA between 3.3 and 3.6, make them apply to geology in Stanford and biology in MIT 7.29

Trigger Examples Implement referential integrity using triggers. Delete all applications of a student from apply table after the student is deleted in the student table 7.30

Trigger Examples Implement referential integrity using triggers on cascade. Update the name of all colleges in the apply table if the name of the college is updated in the college table 7.31

Trigger Examples Implement primary key using triggers. Before inserting into table college check if the college exists. Raise an error if you get a duplicate Example 7.32

Trigger Examples Once a college has received 10 applications, add -Done to the end of the name of the college Example of chains in triggers 7.33

Trigger Examples If the size of the sizehk <100 or sizehk > 5000, then assume that its an error and ignore Example considering before and after insert in chaining 7.34

Trigger Examples Automatically accept students applying to Berkeley that have GPA > 3.7 and are coming from a highschool > 1200 7.35

Trigger Examples Once a college passes a thresh-hold of 16000 applicants, delete new EE applicant and set all the decisions for all majors to undecided 7.36

More Trigger Examples (self triggers) By default this may get triggered only once. Recursive_trigger = on 7.37

More Trigger Examples (cycles) Example 7.38

More Trigger Examples (Conflicts) What happens when you have multiple triggers being triggered at the same time 7.39

More Trigger Examples (Nested Triggers) What order are triggers carried out in you have nested triggers behaves like imperative programming 7.40

END OF CHAPTER 7 7.41