CS 564 Database Management Systems: Design and Implementation Lecture 2: Relational Model; ER to Relational Chapter 3 in Cow Book Slide ACKs: AnHai Doan,

Slides:



Advertisements
Similar presentations
Three-Step Database Design
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Entity-Relationship Model
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Lecture 11: Basic SQL, Integrity constraints
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Database Design & ER Diagrams
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Chapter 14 & 15 Conceptual & Logical Database Design Methodology
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
The Relational Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
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.
Database Technical Session By: Prof. Adarsh Patel.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
CS 564 Database Management Systems: Design and Implementation
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Relational Database. Database Management System (DBMS)
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
The Relational Model1 ER-to-Relational Mapping and Views.
CS 564 Database Management Systems: Design and Implementation Lecture 3: Schema Normalization Chapter 19 in Cow Book Slide ACKs: AnHai Doan, Jeff Naughton,
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Concepts 2440: 180 Database Concepts Instructor:
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
April 20022/CS/3X1 Database Design Design method John Wordsworth Department of Computer Science The University of Reading Room.
COP Introduction to Database Structures
Logical DB Design: ER to Relational
COP Introduction to Database Structures
MODELS OF DATABASE AND DATABASE DESIGN
These slides are based on the slides of your text book.
Chapter 4 Relational Databases
Database Management Systems (CS 564)
Translation of ER-diagram into Relational Schema
From ER to Relational Model
The Relational Model Relational Data Model
Translation of ER-diagram into Relational Schema
The Relational Model Textbook /7/2018.
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
Data Model.
Presentation transcript:

CS 564 Database Management Systems: Design and Implementation Lecture 2: Relational Model; ER to Relational Chapter 3 in Cow Book Slide ACKs: AnHai Doan, Jeff Naughton, and Jignesh Patel Arun Kumar 1

@Wait list students: You must have gotten an invite to enroll to Sec 2. If not, me before EOD! 2

3 General Dos and Do NOTs Do:  Raise your hand before asking questions during Lectures  Participate in class discussions and use our Piazza page  Use “[CS564]” as subject prefix for all s to me/TAs Do NOT:  Take this class if you cannot attend on Fridays also  Use laptops, tablets, mobile phones, or any other electronic devices during Lectures  Use as primary communication mechanism for doubts/questions instead of Office Hours and Discussions  Record/quote my anecdotes outside of class!

Hands up if you have watched and rated movies on Netflix! 4

Surprise ER Review Exercise! Cool. Now, please draw a full-fledged ER diagram for Netflix’s “movie recommendation system” with two Entities: Users and Movies, and one Relationship: Ratings. Attach the following attributes appropriately (reuse allowed): UserID, MovieID, RatingID, NumStars, Name, Timestamp, Age, Director, ReleaseDate, JoinDate 5

Review Exercise Answer User Name Age UserID Movie Name Director MovieID Rating Timestamp RatingID NumStars JoinDate ReleaseDate 6

Database Design Process: ~6 steps 1.Requirements Analysis 2.Conceptual Database Design 3.Logical Database Design 4.Schema Refinement 5.Physical Database Design 6.Application and Security Design Entity Relationship Modeling Relational Model and Normalization Indexing, etc. 7

8 Relational data model in a nutshell Basically, Relation:Table :: Pilot:Driver (okay, a bit more) The model formalizes “operations” to manipulate relations RatingIDNumStarsTimestampUserIDMovieID /27/ /20/ /02/ …………… Ratings

Ugh! Another data model! What is the difference with the ER Model? 9

10 ER Model vs. Relational Model  Key purpose Ease of capturing user app requirements vs. Ease of (semi-)automated management by computer  Concepts and structure Many concepts in a rich, complex graph vs. Single, simple, “flat” concept: “relation”  Data management functionality No notion of “operations” vs. Rich “algebra” of relational operations

11 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID /27/ /20/ /02/ …………… What is a Relation? A glorified table! What are Attributes? These things What are Domains? The mathematical “domains” for the attributes IntegersReal… What is Arity? Number of attributes Ratings

12 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID /27/ /20/ /02/ …………… What are Tuples?What is Cardinality? These thingsNumber of tuples Ratings

13 Referring to “tuples”: Two notations 1.Without using attribute names (positional/sequence) 2.Using attribute names (named/set) RatingIDNumStarsTimestampUserIDMovieID /27/ /20/ /02/ …………… Ratings (R) t[1] = 3.5 t.NumStars = 3.5

14 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID /27/ /20/ /02/ …………… What is Schema? The relation name, and the name and logical descriptions of the attributes (including domains) Aka “metadata” Ratings

15 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID /27/ /20/ /02/ …………… What is an Instance? A given relation populated with a set of tuples (loose analogy: schema:instance::type:value in PL) Instance 1 RatingIDNumStarsTimestampUserIDMovieID /27/ /10/ /08/ …………… Instance 2 Ratings

16 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID /27/ …………… What is a Relational Database? UserIDNameAgeJoinDate 79Alice2301/10/13 ……… MovieIDNameReleaseDateDirector 20Inception07/13/2010Christopher Nolan ……… A collection of relations; similarly, schema vs. instance Ratings Users Movies

