Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

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

3 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 emails 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 email as primary communication mechanism for doubts/questions instead of Office Hours and Discussions  Record/quote my anecdotes outside of class!

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

5 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

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

7 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 8 Relational data model in a nutshell Basically, Relation:Table :: Pilot:Driver (okay, a bit more) The model formalizes “operations” to manipulate relations RatingIDNumStarsTimestampUserIDMovieID 13.508/27/151920 24.007/20/154232293 32.508/02/1554551846 …………… Ratings

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

10 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 11 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID 13.508/27/157920 24.007/20/154232293 32.508/02/1554551846 …………… 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 12 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID 13.508/27/157920 24.007/20/154232293 32.508/02/1554551846 …………… What are Tuples?What is Cardinality? These thingsNumber of tuples Ratings

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

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

15 15 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID 13.508/27/157920 24.007/20/154232293 32.508/02/1554551846 …………… What is an Instance? A given relation populated with a set of tuples (loose analogy: schema:instance::type:value in PL) Instance 1 RatingIDNumStarsTimestampUserIDMovieID 32921.506/27/1479410 2941224.007/10/14232329 744230.503/08/148451846 …………… Instance 2 Ratings

16 16 Relational Model: Basic Terms RatingIDNumStarsTimestampUserIDMovieID 13.508/27/157920 …………… 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

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

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

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

20 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 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 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!)

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

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

25 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)

26 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

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

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

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

30 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 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

32 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))

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

34 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 13.508/27/157920 …………… Ratings

35 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/1 3 13.508/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

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

37 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 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 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

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

41 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

42 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))

43 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

44 What if a Department tuple is deleted?! 44

45 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

46 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)

47 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)

48 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 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)

50 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?

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

52 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

53 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

54 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?

55 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! 

56 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


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

Similar presentations


Ads by Google