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



Similar documents
Lecture 6. SQL, Logical DB Design

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

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

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

The Relational Model. Why Study the Relational Model?

Winter Winter

Conceptual Design Using the Entity-Relationship (ER) Model

Review: Participation Constraints

EECS 647: Introduction to Database Systems

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

The Entity-Relationship Model

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

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

The Entity-Relationship Model

2. Conceptual Modeling using the Entity-Relationship Model

Data Modeling. Database Systems: The Complete Book Ch ,

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

Database Management Systems. Chapter 1

CSC 742 Database Management Systems

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

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

SQL NULL s, Constraints, Triggers

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

Overview. Introduction to Database Systems. Motivation... Motivation: how do we store lots of data?

3. Relational Model and Relational Algebra

Exercise 1: Relational Model

Database Design. Goal: specification of database schema Methodology: E-R Model is viewed as a set of

Database Systems. Lecture 1: Introduction

Databases and BigData

Entity Relationship Diagram

The Entity-Relationship Model

Introduction to Database Systems CS4320/CS5320. CS4320/4321: Introduction to Database Systems. CS4320/4321: Introduction to Database Systems

Topics. Introduction to Database Management System. What Is a DBMS? DBMS Types

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

DATABASE DESIGN. - Developing database and information systems is performed using a development lifecycle, which consists of a series of steps.

SQL: Queries, Programming, Triggers

The Relational Data Model and Relational Database Constraints

Database Design Process. Databases - Entity-Relationship Modelling. Requirements Analysis. Database Design

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

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

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

Lecture 12: Entity Relationship Modelling

Relational Database Concepts

Entity-Relationship Model

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #5: En-ty/Rela-onal Models- - - Part 1

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

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

THE ENTITY- RELATIONSHIP (ER) MODEL CHAPTER 7 (6/E) CHAPTER 3 (5/E)

How To Manage Data In A Database System

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Chapter 6: Integrity Constraints

Chapter 7 Data Modeling Using the Entity- Relationship (ER) Model

IT2305 Database Systems I (Compulsory)

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

DataBase Management Systems Lecture Notes

Introduction to Computing. Lectured by: Dr. Pham Tran Vu

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

CMPT 354 Database Systems. Simon Fraser University Summer Instructor: Oliver Schulte

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

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

SQL DDL. DBS Database Systems Designing Relational Databases. Inclusion Constraints. Key Constraints

Data Modeling Basics

ECS 165A: Introduction to Database Systems

Lesson 8: Introduction to Databases E-R Data Modeling

Overview. Physical Database Design. Modern Database Management McFadden/Hoffer Chapter 7. Database Management Systems Ramakrishnan Chapter 16

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Modern Systems Analysis and Design


Database Design. Adrienne Watt. Port Moody

Fundamentals of Database Design

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL THIRD EDITION

CSC 443 Data Base Management Systems. Basic SQL

SQL: Queries, Programming, Triggers

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

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

IT2304: Database Systems 1 (DBS 1)

Ch.5 Database Security. Ch.5 Database Security Review

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

CMU - SCS / Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013

7. Databases and Database Management Systems

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

Lecture Notes INFORMATION RESOURCES

The core theory of relational databases. Bibliography

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

DATABASE MANAGEMENT SYSTEMS. Question Bank:

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

A brief overview of developing a conceptual data model as the first step in creating a relational database.

Relational Database Design: FD s & BCNF

Chapter 2: Entity-Relationship Model

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

BİL 354 Veritabanı Sistemleri. Entity-Relationship Model

The Relational Data Model: Structure

Foundations of Business Intelligence: Databases and Information Management

IV. The (Extended) Entity-Relationship Model

Foundations of Information Management

- Suresh Khanal. Microsoft Excel Short Questions and Answers 1

EXTENDED LEARNING MODULE A

Transcription:

Outline Data Modeling Yanlei Diao UMass Amherst v Conceptual Design: ER Model v Relational Model v Logical Design: from ER to Relational Slides Courtesy of R. Ramakrishnan and J. Gehrke 1 2 Conceptual Design v Start with application requirements v Use Entity-Relationship (ER) Model: Entities and relationships in the enterprise Integrity constraints (or business rules) that hold Pictorially represented by an ER diagram ER Model Basics: Entities v Entity: A real-world object. Described using a set of attributes. v Entity Set: A collection of entities described by the same set of attributes. Domain of an attribute. Key of an entity set: minimum set of attributes that uniquely identify each entity in the set. 3 4 ER Model Basics: Relationships Ternary Relationships d d v Relationship: Association between 2+ entities. E.g., Joe works in the accounting dept 01/2008. v Relationship Set: Collection of similar relationships. address Locations capacity v A relationship involves: an employee a department a location 5 6

