1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 8.

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Advertisements

©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity (and Security) Domain Constraints Referential Integrity Assertions Triggers.
Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
©Silberschatz, Korth and Sudarshan6.1Database System ConceptsTriggers A trigger is a statement that is executed automatically by the system as a side effect.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
CMSC424: Database Design Instructor: Amol Deshpande
Integrity Constraints
CMSC424: Database Design Instructor: Amol Deshpande
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
©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.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
CMSC424: Database Design Instructor: Amol Deshpande
CMSC424: Database Design Instructor: Amol Deshpande
Department of Computer Science and Engineering, HKUST Slide 1 7. Relational Database Design.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.
©Silberschatz, Korth and Sudarshan7.1Database System Concepts Chapter 7: Relational Database Design First Normal Form Pitfalls in Relational Database Design.
CS 370 Database Systems Lecture 13 Introduction to SQL.
Review Three things managed by a DBMS 1.Data organization  E/R Model  Relational Model 2.Data Retrieval  Relational Algebra  SQL 3.Data Integrity.
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.
Chapter 4: Advanced SQL. 4.2Unite International CollegeDatabase Management Systems Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints.
Relational Database Design by Relational Database Design by Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING.
Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.
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.
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,...)
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
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 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
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.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Assertions and Triggers in SQL
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. ©Sang Ho Lee Chapter 8: Application Design and Development.
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.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Integrity Constraints
Chapter 6: Integrity (and Security)
Constraints and Triggers
Relational Database Design by Dr. S. Sridhar, Ph. D
CPSC-608 Database Systems
Integrity Constraints
CPSC-310 Database Systems
אילוצי שלמות אילוצי תחום מפתחות ומפתחות זרים טריגרים triggers
The Relational Model Relational Data Model
Triggers A trigger is a statement that is executed automatically by the system as a side effect of a modification to the database. To design a trigger.
SQL – Constraints & Triggers
CPSC-608 Database Systems
Presentation transcript:

1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 8

2 CMSC424, Spring 2005 Homework Discussion TA Office Hours Today from 4p to 5p Project info on the class web page

3 CMSC424, Spring 2005 Next: Integrity constraints ?? Prevent semantic inconsistencies

4 CMSC424, Spring 2005 IC’s Predicates on the database Must always be true (:, checked whenever db gets updated) There are the following 4 types of IC’s: Key constraints (1 table) e.g., 2 accts can’t share the same acct_no Attribute constraints (1 table) e.g., accts must have nonnegative balance Referential Integrity constraints ( 2 tables) E.g. bnames associated w/ loans must be names of real branches Global Constraints (n tables) E.g., all loans must be carried by at least 1 customer with a svngs acct

5 CMSC424, Spring 2005 Key Constraints Idea: specifies that a relation is a set, not a bag SQL examples: 1. Primary Key: CREATE TABLE branch( bname CHAR(15) PRIMARY KEY, bcity CHAR(20), assets INT); or CREATE TABLE depositor( cname CHAR(15), acct_no CHAR(5), PRIMARY KEY(cname, acct_no)); 2. Candidate Keys: CREATE TABLE customer ( ssn CHAR(9) PRIMARY KEY, cname CHAR(15), address CHAR(30), city CHAR(10), UNIQUE (cname, address, city));

6 CMSC424, Spring 2005 Key Constraints Effect of SQL Key declarations PRIMARY (A1, A2,.., An) or UNIQUE (A1, A2,..., An) Insertions: check if any tuple has same values for A1, A2,.., An as any inserted tuple. If found, reject insertion Updates to any of A1, A2,..., An: treat as insertion of entire tuple Primary vs Unique (candidate) 1.1 primary key per table, several unique keys allowed. 2.Only primary key can be referenced by “foreign key” (ref integrity) 3.DBMS may treat primary key differently (e.g.: create an index on PK) How would you implement something like this ?

7 CMSC424, Spring 2005 Attribute Constraints Idea: Attach constraints to values of attributes Enhances types system (e.g.: >= 0 rather than integer) In SQL: 1. NOT NULL e.g.: CREATE TABLE branch( bname CHAR(15) NOT NULL,.... ) Note: declaring bname as primary key also prevents null values 2. CHECK e.g.: CREATE TABLE depositor(.... balance int NOT NULL, CHECK( balance >= 0),.... ) affect insertions, update in affected columns

8 CMSC424, Spring 2005 Attribute Constraints Domains: can associate constraints with DOMAINS rather than attributes e.g: instead of: CREATE TABLE depositor(.... balance INT NOT NULL, CHECK (balance >= 0) ) One can write: CREATE DOMAIN bank-balance INT ( CONSTRAINT not-overdrawn CHECK (value >= 0), CONSTRAINT not-null-value CHECK( value NOT NULL)); CREATE TABLE depositor (..... balance bank-balance, ) Advantages?

9 CMSC424, Spring 2005 Attribute Constraints Advantage of associating constraints with domains: 1. can avoid repeating specification of same constraint for multiple columns 2. can name constraints e.g.: CREATE DOMAIN bank-balance INT ( CONSTRAINT not-overdrawn CHECK (value >= 0), CONSTRAINT not-null-value CHECK( value NOT NULL)); allows one to: 1. add or remove: ALTER DOMAIN bank-balance ADD CONSTRAINT capped CHECK( value <= 10000) 2. report better errors (know which constraint violated)

10 CMSC424, Spring 2005 Referential Integrity Constraints Idea: prevent “dangling tuples” (e.g.: a loan with a bname, Kenmore, when no Kenmore tuple in branch) Referencing Relation (e.g. loan) Referenced Relation (e.g. branch) “foreign key” bname primary key bname Ref Integrity: ensure that: foreign key value  primary key value (note: need not to ensure , i.e., not all branches have to have loans)

