DATABASE DESIGN - 1DL400

Similar documents
DATABASE DESIGN - 1DL400

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

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

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

Database System Concepts

DATABASE TECHNOLOGY - 1MB025

Chapter 6: Integrity and Security.! Domain Constraints! Referential Integrity! Assertions! Triggers! Security! Authorization! Authorization in SQL

DATABASTEKNIK - 1DL116

DATABASE TECHNOLOGY - 1MB025

Integrity and Security

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

DATABASE TECHNOLOGY - 1DL124

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

DATABASE DESIGN I - 1DL300

Chapter 4: SQL. Basic Structure

DATABASE DESIGN I - 1DL300

The SQL database language Parts of the SQL language

DATABASE TECHNOLOGY. Spring An introduction to database systems

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

CSCC43H: Introduction to Databases. Lecture 4

Views, assertions, and triggers

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

CSIT5300: Advanced Database Systems

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

DATABASE DESIGN II - 1DL400

CS352 - DATABASE SYSTEMS. To give you experience with developing a database to model a real domain

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

DATABASE TECHNOLOGY. Spring An introduction to database systems

Database Systems SQL SL03

QQ Group

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

CSCI 127 Introduction to Database Systems

Database System Concepts"

Database Systems SQL SL03

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Basic SQL. Basic SQL. Basic SQL

CS352 - DATABASE SYSTEMS

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

A database can be modeled as: + a collection of entities, + a set of relationships among entities.

Intermediate SQL ( )

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

CSIE30600 Database Systems Basic SQL 2. Outline

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Upon completion of this Unit, students will be introduced to the following

CSIT5300: Advanced Database Systems

Chapter 3: SQL. Chapter 3: SQL

A database consists of several tables (relations) AccountNum

Chapter 2: Relational Model

Silberschatz, Korth and Sudarshan See for conditions on re-use

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

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Chapter 3: Relational Model

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

DATABASDESIGN FÖR INGENJÖRER - 1DL124

ADVANCED DATABASES ; Spring 2015 Prof. Sang-goo Lee (11:00pm: Mon & Wed: Room ) Advanced DB Copyright by S.-g.

Introduction to Database Technology Elmasri/Navathe ch 1-2 Padron-McCarthy/Risch ch 1 Sobhan Badiozamany Silvia Stefanova Department of Information Te

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

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

Chapter 4: Intermediate SQL

CSC 261/461 Database Systems Lecture 6. Fall 2017

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

CS275 Intro to Databases

Conceptual Design. The Entity-Relationship (ER) Model

Lecture 07. Spring 2018 Borough of Manhattan Community College

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

The Relational Model 2. Week 3

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

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

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Information Systems and Software Systems Engineering (12CFU)

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

Chapter 6: Relational Database Design

SQL DATA DEFINITION LANGUAGE

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

SQL STRUCTURED QUERY LANGUAGE

Database Systems. Answers

SQL DATA DEFINITION LANGUAGE

Lecture 14 of 42. E-R Diagrams, UML Notes: PS3 Notes, E-R Design. Thursday, 15 Feb 2007

SQL DATA DEFINITION LANGUAGE


ECE 650 Systems Programming & Engineering. Spring 2018

First lecture of this chapter is in slides (PPT file)

Chapter A: Network Model

Chapter 1: Introduction

Relational Database Systems Part 01. Karine Reis Ferreira

