ER to Relational Model. Professor Jessica Lin

Similar documents
Roadmap of This Lecture. Weak Entity Sets Extended E-R Features Reduction to Relation Schemas Database Design UML*

Chapter 7: Entity-Relationship Model

Chapter 7: Entity-Relationship Model

Chapter 7: Entity-Relationship Model. Chapter 7: Entity-Relationship Model

The En'ty Rela'onship Model

Chapter 7: Entity-Relationship Model

Database Design and the E-R Model (7.4, )

Chapter 7: Entity-Relationship Model

Unit I. By Prof.Sushila Aghav MIT

Lecture 10 - Chapter 7 Entity Relationship Model

CS425 Fall 2013 Boris Glavic Chapter 7: Entity-Relationship Model!

Unit1: Introduction. Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Other Relational Languages

The Relational Model. Week 2

Entity-Relationship Model

Chapter 6: Entity-Relationship Model

Chapter 7: Entity-Relationship Model. CS425 Fall 2016 Boris Glavic. Chapter 7: Entity-Relationship Model. Database Design.

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Entity-Relationship Modelling. Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables

Chapter 3: Introduction to SQL

Chapter 6: Entity-Relationship Model

The SQL data-definition language (DDL) allows defining :

Unit 3 : Relational Database Design

Chapter 3: Introduction to SQL

Chapter 2: Entity-Relationship Model

Chapter 8: Relational Database Design

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

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

CSCB20. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 4: Intermediate SQL

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

The Next Step: Designing DB Schema. Chapter 6: Entity-Relationship Model. The E-R Model. Identifying Entities and their Attributes.

Chapter 2 Introduction to Relational Models

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

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

QQ Group

Chapter 4: Intermediate SQL

CSIT5300: Advanced Database Systems

CS W Introduction to Databases Spring Computer Science Department Columbia University

How to translate ER Model to Relational Model

Chapter 6: Entity-Relationship Model. The Next Step: Designing DB Schema. Identifying Entities and their Attributes. The E-R Model.

Relational Database Design (II)

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

ENTITY-RELATIONSHIP MODEL III. CS121: Relational Databases Fall 2017 Lecture 16

Database Design Process

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

Relational model and basic SQL

CS425 Midterm Exam Summer C 2012

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Lecture 6 - More SQL

MySQL Views & Comparing SQL to NoSQL

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

FINAL EXAM REVIEW. CS121: Introduction to Relational Database Systems Fall 2018 Lecture 27

The Relational Model 2. Week 3

Conceptual Database Design. COSC 304 Introduction to Database Systems. Entity-Relationship Modeling. Entity-Relationship Modeling

Homework Assignment 3. November 9th, 2017 Due on November 23th, 11:59pm (midnight) CS425 - Database Organization Results

Chapter 6: Entity-Relationship Model. E-R Diagrams

Systems Analysis and Design in a Changing World, Fourth Edition. Chapter 12: Designing Databases

Database Management Systems,

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

COSC 304 Introduction to Database Systems. Entity-Relationship Modeling

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

Database Management Systems

SE 4DB3 TUTORIAL 2: REDUCTION TO RELATIONAL SCHEMAS

Chapter 9: Relational DB Design byer/eer to Relational Mapping Relational Database Design Using ER-to- Relational Mapping Mapping EER Model

Information Systems (Informationssysteme)

Database Systems ( 資料庫系統 )

CSCC43H: Introduction to Databases

GUJARAT TECHNOLOGICAL UNIVERSITY

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

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

ER to Relational Mapping

High-Level Database Models (ii)

Unit I. Introduction to Database. Prepared By: Prof.Sushila Aghav. Ref.Database Concepts by Korth

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

High Level Database Models

SQL DDL. CS3 Database Systems Weeks 4-5 SQL DDL Database design. Key Constraints. Inclusion Constraints

CS40 Exam #1 October 17, 2001

Database Design Process. Requirements Collection & Analysis

CSCB20 Week 3. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Part 5: Introduction to Logical Design

Translating an ER Diagram to a Relational Schema

CS40 Exam #2 November 13, 2002

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

Data about data is database Select correct option: True False Partially True None of the Above

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

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

