Logical DB Design: ER to Relational

Slides:



Advertisements
Similar presentations
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Advertisements

Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
The Entity-Relationship Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Book Chapter 3 (part 2 ) From ER to Relational Model.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
ER continued, and ER to Relational Mappings R&G Chapters 2, 3 Lecture 22.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
The Relational Model1 ER-to-Relational Mapping and Views.
ICS 321 Fall 2009 The Relational Model (ii) Asst. Prof. Lipyeow Lim Information and Computer Science Department University of Hawaii at Manoa 9/10/20091Lipyeow.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Mapping E/R Diagrams to Relational Database Schemas
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
Conceptual Design and The Entity-Relationship Model
The Entity-Relationship Model
The Entity-Relationship Model
Design Concepts & ER Model
COP4710 Database Systems Relational Model.
MODELS OF DATABASE AND DATABASE DESIGN
These slides are based on the slides of your text book.
The Entity-Relationship Model
The Entity-Relationship Model
The Relational Model Lecture 3 INFS614, Fall
Translation of ER-diagram into Relational Schema
The Relational Model Chapter 3
Instructor: Elke Rundensteiner
From ER to Relational Model
The Entity-Relationship Model
The Entity-Relationship Model
Database Systems (資料庫系統)
The Relational Model Chapter 3
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo
The Relational Model Chapter 3
Translation of ER-diagram into Relational Schema
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
The Entity-Relationship Model
Database Systems (資料庫系統)
More on ER Model and the Relationship Model
The Relational Model Chapter 3
The Entity-Relationship Model
CS4433 Database Systems Relational Model.
The Entity-Relationship Model
The Entity-Relationship Model
The Entity-Relationship Model
The Entity-Relationship (ER) Model
The Relational Model Chapter 3
Presentation transcript:

Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot The slides for this text are organized into several modules. Each lecture contains about enough material for a 1.25 hour class period. (The time estimate is very approximate--it will vary with the instructor, and lectures also differ in length; so use this as a rough guideline.) This covers Lectures 1 and 2 (of 6) in Module (5). Module (1): Introduction (DBMS, Relational Model) Module (2): Storage and File Organizations (Disks, Buffering, Indexes) Module (3): Database Concepts (Relational Queries, DDL/ICs, Views and Security) Module (4): Relational Implementation (Query Evaluation, Optimization) Module (5): Database Design (ER Model, Normalization, Physical Design, Tuning) Module (6): Transaction Processing (Concurrency Control, Recovery) Module (7): Advanced Topics CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) 3

Constraints in Create Table Adding constraints to a table enables the database system to enforce data integrity. Different types of constraints: * Not Null * Default Values * Unique * Primary Key * Foreign Key * Check Condition

Null and Not Null CS174A Relational Data Model

Primary Key Constraint CREATE TABLE Student ( ID NUMBER PRIMARY KEY, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, ); Primary Key implies: * NOT NULL * UNIQUE. There can only be one primary key.

Primary Key Constraint (Syntax 2) CREATE TABLE Students ( ID NUMBER, Fname VARCHAR2(20) NOT NULL, Lname VARCHAR2(20) NOT NULL, PRIMARY KEY(ID) ); Needed when the primary key is made up of two or more fields

Relationship Sets to Tables ssn name lot did dname budget Employees Departments Works_in since CREATE TABLE Works_In ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) We implement a Many-to-Many relationship by adding 2 Tables’ Primary Keys to a third Table CS174A Relational Data Model

Foreign Key Constraint CREATE TABLE Studies( Course NUMBER, Student NUMBER, FOREIGN KEY (Student) REFERENCES Students(ID) ); NOTE: ID must be unique (or primary key) in Student

Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid), FOREIGN KEY (sid) REFERENCES Students ) Enrolled sid cid grade 53666 Carnatic101 C Reggae203 B 53650 Topology112 A History105 Students sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 3.2 53650 smith@math 19 3.8 CS174A Relational Data Model

Key Constraints Translation to relational model? since lot name ssn dname Each dept has at most one manager, according to the key constraint on Manages. did budget Manages Employees Departments Translation to relational model? 1-to-1 1-to Many Many-to-1 Many-to-Many 6

