Chapter 8 INTEGRITY 1

Similar documents
Chapter 4. The Relational Model

Database Systems Relational Model. A.R. Hurson 323 CS Building

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Relational Model

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

The Relational Model of Data (ii)

CS275 Intro to Databases

Information Systems. Relational Databases. Nikolaj Popov

The Relational Data Model and Relational Database Constraints

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

CS 377 Database Systems

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

The Relational Model 2. Week 3

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

The Relational Model. Chapter 3

The Relational Model. Week 2

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Relational data model

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

The Relational Model

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

A database consists of several tables (relations) AccountNum

Database Applications (15-415)

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

High-Level Database Models (ii)

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

Database Systems ( 資料庫系統 )

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Unit 3 The Relational Model

CSC 742 Database Management Systems

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Database Management

The Relational Model and Normalization

Chapter 5. The Relational Data Model and Relational Database Constraints. Slide 5-١. Copyright 2007 Ramez Elmasri and Shamkant B.

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 5-1

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Introduction to Database Systems CSE 414

CS W Introduction to Databases Spring Computer Science Department Columbia University

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

CSE 344 MAY 14 TH ENTITIES

High Level Database Models

CHAPTER 5 SECURITY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

SQL DATA DEFINITION LANGUAGE

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

SQL Constraints and Triggers

Supplier-Parts-DB SNUM SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens

SQL DATA DEFINITION LANGUAGE

CPS510 Database System Design Primitive SYSTEM STRUCTURE

Chapter 3. The Relational database design

The Relational Data Model. Data Model

The Relational Model

Database Management Systems. Chapter 3 Part 1

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

Relational Model Concepts

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

download instant at The Relational Data Model

ECE 650 Systems Programming & Engineering. Spring 2018

Introduction to Data Management CSE 344

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment

The Entity-Relationship Model. Overview of Database Design

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

Intermediate SQL ( )

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

CSE 132A Database Systems Principles

Chapter 2: Intro to Relational Model

Relational Data Model

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Introduction to Data Management CSE 344

The Relational Model Constraints and SQL DDL

SQL DATA DEFINITION LANGUAGE

Introduction to Data Management CSE 344

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Full file at

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Let s briefly review important EER inheritance concepts

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

Database Design Process Entity / Relationship Diagrams

CS2300: File Structures and Introduction to Database Systems

Databases. Jörg Endrullis. VU University Amsterdam

Chapter 5. Relational Model Concepts 5/2/2008. Chapter Outline. Relational Database Constraints

COURSE OVERVIEW THE RELATIONAL MODEL. CS121: Relational Databases Fall 2017 Lecture 1

Chapter 5. Relational Model Concepts 9/4/2012. Chapter Outline. The Relational Data Model and Relational Database Constraints

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Database Systems CSE 414

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

COURSE OVERVIEW THE RELATIONAL MODEL. CS121: Introduction to Relational Database Systems Fall 2016 Lecture 1

Advanced Constraints SQL. by Joe Celko copyright 2007

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Transcription:

Chapter 8 INTEGRITY 1

Introduction Integrity refers to the correctness or accuracy of data in the database For examples: In Supplier-Part-Project database, the status values might have to be in the range of 1 to 100 Shipment quantities might have to be a multiple of 50 Red parts might have to be stored in London Database needs to be informed of such constraints and needs to be enforced 2

Introduction Status values must be in the range 1 to 100 CONSTRAINT SC3 IS_EMPTY (S WHERE STATUS < 1 OR STATUS >100) This constraint will registered in the system catalog under that name Any attempt to violate the constraint will be rejected 3

Introduction Enforcement of integrity Declarative approach Stored or trigger procedure approach (a precompiled procedures) Constraint classification Type constraints Attribute constraints RELVAR constraints Database constraints 4

Type Constraints An enumeration of the legal values of the type TYPE WEIGHT POSSREP (RATIONAL) CONSTRAINT THE_WEIGHT (WEIGHT) > 0.0 WEIGHT can be represented in rational number and is greater than zero. 5

