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

Similar documents
The Relational Model. Chapter 3

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

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

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

The Relational Model 2. Week 3

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

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

Database Applications (15-415)

Relational data model

Database Management Systems. Chapter 3 Part 1

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

Database Systems ( 資料庫系統 )

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

CIS 330: Applied Database Systems

The Relational Data Model. Data Model

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

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

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

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

Database Management Systems. Chapter 3 Part 2

Database Systems. Course Administration

The Relational Model

The Relational Model (ii)

The Relational Model

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

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

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

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

The Relational Model of Data (ii)

The Relational Model. Week 2

The Entity-Relationship Model. Overview of Database Design

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

Database Applications (15-415)

High-Level Database Models (ii)

Introduction to Database Design

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

Database Management Systems. Session 2. Instructor: Vinnie Costa

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

The Entity-Relationship Model

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

Introduction to Databases

Introduction to Database Design

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

ER to Relational Mapping. ER Model: Overview

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

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

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

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

High Level Database Models

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

OVERVIEW OF DATABASE DEVELOPMENT

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

The Entity-Relationship Model

MIS Database Systems Entity-Relationship Model.

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

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

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

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

The Entity-Relationship Model. Steps in Database Design

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

The Entity-Relationship (ER) Model

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

Database Applications (15-415)

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

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

The Relational Model

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

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

The Entity-Relationship Model

Keys, SQL, and Views CMPSCI 645

Introduction to Database Design

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

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

Database Applications (15-415)

The Entity-Relationship (ER) Model 2

Entity-Relationship Diagrams

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

Database Management Systems. Chapter 2 Part 2

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

CSC 261/461 Database Systems Lecture 10

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

SYLLABUS ADMIN DATABASE SYSTEMS I WEEK 2 THE ENTITY-RELATIONSHIP MODEL. Assignment #2 changed. A2Q1 moved to A3Q1

Review: Where have we been?

Conceptual Design. The Entity-Relationship (ER) Model

Database Design CENG 351

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

CSE 530A. ER Model. Washington University Fall 2013

COMP Instructor: Dimitris Papadias WWW page:

Introduction to Data Management. Lecture #3 (Conceptual DB Design)

SQL: The Query Language Part 1. Relational Query Languages

CS275 Intro to Databases

Introduction to Data Management. Lecture #3 (Conceptual DB Design)

The Relational Model Constraints and SQL DDL

CS425 Midterm Exam Summer C 2012

Conceptual Design. The Entity-Relationship (ER) Model

Database Design & Deployment

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

DBMS. Relational Model. Module Title?

Transcription:

Topics Relational Model Linda Wu Relational model SQL language Integrity constraints ER to relational Views (CMPT 354 2004-2) Chapter 3 CMPT 354 2004-2 2 Why Study the Relational Model? Most widely used model Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. Recent competitor: object-oriented model ObjectStore, Versant, Ontos A synthesis emerging: object-relational model Informix Universal Server, UniSQL, O2, Oracle, DB2 Relational Model Relational database: a set of relations A relation is made up of 2 parts Instance: a table, with rows and columns # of Rows = cardinality # of fields = degree / arity Schema: specifies of relation, plus and domain of each column e.g., Students (sid: string, : string, login: string, age: integer, gpa: real) A relation can be thought of as a set of unique rows or tuples Chapter 3 CMPT 354 2004-2 3 Chapter 3 CMPT 354 2004-2 4 1

Relational Model (Cont.) sid login age gpa S0001 Jones jones@cs 18 3.4 S0002 smith@ee 19 3.2 S0003 smith@math 18 3.8 S0004 Mary mary@music 14 1.8 Cardinality = 4, degree = 5, all rows distinct Relational Model (Cont.) A major strength of relational model Supports simple, powerful querying of data Queries can be written intuitively, and the DBMS is responsible for efficient evaluation The key: precise semantics for relational queries Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change Chapter 3 CMPT 354 2004-2 5 Chapter 3 CMPT 354 2004-2 6 SQL Language Query A question about the data Its answer consists of a relation containing the result Query language A specialized language for writing query SQL (Structured Query Language) The standard language for creating, manipulating, and querying data in a relational DBMS Originally developed by IBM (system-r) in the 1970s SQL Language (Cont.) Need for a standard since SQL products are offered by many vendors SQL-86 SQL-89 (minor revision) SQL-92 (major revision) SQL-99 (major extension, current standard) Chapter 3 CMPT 354 2004-2 7 Chapter 3 CMPT 354 2004-2 8 2

