Lecture 2: Part I. Integrity Rules: Applicable to All Databases. Integrity Rules. Key constraint Entity integrity Referential integrity

Similar documents
Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

3. Relational Model and Relational Algebra

Lecture 6. SQL, Logical DB Design

Information Systems SQL. Nikolaj Popov

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

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

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

IT2305 Database Systems I (Compulsory)

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

The Relational Data Model and Relational Database Constraints


System R: Relational Approach to Database Management

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Introduction to Microsoft Jet SQL

CSC 443 Data Base Management Systems. Basic SQL

IT2304: Database Systems 1 (DBS 1)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Chapter 9: Normalization

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

Relational Databases

Databases and BigData

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Overview of Database Management Systems

Review: Participation Constraints

The Relational Model. Why Study the Relational Model?

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Normalisation. Why normalise? To improve (simplify) database design in order to. Avoid update problems Avoid redundancy Simplify update operations

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Review of Business Information Systems Third Quarter 2013 Volume 17, Number 3

Mini User's Guide for SQL*Plus T. J. Teorey

Schema Refinement, Functional Dependencies, Normalization

Figure 4.12.Insurancedatabase.

There are five fields or columns, with names and types as shown above.

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

TYPICAL QUESTIONS & ANSWERS

CIS 631 Database Management Systems Sample Final Exam

Introduction to SQL C H A P T E R3. Exercises

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Normalization in Database Design

Scheme G. Sample Test Paper-I

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

Performance Evaluation of Natural and Surrogate Key Database Architectures

Theory of Relational Database Design and Normalization

The Relational Algebra

SQL NULL s, Constraints, Triggers

Chapter 10. Functional Dependencies and Normalization for Relational Databases. Copyright 2007 Ramez Elmasri and Shamkant B.

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Structured Query Language (SQL)

Introduction to database design

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Theory of Relational Database Design and Normalization

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Chapter 6: Integrity Constraints

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

Database Management Systems. Chapter 1

New York University Computer Science Department Courant Institute of Mathematical Sciences

Fundamentals of Database System

The process of database development. Logical model: relational DBMS. Relation

Data Modeling. Database Systems: The Complete Book Ch ,

Lecture 4: More SQL and Relational Algebra

Relational Foundations for Functorial Data Migration

Physical Database Design Process. Physical Database Design Process. Major Inputs to Physical Database. Components of Physical Database Design


DATABASE MANAGEMENT SYSTEMS. Question Bank:

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Part 4: Database Language - SQL

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

Week 4 & 5: SQL. SQL as a Query Language

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Summary on Chapter 4 Basic SQL

Data Modeling: Part 1. Entity Relationship (ER) Model

How To Manage Data In A Database System

10CS54: DATABASE MANAGEMENT SYSTEM

Relational Database: Additional Operations on Relations; SQL

Exercises. a. Find the total number of people who owned cars that were involved in accidents

Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

SQL: Queries, Programming, Triggers

Chapter 5: Logical Database Design and the Relational Model Part 2: Normalization. Introduction to Normalization. Normal Forms.

EECS 647: Introduction to Database Systems

SQL: QUERIES, CONSTRAINTS, TRIGGERS

A Tool for Generating Relational Database Schema from EER Diagram

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

SQL (structured query language)

CPS352 Database Systems: Design Project

Relational Algebra and SQL

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Query optimization. DBMS Architecture. Query optimizer. Query optimizer.

SQL: Queries, Programming, Triggers

Introduction to SQL ( )

VIEWS virtual relation data duplication consistency problems

Transcription:

Lecture 2: Part I Integrity Rules Relational integrity rules 1 Integrity Rules: Applicable to a Particular Database E.G. An employee s salary must be in the range $30,000 to $100,000. See section 8.6 Elmasri & Navathe. Supported in the SQL 2 standard and available in most relational systems today. Not discussed now. Relational integrity rules 2 Integrity Rules: Applicable to All Databases Key constraint Entity integrity Referential integrity Relational integrity rules 3 1

