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

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

The Relational Model. Chapter 3

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

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

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

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

The Relational Model 2. Week 3

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

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

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

Relational data model

Database Systems ( 資料庫系統 )

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

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

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

Database Applications (15-415)

CIS 330: Applied Database Systems

Database Management Systems. Chapter 3 Part 2

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

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

The Relational Data Model. Data Model

Database Management Systems. Chapter 3 Part 1

The Relational Model of Data (ii)

Database Systems. Course Administration

The Entity-Relationship Model. Overview of Database Design

The Entity-Relationship Model

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

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

The Relational Model. Week 2

The Relational Model

High-Level Database Models (ii)

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

The Relational Model (ii)

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

ER to Relational Mapping. ER Model: Overview

From ER to Relational Model. Book Chapter 3 (part 2 )

The Relational Model

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

High Level Database Models

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Database Management Systems. Session 2. Instructor: Vinnie Costa

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

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

The Entity-Relationship Model

Database Applications (15-415)

OVERVIEW OF DATABASE DEVELOPMENT

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Entity-Relationship Model

DBMS. Relational Model. Module Title?

CSC 261/461 Database Systems Lecture 10

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

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

MIS Database Systems Entity-Relationship Model.

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

The Entity-Relationship Model. Overview of Database Design. ER Model Basics. (Ramakrishnan&Gehrke, Chapter 2)

Introduction to Data Management. Lecture #4 E-R Model, Still Going

Introduction to Databases

Keys, SQL, and Views CMPSCI 645

The Entity-Relationship (ER) Model

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Introduction to Database Design. Dr. Kanda Runapongsa Dept of Computer Engineering Khon Kaen University

Database Management Systems. Chapter 2 Part 2

The Entity-Relationship Model. Steps in Database Design

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Design. ER Model. Overview. Introduction to Database Design. UVic C SC 370. Database design can be divided in six major steps:

Conceptual Design. The Entity-Relationship (ER) Model

Introduction to Database Design

The Relational Model. Database Management Systems

Introduction to Database Design

Overview. Introduction to Database Design. ER Model. Database Design

ER Model Overview. The Entity-Relationship Model. Database Design Process. ER Model Basics

CS/INFO 330 Entity-Relationship Modeling. Announcements. Goals of This Lecture. Mirek Riedewald

CS275 Intro to Databases

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

Database Applications (15-415)

Introduction to Database Design

CIS 330: Web-driven Web Applications. Lecture 2: Introduction to ER Modeling

The Relational Model

Database Applications (15-415)

CS 2451 Database Systems: Relational Data Model

Modern Database Systems Lecture 1

Database Design & Deployment

COMP Instructor: Dimitris Papadias WWW page:

Conceptual Design. The Entity-Relationship (ER) Model

Integrity constraints, relationships. CS634 Lecture 2

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

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

Entity-Relationship Diagrams

The Entity-Relationship (ER) Model 2

CS425 Midterm Exam Summer C 2012

Introduction to Data Management. Lecture #3 (Conceptual DB Design) Instructor: Chen Li

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

The Relational Model Constraints and SQL DDL

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

Transcription:

The Relational Model Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Microsoft, Oracle, Sybase, etc. Legacy systems in older models E.G., IBM s IMS Competitor in the early 90s: object-oriented model A synthesis: object-relational model Oracle, DB2 XML Database Management Systems, R. Ramakrishnan and J. Gehrke 2 Relational Database: Definitions Relational database: a set of relations Relation: made up of two parts: Schema : specifies of relation, plus and type of each column. E.G. Students(sid: string, : string, login: string, age: integer, gpa: real). Instance : a table, with rows and columns. #Rows = cardinality, #fields = degree / arity. Can think of a relation as a set of rows or tuples (i.e., all rows are distinct). Database Management Systems, R. Ramakrishnan and J. Gehrke 3