Attribute Constraints A declaration to the effect that a specified attribute is of a specified type. VAR S BASE RELATION { S# S#, SNAME NAME, STATUS INTEGER, CITY CHAR }; 6

RELVAR Constraints A constraint on an individual relvar CONSTRAIN SC5 IS_EMPTY ( S WHERE CITY = London AND STATUS <> 20 ) (Suppliers in London must have status 20) CONSTRAIN SCK COUNT (S) = COUNT (S {S#} ) (Supplier numbers are unique) Relvar constraints are always checked immediately. 7

Database Constraints A constraint that interrelates two or more distinct relvars CONSTRAINT DBC1 IS_EMPTY ( ( S JOIN P ) WHERE STATUS < 20 AND QTY > QTY(500) ) No supplier with status less than 20 can supply any part in a quantity greater that 500. CONSTRAINT DBC2 SPJ ( S#) <= S{S#} Every supplier number in the shipments relvar also exists in the supplier relvar. Database constraints are checked at COMMIT time. 8

KEYS Candidate keys Let K be a set of attributes of relvar R. The K is a candidate key for if and only if it possesses both of the following properties: a). Uniqueness: No legal value of R ever contains two distinct tuples with the same values for K b). Irreducibility: No proper subset of K has the uniqueness property { S#, CITY} { S#} is a candidate key 9

Candidate Keys TUTORIAL D: KEY {<attribute name commalist>} VAR SPJ BASE RELATION { S# S#, P# P#, J# J#, QTY QTY} KEY {S#, P#, J#}; // Composite candidate key VAR ELEMENT BASE RELATION { NAME NAME, SYMBOL CHAR, ATOMIC# INTEGER } KEY { NAME } KEY { SYMBOL } KEY { ATOMIC# }; 10

Candidate Keys VAR MARRIAGE BASE RELATION { HUSBAND WIFE DATE NAME, NAME, DATE} /* assume no polyandry, no polygyny, and no husband and wife marry each other more than once */ KEY { HUSBAND, DATE} KEY { DATE, WIFE } KEY { WIFE, HUSBAND}; 11

Candidate Keys Candidate keys provide the basic tuple-level addressing mechanism in the relation model For example: S WHERE S# = S#( S3 ) is guaranteed to yield at most one tuple. S WHERE CITY = Paris yields an unpredictable number of tuples. 12

Superkeys A superset of a candidate key is a superkey {S#, CITY} is a superkey for relval S {S#, SNAME, CITY} is a superkey for relval S 13

Primary Keys & Alternate Keys Exactly one of the candidate keys is chosen as primary key And other candidate keys are called alternate keys Choice of primary key from candidate keys is arbitrary Every base RELVAR has always a primary key 14

Foreign Keys A foreign key is a set of attributes of one relvar R2 whose values are required to match values of some candidate key of some relvar R1. For example: S# in SPJ FORMAL DEFINITION: Let R2 be a relvar. Then a foreign key in R2 is a set of attributes of R2, day FK such that a). There exists a relvar R1 with a candidate key CK, and b). For all time, each value of FK in the current value of R2 is identical to the value of CK in some tuple in the currrent value of R1 15

Notes about Foreign Keys Every value of a given foreign key appears as a value of the matching candidate keys. The converse is not a requirement. A foreign key is simple or composite depending on the candidate key it matches Each attribute of a given foreign key must have the same name and type as the matching candidate key 16

Referential Integrity The problem of ensuring that the database does not include any invalid foreign key values is known as REFERENTIAL INTEGRITY Referential Constraint: Values of a given foreign key must match values of the corresponding candidate key. Referencing relvar: The RELVAR contains foreign key Referenced relvar: The RELVAR contains candidate key 17

Referential Diagram S# P# S SPJ P R3 R2 R1 R(n) R(n-1) R2 R1 R(n) R(n-1) R2 R1 R(n) 18

Referential Diagram EMP SSN EMP TABLE EMP {SSN, MGR_EMP} /* self-referencing */ R(n) R(n-1) R2 R1 R(n) /* referential cycle */ 19

Referential Actions DELETE S WHERE S# = S#( S1 ) Assume: The database includes some shipments for supplier S1 No shipments for supplier S1 are deleted When referential constraint from shipment to supplier is checked, a violation will be found 20

Referential Actions VAR SPJ BASE RELATION { } FOREIGN KEY {S# } REFERENCE S ON DELETE CASCADE; CASCADE: automatically delete shipments for supplier S1 when deleting S1 in S RESTRICT: DELETE operations are restricted to the cases where there are no matching shipments, otherwise they are rejected. NO ACTION: Perform as requested Rules are applied to UPDATE operation 21

SQL Facilities Candidate Keys UNIQUE (<column name commalist>) OR PRIMARY KEY(<column name commalist>) CREATE TABLE SP ( S# S# NOT NULL, P# P# NOT NULL, QTY QTY NOT NULL, PRIMARY KEY (S#, P#), FOREIGN KEY (S#) REFERENCE S ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (P#) REFERENCE P ON DELETE CASCADE ON UPDATE CASCADE, CHECK (QTY >0 AND QTY < 5001)); 22

SQL Facilities Assertions General constraints Every supplier has status at least five: CREATE ASSERTION IC13 CHECK ( (SELECT MIN(STATUS) FROM S) > 4) All red parts must be stored in London CREATE ASSERTION IC99 CHECK ( NOT EXISTS ( SELECT * FROM P WHERE COLOR = Red AND CITY <> London 23