Foreign Keys - a Definition DNO in EMP - should be allowed only if that DNO appears as primary key in relation DEPT. MGR in EMP - should be allowed only if that MGR appears as primary key in relation EMP. The attributes DNO and MGR called foreign keys in relation EMP. Relational integrity rules 4 Def. Foreign Key, FK, an attribute (or combination of attributes in one Relation Schema, R 1, such that: Attributes in FK of R 1 have same domain as the primary key attributes, PK, of relation schema R 1. A value of FK in a tuple t 1 of R 1 either occurs As a value of PK of some tuple t 2 of R 2, i.e. T 1 [FK] = t 2 [PK] Or is null. Relational integrity rules 5 The Three Integrity Rules Key constraint Candidate keys are specified for each relation schema. Candidate key values must be unique for every tuple in any relation instance of that schema. Relational integrity rules 6 2

The Three Integrity Rules, Continued Entity integrity: No attribute participating in the primary key is allowed to accept null values. Justification: we must be able to identify each tuple uniquely. Relational integrity rules 7 The Three Integrity Rules, Continued Referential integrity: If relation schema R 1 includes a foreign key, FK, matching the primary key of relation schema R 2, then a value of FK in a tuple t 1 of R 1 must either Be equal to the value of PK in some tuple t 2 of R 2, i.e. T 1 [FK] = t 2 [PK], or Be wholly null (i.e. Each attribute value participating in that FK value must be null). Note: R 1 and R 2 need not be distinct. Justification: if some tuple t 1 references some tuple t 2, then tuple t 2 must exist. Relational integrity rules 8 Implications of the Three Integrity Rules What should happen if an operation on the database is about to cause the violation of one of the integrity rules? i.e. about to put the database into an illegal state. System could reject the operation. System could accept the operation, but perform some additional operations so as to reach a new legal state. Relational integrity rules 9 3

Example: Delete DEPT Where DNO = D2 What should happen if there are employees in DEPT with DNO = D2? Operation could be rejected. Employees in DEPT D2 could also be deleted, i.e. Cascade the deletion. Choice should be up to the user, the database designer. Relational integrity rules 10 Notes Not included in Codd s original definition of relations. Not supported in initial commercial relational database systems. Now supported in all major relational database systems. Actually introduced in network model. Relational integrity rules 11 Foreign Key With Null Value. May an employee not be assigned to a department? Probably May an employee not have a manager? Let s assume no. Relational integrity rules 12 4

Deleting Target of a Foreign Key Reference Example: Delete department where there are still employees in the department. Restricted - delete is restricted to case where there are no employees in the department. Cascades - delete cascades to remove employees also. Nullifies - foreign key is set to null, i.e. Employees are no longer in any department. Then department is deleted. Relational integrity rules 13 Updating Target of a Foreign Key Reference. Example: Change department number (DNO) D50 to D75. Restricted - update is restricted to case where there are no employees in the department. Cascades - update cascades to change the department numbers of all employees in department D50. Nullifies - foreign key, i.e. DNO in EMP is set to null. Department DNO is updated. Relational integrity rules 14 Inserting into Relation With the Foreign Key. Example: Insert a new employee into department D10, but there is no tuple in the DEPT relation for department D10. Restricted - insert is restricted to case where there are already tuples for the corresponding department. Cascades - if done interactively, user could be asked to enter tuple for department D10. Nullifies - cannot be done, since user has specified the department number. Relational integrity rules 15 5

Lecture 2: Part II Relational Algebra Relational integrity rules 16 Set operators Union Intersection Difference Cartesian product Relational Algebra Operators Relational operators Selection Projection Join Division Relational integrity rules 17 Basic Relational Operators in Pictures select (σ) project (π) eliminate duplicates A B C D a1 a2 a3 a4 b1 b1 b1 b3 c1 c2 c3 c3 equijoin (natural) join (*) A a1 a2 a3 a3 a4 a4 A B D a1 a2 a3 a3 a4 a4 b1 b1 b1 b1 B C D b1 c1 b1 c1 c2 c3 c2 c3 c1 c1 c2 c3 c2 c3 Relational integrity rules 18 6

Examples (1): Select (σ) and Project (π) Q1. Find the subset of employees who are in dept. 50. σ DNO = 50 (EMP) Q2. Find the subset of employees who are in dept. 25, 47 or 53. σ DNO = 25 or DNO = 47 or DNO = 53 (EMP) Q3. List names and jobs of all employees. π NAME, JOB (EMP) Relational integrity rules 19 Examples (2): More Complex Select (σ) and Project (π) Q4. List names and jobs of employees in departments 25, 47 or 53. π NAME, JOB (σ DNO = 25 or DNO = 47 or DNO = 53 (EMP)) D_EMPS σ DNO = 25 or DNO = 47 or DNO = 53 (EMP) RESULT π NAME, JOB (D_EMP) Relational integrity rules 20 Examples (3): Union ( ) and Difference (-) Q5. List the numbers of those departments which have an employee named Smith or are located in Columbus. π DNO (σ NAME = Smith (EMP)) π DNO (σ LOC = Columbus (DEPT)) Q6. List the DNO for departments that have no employees. π DNO (DEPT) - π DNO (EMP) Relational integrity rules 21 7

Examples (4): Join: Natural (*) and Theta ( ) Q7. List the names of all employees and the locations of their departments. π NAME, LOC (EMP * DEPT) ) * Natural join π NAME, LOC (EMP EMP.DNO = DEPT.DNO (DEPT) ) Theta-join (θ-join) θ ε {, <, =, >,, } Relational integrity rules 22 Natural join (*) Q R 1 * (list1), (list2) R 2 list i has attributes from relation R i list 1 names go in Q Theta (θ) join ( ) Q R 1 <join condition> R 2 where <join condition> is of the form <condition> AND <condition> AND AND <condition> where each condition is of the form A i θ B j, A i is an attribute of R 1, B j is an attribute of R 2, θ (theta) ε (<,, >,, =, ) e.g. For each employee, those making higher salary. EMP1 EMP1.SAL < EMP2.SAL EMP2 Relational integrity rules 23 Examples (5): Select, project and join Q8. Find the names of employees who work in Ann Arbor. π NAME (EMP * (σ LOC = Ann Arbor (DEPT) ) Natural join * π NAME (EMP EMP.DNO = DEPT.DNO (σ LOC = Ann Arbor (DEPT) ) equi-join when θ is = Relational integrity rules 24 8

Examples (6): Not Equal ( ) in Select Q9. Find the names of employees who do not work in Ann Arbor. π NAME (EMP * (σ LOC Ann Arbor (DEPT) ) Natural join * π NAME (EMP EMP.DNO = DEPT.DNO (σ LOC Ann Arbor (DEPT) ) (θ) Join Relational integrity rules 25 Examples (7): A classic, equi-join as product ( ) and select (σ) Q10. For each employee whose salary exceeds his or her manager s salary, list the employee s name and the manager s name. X EMP Y EMP π X.NAME, Y.NAME (σ X.SAL > Y.SAL (X Y.EMPNO = X.MGR Y)) π X.NAME, Y.NAME (σ X.SAL > Y.SAL and Y.EMPNO = X.MGR (X Y)) Relational integrity rules 26 Examples (8): A new kind of query Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3. Q12. Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Relational integrity rules 27 9

Examples (8): continued Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3. Answer: D1 and D2 EMP( EMPNO,, DNO, JOB,...) 100 D3 electrician 200 D3 plumber 300 D3 electrician 400 D1 electrician 500 D1 plumber 600 D1 carpenter 700 D2 electrician 800 D2 carpenter 900 D2 electrician Relational integrity rules 28 Examples (8): continued Q12. Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1 but not D2 EMP( EMPNO,, DNO, JOB,...) 100 D3 electrician 200 D3 plumber 300 D3 electrician 400 D1 electrician 500 D1 plumber 600 D1 carpenter 700 D2 electrician 800 D2 carpenter 900 D2 electrician Relational integrity rules 29 Examples (8): continued (exists, ) Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3. Answer: D1 and D2 EMP( EMPNO,, DNO, JOB,...) 100 D3 electrician 200 D3 plumber EMP( EMPNO,, DNO, JOB,...) 500 D1 plumber 900 D2 electrician π DNO ( π JOB (σ DNO = D3 (EMP)) * EMP) What about department D3? Relational integrity rules 30 10

Examples (8): continued Q12. Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1, but not D2 EMP( EMPNO,, DNO, JOB,...) 100 D3 electrician 200 D3 plumber 300 D3 electrician 400 D1 electrician 500 D1 plumber 600 D1 carpenter 700 D2 electrician 800 D2 carpenter 900 D2 electrician Relational integrity rules 31 Examples (9): divide ( ) SP( S#, P#) S1 P1 S1 P2 S1 P3 S1 P4 S2 P1 S2 P2 S3 P2 S4 P1 S4 P2 S4 P3 P( P#) P1 P2 P3 SP P ( S# ) S1 S4 Relational integrity rules 32 SP( S#, P#) S1 P1 S1 P2 S1 P3 S1 P4 S2 P1 S2 P2 S3 P2 S4 P1 S4 P2 S4 P3 Examples (9): divide ( ) Dividend Divisor = Quotient (degree m + n) (degree n) (degree m) P( P#) P1 P2 P3 P( P#) P1 P2 P( P#) P1 SP P ( S# ) S1 S4 SP P ( S# ) S1 S2 S4 SP P ( S# ) S1 S2 S4 Relational integrity rules 33 11

Examples (8): Divide ( ), for all ( ) Q12. Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1, but not D2 EMP( EMPNO,DNO,JOB,...) 100 D3 electrician 200 D3 plumber 300 D3 electrician 400 D1 electrician 500 D1 plumber 600 D1 carpenter 700 D2 electrician 800 D2 carpenter 900 D2 electrician D3JOBS π JOB (σ DNO = D3 (EMP)) DEPT_JOBS π DNO, JOB (EMP) GOOD_DEPTS DEPT_JOBS D3JOBS (π DNO, JOB (EMP)) (π JOB (σ DNO = D3 (EMP)) ) Relational integrity rules 34 Examples (10): Functions and groups Q13. Find the average salary of all employees. EMP(EMPNO,DNO, SAL,...) 100 D3 66,000 200 D3 55,000 300 D3 66,000 400 D1 65,000 500 D1 55,000 600 D1 60,000 700 D2 66,000 800 D2 60,000 900 D2 66,000 62,000 AVERAGE SAL ( EMP) Relational integrity rules 35 Examples (10): Functions and groups continued Q14. List the departments (DNO) and the average salary of each. EMP(EMPNO,DNO, SAL,...) 100 D3 66,000 200 D3 55,000 300 D3 66,000 400 D1 66,000 500 D1 55,000 600 D1 60,000 700 D2 66,000 800 D2 60,000 900 D2 66,000 DNO DNO AVG SAL D3 62,333 D1 60,333 D2 64,000 AVERAGE SAL ( EMP) Relational integrity rules 36 12

Examples (11): Outer joins P(P#, PNAME, CITY) S(S#, SNAME, CITY) P1 Nut London S1 Smith London P3 Screw Rome S5 Adams Athens Q15a. For each part list all suppliers in the same city. ( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London Q15b. For each part list all suppliers in the same city. For a part with no supplier in the city, list null. P P.CITY = S.CITY S natural join (inner join) ( P#, PNAME, S#, SNAME, CITY) P P.CITY = S.CITY S P1 Nut S1 Smith London P3 Screw?? Rome left outer join Relational integrity rules 37 Examples (11): Outer joins continued P(P#, PNAME, CITY) P1 Nut London P3 Screw Rome Q15c. For each part list all suppliers in the same city. For a supplier with no part in the city, list null. ( P#, PNAME, S#, SNAME, CITY) P P.CITY = S.CITY S P1 Nut S1 Smith London?? S5 Adams Athens right outer join Q15d. For each part list all suppliers in the same city. For a part with no supplier in the city, list null. For a supplier with no part in the city, list null. S(S#, SNAME, CITY) S1 Smith London S5 Adams Athens ( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London P P.CITY = S.CITY S P3 Screw?? Rome?? S5 Adams Athens outer join Relational integrity rules 38 Examples (12): Recursive closure EMP(EMPNO,MGR,...) 100 700 200 700 300 700 400 800 500 800 600 800 700 900 800 900 900 900 Q 16. List all the superiors of EMPNO 100. 700 900 Q 17. List all those supervised by EMPNO 800. 600 Can t express these queries? WHY? Relational integrity rules 39 13

Relational Completeness Select, project, union, difference, cartesian product sufficient to express Join, division and intersection Is there any new language this powerful? Must be able to translate any relational algebra statement to the new language. Relational integrity rules 40 14