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

Chapter 7 Constraints and Triggers

  • Upload
    patch

  • View
    75

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 7  Constraints and Triggers

Chapter 7 Constraints and Triggers

Spring 2011Instructor: Hassan Khosravi

Page 2: Chapter 7  Constraints and Triggers

7.2

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

Page 3: Chapter 7  Constraints and Triggers

7.3

SQL: Constraints and Triggers

(integrity) constraints (static) Constrain allowable database states

Triggers (Dynamic) Monitor database changes Check conditions and initiate conditions

Page 4: Chapter 7  Constraints and Triggers

7.4

(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

Page 5: Chapter 7  Constraints and Triggers

7.5

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

Page 6: Chapter 7  Constraints and Triggers

7.6

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)

Page 7: Chapter 7  Constraints and Triggers

7.7

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

Page 8: Chapter 7  Constraints and Triggers

7.8

Non-null constraints Defining that a specific attribute in a specific table can not take the

value of null Create table A(A1 int, A2 int not null, A3 text) Examples

Page 9: Chapter 7  Constraints and Triggers

7.9

Key Constraints

The primary key of the tables has to be unique Create table A (A1 int primary key, A2 int, A3 text) Create table A (A1 int, A2 int, A3 text) primary key A1, A2))

You can also define other combination of attributes to be unique ( without declaring them as key) Create table A (A1 int primary key, A2 int, A3 text unique) Create table A (A1 int primary key, A2 int, A3 text , unique(A1, A2))

Examples

Page 10: Chapter 7  Constraints and Triggers

7.10

Attribute base check constraints

Constraints on the attributes to have specific ranges or types Create table A (A1 int, A2 int, A3 text, check (A2 >0))

Examples

Page 11: Chapter 7  Constraints and Triggers

7.11

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

Page 12: Chapter 7  Constraints and Triggers

7.12

Referential integrity

Referential integrity is integrity of references. No dangling pointers

What would referential integrity from S.sid to A.sid mean?

Page 13: Chapter 7  Constraints and Triggers

7.13

Example

Page 14: Chapter 7  Constraints and Triggers

7.14

Example

Page 15: Chapter 7  Constraints and Triggers

7.15

Referential integrity

Page 16: Chapter 7  Constraints and Triggers

7.16

Referential integrity

Page 17: Chapter 7  Constraints and Triggers

7.17

Page 18: Chapter 7  Constraints and Triggers

7.18

Page 19: Chapter 7  Constraints and Triggers

7.19

Restrict

Page 20: Chapter 7  Constraints and Triggers

7.20

Set Null

Page 21: Chapter 7  Constraints and Triggers

7.21

Cascade

Page 22: Chapter 7  Constraints and Triggers

7.22

Update

Page 24: Chapter 7  Constraints and Triggers

7.24

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

Page 25: Chapter 7  Constraints and Triggers

7.25

Triggers

Page 26: Chapter 7  Constraints and Triggers

7.26

Triggers Using Each Row

Page 27: Chapter 7  Constraints and Triggers

7.27

Triggers using table statement

Page 28: Chapter 7  Constraints and Triggers

7.28

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

Page 29: Chapter 7  Constraints and Triggers

7.29

Trigger Examples

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

Page 30: Chapter 7  Constraints and Triggers

7.30

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

Page 31: Chapter 7  Constraints and Triggers

7.31

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

Page 32: Chapter 7  Constraints and Triggers

7.32

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

Page 33: Chapter 7  Constraints and Triggers

7.33

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

Page 34: Chapter 7  Constraints and Triggers

7.34

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

Page 35: Chapter 7  Constraints and Triggers

7.35

Trigger Examples

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

Page 36: Chapter 7  Constraints and Triggers

7.36

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

Page 37: Chapter 7  Constraints and Triggers

7.37

More Trigger Examples (self triggers)

By default this may get triggered only once. Recursive_trigger = on

Page 38: Chapter 7  Constraints and Triggers

7.38

More Trigger Examples (cycles)

Example

Page 39: Chapter 7  Constraints and Triggers

7.39

More Trigger Examples (Conflicts)

What happens when you have multiple triggers being triggered at the same time

Page 40: Chapter 7  Constraints and Triggers

7.40

More Trigger Examples (Nested Triggers) What order are triggers carried out in you have nested triggers

behaves like imperative programming 

Page 41: Chapter 7  Constraints and Triggers

7.41

END OF CHAPTER 7