Relational Database Design: Part I. Introduction to Databases CompSci 316 Fall 2017

Similar documents
Relational Database Design Part I. Announcements (September 5) Relational model: review. CPS 116 Introduction to Database Systems

Relational Database Design Part I. Announcement. Relational model: review. CPS 116 Introduction to Database Systems

Announcement. Relational Database Design Part I. Keys. Relational model: review. Schema vs. data. More examples of keys

Announcements (January 20) Relational Database Design. Database (schema) design. Entity-relationship (E/R) model. ODL (Object Definition Language)

Relational Database Design. Announcements. Database (schema) design. CPS 216 Advanced Database Systems. DB2 accounts have been set up

Database Design Using E/R Model

Relational Database Design Part II. Announcements. E/R model: review. CPS 116 Introduction to Database Systems

Relational Model and Algebra. Introduction to Databases CompSci 316 Fall 2018

CS54100: Database Systems

CSE 530A. ER Model. Washington University Fall 2013

CMPT 354 Database Systems I

Conceptual Design. The Entity-Relationship (ER) Model

Relational Database Design Theory. Introduction to Databases CompSci 316 Fall 2017

OVERVIEW OF DATABASE DEVELOPMENT

Entity-Relationship Model. Purpose of E/R Model

Chapter 2: Entity-Relationship Model

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

Conceptual Design with ER Model

High Level Database Models

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

CS 405G: Introduction to Database Systems

Informatics 1: Data & Analysis

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

CS 146 Database Systems

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

High-Level Database Models. Spring 2011 Instructor: Hassan Khosravi

Lecture 2. Database design. The Entity-Relationship model

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

1/24/2012. Chapter 7 Outline. Chapter 7 Outline (cont d.) CS 440: Database Management Systems

Database Applications (15-415)

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

A l Ain University Of Science and Technology

Chapter 6: Entity-Relationship Model

Entity-Relationship Model

COMP Instructor: Dimitris Papadias WWW page:

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

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

A l Ain University Of Science and Technology

Introduction to Database Design

COSC 304 Introduction to Database Systems. Entity-Relationship Modeling

Chapter 6: Entity-Relationship Model

Introduction to Database Design

Chapter 2: Entity-Relationship Model. Entity Sets. Entity Sets customer and loan. Attributes. Relationship Sets. A database can be modeled as:

E/R Diagrams! Converting E/R Diagrams to Relations!

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

The En'ty Rela'onship Model

Chapter 7: Entity-Relationship Model

Chapter 6: Entity-Relationship Model

Informatics 1: Data & Analysis

Computer Science Applications to Cultural Heritage. Relational Databases

Chapter 7: Entity-Relationship Model

Lecture 6a Design Theory and Normalization 2/2

CSIT5300: Advanced Database Systems

The Entity-Relationship Model. Steps in Database Design

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #5: Entity/Relational Models---Part 1

ENTITY-RELATIONSHIP MODEL

Chapter 7: Entity-Relationship Model

The Entity-Relationship Model

Chapter 7: Entity-Relationship Model

ENTITY-RELATIONSHIP MODEL. CS 564- Spring 2018

Steps in designing a database. Steps in picture. Entity-Relationship Model. Entity Set. Attributes. The Entity-Relationship Data Model

EECS-3421a: Test #1 Design

course 3 Levels of Database Design CSCI 403 Database Management Mines Courses ERD Attributes Entities title 9/26/2018

Project Assignment 2 (due April 6 th, 2016, 4:00pm, in class hard-copy please)

High-Level Database Models (ii)

Object Modeling. Entity-Relationship (ER) diagrams (1976) Object Modelling Technique (OMT) diagrams (1991)

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

The Entity-Relationship (ER) Model 2

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

Lecture 4. Lecture 4: The E/R Model

Lecture 5. Lecture 5: The E/R Model

The Entity Relationship Model

Informatics 1: Data & Analysis

The Entity-Relationship Model. Overview of Database Design

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

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

Part 5: Introduction to Logical Design

Review The Big Picture

Entity-Relationship Models: Good Design and Constraints

Weak Entity Sets. A weak entity is an entity that cannot exist in a database unless another type of entity also exists in that database.

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

Chapter 3 Database Modeling and Design II. Database Modeling

Database Management System 5 ER Modeling