U1. Data Base Management System (DBMS) Unit -1. MCA 203, Data Base Management System

Handout 4. Logical Database Modeling, Part 1: Relational Data Model. Transforming EER model to Relational.

Chapter 6: Entity-Relationship Model

CS 405G: Introduction to Database Systems

Explain in words what this relational algebra expression returns:

Chapter 4: Intermediate SQL

DATABASE MANAGEMENT SYSTEMS

Relational Algebra. Procedural language Six basic operators

E-R Diagram to Relational Schema

Entity-Relationship Model &

Database System Concepts

Transcription:

ER to Relational Model Professor Jessica Lin 1

Reduction to Relation Schemas Entity sets and relationship sets can be expressed uniformly as relation schemas that represent the contents of the database. A database which conforms to an E-R diagram can be represented by a collection of schemas. For each entity set and relationship set there is a unique schema that is assigned the of the corresponding entity set or relationship set. Each schema has a number of columns (generally corresponding to attributes), which have unique s.

Translating ER diagrams into Relations We need to figure out how to translate ER diagrams into relations. There are three cases to worry about. Strong entity sets Weak entity sets Relationship sets instructor student salary advisor tot_cred course course_id title credits sec_course section sec_id semester year 3

Strong entity sets instructor salary advisor student tot_cred instructor(: string, : string, salary: integer) This is trivial, the primary key of the ER diagram becomes the primary key of the relation. All other fields are copied in (in any order) salary 10101 Srinivasan 65000 12121 Wu 90000 15151 Mozart 40000 22222 Einstein 95000

Strong entity sets instructor salary advisor student tot_cred student(: string, : string, tot_cred: integer) This is trivial, the primary key of the ER diagram becomes the primary key of the relation. All other fields are copied in (in any order) tot_cred 98988 Tanaka 60 99001 Smith 51 90021 Jackson 12 95012 Lincoln 96

