CSE 1561 Designing Relational Databases Stephen Scott.

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

Data Design The futureERD - CardinalityCODINGRelationshipsDefinition.
Copyright © 2015 Pearson Education, Inc. Database Design Chapters 17 and
Accounting System Design
Systems Development Life Cycle
CSE 1561 A Brief MySQL Primer Stephen Scott. CSE 1562 Introduction Once you’ve designed and implemented your database, you obviously want to add data.
Concepts of Database Management Sixth Edition
Database Design & Mapping
Modeling Data The Entity Relationship Model (ER) For Database Design.
Database Design Chapter 2. Goal of all Information Systems  To add value –Reduce costs –Increase sales or revenue –Provide a competitive advantage.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Entity-Relationship Model Database Management Systems I Alex Coman, Winter.
APPENDIX C DESIGNING DATABASES
 Keys are special fields that serve two main purposes: ◦ Primary keys are unique identifiers of the relation in question. Examples include employee numbers,
MySql In Action Step by step method to create your own database.
Database Systems Marcus Kaiser School of Computing Science Newcastle University.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
A Guide to SQL, Eighth Edition Chapter Two Database Design Fundamentals.
Introduction to Accounting Information Systems
1 ER Modeling BUAD/American University Entity Relationship (ER) Modeling.
MIS 385/MBA 664 Systems Implementation with DBMS/ Database Management Dave Salisbury ( )
Concepts and Terminology Introduction to Database.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Introduction to MySQL Lab no. 10 Advance Database Management System.
SQL, Data Storage Technologies, and Web-Data Integration Week 2.
1 A Guide to MySQL 2 Database Design Fundamentals.
What we’ve learnt Doc 5.69 Doc 5.70 Section 1-3. A simple database Related objects Tables hold the data Forms, reports, queries to access the data.
1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database.
1 Relational Databases and SQL. Learning Objectives Understand techniques to model complex accounting phenomena in an E-R diagram Develop E-R diagrams.
M1G Introduction to Database Development 2. Creating a Database.
Chapter 4 Entity Relationship (ER) Modeling.  ER model forms the basis of an ER diagram  ERD represents conceptual database as viewed by end user 
Entity-Relationship (ER) Modelling ER modelling - Identify entities - Identify relationships - Construct ER diagram - Collect attributes for entities &
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21.
Entity-Relationship Model Using High-Level Conceptual Data Models for Database Design Entity Types, Sets, Attributes and Keys Relationship Types, Sets,
Chapter 9: Logical Database Design and the Relational Model (ERD Mapping)
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Concepts of Database Management, Fifth Edition Chapter 6: Database Design 2: Design Methodology.
Database Management Systems MIT Lesson 02 – Database Design (Entity Relationship Diagram) By S. Sabraz Nawaz.
An Entity Relationship (ER) Diagram is a graphic that shows the interrelationship between entities in a database.
 Entity-relationship models (ERM) Entity-relationship models (ERM)  Simple E-R Diagram Simple E-R Diagram  Weak Entity Weak Entity  Strong Entity.