SQL Language (Cont.) Querying single relation E.g., to find all 18 year old students: SELECT * FROM Students S WHERE S.age=18 Instance of Students sid S0001 S0002 S0003 S0004 S0005 Jones Mary Gary jones@cs smith@ee smith@math mary@music gary@biz SELECT S., S.login FROM Students S WHERE S.age=18 login age Chapter 3 CMPT 354 2004-2 9 18 19 18 14 12 gpa 3.4 3.2 3.8 1.8 2.0 SQL Language (Cont.) Querying multiple relations SELECT S., E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade= A Instance of Enrolled sid S0002 S0003 S0004 S0005 cid CMPT101 ECON205 BUS310 CMPT250 grade C B A B The query result is: S. Mary E.cid BUS310 Chapter 3 CMPT 354 2004-2 10 SQL Language (Cont.) Creating relations SQL uses the word table to denote relation The subset of SQL that supports the creation, deletion and modification of tables is called Data Definition Language (DDL) The type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified CREATE TABLE Students CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2) ) ( sid CHAR(20), CHAR(20), login CHAR(10), age INTEGER, gpa REAL ) Chapter 3 CMPT 354 2004-2 11 SQL Language (Cont.) Adding and deleting Tuples Insert tuples INSERT INTO Students (sid,, login, age, gpa) VALUES (S0002,, smith@ee, 19, 3.2) OR INSERT INTO Students VALUES (S0002,, smith@ee, 19, 3.2) Delete all tuples satisfying some conditions DELETE FROM Students S WHERE S. = Chapter 3 CMPT 354 2004-2 12 3

SQL Language (Cont.) Modifying tuple values Modify the column values in an existing row UPDATE Students S SET S.age = S.age + 1, S.gpa = S.gpa - 1 WHERE S.sid = S0003 WHERE clause is applied first and determine the rows to modify SET clause then determines how to modify the rows; the column value at the right side of = is the old value SQL Language (Cont.) Destroying and Altering Relations DROP TABLE Students RESTRICT/CASCADE 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 Chapter 3 CMPT 354 2004-2 13 Chapter 3 CMPT 354 2004-2 14 Integrity Constraints Integrity constraint (IC) Condition that must be true for any instance of the database Specified when schema is defined Checked when relations are modified E.g., domain constraints, primary/foreign key constraints A legal instance of a relation is one that satisfies all specified ICs DBMS should not allow illegal instances Chapter 3 CMPT 354 2004-2 15 Primary Key Constraints A set of fields is a (candidate) key for a relation if: 1. No two distinct tuples have same values in all key fields, and, 2. No subset of the key fields is a unique identifier for a tuple Condition 2 false? A superkey If there s more than one key for a relation, one of the keys is chosen (by DBA) to be the primary key Example: in Students relation sid is a key; is not a key The set {sid, gpa} is a superkey Chapter 3 CMPT 354 2004-2 16 4

Primary Key Constraints (Cont.) Specifying key constraints in SQL candidate keys specified using UNIQUE primary key specified using PRIMARY KEY CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid) ) For a given student and course, there is a single grade. CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) Students can take only one course, and receive a single grade for that course; no two students in a course receive the same grade. Chapter 3 CMPT 354 2004-2 17 Primary Key Constraints (Cont.) Enforcing primary key constraints Insertion and update can cause violations Deletion does not cause a violation Examples Insert (S0002, Mike, mike@cs, 20, 2.7) to Students Insert (null, Mike, mike@cs, 20, 2.7) to Students Change sid S0001 to S0002 in the first tuple of Students Chapter 3 CMPT 354 2004-2 18 Foreign Key Constraints Foreign key A set of fields in one relation that is used to refer to a tuple in another relation like a logical pointer The foreign key in the referencing relation must match the primary key of the referenced relation E.g., Enrolled (sid: string, cid: string, grade: string) sid is a foreign key referring to Students If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references Foreign Key Constraints (Cont.) Foreign Keys in SQL CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid), FOREIGN KEY (sid) REFERENCES Students ) Only students listed in the Students relation are allowed to enroll in courses A student has exactly one grade for each course s/he is enrolled in Chapter 3 CMPT 354 2004-2 19 Chapter 3 CMPT 354 2004-2 20 5