Relationship entity sets instructor salary For one-to-one relationship sets, the relation s primary key can be that of either entity set. advisor student tot_cred The imported key from both entity sets is called the foreign key. For many-to-many relationship sets, the union of the primary keys becomes the relation s primary key For many-to-one or one-tomany relationship sets, the relation s primary key is the primary key of the many side of the relationship set. advisor(i_: string, s_: string) i_ s_ 10101 98988 10101 99001 12121 98988 6

Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to refer to a tuple in another relation. (Usually correspond to primary key of the second relation.) Like a `logical pointer. e.g. s_ is a foreign key referring to student(): advisor(i_: string, s_: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references. Can you a data model w/o referential integrity? 7

8

So, this ER Model instructor salary advisor student tot_cred maps to this database schema instructor(: string, : string, salary: integer) student(: string, : string, tot_cred: integer) advisor(i_: string, s_: string) 9

Suppose we add a constraint that a student can have at most one advisor instructor salary For one-to-one relationship sets, the relation s primary key can be that of either entity set. advisor student tot_cred The imported key from both entity sets is called the foreign key. For many-to-many relationship sets, the union of the primary keys becomes the relation s primary key. For many-to-one or one-tomany relationship sets, the relation s primary key is the primary key of the many side of the relationship set. advisor(s_: string, i_: string) s_ i_ 98988 10101 99001 12121 90021 10101 10

The modified ER model instructor salary advisor student tot_cred maps to this database schema instructor(: string, : string, salary: integer) student(: string, : string, tot_cred: integer) advisor(s_: string, i_: string) Later we ll see how we can take advantage of the key constraint and come up with a better design. 11

Let s look at another example that involves a weak entity set. course course_id title credits sec_course section sec_id semester year course(course_id : string, title: string, credits: integer) course_id title credits CS450 Database Concepts 3 CS484 Data Mining 3 Strong entity set: trivial case CS310 Data Structures 3 CS101 Preview to CS 2

Let s look at another example that involves a weak entity set. course course_id title credits sec_course section sec_id semester year section(course_id : string, sec_id: string, semester: string, year: integer) course_id sec_id semester year CS450 001 Fall 2015 CS450 002 Fall 2015 CS310 001 Fall 2015 CS450 001 Spring 2015 The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set. The imported key from the strong entity set is called the foreign key. All other fields are copied in (in any order)

Relationship entity sets course course_id title credits sec_course section sec_id semester year? A relationship set involving a weak entity set is treated specially. Since sec_course has no descriptiove attributes, the sec_course schema has attributes (course_id, sec_id, semester, year). Every (course_id, sec_id, semester, year) combination in a sec_course relation would also be present in the relation on schema section, and vice versa. => Combine them. 14

So, this ER Model course course_id title credits sec_course section sec_id semester year maps to this database schema course(course_id : string, title: string, credits: integer) section(course_id : string, sec_id: string, semester: string, year: integer) 15

Composite and Multivalued Attributes instructor first_ middle_initial last_ address street street_number street_ apt_number city state zip { phone_number } date_of_birth age ( ) Composite attributes are flattened out by creating a separate attribute for each component attribute Example: given entity set instructor with composite attribute with component attributes first_ and last_ the schema corresponding to the entity set has two attributes _first_ and _last_ Prefix omitted if there is no ambiguity Ignoring multivalued attributes, extended instructor schema is instructor(, first_, middle_initial, last_, street_number, street_, apt_number, city, state, zip_code, date_of_birth)

Composite and Multivalued Attributes A multivalued attribute M of an entity E is represented by a separate schema EM Schema EM has attributes corresponding to the primary key of E and an attribute corresponding to multivalued attribute M Example: Multivalued attribute phone_number of instructor is represented by a schema: inst_phone = (, phone_number) Each value of the multivalued attribute maps to a separate tuple of the relation on schema EM For example, an instructor entity with primary key 22222 and phone numbers 456-7890 and 123-4567 maps to two tuples: (22222, 456-7890) and (22222, 123-4567)

Representing Specialization via Schemas Method 1: Form a schema for the higher-level entity Form a schema for each lower-level entity set, include primary key of higher-level entity set and local attributes schema attributes person,, street, city student, tot_cred employee, salary Drawback?

Representing Specialization as Schemas Method 2: Form a schema for each entity set with all local and inherited attributes schema attributes person,, street, city student,, street, city, tot_cred employee,, street, city, salary If specialization is total, the schema for the generalized entity set (person) not required to store information Can be defined as a view relation containing union of specialization relations But explicit schema may still be needed for foreign key constraints Drawback?

Schemas Corresponding to Aggregation To represent aggregation, create a schema containing primary key of the aggregated relationship the primary key of the associated entity set any descriptive attributes

Schemas Corresponding to Aggregation For example, to represent aggregation manages between relationship works_on and entity set manager, create a schema eval_for (s_, project_id, i_, evaluation_id) Schema proj_guide is redundant provided we are willing to store null values for attribute manager_ in relation on schema manages project instructor proj_ guide student eval_ for evaluation

Practice author address URL publisher address phone URL customer written_by published_by email address phone ISBN title year price book number contains shopping_basket basket_id basket_of number stocks warehouse code address phone 22

We have seen how to create a database schema, how do we create an actual database on our computers? instructor(: string, : string, salary: integer) student(: string, : string, tot_cred: integer) advisor(s_: string, i_: string) 23

how do we create an actual database on our computers? We use SQL, a language that allows us to build, modify and query databases. instructor( : string, : string, salary: real) SQL> CREATE TABLE instructor 2 ( CHAR(5), 3 CHAR(50), 4 salary integer, 5 PRIMARY KEY()); Table created. 24

SQL (Structured Query Language) SQL is a language that allows us to build, modify and query databases. SQL is an ANSI standard language. American National Standards Institute SQL is the engine behind Oracle, Microsoft SQL Server, etc. Most of these systems have built GUIs on top of the command line interface, so you don t normally write statements directly in SQL (although you can). 25

Creating Relations in SQL Creates a student relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. SQL> CREATE TABLE student ( CHAR(5), CHAR(50), tot_cred integer, PRIMARY KEY()); As another example, the advisor table holds information about the advising relationship between instructors and students. SQL> CREATE TABLE advisor (s_ CHAR(5), i_ CHAR(5), PRIMARY KEY(s_)); 26