DATABASE DESIGN I - 1DL300

Data Modeling Using the Entity-Relationship Model

DATABASE DESIGN I - 1DL300

Intro to DB CHAPTER 6

Relational Model (cont d) & Entity Relational Model. Lecture 2

CS 405G: Introduction to Database Systems. Database Design II

Information Systems (Informationssysteme)

Conceptual Modeling in ER and UML

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 (ER Model) - Part 2

6.1 RELATIONSHIP CONCEPTS

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

Modeling Databases Using UML

The Entity-Relationship (ER) Model

2. DatabaseDesign. Master I Software Engineering. Dr. Imed Bouchrika Dept of Mathematics & Computer Science University of Souk-Ahras

ER to Relational Mapping

Transcription:

Relational Database Design: Part I Introduction to Databases CompSci 316 Fall 2017

2 Announcements (Thu. Sep. 7) Homework #1 due in 12 days Get started early! Please set up VM now! Office hours have been posted More details on the course project available next week

3 Relational model: review A database is a collection of relations (or tables) Each relation has a set of attributes (or columns) Each attribute has a name and a domain (or type) Each relation contains a set of tuples (or rows)

4 Keys A set of attributes K is a key for a relation R if In no instance of R will two different tuples agree on all attributes of K That is, K can serve as a tuple identifier No proper subset of K satisfies the above condition That is, K is minimal Example: User (uid, name, age, pop) uid is a key of User age is not a key (not an identifier) {uid, name} is not a key (not minimal)

5 Schema vs. instance uid name age pop 142 Bart 10 0.9 123 Milhouse 10 0.2 857 Lisa 8 0.7 456 Ralph 8 0.3 Is name a key of User? Key declarations are part of the schema

6 More examples of keys Member (uid, gid) FA key can contain multiple attributes Address (street_address, city, state, zip) FA relation can have multiple keys! We typically pick one as the primary key, and underline all its attributes

7 Use of keys More constraints on data, fewer mistakes Look up a row by its key value Many selection conditions are key = value Pointers to other rows (often across tables) Example: Member (uid, gid) uid is a key of User gid is a key of Group A Member row links a User row with a Group row Many join conditions are key = key value stored in another table

8 Database design Understand the real-world domain being modeled Specify it using a database design model More intuitive and convenient for schema design But not necessarily implemented by DBMS A few popular ones: Entity/Relationship (E/R) model Object Definition Language (ODL) UML (Unified Modeling Language) Translate specification to the data model of DBMS Relational, XML, object-oriented, etc. Create DBMS schema

9 But what about ORM? Automatic object-relational mappers are made popular by rapid Web development frameworks For example, with Python SQLAlchemy: You declare Python classes and their relationships It automatically converts them into database tables If you want, you can just work with Python objects, and never need to be aware of the database schema or write SQL But you still need designer discretion in all but simple cases Each language/library has its own syntax for creating schema and for querying/modifying data Quirks and limitations cause portability problems They are not necessarily easier to learn than SQL

10 Entity-relationship (E/R) model Historically and still very popular Concepts applicable to other design models as well Can think of as a watered-down object-oriented design model Primarily a design model not directly implemented by DBMS Designs represented by E/R diagrams We use the style of E/R diagram covered by the GMUW book; there are other styles/extensions Very similar to UML diagrams

11 E/R basics Entity: a thing, like an object Entity set: a collection of things of the same type, like a relation of tuples or a class of objects Represented as a rectangle Relationship: an association among entities Relationship set: a set of relationships of the same type (among same entity sets) Represented as a diamond Attributes: properties of entities or relationships, like attributes of tuples or objects Represented as ovals

12 An example E/R diagram Users are members of groups uid name Users IsMemberOf Groups gid name A key of an entity set is represented by underlining all attributes in the key A key is a set of attributes whose values can belong to at most one entity in an entity set like a key of a relation

13 Attributes of relationships Example: a user belongs to a group since a particular date uid name Users IsMemberOf Groups gid name Where do the dates go? With Users? With Groups? With IsMemberOf! fromdate

14 More on relationships There could be multiple relationship sets between the same entity sets Example: Users IsMemberOf Groups; Users Likes Groups In a relationship set, each relationship is uniquely identified by the entities it connects Example: Between Bart and Dead Putting Society, there can be at most one IsMemberOf relationship and at most one Likes relationship FWhat if Bart joins DPS, leaves, and rejoins? How can we modify the design to capture historical membership information?