Wait a minute! Did we not see this schema earlier (the Netflix question)? 17

Spot six differences! 18 User Name Age UserID Movie Name Director MovieID Rating Timestamp RatingID NumStars JoinDate ReleaseDate

19 Spot six differences! RatingIDNumStarsTimestampUserIDMovieID /27/ …………… UserIDNameAgeJoinDate 79Alice2301/10/13 ……… MovieIDNameReleaseDateDirector 20Inception07/13/2010Christopher Nolan ……… Ratings Users Movies

20 ER Model vs. Relational Model  Key purpose Ease of capturing user app requirements vs. Ease of (semi-)automated management by computer  Concepts and structure Many concepts in a rich, complex graph vs. Single, simple, “flat” concept: “relation”  Data management functionality No notion of “operations” vs. Rich “algebra” of relational operations

21 “Write Operations” on a Relation  Insert Add tuples to a relation  Delete Remove tuples from a relation (typically based on “predicate” matches, e.g., “NumStars <= 4.5”  Modify Logically, deletes + inserts, but typically implemented as in-place updates to a relation instance

22 “Read Operations” on a Relation  “Select” Select all tuples from Ratings with “UserID == 19”  “Project” Select only Director attribute from Movies  “Aggregate” Select Average of all NumStars in Ratings And a few more formal operations … (Sneak peak: SQL to express both the write/read ops!)

Bottomline: ER model is for conceptual schema modeling; no notion of operations Relational model includes operations on data; implementable as fast software 23

So, how do we go from ER model to a relational model in an application? 24

ER to Relational: Entity as a Table 25 User Name Age UserID JoinDate Movie Name Director MovieID Rating Timestamp RatingID NumStars ReleaseYear UserIDNameAgeJoinDate 79Alice2301/10/13 ……… Users Entity Name → Relation Name Attribute Names → Attribute Names Entity Set → Relation Instance (Tuples)

ER to Relational: Key Constraint UserID uniquely identifies a User Underline it in the relation too! “Primary Key” 26 User Name Age UserID JoinDate UserIDNameAgeJoinDate 79Alice2301/10/13 ……… Users

ER to Relational: More Examples MovieIDNameReleaseDateDirector 20Inception07/13/2010Christopher Nolan ……… Movies 27 Movie Name Director MovieID ReleaseDate

It is nice pictorially, but how do we define/create a relation precisely? 28

29 Introducing SQL  Structured English QUEry Language (SEQUEL); TL;DR name is SQL  Invented at - you guessed it - IBM!

30 What is SQL?  SQL is a querying language for relational data  Simple English-based syntax, but precise, formal semantics (compiled down to relational algebra)  Key advantages: Physical Data Independence (“how” data is stored on machine independent of “what”, i.e., SQL queries) Logical Data Independence (notion of views in SQL enables simpler queries on same schema)

31 Major SQL Components  Data Definition Language (DDL)  Data Manipulation Language (DML)  Embedded and Dynamic SQL  Triggers and Cursors  Security  Transaction Management  Remote Database Access

Creating a table for an Entity in SQL MovieIDNameReleaseDateDirector 20Inception07/13/2010Christopher Nolan ……… Movies 32 CREATE TABLE Movies ( MovieID INTEGER, Name CHAR(30), ReleaseDate DATE, Director CHAR(20), PRIMARY KEY (MovieID))

Relationship as a Relation? 33 User Name Age UserID Movie Name Director MovieID Rating Timestamp RatingID NumStars JoinDate ReleaseYear RatingIDNumStarsTimestampUserIDMovieID /27/ …………… “Foreign Keys” Ratings

Table for Relationships in SQL 34 CREATE TABLE Ratings( RatingID INTEGER, Numstars REAL, Timestamp DATE, UserID INTEGER, MovieID INTEGER, PRIMARY KEY (RatingID), FOREIGN KEY (UserID) REFERENCES Users(UserID), FOREIGN KEY (MovieID) REFERENCES Movies(MovieID)) RatingIDNumStarsTimestampUserIDMovieID /27/ …………… Ratings

Q: Why not this? UserI D Nam e Ag e JoinDat e RatingI D NumSta rs Timesta mp MovieI D NameRelease YEar Director 79Alice2301/10/ /27/1 5 20Inceptio n 2010Christop her Nolan …………………………… AllStuff (Sneak peak: Redundancy in the data! Waste of storage; causes write anomalies! Mitigated by Schema normalization) 35

How to represent self-relationships? Mention Movie Name DirectorMovieID ReleaseYear MovieIDMentionMovieID …… Mention 2 Foreign Keys Q: How to express in SQL? Q: What is the primary key? 36

NULL Values in Relations User Name Age UserID JoinDate (Sneak peak: A headache for SQL query processing!) UserIDNameAgeJoinDate 79Alice2301/10/13 48JohnNULL04/08/15 ……… Users NULL “stands in” for attribute values that are missing/unknown 37 SQL has “NOT NULL”, e.g., “ Age REAL NOT NULL ”

38 What about many-to-one? Student NameAgeSID Department NameAddressDID Major SIDNameAge 79Alice19 13Bob21 48JohnNULL ……… Students DIDNameAddre ss CSComputer Sciences 1210 … STStatisticsBlah ……… Department SIDDID 79CS 48ST …… Major

39 What about many-to-one? Student NameAgeSID Department NameAddressDID Major SIDNameAgeMajorDID 79Alice19CS 13Bob21NULL 48JohnNULLST ………… Students Foreign Key? DIDNameAddre ss CSComputer Sciences 1210 … STStatisticsBlah ……… Department

More Advanced Stuff  Integrity constraints  Key constraints  Referential integrity and participation constraints  Weak entity set  “Is A” hierarchy 40

Integrity Constraint (IC)  A logical condition (invariant) that must hold true on any instance of a given database schema  A legal relation instance satisfied all ICs  Overuse/abuse of ICs is a danger!  Part of schema; cannot infer from data exactly!  Two main types: Key Constraint Referential Integrity Constraint 41

Key Constraint in SQL  Key vs. Superkey  Primary key vs. Candidate key vs. Alternate key 42 MovieIDNameReleaseDateDirectorIMDB_URL Movies CREATE TABLE Movies ( MovieID INTEGER, Name CHAR(30), ReleaseDate DATE, Director CHAR(20), IMDB_URL CHAR(20), PRIMARY KEY (MovieID), UNIQUE (IMDB_URL))

Referential Integrity Constraint (RIC)  A Foreign Key value should not be NULL! 43 Student NameAgeSID Department NameAddressDID Major SIDNameAgeMajorDID 79Alice19CS 13Bob21NULL 48JohnNULLST ………… Students Foreign Key? DIDNameAddre ss CSComputer Sciences 1210 … STStatisticsBlah ……… Department

What if a Department tuple is deleted?! 44

Enforcing RIC  We have 3 options:  Refuse to allow the deletion!  Delete all tuples in Students that reference the deleted DID in Department  Set the corresponding DID in Students to some default value, or in the worst case, NULL  45

Enforcing RIC in SQL  Refuse to allow the deletion! 46 CREATE TABLE Student( SID INTEGER, Name CHAR(30), Age INTEGER, DID INTEGER, PRIMARY KEY (SID), FOREIGN KEY (DID) REFERENCES Department(DID) ON DELETE NO ACTION)

Enforcing RIC in SQL  Delete all tuples in Students that reference the deleted DID in Department 47 CREATE TABLE Student( SID INTEGER, Name CHAR(30), Age INTEGER, DID INTEGER, PRIMARY KEY (SID), FOREIGN KEY (DID) REFERENCES Department(DID) ON DELETE CASCADE)

Enforcing RIC in SQL  Set the corresponding DID in Students to some default value, or in the worst case, NULL  48 CREATE TABLE Student( SID INTEGER, Name CHAR(30), Age INTEGER, DID INTEGER, PRIMARY KEY (SID), FOREIGN KEY (DID) REFERENCES Department(DID) ON DELETE SET DEFAULT)

49 Participation Constraint in SQL Student NameAgeSID Department NameAddressDID Major CREATE TABLE Student( SID INTEGER, Name CHAR(30), Age INTEGER, DID INTEGER NOT NULL, PRIMARY KEY (SID), FOREIGN KEY (DID) REFERENCES Department(DID) ON DELETE NO ACTION)

Translating a Weak Entity Set Floor NumberNumRooms Department NameAddressDID PartOf 50 NumberNumRoomsDID 114CS 212CS ……… Floor DIDNameAddre ss CSComputer Sciences 1210 … STStatisticsBlah ……… Department Q. What ICs needed on Floor?

Translating an “Is A” Hierarchy Student NameAgeSID UndergradDoctoral Masters IsA IsHonorsQualScore ByThesis 51

Translating an “Is A” Hierarchy  We have 3 options:  “OOPL Approach”: separate relations for each Entity Set; an entity present in exactly one  “TrueER Approach”: relations for “subclasses” have foreign key to parent  “Truly Terrible Approach”: AllStuff with NULL! 52

Translating an “Is A” Hierarchy  “OOPL Approach”: separate relations for each Entity Set; an entity present in exactly one 53 SIDNameAge Students SIDNameAgeIsHonors Undergrad SIDNameAgeIsThesis Masters SIDNameAgeQualScore Doctoral

Translating an “Is A” Hierarchy  “TrueER Approach”: relations for “subclasses” have foreign key to parent 54 SIDNameAge Students SIDIsHonors Undergrad SIDIsThesis Masters SIDQualScore Doctoral Q. How do the ICs here differ with OOPL?

Translating an “Is A” Hierarchy  “Truly Terrible Approach”: AllStuff with NULL! 55 AllStuff SIDNameAgeIsHonorsIsThesisQualScore Q. How do the ICs here differ with the other 2? NULL is awful! 

Review: Relational; ER to Relational 1.Basic Terms of Relational Model 2.Introduction to SQL; CREATE TABLE 3.Translating ER to Relational 4.Integrity Constraints 5.Weak Entities; “IsA” Hierarchies 56