Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Advertisements

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,
R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.
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.
Database Design The process of finding user requirement
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
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 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
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.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
Conceptual Design Using the Entity-Relationship (ER) Model
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
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,
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
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.
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.
The Relational Model1 ER-to-Relational Mapping and Views.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
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.
1 Database Systems Defining Database Schema Views.
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.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
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.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Xin Zhang.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
CPSC-310 Database Systems
Logical DB Design: ER to Relational
COP Introduction to Database Structures
COP4710 Database Systems Relational Model.
Translation of ER-diagram into Relational Schema
Database Models Relational Model
From ER to Relational Model
Translation of ER-diagram into Relational Schema
Defining a Database Schema
CS4433 Database Systems Relational Model.
CMSC-461 Database Management Systems
Presentation transcript:

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014

ER Model vs. Relational Model Both are used to model data ER model has many concepts – Entities, relationships, attributes, etc. – Well-suited for capturing the app. requirements – Not well-suited for computer implementation Relational model – Has just a single concept: relation – World is represented with a collection of tables – Well-suited for efficient manipulations on computers 1

Behind the Scene: It’s All About Modeling 1973 Charles W. Bachman – For his outstanding contributions to database technology 1981 Edgar F. Codd – For his fundamental and continuing contributions to the theory and practice of database management systems 1998 James Gray – For seminal contributions to database and transaction processing research and technical leadership in system implementation And we certainly need more! 2

Relation: An Example 3 Products Name of Table (Relation) Column (Field, Attribute) Row (Record, Tuple)Domain (Atomic type)

Relations Schema vs. instance = columns vs. rows Schema of a relation: R(A 1, A 2, …, A k ) 1.Relation name 2.Attribute names 3.Attribute types (domains) Schema of a database: R 1 (…), R 2 (…),…, R n (…) – A set of relation schemas Questions – When do you determine a schema (instance)? – How often do you change your mind? 4

Relations The database maintains a current database state Updates to the data happen very frequently – Insert a tuple – delete a tuple – update an attribute in a tuple Updates to the schema are relatively rare, and rather painful. Why? 5

Defining a Database Schema A database schema comprises declarations for the relations (“tables”) of the database Simplest form of creation is: CREATE TABLE ( ); And you may remove a relation from the database schema by: DROP TABLE ; 6

Elements of Table Declarations The principal element is a pair consisting of an attribute and a type The most common types are: – INT or INTEGER (synonyms) – REAL or FLOAT (synonyms) – CHAR(n ) = fixed-length string of n characters – VARCHAR(n ) = variable-length string of up to n characters 7

Example: Create Table CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL ); 8

Dates and Times DATE and TIME are types in SQL The form of a date value is DATE ‘yyyy-mm-dd’ – Example: DATE ‘ ’ for Sept. 30, 2002 The form of a time value is TIME ‘hh:mm:ss’ with an optional decimal point and fractions of a second following – Example: TIME ‘15:30:02.5’ = two and a half seconds after 3:30PM 9

Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE – Each says the attribute(s) so declared functionally determines all the attributes of the relation schema – Single attribute keys CREATE TABLE Beers ( nameCHAR(20) UNIQUE, manfCHAR(20) ); 10

Multi-attribute Keys CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL, PRIMARY KEY (bar, beer) ); 11

PRIMARY KEY vs. UNIQUE Standard SQL requires these distinctions 1.There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes 2.No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULL’s, and there may be several tuples with NULL SQL standard also allows DBMS implementers to make their own distinctions between PRIMARY KEY and UNIQUE – Example: some DBMS might automatically create an index (data structure to speed search) in response to PRIMARY KEY, but not UNIQUE 12

Other Declarations for Attributes Two other declarations we can make for an attribute are: 1.NOT NULL means that the value for this attribute may never be NULL 2.DEFAULT says that if there is no specific value known for this attribute’s component in some tuple, use the stated CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ‘123 Monroe St.’, phone CHAR(16) ); 13

Example for NULL and DEFAULT Suppose we insert the fact that Sally is a drinker, but we know neither her address nor her phone – An INSERT with a partial list of attributes makes the insertion possible: INSERT INTO Drinkers(name) VALUES(‘Sally’); – If we had declared phone NOT NULL, this insertion would have been rejected 14 NameAddressPhone ‘Sally’‘123 Monroe St.’NULL