Example Instance of Students Relation sid login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Cardinality = 3, degree = 5, all rows distinct Do all columns in a relation instance have to be distinct? Database Management Systems, R. Ramakrishnan and J. Gehrke 4 Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY ()) Database Management Systems, R. Ramakrishnan and J. Gehrke 5 Example Instance 0983763423 John 9384392483 Jane 3743923483 Jill 10 10 20 Database Management Systems, R. Ramakrishnan and J. Gehrke 6

Integrity Constraints (ICs) IC: condition that must be true for any instance of the database Domain constraints Key constraints Foreign key constraints (later) A legal instance of a relation is one that satisfies all specified ICs. DBMS should not allow illegal instances Avoids data entry errors too! Database Management Systems, R. Ramakrishnan and J. Gehrke 7 Primary Key Constraints A set of fields is a superkey for a relation if : 1. No two distinct tuples can have same values in all fields A set of fields is a key if: 1. The set of fields is a superkey 2. No proper subset of the set of fields is a superkey If there s >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key. E.g., is a key for. (What about?) The set {, } is a superkey. Database Management Systems, R. Ramakrishnan and J. Gehrke 8 What does this mean? CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) ) Database Management Systems, R. Ramakrishnan and J. Gehrke 9

Candidate Keys Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key. CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) Each student is enrolled in at most one course No two students in a course get the same grade Database Management Systems, R. Ramakrishnan and J. Gehrke 10 Where do ICs Come From? ICs are based upon the semantics of the realworld enterprise that is being described in the database relations. We can check a database instance to see if an IC is violated, but we can NEVER infer that an IC is true by looking at an instance. An IC is a statement about all possible instances! From example, we know is not a key, but the assertion that sid is a key is given to us. Key and foreign key ICs are the most common; more general ICs supported too. Database Management Systems, R. Ramakrishnan and J. Gehrke 11 ER to Relational (contd.) since did d budget Works_In Database Management Systems, R. Ramakrishnan and J. Gehrke 12

Relationship Sets to Tables CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY ()) CREATE TABLE (did INTEGER, d CHAR(20), budget FLOAT, PRIMARY KEY (did)) CREATE TABLE Works_In( CHAR(11), did INTEGER, since DATE, PRIMARY KEY (, did), FOREIGN KEY () REFERENCES, FOREIGN KEY (did) REFERENCES ) Database Management Systems, R. Ramakrishnan and J. Gehrke 13 Example Instance 0983763423 9384392483 3743923483 John Jane Jill 10 10 20 did 101 105 108 d Sales Purchasing Databases budget 10K 20K 1000K Works_In did since 0983763423 101 1 Jan 2003 0983763423 108 2 Jan 2003 9384392483 108 1 Jun 2002 Database Management Systems, R. Ramakrishnan and J. Gehrke 14 Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to `refer to a tuple in another relation Must correspond to primary key of the second relation Like a `logical pointer. If all foreign key constraints enforced, referential integrity is achieved, i.e., no dangling references. Not like HTML links! Database Management Systems, R. Ramakrishnan and J. Gehrke 15