15 Multiplicity of relationships E and F: entity sets Many-many: Each entity in E is related to 0 or more entities in F and vice versa Example: Many-one: Each entity in E is related to 0 or 1 entity in F, but each entity in F is related to 0 or more in E Example: Users IsMemberOf Groups Groups IsOwnedBy Users One-one: Each entity in E is related to 0 or 1 entity in F and vice versa Example: Users IsLinkedTo TwitterUsers One (0 or 1) is represented by an arrow Exactly one is represented by a rounded arrow

16 Roles in relationships An entity set may participate more than once in a relationship set FMay need to label edges to distinguish roles Examples Users may be parents of others; label needed Users may be friends of each other; label not needed parent IsFriendOf Users IsParentOf child

17 n-ary relationships Example: a user must have an initiator in order to join a group member Users initiator IsMemberOf Groups Rule for interpreting an arrow into entity set E in an nary relationship: Pick one entity from each of the other entity sets; together they can be related to at most one entity in E Exercise: hypothetically, member what do these Users IsMemberOf Groups arrows imply? initiator

18 n-ary versus binary relationships Can we model n-ary relationships using just binary relationships? Users IsMemberOf Groups member initiator InitiatesFor IsInitiatedBy

19 Next: two special relationships is part of/belongs to is a kind of http://blogs.library.duke.edu/renovation/files/2012/08/rubenstein-library-first-floor-floorplan.jpg http://www.sharky-jones.com/sharkyjones/artwork/taxonomy%20artwork/class1.jpg

20 Weak entity sets Sometimes, an entity s identity depends on some others The key of a weak entity set E comes not completely from its own attributes, but from the keys of one or more other entity sets E must link to them via many-one or one-one relationship sets Example: Rooms inside Buildings are partly identified by Buildings name A weak entity set is drawn as a double rectangle The relationship sets through which it obtains its key are called supporting relationship sets, drawn as double diamonds E

21 Weak entity set examples Seats in rooms in building number capacity Rooms In Buildings name year In number L/R? Seats Why must double diamonds be many-one/one-one? With many-many, we would not know which entity provides the key value!

22 Remodeling n-ary relationships An n-ary relationship set can be replaced by a weak entity set (called a connecting entity set) and n binary relationship sets member Users initiator IsMemberOf Groups Users Member Memberships Group Groups Initiator Note that the multiplicity constraint for IsMemberOf is lost

23 ISA relationships Similar to the idea of subclasses in object-oriented programming: subclass = special case, fewer entities, and possibly more properties Represented as a triangle (direction is important) Example: paid users are users, but they also get avatars (yay!) uid name Users IsMemberOf Groups gid name ISA fromdate avatar PaidUsers Automatically inherits key, attributes, relationships

24 Summary of E/R concepts Entity sets Keys Weak entity sets Relationship sets Attributes of relationships Multiplicity Roles Binary versus n-ary relationships Modeling n-ary relationships with weak entity sets and binary relationships ISA relationships

25 Case study 1 Design a database representing cities, counties, and states For states, record name and capital (city) For counties, record name, area, and location (state) For cities, record name, population, and location (county and state) Assume the following: Names of states are unique Names of counties are only unique within a state Names of cities are only unique within a county A city is always located in a single county A county is always located in a single state

26 Case study 1: first design name population Cities In States name capital county_name county_area

27 Case study 1: second design name population Cities In IsCapitalOf name area Counties In States name Technically, nothing in this design prevents a city in state X from being the capital of another state Y, but oh well

28 Case study 2 Design a database consistent with the following: A station has a unique name and an address, and is either an express station or a local station A train has a unique number and an engineer, and is either an express train or a local train A local train can stop at any station An express train only stops at express stations A train can stop at a station for any number of times during a day Train schedules are the same everyday

29 Case study 2: first design number engineer E/L? Trains StopsAt Stations time name address E/L?

30 Case study 2: second design number engineer Trains time LocalTrainStops Stations name address ISA ISA LocalTrains LocalStations ExpressTrains time ExpressTrainStops ExpressStations No double-diamonds here because train number + time uniquely determine a stop Is the extra complexity worth it?