CSC 443 Database Management Systems. Database Design

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

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

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

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

Conceptual Design Using the Entity-Relationship (ER) Model

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

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

Review: Participation Constraints

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

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

Lecture 6. SQL, Logical DB Design

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

The Entity-Relationship Model

Data Modeling. Database Systems: The Complete Book Ch ,

2. Conceptual Modeling using the Entity-Relationship Model

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

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

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

The Entity-Relationship Model

Entity Relationship Diagram

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

SQL. Ilchul Yoon Assistant Professor State University of New York, Korea. on tables. describing schema. CSE 532 Theory of Database Systems

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

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

Databases and BigData

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

The Entity-Relationship Model

Winter Winter

Exercise 1: Relational Model

CSC 742 Database Management Systems

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

The Relational Data Model: Structure

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

XV. The Entity-Relationship Model

IV. The (Extended) Entity-Relationship Model

Database Design Process

Modern Systems Analysis and Design

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

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

IT2305 Database Systems I (Compulsory)

Data Modeling Basics

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Database Design Process

Entity-Relationship Model

IT2304: Database Systems 1 (DBS 1)

Databases What the Specification Says

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

DATABASE MANAGEMENT SYSTEMS. Question Bank:

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

CSCI-GA Database Systems Lecture 7: Schema Refinement and Normalization

Part 7: Object Oriented Databases

CSC 443 Data Base Management Systems. Basic SQL

The Relational Data Model and Relational Database Constraints

2. Supports eæcient access to very large amounts


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

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

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

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

The Relational Model. Why Study the Relational Model?

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

Lecture 12: Entity Relationship Modelling

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

Foundations of Information Management

ER & EER to Relational Mapping. Chapter 9 1

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

CHAPTER 3: DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

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

three Entity-Relationship Modeling chapter OVERVIEW CHAPTER

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

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

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

CSC 443 Fall 2011 Dr. R. M. Siegfried. Answers to Assignment #1

SQL NULL s, Constraints, Triggers

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

3. Relational Model and Relational Algebra

EECS 647: Introduction to Database Systems

Fundamentals of Database System

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

Chapter 8 The Enhanced Entity- Relationship (EER) Model

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

Basic Concepts of Database Systems

CS411 Database Systems

Data Analysis 1. SET08104 Database Systems. Napier University

Preview DESIGNING DATABASES WITH VISIO PROFESSIONAL: A TUTORIAL

Database Management Systems. Chapter 1

Database IST400/600. Jian Qin. A collection of data? A computer system? Everything you collected for your group project?

Chapter 2: Entity-Relationship Model

DATABASE INTRODUCTION

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

ER modelling, Weak Entities, Class Hierarchies, Aggregation

Database Design Methodology

Rose Data Modeler (logical)

6.830 Lecture PS1 Due Next Time (Tuesday!) Lab 1 Out today start early! Relational Model Continued, and Schema Design and Normalization

Database Design Process. ER Model. Goals. Relational Model. SQL - The Language of Databases. IT420: Database Management and Organization

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

Database Design and the E-R Model

Conceptual Design: Entity Relationship Models. Objectives. Overview

CPS352 Database Systems: Design Project

Transcription:

CSC 443 Database Management Systems Lecture 4 Conceptual Modeling Database Design Goal: specification of database schema Methodology: Use E-R model to get a high-level graphical view of essential components of enterprise and how they are related Convert E-R diagram to DDL E-R Model: enterprise is viewed as a set of Entities Relationships among entities 1

Entities Entity: an object that is involved in the enterprise Ex: John, CSE305 Entity Type: set of similar objects Ex: students, courses Attribute: describes one aspect of an entity type Ex: name, maximum enrollment Entity Type Entity type described by set of attributes Person: Id, Name, Address, Hobbies Domain: possible values of an attribute Value can be a set (in contrast to relational model) (111111, John, 123 Main St, {stamps, coins}) Key: minimum set of attributes that uniquely identifies an entity (candidate key) Entity Schema: entity type name, attributes (and associated domain), key constraints 2

Entity Type (continued) Graphical Representation in E-R diagram: Set valued Relationships Relationship: relates two or more entities John majors in Computer Science Relationship Type: set of similar relationships Student (entity type) related to Department (entity type) by MajorsIn (relationship type). Distinction: relation (relational model) - set of tuples relationship (E-R Model) describes relationship between entities of an enterprise Both entity types and relationship types (E-R model) may be represented as relations (in the relational model) 3

Attributes and Roles Attribute of a relationship type describes the relationship e.g., John majors in CS since 2000 John and CS are related 2000 describes relationship - value of SINCE attribute of MajorsIn relationship type Role of a relationship type names one of the related entities e.g., John is value of Student role, CS value of Department role of MajorsIn relationship type (John, CS; 2000) describes a relationship Relationship Type Described by set of attributes and roles e.g., MajorsIn: Student, Department, Since Here we have used as the role name (Student) the name of the entity type (Student) of the participant in the relationship, but... 4