The Relational Model

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Basic Concepts. Chapter A: Network Model. Cont.) Data-Structure Diagrams (Cont( Data-Structure Diagrams. Basic Concepts

Chapter 5: Other Relational Languages

Chapter 4: Intermediate SQL

CS W Introduction to Databases Spring Computer Science Department Columbia University

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

UNIT- II (Relational Data Model & Introduction to SQL)

Transcription:

DATABASE DESIGN - 1DL400 Spring 2014 2014-01-21 A course on modern database systems http://www.it.uu.se/research/group/udbl/kurser/dbii_vt14/integrity.pdf Tore Risch Uppsala Database Laboratory Department of Information Technology, Uppsala University, Uppsala, Sweden 2014-01-28 1

Database Integrity Constraints (Elmasri/Navathe ch. 5.2 and 8.2) (Padron-McCarthy/Risch ch 12 and 14) Tore Risch Department of Information Technology Uppsala University, Uppsala, Sweden 2014-01-28 2

Integrity constraints (for relational database schemas) Integrity constraints guard against accidental insertion of illegal or inconsistent data into the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. Different kinds of integrity constraints supported in modern RDBMS: 1. Domain constraints (domain = data type of column in table) attribute values for attribute A shall be atomic values from dom(a), c.f. user defined atomic datatype 2. Key constraint candidate keys for a relation must be unique. The key identifies uniquely an entity (object). 2014-01-28 3

Integrity constraints (for relational database schemas) 3. Entity integrity constraint no primary key is allowed to have a null value (the primary key identifies entities/objects) 4. Referential integrity constraint a tuple that refers to another tuple in another relation must refer to an existing tuple (no dangling references to other entities) 5. Semantic integrity constraint e.g. an employee s total work time per week can not exceed 40 hours for all projects taken all together. 2014-01-28 4

Domain constraints Domains created by the: CREATE DOMAIN domain-name [AS] data type E.g. CREATE DOMAIN ssn_type [AS] CHAR(9); Domain constraints are the most elementary form of integrity constraint. The DBMS tests values inserted into the database to ensure that the inserted values do not violate domain restrictions. 2014-01-28 5

Domain constraints cont The check clause in SQL-92 permits domains to have restricted values: For example, the following check clause ensures that hourly-wage values are always greater than 9: create domain hourly-wage numeric(5,2) constraint valuetest check (value >= 9.00) The domain hourly-wage is declared to be a decimal number with 5 digits, 2 of which are after the decimal point The domain has a constraint that ensures that hourly-wage is greater than 9.00. The constraint check is optional and very useful to check integrity constraintys on inserted values in rows. The atomic constraint check is very efficient! 2014-01-28 6

Key constraint & Entity integrity constraint Key constraint - candidate keys for a relation must be unique Dnumber INT PRIMARY KEY; alternative: PRIMARY KEY(Dnumber); Dname VARCHAR(15) UNIQUE; alternative: UNIQUE(Dname); Entity integrity constraint - no primary key is allowed to have a null value Implicitly imposed by the PRIMARY KEY clause. Dname VARCHAR(15) NOT NULL; 2014-01-28 7

Referential integrity in SQL - example create table customer (customer-name char(20) not null, customer-street char(30), customer-city char(30), primary key (customer-name)) create table branch (branch-name char(15) not null, branch-city char(30), assets integer, primary key (branch-name)) 2014-01-28 8

Referential integrity in SQL - example cont d create table account (account-number char(10) not null, branch-name char(15), balance integer, primary key (account-number), foreign key (branch-name) references branch) create table depositor (customer-name char(20) not null, account-number char(10) not null, primary key (customer-name,account-number), foreign key (account-number) references account, foreign key (customer-name) references customer) 2014-01-28 9

Cascading actions in SQL create table account... foreign key (branch-name) references branch on delete cascade on update cascade...) If a tuple in branch is deleted (updated), there is a tuple in account that will also be deleted (updated), i.e. the delete (update) cascades. In addition to cascade there are options of set default and set null. 2014-01-28 10

Cascading actions in SQL cont d If there is a chain of foreign-key dependencies across multiple relations, with on delete cascade specified for each dependency, a deletion or update at one end of the chain can propagate across the entire chain. If a cascading update or delete causes a constraint violation that cannot be handled by a further cascading operation, the system aborts the transaction. As a result, all the changes caused by the transaction and its cascading actions are undone. 2014-01-28 11

Constraints on tuples using check conditions Tuple-based (c.f. domain based) constraints can be defined by a check(p) clause, where P is a predicate E.g. declare and ensure that the values of assets are non-negative for the branch table. create table branch (branch_name char(15) primary key, branch_city char(30), assets integer, check (assets >= 0)) 2014-01-28 12

Assertions An assertion is a predicate expressing a condition that we wish the database always to satisfy. An assertion in SQL-92 takes the form: create assertion <assertion-name> check <predicate> When an modification (insert/delete/update)of the db is made, the system tests it for validity. This testing may introduce a significant amount of overhead; hence assertions should be used with great care. 2014-01-28 13

Assertion example The sum of all loan amounts for each branch must be less than the sum of all account balances at the branch. create assertion sum-constraint check (not exists (select * from branch b where (select sum(amount) from loan where loan.branch-name = b.branch-name) >= (select sum(amount) from account where account.branch-name = b.branch-name))) 2014-01-28 14

Another assertion example Every loan has at least one borrower who maintains an account with a minimum balance of $1000.00. create assertion balance-constraint check (not exists (select * from loan where not exists (select * from borrower, depositor, account where loan.loan-number = borrower.loan-number and borrower.customer-name = depositor.customer-name and depositor.account-number = account.account-number and account.balance >= 1000))) 2014-01-28 15