Foreign Keys A Foreign Key is a field whose values are keys in another relation – Must correspond to primary key of the second relation – Like a `logical pointer’ 15 Enrolled Students CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students, FOREIGN KEY (cid) REFERENCES Courses )

Referential Integrity Consider relations Students and Enrolled; sid in Enrolled is a foreign key that references Students – What should be done if an Enrolled tuple with a non-existent student id is inserted? Reject it! – What should be done if a Students tuple is deleted? 1.Also delete all Enrolled tuples that refer to it 2.Disallow deletion of a Students tuple that is referred to 3.Set sid in Enrolled tuples that refer to it to a default sid 4.In SQL, also: set sid in Enrolled tuples to NULL Similar if primary key of Students tuple is updated 16

Adding Attributes We may change a relation schema by adding a new attribute (“column”) by: ALTER TABLE ADD ; – Example: ALTER TABLE Bars ADD phone CHAR(16) DEFAULT ‘unlisted’; 17

Deleting Attributes Remove an attribute from a relation schema by: ALTER TABLE DROP ; – Example: we don’t really need the license attribute for bars: ALTER TABLE Bars DROP license; 18

Translating ER Diagram to Rel. Design Basic cases – entity set E = relation with attributes of E – relationship R = relation with attributes being keys of related entity sets + attributes of R Special cases – combining two relations – translating weak entity sets – translating is-a relationships and subclasses 19

Entity Set to Relation 20 CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY KEY (ssn) ) Employees ssn name lot

Relationship Set to Relation In translating a many-to-many relationship set to a relation, attributes of the relation must include: 1.Keys for each participating entity set (as foreign keys) This set of attributes forms a superkey for the relation 2.All descriptive attributes 21 CREATE TABLE Works_In( ssn CHAR (1), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments )

Translating ER Diagrams 22 dname budget did since lot name ssn Manages Employees Departments Translation to relational model? Many-to-Many1-to-11-to-ManyMany-to-1

Translating ER Diagrams 1.Map relationship set to a (virtual) Manages table: 1.Note that did is the key now! 2.Separate tables for Employees and Departments 2.Since each department has a unique manager, we could instead combine Manages and Departments 23 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 )

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

Another Example 25 LoginsHostsAt loginnamehostname Hosts(hostname, IP) Logins(loginname, hostname, time) At(loginName, hostName) time At becomes part of Logins IP

Translating ISA Hierarchies to Relations 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) 26 Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked

Translating ISA Hierarchies to Relations General approach – 3 relations: Employees, Hourly_Emps and Contract_Emps Hourly_Emps: Every employee is recorded in Employees. For hourly emps, 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 27

Summary: ER Diagram to Relations 28 E/R diagram Relational schema e.g.account=(bname, acct_no, bal) E a 1 ….. a n E = ( a 1, …, a n ) E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m R1= ( a 1, b 1, c 1, …, c k )

Summary: ER Diagram to Relations 29 Could have : R1= ( a 1, b 1, c 1, …, c k ) – Put b 1 as the key for R1, it is also the key for E2=(b 1, …., b n ) Usual strategy (combination) – ignore R1 – Add a1, c1, …., ck to E2 instead, i.e. – E2=(b 1, …., b n, a 1, c 1, …, c k ) E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m

Summary: ER Diagram to Relations 30 E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m ? ? R1 E1 = ( a 1, …, a n ) E2 = ( b 1, …, b m ) R1 = ( a 1, b 1, c 1 …, c k ) E1 = ( a 1, …, a n ) E2 = ( b 1, …, b m, a 1, c 1, …, c k ) E1 = ( a 1, …, a n, b 1, c 1, …, c k ) E2 = ( b 1, …, b m ) Treat as n:1 or 1:m

Summary: ER Diagram to Relations 31 Weak Entity sets E1 E2 IR a 1 …. a n b 1 …. b m E1 = ( a 1, …, a n ) E2 = (a 1, b 1, …, b m ) E1 S2 Isa S1 a1 … an c 1 …. c k b 1 …. b m Method 1: E = ( a 1, …, a n ) S1 = (a 1, b 1, …, b m ) S2 = ( a 1, c 1 …, c k ) Method 2: S1 = (a 1,…, a n, b 1, …, b m ) S2 = ( a 1, …, a n, c 1 …, c k ) Sub-classes