Roles Problem: relationship can relate elements of same entity type e.g., ReportsTo relationship type relates two elements of Employee entity type: Bob reports to Mary since 2000 We do not have distinct names for the roles It is not clear who reports to whom Roles (continued) Solution: role name of relationship type need not be same as name of entity type from which participants are drawn ReportsTo has roles Subordinate and Supervisor and attribute Since Values of Subordinate and Supervisor both drawn from entity type Employee 5

Schema of a Relationship Type Role names, R i, and their corresponding entity sets. Roles must be single valued (number of roles = degree of relationship) Attribute names, A j, and their corresponding domains. Attributes may be set valued Key: Minimum set of roles and attributes that uniquely identify a relationship Relationship: <e 1, e n ; a 1, a k > e i is an entity, a value from R i s entity set a j is a set of attribute values with elements from domain of A j Graphical Representation Roles are edges labeled with role names (omitted if role name = name of entity set). Most attributes have been omitted. 6

Single-role Key Constraint If, for a particular participant entity type, each entity participates in at most one relationship, corresponding role is a key of relationship type E.g., Professor role is unique in WorksIn Representation in E-R diagram: arrow Professor WorksIn Department Entity Type Hierarchies One entity type might be subtype of another Freshman is a subtype of Student A relationship exists between a Freshman entity and the corresponding Student entity e.g., Freshman John is related to Student John This relationship is called IsA Freshman IsA Student The two entities related by IsA are always descriptions of the same real-world object 7

IsA Student IsA Represents 4 relationship types Freshman Sophmore Junior Senior Properties of IsA Inheritance - Attributes of supertype apply to subtype. E.g., GPA attribute of Student applies to Freshman Subtype inherits all attributes of supertype. Key of supertype is key of subtype Transitivity - Hierarchy of IsA Student is subtype of Person, Freshman is subtype of Student, so Freshman is also a subtype of Student 8

Advantages of IsA Can create a more concise and readable E-R diagram Attributes common to different entity sets need not be repeated They can be grouped in one place as attributes of supertype Attributes of (sibling) subtypes can be different IsA Hierarchy - Example 9

Constraints on Type Hierarchies Might have associated constraints: Covering constraint: Union of subtype entities is equal to set of supertype entities Employee is either a secretary or a technician (or both) Disjointness constraint: Sets of subtype entities are disjoint from one another Freshman, Sophomore, Junior, Senior are disjoint set Participation Constraint If every entity participates in at least one relationship, a participation constraint holds: A participation constraint of entity type E having role ρ in relationship type R states that for e in E there is an r in R such that ρ(r) = e. e.g., every professor works in at least one department Reprsentation in E-R Professor WorksIn Department 10

Participation and Key Constraint If every entity participates in exactly one relationship, both a participation and a key constraint hold: e.g., every professor works in exactly one department E-R representation: thick line Professor WorksIn Department Representation of Entity Types in the Relational Model An entity type corresponds to a relation Relation s attributes = entity type s attributes Problem: entity type can have set valued attributes, e.g., Person: Id, Name, Address, Hobbies Solution: Use several rows to represent a single entity (111111, John, 123 Main St, stamps) (111111, John, 123 Main St, coins) Problems with this solution: Redundancy Key of entity type (Id) not key of relation Hence, the resulting relation must be further transformed 11

Representation of Relationship Types in the Relational Model Typically, a relationship becomes a relation in the relational model Attributes of the corresponding relation are Attributes of relationship type For each role, the primary key of the entity type associated with that role Example: SectNo CrsCode Enroll RoomNo DeptId Name S2000Courses Teaching S2000Courses (CrsCode, SectNo, Enroll) TAs Professor (Id, DeptId, Name) Teaching (CrsCode, SecNo, Id, RoomNo, TAs) Professor Id Representation of Relationship Types in the Relational Model Candidate key of corresponding table = candidate key of relation Except when there are set valued attributes Example: Teaching (CrsCode, SectNo, Id, RoomNo, TAs) Key of relationship type = (CrsCode, SectNo) Key of relation = (CrsCode, SectNo, TAs) CrsCode SectNo Id RoomNo TAs CSE305 1 1234 Hum 22 Joe CSE305 1 1234 Hum 22 Mary Set valued 12

Representation in SQL Each role of relationship type produces a foreign key in corresponding relation Foreign key references table corresponding to entity type from which role values are drawn Example 1 Since Status Professor WorksIn Department CREATE TABLE WorksIn ( Since DATE, -- attribute Status CHAR (10), -- attribute ProfId INTEGER, -- role (key of Professor) DeptId CHAR (4), -- role (key of Department) PRIMARY KEY (ProfId), -- since a professor works in at most one department FOREIGN KEY (ProfId) REFERENCES Professor (Id), FOREIGN KEY (DeptId) REFERENCES Department ) 13