cid CMPT101 ECON205 BUS310 CMPT250 Foreign Key Constraints (Cont.) Enforcing Referential Integrity grade C B A B Enrolled Foreign key sid S0002 S0003 S0004 S0005 Primary key sid S0001 S0002 S0003 S0004 S0005 Jones Mary Gary login jones@cs smith@ee smith@math mary@music gary@biz Students age Insert (PHYS110, A, S1234) to Enrolled (rejected!) Delete (S0002,, smith@ee, 19, 3.2) from Students (?) Change sid S005 to S9999 in the last tuple (?) Chapter 3 CMPT 354 2004-2 21 18 19 18 14 12 gpa 3.4 3.2 3.8 1.8 2.0 Foreign Key Constraints (Cont.) A foreign key could refer to the same relation sid S0001 S0002 S0003 S0004 S0005 Jones Mary Gary login jones@cs smith@ee smith@math mary@music gary@biz Students age 18 19 18 14 12 gpa 3.4 3.2 3.8 1.8 2.0 partner S0002 S0001 null null null a foreign key referring to Students Chapter 3 CMPT 354 2004-2 22 Foreign Key Constraints (Cont.) Referential Integrity in SQL: SQL-92/99 support 4 options on DELETE and UPDATE NO ACTION (default) delete/update is rejected CASCADE Also delete/update all tuples that refer to deleted/updated tuple SET NULL Set foreign key value of the referencing tuple to null SET DEFAULT Set foreign key value of the referencing tuple to a default value Foreign Key Constraints (Cont.) CREATE TABLE Enrolled ( sid CHAR(20) DEFAULT S9999, cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT ) Chapter 3 CMPT 354 2004-2 23 Chapter 3 CMPT 354 2004-2 24 6

Where do ICs Come From? ICs are based upon the semantics of the real-world 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! Key and foreign key ICs are the most common More general ICs are supported too Logical DB Design: ER to Relational Entity set Relationship set without constraints Relationship set with key constraints Relationship set with participation constraints Weak entity set Class hierarchy Aggregation Chapter 3 CMPT 354 2004-2 25 Chapter 3 CMPT 354 2004-2 26 Entity sets to Tables Attributes of entity set attributes of table Domain of each attribute Primary key of entity set SSN Name Lot CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY () ) Chapter 3 CMPT 354 2004-2 27 123-22-3666 231-31-5368 131-24-3650 Andrew Emily 44 22 15 Relationship Sets to Tables In translating a relationship set without key or participation constraints to a relation, attributes of the relation must include: The primary key attributes for each participating entity set, as foreign keys This set of attributes forms a superkey for the relation If no key constraints, this set of attributes is a candidate key All descriptive attributes Chapter 3 CMPT 354 2004-2 28 7

Relationship Sets to Tables (Cont.) since Works_In did d Departments budget CREATE TABLE Works_In ( CHAR(1), did INTEGER, since DATE, PRIMARY KEY (, did), FOREIGN KEY () REFERENCES, FOREIGN KEY (did) REFERENCES Departments ) Relationship Sets to Tables (Cont.) supervisor subordinate Reports_To CREATE TABLE Reports_To ( supervisor_ CHAR(11), subordinate_ CHAR(11), PRIMARY KEY (supervisor_, subordinate_), FOREIGN KEY (supervisor_) REFERENCES (), FOREIGN KEY (subordiante_) REFERENCES () ) Chapter 3 CMPT 354 2004-2 29 Chapter 3 CMPT 354 2004-2 30 Translating Relationship Set with Key Constraints Map Manages relationship to a table did is the key now Separate tables for and Departments since Manages d budget CREATE TABLE Manages ( CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY () REFERENCES, FOREIGN KEY (did) REFERENCES Departments ) Chapter 3 CMPT 354 2004-2 31 did Departments Translating Relationship Set with Key Constraints (Cont.) Since each department has a unique manager, we could instead combine Manages and Departments Avoid a distinct table for the relationship set, therefore, fast query Space would be wasted if some departments have no managers (null) CREATE TABLE Dept_Mgr ( did INTEGER, d CHAR(20), budget REAL, CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY () REFERENCES ) Chapter 3 CMPT 354 2004-2 32 8