Enforcing Referential Integrity What if a new Works_In tuple is added that references a non-existent employee? Reject it! What if an Employee tuple is deleted? Also delete all Works_In tuples that refer to it. Disallow deletion of Employee tuple that is referred to. Set to some default value Set in Works_In to null, denoting `unknown Similar if primary key of Employee tuple is updated Database Management Systems, R. Ramakrishnan and J. Gehrke 16 Referential Integrity in SQL/92 SQL/92 supports all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT CREATE TABLE Works_In( CHAR(11), did INTEGER, since DATE, PRIMARY KEY (, did), FOREIGN KEY () REFERENCES ON DELETE CASCADE ON UPDATE SET DEFAULT, FOREIGN KEY (did) REFERENCES ON DELETE SET NULL ON UPDATE CASCADE) Database Management Systems, R. Ramakrishnan and J. Gehrke 17 ER to Relational (contd.) subordinate supervisor Reports_To Database Management Systems, R. Ramakrishnan and J. Gehrke 18

Relationship Sets to Tables CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY ()) CREATE TABLE Reports_To ( supervisor_ CHAR(11), subordinate_ CHAR(11), FOREIGN KEY (supervisor_) REFERENCES, FOREIGN KEY (subordinate_) REFERENCES ) Database Management Systems, R. Ramakrishnan and J. Gehrke 19 ER to Relational (contd.) id Parts id id Suppliers Contract Database Management Systems, R. Ramakrishnan and J. Gehrke 20 Relationship Sets to Tables CREATE TABLE Contracts ( supplier_id INTEGER, part_id INTEGER, department_id INTEGER, PRIMARY KEY (supplier_id, part_id, department_id), FOREIGN KEY (supplier_id) REFERENCES Suppliers, FOREIGN KEY (part_id) REFERENCES Parts, FOREIGN KEY (department_id) REFERENCES ) Database Management Systems, R. Ramakrishnan and J. Gehrke 21

ER to Relational (contd.) Each dept has at most one manager, according to the key constraint on Manages. since d did budget Manages Database Management Systems, R. Ramakrishnan and J. Gehrke 22 Relationship Sets to Tables CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY ()) CREATE TABLE (did INTEGER, d CHAR(20), budget FLOAT, mgr_ CHAR(11), PRIMARY KEY (did) FOREIGN KEY (mgr_) REFERENCES ) Database Management Systems, R. Ramakrishnan and J. Gehrke 23 ER to Relational (contd.) Each employee works in at least one department according to the participation constraint on Works_In since did d budget Works_In Database Management Systems, R. Ramakrishnan and J. Gehrke 24

ER to Relational (contd.) since did d budget Manages Database Management Systems, R. Ramakrishnan and J. Gehrke 25 Relationship Sets to Tables CREATE TABLE Department ( did INTEGER, d CHAR(20), budget REAL, mgr_ CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY () REFERENCES, ON DELETE NO ACTION) Database Management Systems, R. Ramakrishnan and J. Gehrke 26 ER to Relational (contd.) A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. cost p age Policy Dependents Database Management Systems, R. Ramakrishnan and J. Gehrke 27

Translating Weak Entity Sets Weak entity set and identifying relationship set are translated into a single table. When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( p CHAR(20), age INTEGER, cost REAL, CHAR(11) NOT NULL, PRIMARY KEY (p, ), FOREIGN KEY () REFERENCES, ON DELETE CASCADE) Database Management Systems, R. Ramakrishnan and J. Gehrke 28 ER to Relational (Contd.) hourly_wages hours_worked ISA contractid Hourly_Emps Contract_Emps Database Management Systems, R. Ramakrishnan and J. Gehrke 29 Translating ISA Hierarchies to Relations General approach: 3 relations:, Hourly_Emps and Contract_Emps. Hourly_Emps: Every employee is recorded in. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ); must delete Hourly_Emps tuple if referenced tuple is deleted). Queries involving all employees easy, those involving just Hourly_Emps require a join to get some attributes. Alternative: Just Hourly_Emps and Contract_Emps. Hourly_Emps:,,, hourly_wages, hours_worked. Each employee must be in one of these two subclasses. Database Management Systems, R. Ramakrishnan and J. Gehrke 30

Destroying and Altering Relations DROP TABLE Students Destroys the relation Students. The schema information and the tuples are deleted. ALTER TABLE Students ADD COLUMN firstyear: integer The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. Database Management Systems, R. Ramakrishnan and J. Gehrke 31 Adding and Deleting Tuples Can insert a single tuple using: INSERT INTO Students (sid,, login, age, gpa) VALUES (53688, Smith, smith@ee, 18, 3.2) Can delete all tuples satisfying some condition (e.g., = Smith): DELETE FROM Students S WHERE S. = Smith Powerful variants of these commands are available; more later! Database Management Systems, R. Ramakrishnan and J. Gehrke 32 Relational Model: Summary A tabular representation of data. Simple and intuitive, currently the most widely used. Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks for violations. Two important ICs: primary and foreign keys In addition, we always have domain constraints. Rules to translate ER to relational model Database Management Systems, R. Ramakrishnan and J. Gehrke 33