ER Relationship Sets  Relations since ssn name lot did dname budget Manages Employees Departments CREATE TABLE Employees ( ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Departments ( did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)) With the key constraint? CS174A Relational Data Model

… with Key Constraints Map relationship Manages to one table: Note that did is the key Separate tables for Employees and Departments CREATE TABLE Departments ( did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)) CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) Relational Data Model CS174A

Improvements Using Key Constraints CREATE TABLE Departments ( did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)) CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Dept_Mgr ( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) Since each department has a unique manager, we could instead combine Manages and Departments Relational Data Model CS174A

Review: Participation Constraints Does every department have a manager? If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial) Every did value in Departments must appear in a row of Manages (with a non-null ssn value!) since ssn name lot did dname budget Manages Employees Departments Works_In since CS174A Relational Data Model

Participation Constraints in SQL We can capture participation constraints involving one entity set in a binary relationship, but little else (without resorting to CHECK constraints) CREATE TABLE Dept_Mgr ( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees ON DELETE NO ACTION) CS174A Relational Data Model

Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another (owner) entity Owner entity set and weak entity set must participate in a one-to-many relationship set (1 owner, many weak entities) Weak entity set must have total participation in this identifying relationship set - - - - under pname denotes a partial key. both relationship set and entity set are thick lines. cost ssn name lot pname age Policy Employees Dependents CS174A Relational Data Model

Translating Weak Entity Sets Weak entity set and identifying relationship set are translated into a single table When the owner entity is deleted, all owned weak entities must also be deleted CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees ON DELETE CASCADE) CS174A Relational Data Model

Translating ISA: Option 1 address Translating ISA: Option 1 id Movie Person name ISA picture Actor Director create table MoviePerson( ... ) create table Actor(id varchar2(20), picture bfile, primary key(id), foreign key (id) references MoviePerson)) create table Director(...)

Translating ISA: Option 2 address Translating ISA: Option 2 id Movie Person name ISA picture Actor Director No table for MoviePerson! create table Actor(id varchar2(20), address varchar2(100), name varchar2(20), picture blob, primary key(id)); create table Director(...)

Review: ISA Hierarchies Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed) Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no) Reasons for using ISA: To add descriptive attributes specific to a subclass To identify entities that participate in a relationship ssn name lot Employees ISA Hourly_Emps Contract_Emps Hourly_wages contractid Hours_worked Relational Data Model CS174A

Translating ISA Hierarchies to Relations General approach: 3 relations Employees, Hourly_Emps and Contract_Emps Every employee is recorded in Employees For hourly employees, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn) must delete Hourly_Emps tuple if referenced Employees tuple is deleted Queries involving all employees easy, those involving just Hourly_Emps require a join to get some attributes Alternative: Just Hourly_Emps and Contract_Emps Hourly_Emps : ssn, name, lot, hourly_wages, hours_worked Each employee must be in one of these two subclasses CS174A Relational Data Model

Views CS174A Relational Data Model

What is a View? In SQL, a VIEW is a virtual relation based on the result-set of a SELECT statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. In some cases, we can modify a view and present the data as if the data were coming from a single table. Syntax: CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

SQL – Relations, Tables & Views When we say Relation, it could be a Table or a View. There are three kind of relations: Stored relations tables We sometimes use the term “base relation” or “base table” Virtual relations views Temporary results

SQL – Create View Example: Create a view with title and year and made by Paramount studio. Movie (title, year, length, inColor, studioName, producerC#) CREATE VIEW ParamountMovie AS SELECT title,year FROM Movie WHERE studioName = ‘Paramount’;

The SQL Query Language The most widely used relational query language. Current standard is SQL-99 To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 3.2 To find just names and logins, replace the first line: SELECT S.name, S.login CS174A Relational Data Model

SQL - Basic Queries Example: Pose a query to get the names of all students: SELECT LastName, FirstName FROM Students

SQL - Basic Queries (cont.) Names of freshmen students

SQL - Basic Queries –Joining tables based on foreign key Connecting majors with their department chairs: we need to have ChairID = FacultyID Majors(MajorID, MajorName, Department, ChairID) Faculty (FacultyID, FirstName, LastName, Phone, EMail)

SQL - Basic Queries – Joining tables Student last name, advisor last name OR (aliasing)

SQL - Basic Queries – Joining tables

** END **