Description and exemplification of entity-relationship modelling.
Jozef Kuper.  Describe a Database  Entities  Atributes  Relationships.
ICOM 5016 – Introduction to Database Systems Lecture 9 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
In this session, you will learn to: Map an ER diagram to a table Objectives.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan Lecture-03 Introduction –Data Models Lectured by, Jesmin Akhter.
MIS 385/MBA 664 Systems Implementation with DBMS/ Database Management
CS263 Lecture 5: Logical Database Design Can express the structure of a relation by a Tuple, a shorthand notation Name of the relation is followed (in.
Lesson 2: Designing a Database and Creating Tables.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
CSE 412/598 DATABASE MANAGEMENT COURSE NOTES 3. ENTITY-RELATIONSHIP CONCEPTUAL MODELING Department of Computer Science & Engineering Arizona State University.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Mapping Constraints Keys.
Hibernates - Many to One Association. May 12, 2011 What is Association? Association is the relation between two objects. One class holds a reference of.
Data Modeling AND ER MODELS.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
IS 4420 Database Fundamentals Chapter 3: Modeling Data in the Organization Leon Chen.
CS422 Principles of Database Systems Entity-Relationship Model Chengyu Sun California State University, Los Angeles.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Rationale Databases are an integral part of an organization. Aspiring Database Developers should be able to efficiently design and implement databases.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Database Designsemester Slide 1 Database Design Lecture 7 Entity-relationship modeling Text , 7.1.
CompSci 280 S Introduction to Software Development
Entity-Relationship Model
Tables and Their Characteristics
Chapter 12 Information Systems.
CSCI-100 Introduction to Computing
MIS2502: Data Analytics SQL – Putting Information Into a Database
Database Management system
Presentation transcript:

CSE 1561 Designing Relational Databases Stephen Scott

CSE 1562 Introduction Now that we know basic database terminology, it’s time to delve into designing one The problem: given a text-based description (specification) of a design problem, come up with a concrete specification of the database and the SQL statements to implement it Where to begin????

CSE 1563 The General Process 1.Identify the entities (tables), and the attributes (fields) that are associated with each one Kind of like naming the objects in a software design problem E.g. a winery is an object, and each has a name 2.Identify the relationships between the entities, and their cardinality E.g. a wine comes from a single winery, but one winery makes multiple wines 3.Use this information to build an initial entity- relationship (ER) model A graphical representation of our database Represents each entity, their attributes, and the relationships (including cardinality) between the entities

CSE 1564 The General Process (cont’d) 4.Identify or add a primary key for each table Must be unique Many possibilities Keep it simple, if possible! 5.Label the weak entities of the ER model Entities that depend on other entities 6.Convert the ER model to SQL Start with regular entities, then the weak ones, then do the relationships: one-to-one, then one-to-many, then many-to-many

CSE 1565 An Example Specification The owner of a local scuba store wants to track his customers and their purchases. Each customer has a first and a last name and has a highest certification level (e.g. Open Water, Advanced OW) from some agency (e.g. PADI, NAUI, SSI). Also, each customer has purchased a (possibly empty) set of items from {BCD, regulator, wet suit, dry suit, mask, tank, fins, snorkel}, but at most one item of each type. Each such item has a manufacturer, and multiple manufacturers make similar items. Each manufacturer has a name and address. For simplicity, assume that each manufacturer makes only one item of each type. (Also assume that store owner is insane and destroys all serial numbers and other identifying information on each product.)

CSE 1566 Step 1 What are the entities and their attributes?

CSE 1567 Step 1 What are the entities and their attributes? 1.Customer –First name (string) –Last name (string) 2.Certification –Level (one of {Open Water, Advanced OW, …}) –Agency (one of {PADI, NAUI, SSI, …}) 3.Item –Type (one of {BCD, regulator, …}) 4.Manufacturer –Name (string) –Address (string)

CSE 1568 Step 2 What are the relationships between the entities?

CSE 1569 Step 2 What are the relationships between the entities? 1.A customer has a certification level –Each customer has one highest level, and each level can be held by multiple customers  One-to-many 2.A customer purchases items –Each customer can purchase multiple items, and each item can be purchased by multiple customers –E.g. each customer can own 0 or more from {BCD, regulator,…}, and a regulator can be owned by multiple customers  Many-to-many

CSE Step 2 (cont’d) What are the relationships between the entities? 3.An item is made by a manufacturer –Each item has only one manufacturer, but a manufacturer can make multiple items  One-to-many

CSE Step 3: Initial ER Model Entities represented by rectangles Attributes represented by ellipses, connected to entities by lines Relationships represented by diamonds Lines connect entities to relationships, cardinality labeled by letters

CSE Step 3: Initial ER Model customer item manufacturer buys makes first namelast name itemtype address name MN 1 N has certification levelagency 1 N

CSE Step 4: Primary Keys Combination of level and agency uniquely defines a certification Customer may not be uniquely identified by first and/or last name, so we’ll add a customer ID (int)  Even if combination is unique for each customer, a numeric ID is less prone to spelling errors when inserting, querying, etc. We’ll do the same with manufacturer With respect to a particular manufacturer, type uniquely identifies item –More on this later

CSE Step 4: Primary Keys customer item manufacturer buys makes first namelast nameitemtype address name MN 1 N custIDmanufacID has certification levelagency 1 N

CSE Step 5: Label Weak Entities Are there any entities that cannot exist without another?

CSE Step 5: Label Weak Entities Are there any entities that cannot exist without another? –A manufacturer and a customer and a certification can each exist on its own –An item cannot exist unless it is manufactured (but it doesn’t depend on a customer) So item fully participates in the makes relation, and is connected by a double line –Also, type uniquely defines an item, but only when a manufacturer is specified (since multiple manufacturers make e.g. BCDs, but each manufacturer only makes one) So item is a weak entity and gets a double box

CSE Step 5: Label Weak Entities customer item manufacturer buys makes firstnamelastname address name MN 1 N custIDmanufacIDitemtype has certification levelagency 1 N

CSE Step 6: Convert to SQL 1.Regular (non-weak) entities: CREATE TABLE customer ( customerID int NOT NULL AUTO_INCREMENT, firstname varchar(30) NOT NULL, lastname varchar(30) NOT NULL, PRIMARY KEY (customerID) ) type=MyISAM; CREATE TABLE manufacturer ( manufacID int NOT NULL AUTO_INCREMENT, name varchar(50) NOT NULL, address varchar(100) NOT NULL, PRIMARY KEY (manufacID) ) type=MyISAM; CREATE TABLE certification ( level enum(‘Open Water’,’Advanced OW’,’Rescue Diver’,...) NOT NULL, agency enum(‘PADI’, ‘NAUI’, ‘SSI’,...) NOT NULL, PRIMARY KEY (level,agency) ) type=MyISAM;

CSE Step 6: Convert to SQL (cont’d) 2.Weak entity: –item is uniquely identified if manufacturer is also specified, so add manufacID as a primary key for item CREATE TABLE item ( itemtype enum(‘BCD’,’regulator’,’wet suit’,...) NOT NULL, manufacID int NOT NULL, PRIMARY KEY (manufacID,itemtype) ) type=MyISAM;

CSE Step 6: Convert to SQL (cont’d) 3.No one-to-one relationships, so do one-to-many: –The idea is as follows: if a certification can be held by multiple customers, then we need to relate each customer to the held certification –Easiest way is to add certification’s primary key(s) to item, to serve as foreign keys: CREATE TABLE customer ( customerID int NOT NULL AUTO_INCREMENT, firstname varchar(30) NOT NULL, lastname varchar(30) NOT NULL, level enum(‘Open Water’,’Advanced OW’,’Rescue Diver’,...) NOT NULL, agency enum(‘PADI’, ‘NAUI’, ‘SSI’,...) NOT NULL, PRIMARY KEY (customerID) ) type=MyISAM; –We have a similar case for the makes relation, but in this case we don’t need to update the item table, since we already added manufacID to it (since item was weak) –Note that if item were not weak, then we would add manufacID as a foreign (but not primary) key, to capture the relationship

CSE Step 6: Convert to SQL (cont’d) 4.Many-to-many relationships: –Each item can be purchased by multiple customers, and each customer can purchase multiple items –Thus we need to be able to relate each customer to multiple purchases, and vice-versa –Create a new table that takes the primary keys of the related entities as its primary keys –Note that since item is weak, one of its primary keys is manufacID, so we need to add that as well

CSE Step 6: Convert to SQL (cont’d) 4.Many-to-many relationships (cont’d): CREATE TABLE purchase ( itemtype enum(‘BCD’,’regulator’,’wet suit’,...) NOT NULL, manufacID int NOT NULL, customerID int NOT NULL, PRIMARY KEY (manufacID,itemtype,customerID) ) type=MyISAM; (Where’d the AUTO_INCREMENT go?)

CSE Final Notes 1.Relationships can have attributes as well! –E.g. if a purchase has a date CREATE TABLE purchase ( itemtype enum(‘BCD’,’regulator’,’wet suit’,...) NOT NULL, manufacID int NOT NULL, customerID int NOT NULL, dateofpurchase timestamp(8), PRIMARY KEY (manufacID,itemtype,customerID) ) type=MyISAM;

CSE Final Notes (cont’d) In ER diagram: (could also add date and certification number to “has” relationship) 2.Also, be careful about one-to-one relationships! –Ask if they’re really necessary customer item buys M N dateofpurchase