Translating Relationship Set with Participation Constraints Every department has a manager Use a combined table for Manages and Departments CREATE TABLE Dept_Mgr ( did INTEGER, d CHAR(20), budget REAL, CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY () REFERENCES ON DELETE NO ACTION ) If wish to delete an tuple: first change the Dept_Mgr tuple to have a new employee as manager Chapter 3 CMPT 354 2004-2 33 Translating Relationship Set with Participation Constraints (Cont.) If use a separate table for Manages: Does not ensure that a manager is initially appointed for each department CREATE TABLE Manages ( CHAR(11) NOT NULL, did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY () REFERENCES, FOREIGN KEY (did) REFERENCES Departments ) Combined table (Dept_Mgr) is better than separate table (Manages) for one-to-many relationship, especially when the entity set with key constraint also has a total participation constraint Chapter 3 CMPT 354 2004-2 34 Translating Relationship Set with Participation Constraints (Cont.) We can capture participation constraints involving one entity set in a binary relationship, but little else (without resorting to CHECK constraints) since Works_In did d budget Departments Ensuring total participation of Departments in Works_In Possible way: declares that did in Departments is a foreign key referring to Works_In Problem: this is not a valid FK constraint, since did is not a key in Works_In Solution: assertion Translating Weak Entity Set A weak entity can be identified uniquely only by considering the primary key of another (owner) entity Owner entity set and weak entity set participate in a one-to-many relationship set: 1 owner, many weak entities Weak entity set have total participation in its identifying relationship set 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 Chapter 3 CMPT 354 2004-2 35 Chapter 3 CMPT 354 2004-2 36 9

Translating Weak Entity Set (Cont.) Translating Class Hierarchies cost p age Example: Hourly_Emps and Contract_Emps Policy Dependents CREATE TABLE Dep_Policy ( p CHAR(20), age INTEGER, cost REAL, CHAR(11) NOT NULL, PRIMARY KEY (p, ), FOREIGN KEY () REFERENCES, ON DELETE CASCADE ) hourly_wages hours_worked Hourly_Emps ISA contractid Contract_Emps Chapter 3 CMPT 354 2004-2 37 Chapter 3 CMPT 354 2004-2 38 Translating Class Hierarchies (Cont.) General approach: 3 distinct relations (,, ) All employees are recorded Hourly_Emps (, hourly_wages, hours_worked) is FK referencing Must delete Hourly_Emps tuple if the referenced tuple is deleted Contract_Emps (, contractid) * Queries involving all employees easy * Queries involving just Hourly_Emps require a join with to get some attributes Translating Class Hierarchies (Cont.) Alternative: 2 relations Hourly_Emps: (,,, hourly_wages, hours_worked) Contract_Emps: (,,, contractid) * Each employee must be in one of these two subclasses * Duplication: and are stored twice if an employee is both an Hourly_Emps and a Contract_Emps * Queries involving all employees have to examine two relations Chapter 3 CMPT 354 2004-2 39 Chapter 3 CMPT 354 2004-2 40 10

Translating Aggregation Translating Aggregation (Cont.) Use the standard mapping for a relationship set The primary key attributes of each participating entity set The descriptive attributes of the relationship set Example: Monitors 5 relations:, Projects, Departments, Sponsors, Monitors (, did, pid, until) Sponsors can be dropped if it: Has no descriptive attributes, and, Has total participation in Monitors pid Monitors started_on since pbudget Projects Sponsors until d did budget Departments Chapter 3 CMPT 354 2004-2 41 Chapter 3 CMPT 354 2004-2 42 Translating Ternary Relationship Key constraints Combine Purchaser with Policies Combine Beneficiary with Dependents Participation constraints Lead to NOT NULL constraints Weak entity set Combine Dependents and Beneficiary Purchaser policyid Policies p Beneficiary cost Chapter 3 CMPT 354 2004-2 43 age Dependents Translating Ternary Relationship (Cont.) CREATE TABLE Policies ( policyid INTEGER, cost REAL, CHAR(11) NOT NULL, PRIMARY KEY (policyid), FOREIGN KEY () REFERENCES, ON DELETE CASCADE ) CREATE TABLE Dependents ( p CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (p, policyid), FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE ) Chapter 3 CMPT 354 2004-2 44 11

Views A view is just a relation, but we store a definition of it, rather than a set of tuples CREATE VIEW YoungActiveStudents (, grade) AS SELECT S., E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age < 21 * To drop a view: DROP VIEW command * To drop a table when there is a view on the table: DROP TABLE command with CASCADE option Views (Cont.) View provides support for logical data independence Views provides support for security View can be used to present necessary information (or a summary), while hiding details in underlying relations We can insert a row into a view by inserting a row into its base table An INSERT or UPDATE to the base table may lead to a row not in the view Chapter 3 CMPT 354 2004-2 45 Chapter 3 CMPT 354 2004-2 46 Relational Model: Summary A tabular representation of data Simple and intuitive, 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 Powerful and natural query languages exist Rules to translate ER to relational model Chapter 3 CMPT 354 2004-2 47 12