Example 2 Date Price Project Sold Part Supplier CREATE TABLE Sold ( Price INTEGER, -- attribute Date DATE, -- attribute ProjId INTEGER, -- role SupplierId INTEGER, -- role PartNumber INTEGER, -- role PRIMARY KEY (ProjId, SupplierId, PartNumber, Date), FOREIGN KEY (ProjId) REFERENCES Project, FOREIGN KEY (SupplierId) REFERENCES Supplier (Id), FOREIGN KEY (PartNumber) REFERENCES Part (Number) ) Representation of Single Role Key Constraints in the Relational Model Relational model representation: key of the relation corresponding to the entity type is key of the relation corresponding to the relationship type Id is primary key of Professor; ProfId is key of WorksIn. Professor 4100 does not participate in the relationship. Cannot use foreign key in Professor to refer to WorksIn since some professors may not work in any dept. (But ProfId is a foreign key in WorksIn that refers to Professor.) Professor Professor Id 1123 4100 3216 WorksIn ProfId 1123 CSE 3216 AMS WorksIn Department Key 14

Representing Type Hierarchies in the Relational Model Supertypes and subtypes can be realized as separate relations Need a way of identifying subtype entity with its (unique) related supertype entity Choose a candidate key and make it an attribute of all entity types in hierarchy Type Hierarchies and the Relational Model Translated by adding the primary key of supertype to all subtypes. Plus foreign key from subtypes to the supertype. Id attribs0 Student Id attribs1 Id attribs2 Id attribs3 Id attribs4 Freshman Sophmore Junior Senior FOREIGN KEY Id REFERENCES Student in Freshman, Sophomore, Sunior, Senior 15

Type Hierarchies and the Relational Model Redundancy eliminated if IsA is not disjoint For individuals who are both employees and students, Name and DOB are stored only once Person Employee Student SSN Name DOB SSN Department Salary SSN GPA StartDate 1234 Mary 1950 1234 Accounting 35000 1234 3.5 1997 Type Hierarchies and the Relational Model Other representations are possible in special cases, such as when all subtypes are disjoint 16

Representing Participation Constraints in the Relational Model Professor WorksIn Department Inclusion dependency: Every professor works in at least one dep t. in the relational model: (easy) Professor (Id) references WorksIn (ProfId) in SQL: Simple case: If ProfId is a key in WorksIn (i.e., every professor works in exactly one department) then it is easy: FOREIGN KEY Id REFERENCES WorksIn (ProfId) General case ProfId is not a key in WorksIn, so can t use foreign key constraint (not so easy): CREATE ASSERTION ProfsInDepts CHECK ( NOT EXISTS ( SELECT * FROM Professor P WHERE NOT EXISTS ( SELECT * FROM WorksIn W WHERE P.Id = W.ProfId ) ) ) Representing Participation Constraint in the Relational Model Example (can t use foreign key in Professor if ProfId is not a candidate key in WorksIn) Professor Id 1123 4100 3216 ProfId 1123 CSE 1123 AMS 4100 ECO 3216 AMS WorksIn ProfId not a candidate key 17

Representing Participation and Key Constraint in SQL If both participation and key constraints apply, use foreign key constraint in entity table (but beware: if candidate key in entity table is not primary, presence of nulls violates participation constraint). CREATE TABLE Professor ( Id INTEGER, PRIMARY KEY (Id), -- Id can t be null FOREIGN KEY (Id) REFERENCES WorksIn (ProfId) --all professors participate ) Professo r WorksI n Departme nt Participation and Key Constraint in the Relational Model Example: xxxxxx 1123 yyyyyy 4100 zzzzzzz 3216 Professor Id ProfId 1123 CSE 4100 ECO 3216 AMS WorksIn 18

Participation and Key Constraint in Relational Model (again) Alternative solution if both key and participation constraints apply: merge the tables representing the entity and relationship sets Since there is a 1-1 and onto relationship between the rows of the entity set and the relationship sets, might as well put all the attributes in one table Participation and Key Constraint in Relational Model Example Name Id DeptId xxxxxxx 1123 CSE yyyyyyy 4100 ECO zzzzzzzz 3216 AMS Prof_WorksIn 19

Entity or Attribute? Sometimes information can be represented as either an entity or an attribute. Student Transcript Course Grade Semester Student Transcript Course Semester Grade Appropriate if Semester has attributes (next slide) Entity or Relationship? 20

(Non-) Equivalence of Diagrams Transformations between binary and ternary relationships. Date Part Project Sold Price Supplier 21