More on Relationships Key Constraints subordinate supervisor Reports_To v An entity set can participate in same relationship set, but in different roles. v : an employee can work in many depts; a dept can have many employees many-to-many v Manages: each dept is managed by at most one manager key constraint on Manages ( ) one-to-many d d Manages 7 8 Participation Constraints Weak Entities Manages d cost p age Policy Dependents v : every employee works in at least one dept (or, every employee must work for some dept) Participation constraint on Works_in (denoted using a thick line) Participation of in is total (vs. partial). v Key and participation constraints: exactly one 9 Does an entity set always have a key? v A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. A weak entity must have an exactly one relationship with its owner. Notation: see the example. 10 ISA ( is a ) Hierarchies Issues with ISA Hierarchies hourly_wages hours_worked Hourly_Emps Contract_Emps v It is sometimes natural to classify entities into subclasses. v Y ISA X: every Y entity is also considered to be an X entity. Y entity set inherits all attributes of X entity set. Y entity set has its own descriptive attributes. ISA contractid 11 v Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? Allowed/disallowed v Covering constraints: Does every entity have to be an Hourly_Emps or a Contract_Emps entity? Yes/no v Reasons for using ISA: Add descriptive attributes specific to a subclass. Identify entities that participate in a specific relationship. 12

Useful things to know about ER v When reading application requirements: Entities, attributes are often extracted from nouns Entities vs. attributes: should address be an attribute of or an entity? Sub-structure? Participation in other relationships? Relationships are often from verbs Outline v Conceptual Design: ER Model v Relational Model v Logical Design: from ER to Relational v Software tools for ER modeling Word on both Mac and Windows Microsoft Visio for Windows OmniGraffle for Mac Draw.io (Google) 13 14 Relational Model v A relational database is a set of relations. v A relation has: Schema : specifies (1) of relation, (2) and domain of each attribute. E.g. Students(sid:string, :string, login:string, age:integer, gpa:real) Instance : a table with rows (tuples) and columns (attributes, fields). cardinality = #rows, degree / arity = #columns. v Relation is a set of tuples (in theory). All rows must be distinct, no duplicates. Example Instance of Students Relation STUDENTS 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 v Cardinality = 3, degree = 5 v All rows are distinct. v Some columns of two rows can be the same. 15 16 Creating Relations in SQL Destroying and Altering Relations v Create the Students relation Specify schema Domain constraint: type of each attribute later enforced by the DBMS upon tuple insertion or update. CREATE TABLE Students (sid CHAR(20), CHAR(20), login CHAR(10), age INTEGER, gpa REAL); (sid CHAR(20), grade CHAR(2) ); DROP TABLE Students; v Destroy the relation Students. ALTER TABLE Students ADD COLUMN firstyear: integer; v Alter the Students relation by adding a new field. Every tuple in the current instance is extended with a null value in the new field. 17 18

Integrity Constraints v Integrity Constraints (IC s): condition that must be true for any instance of the database. Domain constraint Primary key constraint Foreign key constraint Specified when schema is defined. Primary Key Constraints v Key of a relation: minimum set of attributes that uniquely identify each entity. 1. No two tuples can have same values in all key fields. 2. This is not true for any subset of the key. Part 2 false? A superkey. If more than 1 key for a relation, canate keys. One of canate keys is chosen to be the primary key. v E.g., Students(sid,, login, age, gpa) v E.g., Enrolled (sid, cid, grade) 19 20 Primary and Canate Keys in SQL 1. Specify canate keys using UNIQUE. 2. Choose one canate key as the primary key. For a given student and course, there is a single grade. and no two students in a course receive the same grade. ( sid CHAR(20), PRIMARY KEY (sid,cid)); ( sid CHAR(20), PRIMARY KEY (sid,cid), Foreign Keys v Foreign key: set of attributes used to refer to the primary key of another relation. v E.g., Enrolled(sid: string, cid: string, grade: string): sid is a foreign key referring to sid in Students. Enrolled sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Students 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 UNIQUE (cid, grade) ); 21 22 Foreign Keys in SQL ( sid CHAR(20), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students(sid) ); Referential Integrity v Referential integrity: Any foreign key value must have a matching primary key value in the referenced reln. E.g., every sid value in Enrolled must appear in Students. No dangling references. v In contrast, consider links in HTML. Does referential integrity hold? 23 24

