Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.

Slides:



Advertisements
Similar presentations
Chapter 6: Integrity and Security  Domain Constraints  Referential Integrity  Assertions  Triggers  Security  Authorization  Authorization in SQL.
Advertisements

1 Constraints, Triggers and Active Databases Chapter 9.
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.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity (and Security) Domain Constraints Referential Integrity Assertions Triggers.
SQL Constraints and Triggers
Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
Dr. Kalpakis CMSC 461, Database Management Systems URL: Integrity and Security.
1.1R. Meersman --Adapted from: Database System Concepts --Silberschatz, Korth, Sudarshan Chapter 6: Integrity and Security Domain Constraints Referential.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
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.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security (完整性与安全性) Domain Constraints (域约束) Referential Integrity.
CMSC424: Database Design Instructor: Amol Deshpande
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
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.
Chapter 6: Integrity and Security Domain Constraints Domain Constraints Referential Integrity Referential Integrity Assertions Assertions Triggers Triggers.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Department of Computer Science and Engineering, HKUST Slide 1 Comp 231 Database Management Systems Comp 231 Database Management Systems 6. Integrity Constraints.
Chapter 4 Intermediate SQL: Integrity Constraints.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Chapter 4: Advanced SQL. 4.2Unite International CollegeDatabase Management Systems Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
-- Introduction to database principles Maoying Wu March 25, 2013 Chapter 4: Advanced SQL (1)
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
Chapter 4: Advanced SQL. 4.2Unite International CollegeDatabase Management Systems Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
International Computer Institute, Izmir, Turkey Integrity and Security Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Computing & Information Sciences Kansas State University Monday, 04 Feb 2008CIS 560: Database System Concepts Lecture 6 of 42 Monday, 04 February 2008.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Chapter 4 Advanced SQL: Integrity Constraints. 2 n Ensure Data Consistency n Domain Constraints  enforce valid attribute values from domain sets  domain.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Integrity and Security Domain Constraints Referential Integrity Assertions Triggers Security Authorization Authorization in SQL.
Database System Concepts, 5th Ed. Chapter 4: Advanced SQL.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Computing & Information Sciences Kansas State University Wednesday, 05 Sep 2007CIS 560: Database System Concepts Lecture 6 of 42 Wednesday, 05 September.
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
Computing & Information Sciences Kansas State University Monday, 17 Sep 2007CIS 560: Database System Concepts Lecture 12 of 42 Monday, 18 February 2008.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Advanced SQL Lecture 4. 2 Advanced SQL Advanced SQL SQL Data Types and Schemas Integrity Constraints Authorization Embedded SQL Dynamic SQL Functions.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
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.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Advanced SQL.
International Computer Institute, Izmir, Turkey Integrity and Security Asst.Prof.Dr.İlker Kocabaş UBİ502 at
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity Constraints Domain Constraints Referential Integrity Assertions Triggers.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Chapter 6: Integrity (and Security)
Integrity Constraints
SQL: Constraints and Triggers
Presentation transcript:

Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers

Integrity Constraints Objective –Maintain DB consistency –Ensure that authorized changes to the database do not result in a loss of data consistency

Domain Constraints Domain constraints –The most elementary form of integrity constraint –Test values & queries Domain Constraints –New domains can be created from existing data types –E.g. create domain Dollars numeric(12, 2) create domain Pounds numeric(12,2) Type conversion

Domain Constraints (Cont.) The check clause in SQL-92 –create domain hourly-wage numeric(5,2) constraint value-test check(value > = 4.00) –constraint: the hourly-wage >= 4.00 –constraint value-test is optional –A domain check can have complex conditions create domain AccountType char(10) constraint account-type-test check (value in (‘Checking’, ‘Saving’)) check (branch-name in (select branch-name from branch))

Referential Integrity Foreign key constraint –Assume a branch name “Perryridge” appears in the account relation –“Perryridge” should also appear in branch relation Formal definition –Foreign key The subset of R2’s attributes The primary key of R1

Referential Integrity and the E-R Model Consider relationship set R between entity sets E 1 and E 2 –The schema of R should include the primary keys K 1 of E 1 and K 2 of E 2 –K 1 and K 2 form foreign keys on the relational schemas for E 1 and E 2 respectively R E1E2

Referential Integrity and the E-R Model (Cont.) Weak entity sets –The schema of a weak entity set must include the primary key of the entity set on which it depends –Descriminator

Checking Referential Integrity on DB Modification Rule of Thumb –Maintain the subset dependency   (r 2 )   K (r 1 ) Insert –When inserting a tuple t 2 into r 2, ensure that there is a tuple t 1 in r 1 such that t 1 [K] = t 2 [  ]: t 2 [  ]   K (r 1 )

DB Modification (Cont.) Delete –If a tuple t 1 is deleted from r 1, the system must compute the set of tuples in r 2 that reference t 1 :   = t1[K] (r 2 ) If this set is not empty –reject the delete command as an error, or –delete the tuples that reference t 1 (cascading deletions)

Database Modification (Cont.) Update –Case 1: If the value of the foreign key  is modified, ensure that t 2 ’[  ]   K (r 1 )

Database Modification (Cont.) Update –Case 2: If the update modifies the value of a primary key, compute   = t1[K] (r 2 ) using the old value of t 1 [K] If this set is not empty –the update may be rejected as an error, –the update may be cascaded to the tuples in the set, or –the tuples in the set may be deleted.

Referential Integrity in SQL Specify keys in the create table statement: –primary key clause –unique key clause –foreign key clause foreign key (account-number) references account(account-number) foreign key (account-number) references account

Referential Integrity in SQL create table customer (customer-namechar(20), customer-streetchar(30), customer-citychar(30), primary key (customer-name)) create table branch (branch-namechar(15), branch-citychar(30), assetsinteger, primary key (branch-name))

Referential Integrity in SQL create table account (account-numberchar(10), branch-namechar(15), balanceinteger, primary key (account-number), foreign key (branch-name) references branch) create table depositor (customer-namechar(20), account-numberchar(10), primary key (customer-name, account-number), foreign key (account-number) references account, foreign key (customer-name) references customer)

Cascading Actions in SQL create table account... foreign key(branch-name) references branch on delete cascade on update cascade... ) If a delete of a tuple in branch results in referential-integrity constraint violation, the delete “cascades” to the account relation A chain of foreign dependency

Cascading Actions in SQL (Cont.) Referential integrity –Check at the end of a transaction –Intermediate steps may violate referential integrity if later steps remove the violation –E.g. insert two tuples whose foreign keys point to each other spouse attribute of relation marriedperson(name, address, spouse) Any other example? Is this a good idea?

Alternatives to Cascading on delete set null, e.g., null branch-name on delete set default Null values in foreign key attributes complicate SQL referential integrity semantics –Use not null to prevent

Assertions A predicate expressing a condition that DB always need to satisfy create assertion check System tests the validity –May introduce a significant overhead

Assertion Example Sum(all loan amounts for each branch) < Sum(all account balances at the branch) create assertion sum-constraint check (not exists (select * from branch where (select sum(amount) from loan where loan.branch-name = branch.branch-name) >= (select sum(amount) from account where loan.branch-name = branch.branch-name)))

Triggers An automatically executed statementment as a side effect of a modification to the database –Specify the conditions under which the trigger is to be executed –Specify the actions Example –Conidtion: an overdraft –Possible actions: Set the account balance to zero Create a loan in the amount of the overdraft

End of Chapter