Integrity Constraints

Slides:



Advertisements
Similar presentations
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Advertisements

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.
©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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Integrity Constraints
CMSC424: Database Design Instructor: Amol Deshpande
©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.
Chapter 5 The Relational Data Model and Relational Database Constraints.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
MySQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
CMSC424: Database Design Instructor: Amol Deshpande
Still More Operators: Outer Join outer join is an extension of the join operation to deal with missing information –three types: left outer join, right.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
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.
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.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Chapter 5 Integrity Constraints 5.1 Domain ConstraintsDomain Constraints 5.2 Referential IntegrityReferential Integrity 5.3 AssertionsAssertions 5.4 TriggersTriggers.
Chapter 4: Advanced SQL. 4.2Unite International CollegeDatabase Management Systems Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints.
Holliday - COEN 1781 Introduction to SQL. Holliday - COEN 1782 DB Tables and SQL The data is stored in the database in relations or tables Data types.
Relational Algebra Instructor: Mohamed Eltabakh 1.
-- Introduction to database principles Maoying Wu March 25, 2013 Chapter 4: Advanced SQL (1)
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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.
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.
Assertions and Triggers in SQL
Lecture 03 Constraints. Example Schema CONSTRAINTS.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
©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.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
Chapter 6: Integrity (and Security)
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Constraints and Triggers
SQL: Constraints and Triggers
Referential Integrity
CPSC-310 Database Systems
אילוצי שלמות אילוצי תחום מפתחות ומפתחות זרים טריגרים triggers
The Relational Data Model and Relational Database Constraints
Advanced SQL: Views & Triggers
Referential Integrity
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
Introduction to SQL Holliday - COEN 178.
Relational Database Design
CPSC-608 Database Systems
Example: Banking Database
CS 405G: Introduction to Database Systems
Presentation transcript:

Integrity Constraints B.Ramamurthy 11/8/2018 B.Ramamurthy

Introduction Integrity constraints make sure that changes to the database do not result in data inconsistency. Key declarations (candidate, primary, foreign) are a basic form of integrity constraint. A Integrity constraint can be any arbitrary predicate pertaining to the database. 11/8/2018 B.Ramamurthy

Domain Constraint Domain constraints allows us to test the values inserted into the database and to test the queries to make sure comparisons made are appropriate. Examples: create domain wage numeric (5,2) constraint wage-value-test check (value >= 4.0) 11/8/2018 B.Ramamurthy

Referential Integrity Let r1(R1) and r2(R2) be relations with primary key K1 and K2 A subset “a” of attributes R2 is a foreign key in relation r1, if for every tuple t2 in r2, there must be a tuple t1 in r1 such that t1[K1] = t2[a] Example: r1 Branch-scheme (branch_name, … r2 Account-schema (account#, branch_name Foreign key 11/8/2018 B.Ramamurthy

Foreign Key  (r2)  K1 (r1) Formally expressing the integrity constraint. For referential constraint to make sense  must be equal to K1. Intuitively, consider a query “insert account xyz with abc as branch” . What if “abc” does not exist? 11/8/2018 B.Ramamurthy

Referential Integrity in SQL When creating table we specify primary , candidate and foreign keys. These are specifying referential integrity. Example Fig6.2 Create table depositor (cname char(20) not null, acctNo char(10) not null, primary key (cname, acctNo), foreign key (cname) references customer, foreign key (acctNo) references account) 11/8/2018 B.Ramamurthy

Cascading delete and update Create table depositor (cname char(20) not null, acctNo char(10) not null, primary key (cname, acctNo), foreign key (cname) references customer on delete cascade on update cascade, foreign key (acctNo) references account) 11/8/2018 B.Ramamurthy

Assertions An assertion is a predicate expressing a condition that the database should always satisfy. It is more global in scope than domain and referential integrity constraints. Format: Create assertion <assertion_name> check predicate 11/8/2018 B.Ramamurthy

Assertion : example Create assertion sum_constraint check (not exists (select * from branch where (select sum(amount) from loan where loan.bname = branch.bname) >= (select sum(amount) from account where loan.bname =branch.bname))) 11/8/2018 B.Ramamurthy

Assertion Semantics When an assertion is created system tests it for validity. If the assertion is valid any future modifications to the database will be allowed only if it does not violate the assertion. The example in last slide makes sure total loan amount is NOT > total balance in a branch involved in loans. 11/8/2018 B.Ramamurthy

Trigger A trigger is a statement that is executed automatically by the system as a side effect of modification to the database. To design a trigger: Specify conditions under which the trigger is to be executed. Specify actions to be taken when trigger is executed. Triggers are performance deflators. 11/8/2018 B.Ramamurthy

Trigger example define trigger overdraft on update of account T (if new T.balance < 0 then (insert into loan values (T.bname , T.acctNo, - new T.balance) insert into borrower (select cname,acctNo from depositor where T.acctNo = depositor.acctNo) update account S set S.balance = 0 where S.acctNo = T.acctNo)) 11/8/2018 B.Ramamurthy

Functional Dependencies Functional dependency is a an extension of the notion of “key”. Functional dependencies are constrains on a set of legal relations. Functional dependency for   R,   R,   Holds on R if for all pairs of tuples t1 and t2 if t1[] = t2[] then t1[] = t2[] 11/8/2018 B.Ramamurthy

Example A B C D a1 b1 c1 d1 a1 b2 c1 d2 a2 b2 c2 d2 a2 b3 c2 d3 A  C but not C  A ABD 11/8/2018 B.Ramamurthy

Functional dependency in Bank Example Branch-schema Bnamebranch-city Bname  assets Customer-schema Cname customer-city Cnamecustomer-street Loan-Schema loanNo amount loanNo bname Account-schema accntNobname acctNo balance Others : none 11/8/2018 B.Ramamurthy

Closure of a set of Functional Dependencies Given a set of functional dependencies determine all the set of all functional dependencies that are implied. Consider R = (A, B, C, G, H, I) and a set of functional dependencies F A  B A  C CG  H CG I B  H Determine the closure F denoted by F. 11/8/2018 B.Ramamurthy