Enforcing Referential Integrity v What if an Enrolled tuple with a non-existent student id is inserted to the DB? Reject it! v What if a Students (referenced) tuple is deleted? CASCADE: delete all Enrolled tuples that refer to it. NO ACTION: disallow if the Students tuple is referred to. SET DEFAULT: set the foreign key to a default sid. SET NULL: set the foreign key to a special value null, denoting unknown or inapplicable. Enforcing Referential Integrity v Updates to sid in Students are treated similarly. CASCADE: propagate the update to Enrolled NO ACTION: disallow the update in Students SET DEFAULT: set the sid in Enrolled to a default value SET NULL: set the sid in Enrolled to null 25 26 Referential Integrity in SQL (sid CHAR(20), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students(sid) ON DELETE NO ACTION ON UPDATE CASCADE); Comments on Integrity Constraints v IC s are based on real-world business logic. Can check violation in a database instance, but can never infer an IC by looking at an instance. An IC is a statement about all possible instances! E.g., of Students can be unique in an instance. v IC s are specified when defining the schema (CREATE TABLE). v DBMS later enforces IC s. Stored data is faithful to the real-world meaning. Avoids data entry errors, too! 27 28 Outline Logical DB Design: ER to Relational v Conceptual Design: ER Model v Relational Model v Logical Design: from ER to Relational v An entity set is translated to a table. CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY ()); 29 30

Relationship Sets to Tables Review: Key Constraints d v Key constraint: Each dept is managed by at most one manager. v Each relationship set is also translated to a table with all descriptive attributes, primary key of each related entity set as a foreign key. v All foreign keys form a superkey of the relation. CREATE TABLE ( CHAR(11), INTEGER, DATE, PRIMARY KEY (, ), FOREIGN KEY () REFERENCES (), FOREIGN KEY () REFERENCES ()); 31 Manages d 32 Translating ER Diagrams w. Key Constraints v A separate table for Manages: Borrow primary key from the entity with the key constraint. CREATE TABLE Manages( CHAR(11), INTEGER, DATE, PRIMARY KEY (), FOREIGN KEY () REFERENCES (), FOREIGN KEY () REFERENCES ()); Review: Participation Constraints d lot Manages v Merge Manages into : Merge the relationship into the entity with the key constraint. CREATE TABLE Dept_Mgr( INTEGER, d CHAR(20), REAL, CHAR(11), DATE, PRIMARY KEY (), FOREIGN KEY () REFERENCES ); v Participation constraint: Every employee works in at least one dept. Each Dept has at least one employee. v Participation + Key constraints: Every department must have one manager. 33 34 Key and Participation Constraints v If we have key + participation constraints (exactly one): CREATE TABLE Dept_Mgr( INTEGER, d CHAR(20), REAL, CHAR(11) NOT NULL, DATE, PRIMARY KEY (), FOREIGN KEY () REFERENCES () ON DELETE NO ACTION ON UPDATE CASCADE); v For participation constraints only, need to resort to assertions (dynamic checks in SQL). More in SQL lecture Review: Weak Entities lot cost Policy p Dependents v A weak entity can be identified uniquely only by considering the primary key of the owner entity. Must have an exactly one relationship with its owner. age 35 36

Translating Weak Entity Sets Review: ISA Hierarchies v Merge weak entities and identifying relationships in one table. What is the primary key? What if the owner entity is deleted? CREATE TABLE Depndt_Policy ( p CHAR(20), age INTEGER, cost REAL, CHAR(11), PRIMARY KEY (, p, age), FOREIGN KEY () REFERENCES () ON DELETE CASCADE ON UPDATE CASCADE); hourly_wages hours_worked ISA contractid Hourly_Emps Contract_Emps 37 38 Translating ISA Hierarchies to Relations 1. Create tables for both parent and child entities : (,, lot) Hourly_Emps: (, hourly_wages, hours_worked) is both primary and foreign key! Must delete Hourly_Emp if referenced Emp is deleted. 2. Create tables only for child entities Hourly_Emps: (,, lot, hourly_wages, hours_worked). Each employee must be in one of these two subclasses. 3. Create a table only for the parent entity Which one is better for: covering constraint (Y/N), overlap constraint (Y/N)? 39