Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.

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

1 Constraints, Triggers and Active Databases Chapter 9.
Database Administration, Integrity and Performance.
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Chapter 3 An Introduction to Relational Databases.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products.
CSC271 Database Systems Lecture # 6. Summary: Previous Lecture  Relational model terminology  Mathematical relations  Database relations  Properties.
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.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Relational Model Session 6 Course Name: Database System Year : 2012.
Chapter 4 The Relational Model.
Chapter 3 An Introduction to Relational Databases.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Data Integrity An empty database is a correct database.
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.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
1 Functional Dependencies and Normalization Chapter 15.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Chapter 3 Part II Describing Syntax and Semantics.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
Chapter 10 Views. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.10-2 Topics in this Chapter What are Views For? View Retrievals View Updates.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Chapter 4 The Relational Model Pearson Education © 2009.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Chapter 3 An Introduction to Relational Databases.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 6: Integrity (and Security)
Chapter 10 Views.
Constraints and Triggers
STRUCTURE OF PRESENTATION :
STRUCTURE OF PRESENTATION :
STRUCTURE OF PRESENTATION :
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
SQL – Constraints & Triggers
Presentation transcript:

Chapter 9 Integrity

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs. External Predicates Correctness vs. Consistency A Constraint Classification Scheme Keys Triggers SQL Facilities

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-3 Integrity Originally focused on keys, the theory of integrity has evolved to focus on constraints in general An integrity constraint is a boolean expression associated with a database that is required to evaluate at all times to true An integrity constraint can regarded as a formal expression of a business rule Constraints may be a priori or a posteriori

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-4 A priori vs a posteriori constraints Type implies an a priori constraint Since every attribute of every relvar is of some type, the collection of types is a priori for the relvar Business rule constraints – of the sort that are represented by uniqueness and value constraints, are a posteriori, that is, a result of decisions after the fact

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-5 Constraint Example CONSTRAINT SC1 FORALL SX ( SX.STATUS > 1 AND SX.STATUS < 100 ) ; Represents a business decision about acceptable values, as opposed to the type of STATUS (be it INTEGER or STATUS), which represents the set of possible values

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-6 Key Constraints The general formulation for a relvar constraint is as follows: If a certain tuple appears in a certain relvar then that tuple satisfies a certain condition This applies equally to value constraints, uniqueness constraints and key constraints More generally, if certain tuples appear in certain relvars then those tuples satisfy a certain condition

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-7 Key Constraint Formal Definition – Beginning FORALL X#  S#, xn  NAME, xt  INTEGER, xc  CHAR, Y#  S#, yn  NAME, yt  INTEGER, yc  CHAR (IF { S#, x#, SNAME xn, STATUS xt, CITY xc}  S AND { S#, y#, SNAME yn, STATUS yt, CITY yc}  S

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-8 Key Constraint Formal Definition – Conclusion THEN (IF x# = y# THEN xn = yn, AND xt = yt, AND xc = yc ) ) This expresses that S# is a superkey, and possibly a candidate key

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-9 Predicates and Propositions An expression is a predicate Its variables are parameters to the predicate When we instantiate the variables, we are passing arguments to the predicate, and so turning it into: A proposition, which is either true or false A constraint is an expression, and therefore a predicate, which is checked by passing it arguments, and testing the proposition

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-10 The Golden Rule A relvar predicate is the conjunction (logical AND) of all constraints associated with any of its components Golden Rule: No update operation must ever assign to any relvar a value that causes its relvar predicate to evaluate to false A database predicate is the conjunction of all predicates of its relvars A database predicate is also golden

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-11 Checking the Constraints Constraints should be checked before attempting any insert or update or delete This is equally true from an implementation perspective and from the model To do otherwise is inefficient, and violates the Golden Rule

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-12 Internal vs. External Predicates – The Closed World Assumption Internal predicates are those understood and enforced by the system External predicates are those understood and implemented by the user Internal predicates should reflect external predicates If an otherwise valid tuple does not appear in a relvar, its corresponding proposition is false: The Closed World Assumption

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-13 Correctness vs. Consistency The Closed World Assumption is logically valid, but is unenforceable by the system External predicates are not understood by the system; therefore the system can enforce consistency, but not truth The external predicate for a relvar is its intended interpretation Thus a database may be populated by valid but false propositions

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-14 A Constraint Classification Scheme Constraints can apply to a database, a relvar, an attribute, or a type Type constraints check format and values immediately Attribute constraints are inherited from those of the declared type Relvar and database constraints inherit constraints from attributes and add business rule constraints in addition

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-15 Transition Constraints Transition constraints can apply to a database or a relvar, but not an attribute, or a type Transition constraints constrain certain actions, for example forbidding an update to change a status from “married” to “never married”

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-16 Keys – Topics Candidate Keys Superkeys Primary Keys Alternate Keys Foreign Keys Referential Integrity Referential Actions

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-17 Keys – Candidate Keys Let K be a set of attributes of relvar R. Then K is a candidate key for R if and only if it has both of the following properties: Uniqueness: No legal value of R ever contains two distinct tuples with the same value for k Irreducibility: No proper subset of K has the uniqueness property

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-18 Keys – Superkeys If SK is a superkey for relvar R and A is an attribute of R then SK implies A A superkey has the uniqueness property: no two tuples will have the same value But it does not have the irreducibility property: it can contain a subset that has the uniqueness property A superkey can contain subset superkeys; a candidate key cannot

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-19 Keys – Primary Keys and Alternate Keys If a relvar has two or more candidate keys, one must be chosen to be the primary key The others are then designated alternate keys This choice is logically arbitrary Logically, candidate keys are of paramount importance; choosing the primary key is ancillary

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-20 Keys – Foreign Keys Loosely, a foreign key is a set of attributes of some relvar R2 whose values are required to match values of some candidate key of some relvar R1 R1 and R2 may be the same relvar, in the case of a recursive constraint, for example employee and manager Since relvars can be both referenced and referencing, the database contains referential paths connecting relvars in chains

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-21 Keys – Foreign Keys – Referential Integrity The database must not contain any unmatched foreign key values Originally foreign keys were defined in terms of the primary key in the referenced relvar, but this qualification is superfluous, although perhaps desirable in practice The relationship in one directional; it is not a requirement that every referenced candidate key value appear in the foreign keys of the referrer

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-22 Keys – Foreign Keys – Referential Actions DELETE may not violate the referential integrity constraint RESTRICT limits the action of the DELETE to just those tuples that do not have referring tuples in another relvar CASCADE broadcasts the DELETE to include any tuples that reference the affected tuples UPDATE requires similar behavior

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-23 Triggers Triggers were used to implement constraints in the days when the database software didn’t They are used more now for auditing, or to carry out corresponding actions beyond those handled via referential integrity They should be avoided if possible, because they quickly become hard to manage: triggers can become chained, and ultimately may become recursive unintentionally

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-24 SQL Facilities SQL does not support type constraints, nor attribute constraints, nor relvar constraints, nor database constraints SQL supports base table constraints, which are a superset of a subset of relvar and database constraints SQL supports, for base tables: PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, ASSERTION

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-25 Deferred Checking SQL constraint verification can be DEFERRABLE or NOT DEFERRABLE NOT DEFERRABLE means the check will be immediate DEFERRABLE offers the option of SET IMMEDIATE

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-26 SQL Trigger Syntax CREATE TRIGGER ON [REFERENCING ] [FOR EACH ] [WHEN ] ;