Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.

Slides:



Advertisements
Similar presentations
Relational data integrity
Advertisements

MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
1 Constraints, Triggers and Active Databases Chapter 9.
Database Administration, Integrity and Performance.
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.
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. 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,
Domains, Relations & Base RelVars (Ch
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
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.
Chapter 4 The Relational Model.
Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
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.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
NormalizationNormalization Chapter 4. Purpose of Normalization Normalization  A technique for producing a set of relations with desirable properties,
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
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.
An Introduction to Relational Databases Prof. Yin-Fu Huang CSIE, NYUST Chapter 3.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
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.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
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.
Relations Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Types Prof. Yin-Fu Huang CSIE, NYUST Chapter 5. Advanced Database SystemYin-Fu Huang Relation, tuple, cardinality, attribute, degree, domain, primary.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Advanced Database System
Chapter 4 The Relational Model Pearson Education © 2009.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
Chapter 8 Relational Calculus.
Chapter 6: Integrity (and Security)
Constraints and Triggers
Active Database Concepts
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.
Unit 7 Normalization (表格正規化).
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Presentation transcript:

Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9

Advanced Database SystemYin-Fu Huang An integrity constraint is a boolean expression that is associated with some database and is required to evaluate at all times to TRUE. e.g. Constraint SC1 Is_Empty (S Where Status 100); Drop Constraint SC1; 9.1Introduction

Advanced Database SystemYin-Fu Huang The fact that a given variable is of some given type represents an a prior constraint on the variable in question. Tutorial D examples: 1. Constraint SC2 Forall SX (If SX.City = ‘London’ Then SX.Status = 20 End If); 2. Constraint PC3 If Exists PX (True) Then Exists PX (PX.Color = Color(‘Blue’)) End If; 9.2A Closer Look

Advanced Database SystemYin-Fu Huang 3. Constraint SC4 Forall SX Forall SY (If SX.S# = SY.S# Then SX.Sname = SY.Sname And SX.Status = SY.Status And SX.City = SY.City End If); 4. Constraint SSP5 Forall SPX Exists SX (Sx.S# = SPX.S#) 5. Constraint SSP6 Forall SX Forall SPX (If Sx.S# = SPX.S# Then SX.Status  20 Or SPX.Qty  500 End If); 9.2A Closer Look (Cont.)

Advanced Database SystemYin-Fu Huang 9.3Predicates and Propositions A constraint as formally stated is a predicate. When that constraint is checked, arguments are substituted for the parameters and the predicate is thereby reduced to a proposition – and that proposition is then required to evaluated to TRUE.

Advanced Database SystemYin-Fu Huang 9.4Relvar Predicates and Database Predicates The relvar predicate for R is the logical And or conjunction of all of the constraints that apply to relvar R. The Golden Rule No update operation must ever assign to any relvar a value that causes its relvar predicate to evaluate to FALSE. The database predicate for D is the conjunction of all of those relvar predicates. The Golden Rule No update operation must ever assign to any database a value that causes its database predicate to evaluate to FALSE.

Advanced Database SystemYin-Fu Huang 9.5Checking the Constraints The sequence of events: 1. Insert the new tuple. 2. Check the constraint. 3. Undo the update (because the check fails). But this is absurd! All constraint checking is immediate? Most of the literature argues that “the unit of integrity” is the transaction and that at least some checking has to be deferred until end-of-transaction (i.e., COMMIT time).

Advanced Database SystemYin-Fu Huang 9.6 Internal vs. External Predicates a formal construct vs. an informal construct. what the data means to the system vs. what the data means to the users. A given internal predicate is the system’s approximation to the corresponding external predicate. If (s  S) = TRUE Then XPS(s) = TRUE If (s  S) = FALSE Then XPS(s) = FALSE  s  S  XPS(s)

Advanced Database SystemYin-Fu Huang 9.7Correctness vs. Consistency Views too are subject to constraints, and they have relvar predicates, both internal and external. e.g., There exists some city City such that supplier S# is under contract, is named Sname, has status Status, and is located in City. 9.8 Integrity and Views The system cannot enforce truth, only consistency.  If the database contains only true propositions, then it is consistency, but the converse is not necessarily so.  If the database is inconsistency, then it contains at least one false proposition, but the converse is not necessarily so.

Advanced Database SystemYin-Fu Huang 9.9A Constraint Classification Scheme Four broad categories: database, relvar, attribute, and type constraints Type constraints e.g., Type Weight Possrep { D Decimal (5, 1) Constraint D > 0.0 And D < };  Type constraints are always checked immediately. Attribute constraints e.g., Var S Base Relation { S#S#, SnameName, StatusInteger, CityChar } … ;

Advanced Database SystemYin-Fu Huang 9.9A Constraint Classification Scheme (Cont.) Relvar and database constraints  A transition constraint is a constraint on the legal transitions that a given variable-in particular, a given relvar or a given database- can make from one value to another. e.g. Constraint TRC1 Forall SX’ Forall SX ( SX’.S#  SX.S# Or SX’.Status  SX.Status ); Constraint TRC2 Forall PX Sum ( SPX’ Where SPX’.P# = PX.P#, Qty )  Sum ( SPX Where SPX.P# = PX.P#, Qty );  The concept of transition constraints does not apply to type or attribute constraints.

Advanced Database SystemYin-Fu Huang Keys 9.10Keys Candidate Keys  Properties: 1. Uniqueness 2. Irreducibility  Key { }  Tuple-level addressing mechanism Primary Keys and Alternative Keys  Exactly one of candidate keys the primary key  The others alternative keys

Advanced Database SystemYin-Fu Huang Keys (Cont.) 9.10Keys (Cont.) Foreign Keys  A foreign key in R2 is a set of attributes of R2, say FK, such that: 1.There exists a relvar R1 (R1 and R2 not necessarily distinct) with a candidate key CK. 2.It is possible to rename some subset of the attributes of FK, such that FK becomes FK’ (say) and FK’ and CK are of the same (tuple) type. 3.For all time, each value of FK in the current value of R2 yields a value for FK’ that is identical to the value of CK in some tuple in the current value of R1.

Advanced Database SystemYin-Fu Huang Keys (Cont.) 9.10Keys (Cont.)  Points arising: 1.It will rarely be necessary in practice to perform any actual renaming. 2.While every value of FK must appear as a value of CK, the converse is not a requirement. 3.Simple or composite 4.Referencing relvar vs. referenced relvar 5.Referential diagrams S# P# S ← SP → P 6.A given relvar can be both referenced and referencing. R3 → R2 → R1

Advanced Database SystemYin-Fu Huang Keys (Cont.) 9.10Keys (Cont.) 7.Relvars R1 and R2 in the foreign key definition are not necessarily distinct. Var Emp Base Relation {Emp# Emp#,..., Mgr_emp# Emp#,...} Key {Emp#} Foreign Key {Rename Mgr_emp# As Emp#} References Emp; 8.Referential cycles 9.Such matches represent certain relationships. 10.Referential integrity The database must not contain any unmatched foreign key values.

Advanced Database SystemYin-Fu Huang Keys (Cont.) 9.10Keys (Cont.) Referential Actions  Foreign Key {... } References On Delete On Update ;  Cascade Restrict No Action

Advanced Database SystemYin-Fu Huang Triggers (A Digression) 9.11Triggers (A Digression) A triggered procedure is a precompiled procedure that is stored along with the database and invoked automatically whenever some specified event occurs. e.g., Create Trigger London_Supplier_Insert Instead Of Insert On London_Supplier Referencing New Row As R For Each Row Insert Into S ( S#, Sname, Status, City ) Values ( R.S#, R.Sname, R.Status, ‘London’) ;  The event  The condition  The action

Advanced Database SystemYin-Fu Huang 9.12SQL Facilities SQL does not support type constraints at all. SQL does support attribute constraints. SQL does not support relvar constraints as such.  base table constraints SQL does not support database constraints as such.  general constraints (“assertions”)

Advanced Database SystemYin-Fu Huang 9.12SQL Facilities (Cont.) Base Table Constraints 1. Candidate keys Primary Key ( ) at most one Unique ( ) any number 2. Foreign keys Foreign key ( ) References [( )] [On Delete ] [On Update ] Action: No Action, Restrict, Cascade, Set Default, Set Null

Advanced Database SystemYin-Fu Huang 3. Check constraints Check ( ) e.g., Create Table Sp (S# S# Not Null, P# P# Not Null, Qty Qty Not Null, Primary Key (S#, P#), Foreign Key(S#) References S On Delete Cascade On Update Cascade, Foreign Key(P#) References P On Delete Cascade On Update Cascade, Check (Qty  Qty(0) And Qty  Qty(5000))); 9.12SQL Facilities (Cont.)

Advanced Database SystemYin-Fu Huang 9.12SQL Facilities (Cont.) Assertions Create Assertion Check ( ); Drop Assertion ; e.g., Create Assertion SC1 Check (Not Exists (Select* From S WhereS.Status < 0 OrS.Status > 100)); Deferred Checking  In SQL, constraints can be defined to be Deferrable or Not Deferrable.

Advanced Database SystemYin-Fu Huang 9.12SQL Facilities (Cont.)  Deferrable constraints can be dynamically switched on and off by means of the statement Set Constraints ; option: Immediate, Deferred Triggers Create Trigger On [ Referencing ] [ For Each ] [ When ( ) ] ; Drop Trigger ;

Advanced Database SystemYin-Fu Huang The End.