11 CMSC424, Spring 2005 Referential Integrity Constraints Referencing Relation (e.g. loan) Referenced Relation (e.g. branch) bname x x x In SQL: CREATE TABLE branch( bname CHAR(15) PRIMARY KEY....) CREATE TABLE loan ( FOREIGN KEY bname REFERENCES branch); Affects: 1) Insertions, updates of referencing relation 2) Deletions, updates of referenced relation

12 CMSC424, Spring 2005 Referential Integrity Constraints c c x x x A B what happens when we try to delete this tuple? titi tjtj Ans: 3 possibilities 1) reject deletion/ update 2) set t i [c], t j [c] = NULL 3) propagate deletion/update DELETE: delete ti, tj UPDATE: set ti[c], tj[c] to updated values

13 CMSC424, Spring 2005 Referential Integrity Constraints c c x x x A B what happens when we try to delete this tuple? titi tjtj CREATE TABLE A (..... FOREIGN KEY c REFERENCES B action ) Action: 1) left blank (deletion/update rejected) 2) ON DELETE SET NULL/ ON UPDATE SET NULL sets ti[c] = NULL, tj[c] = NULL 3) ON DELETE CASCADE deletes ti, tj ON UPDATE CASCADE sets ti[c], tj[c] to new key values

14 CMSC424, Spring 2005 Global Constraints Idea: two kinds 1) single relation (constraints spans multiple columns) E.g.: CHECK (total = svngs + check) declared in the CREATE TABLE 2) multiple relations: CREATE ASSERTION SQL examples: 1) single relation: All Bkln branches must have assets > 5M CREATE TABLE branch ( bcity CHAR(15), assets INT, CHECK (NOT(bcity = ‘Bkln’) OR assets > 5M)) Affects: insertions into branch updates of bcity or assets in branch

15 CMSC424, Spring 2005 Global Constraints SQL example: 2) Multiple relations: every loan has a borrower with a savings account CHECK (NOT EXISTS ( SELECT * FROM loan AS L WHERE NOT EXISTS( SELECT * FROM borrower B, depositor D, account A WHERE B.cname = D.cname AND D.acct_no = A.acct_no AND L.lno = B.lno))) Problem: Where to put this constraint? At depositor? Loan?.... Ans: None of the above: CREATE ASSERTION loan-constraint CHECK(..... ) Checked with EVERY DB update! very expensive.....

16 CMSC424, Spring 2005 Summary: Integrity Constraints Constraint TypeWhere declaredAffects...Expense Key Constraints CREATE TABLE (PRIMARY KEY, UNIQUE) Insertions, UpdatesModerate Attribute Constraints CREATE TABLE CREATE DOMAIN (Not NULL, CHECK) Insertions, UpdatesCheap Referential Integrity Table Tag (FOREIGN KEY.... REFERENCES....) 1.Insertions into referencing rel’n 2. Updates of referencing rel’n of relevant attrs 3. Deletions from referenced rel’n 4. Update of referenced rel’n 1,2: like key constraints. Another reason to index/sort on the primary keys 3,4: depends on a. update/delete policy chosen b. existence of indexes on foreign key Global ConstraintsTable Tag (CHECK) or outside table (CREATE ASSERTION) 1. For single rel’n constraint, with insertion, deletion of relevant attrs 2. For assesrtions w/ every db modification 1. cheap 2. very expensive

17 CMSC424, Spring 2005 Triggers A trigger is a statement that is executed automatically by the system as a side effect of a modification to the database.

18 CMSC424, Spring 2005 Trigger Example Suppose that instead of allowing negative account balances, the bank deals with overdrafts by setting the account balance to zero creating a loan in the amount of the overdraft giving this loan a loan number identical to the account number of the overdrawn account

19 CMSC424, Spring 2005 Trigger Example in SQL:1999 create trigger overdraft-trigger after update on account referencing new row as nrow for each row when nrow.balance < 0 begin atomic actions to be taken end

20 CMSC424, Spring 2005 Trigger Example in SQL:1999 create trigger overdraft-trigger after update on account referencing new row as nrow for each row when nrow.balance < 0 begin atomic insert into borrower (select customer-name, account-number from depositor where nrow.account-number = depositor.account-number); insert into loan values (nrow.account-number, nrow.branch-name, nrow.balance); update account set balance = 0 where account.account-number = nrow.account-number end

21 CMSC424, Spring 2005 Triggers… Statement Level Triggers External World Actions How does the DB order something if the inventory is low ? Syntax.. Careful with triggers Cascading triggers, Infinite Sequences…

22 CMSC424, Spring 2005 Next… Security and authorization (Chapter 6) Next: Relational Database Design What is it ? Why ?

23 CMSC424, Spring 2005 Pitfalls in Relational Database Design Find a “good” collection of relation schemas; otherwise –Repetition of Information. Leads to anomalies –Inability to represent certain information. Design Goals: –Avoid redundant data –Ensure that relationships among attributes are represented –Facilitate the checking of updates for violation of database integrity constraints.

24 CMSC424, Spring 2005 Example Consider the relation schema: Lending-schema = (branch-name, branch-city, assets, customer-name, loan-number, amount)

25 CMSC424, Spring 2005 Decomposition Decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city,assets) Loan-info-schema = (customer-name, loan-number, branch-name, amount) All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): R = R 1  R 2 Lossless-join decomposition. For all possible legal relations r on schema R r =  R1 (r)  R2 (r) How do you define legal ?

26 CMSC424, Spring 2005 Example of Non Lossless-Join Decomposition Decomposition of R = (A, B) R 1 = (A)R 2 = (B) AB  A  B 1212 r A(r)A(r)  B(r)  A (r)  B (r) AB 