COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model.

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.
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.
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.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo Lecture #3 R & G, Chap. 3.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
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 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.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
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,
The Relational Model These slides are based on the slides of your text book.
The Relational Model Chapter 3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
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.
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.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
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 Database Management Systems Chapter 3 The Relational Model.
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.
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.
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 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.
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.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Multi-billion dollar industry, $15+ bill in  Vendors:
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
These slides are based on the slides of your text book.
The Relational Model Lecture 3 INFS614, Fall
Translation of ER-diagram into Relational Schema
The Relational Model Chapter 3
The Relational Model Chapter 3
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 Relational Model Chapter 3
The Relational Model Chapter 3
Presentation transcript:

COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model

22 L3 Download from our website Individual work with your academic honesty declaration form Hardcopy or written work not electronic form Due date: 20/08/2007(Monday, week 5), late submission will not be accepted ER Diagram, relational schema and SQL DDL Assignment 1

33 L3 Relational Data Model Introduction to Conceptual Data Model Database Design Sequence Conceptual data model Types of conceptual data model Entity Relationship Model Entity, Relationship, Attribute Degree of relationship Cardinality and participation Ternary relationship Weak entity Review of Last Class

44 L3 Relational Data Model Defining the Schema Mathematics of Relations Logical Database Design Today’s Agenda

55 L3 Relational Data Model Review Relational database: a set of relations Relation: Instance : a table, with rows and columns. The value in the database at a given time. Schema : specifies name of relation, plus name and type of each column. E.G. Student (sid: string, name: string, login: string, age: integer, gpa: real). Can think of a relation as a set of rows or tuples (i.e., all rows are distinct).

66 L3 Relational Data Model Example Instance   All rows are distinct.   Do all columns in a relation instance have to be distinct? NO! Student Fields (Attributes, Columns) Tuples (Records, Rows)

77 L3 Relational Data Model Defining Schema in SQL Creates the Student relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Student (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL ) CREATE TABLE Enrolled (sid: CHAR(20), cid: CHAR(20), grade: CHAR (2))

88 L3 Relational Data Model Destroying and Altering Relations DROP TABLE Student Destroys the relation Student. The schema information and the tuples are deleted. ALTER TABLE Student ADD COLUMN firstYear: integer The schema of Student is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field.

99 L3 Relational Data Model Adding Tuples Can insert a single tuple using: INSERT INTO Student (sid, name, login, age, gpa) VALUES (53688, ‘ Smith ’, ‘ ’, 18, 3.2) Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Student S WHERE S.name = ‘ Smith ’ Powerful variants of these commands are available; more later!

1010 L3 Relational Data Model Integrity Constraints Integrity Constraint (IC): condition that must be true for any instance of the database; e.g., domain constraints. ICs can be declared in the schema They are specified when schema is defined. All declared ICs are checked whenever relations are modified. A legal instance of a relation is one that satisfies all specified ICs. If ICs are declared, DBMS will not allow illegal instances. If the DBMS checks ICs, stored data is more faithful to real-world meaning. Avoids data entry errors, too!

1111 L3 Relational Data Model Non-Null Columns One domain constraint is to insist that no value in a given column can be null The value can’t be unknown; The concept can’t be inapplicable In SQL CREATE TABLE Student (sid: CHAR(20) NOT NULL, name: CHAR(20), login: CHAR(10) NOT NULL, age: INTEGER, gpa: REAL)

1212 L3 Relational Data Model Relational Keys Keys are special fields that serve two main purposes: Primary keys are unique not-null identifiers of the relation in question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique Foreign keys are where one table has a column that refers to the primary key of another table (to allow connections to be made) Keys can be simple (a single field) or composite (more than one field) Keys usually are used as indices to speed up the response to user queries (More on this later in the semester)

1313 L3 Relational Data Model Example: Relational Keys Primary Key Foreign Key (implements 1:N relationship between customer and order) Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product)

1414 L3 Relational Data Model Primary Key Constraints A set of fields is a key for a relation if : 1. No two distinct tuples can have same values in all key fields, and 2. This is not true for any subset of the key. Part 2 false? A superkey. If there’s >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key. E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.

1515 L3 Relational Data Model Student and Course studentcourse enroll title cid credits sid name grade CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid))

1616 L3 Relational Data Model Primary and Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key.   “For a given student and course, there is a single grade.” vs. “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.”   Used carelessly, an IC can prevent the storage of database instances that arise in practice! CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid)) CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid), UNIQUE (cid, grade)) vs

1717 L3 Relational Data Model Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer’. E.g. sid is a foreign key referring to Student: Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references. Can you name a data model w/o referential integrity? Links in HTML! sidname cidtitle Student Course credits grade Enrolled sidcid

1818 L3 Relational Data Model 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 Student ) Enrolled Student

1919 L3 Relational Data Model Enforcing Referential Integrity Consider Student and Enrolled; sid in Enrolled is a foreign key that references Student. What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!) What should be done if a Student tuple is deleted? Also delete all Enrolled tuples that refer to it. Disallow deletion of a Student tuple that is referred to. Set sid in Enrolled tuples that refer to it to a default sid. (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’.) Similar if primary key of Student tuple is updated.

2020 L3 Relational Data Model Referential Integrity in SQL SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (also delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Student ON DELETE CASCADE ON UPDATE SET DEFAULT ) sidname cidtitle Student Course credits grade Enrolled sidcid

2121 L3 Relational Data Model Named Constraints Tip: give a name to constraints (this makes error messages clearer) CREATE TABLE Student ( sid INTEGER, …, CONSTRAINT Students_PK PRIMARY KEY (sid) ); CREATE TABLE Course ( cid CHAR(8), …, CONSTRAINT Courses_PK PRIMARY KEY (cid) ); CREATE TABLE Enroll ( sid INTEGER, cid CHAR(8), CONSTRAINT Enroll_FK1 FOREIGN KEY (sid) REFERENCES Students, CONSTRAINT Enroll_FK2 FOREIGN KEY (cid) REFERENCES Course, CONSTRAINT Enroll_PK PRIMARY KEY (sid,cid) ); studentcourse enroll title cid credits sid name

2222 L3 Relational Data Model Views A view is just a relation, but we store a definition, rather than a set of tuples. The definition is given by a query (see later lectures) which combine information from one or more tables Users can operate on the view as if it were a stored table DBMS calculates the value whenever it is needed CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21

2323 L3 Relational Data Model Defining the Schema Mathematics of Relations Logical Database Design Today’s Agenda

2424 L3 Relational Data Model Relation The model was first proposed by Dr. E.F. Codd of IBM in 1970 in the following paper: "A Relational Model for Large Shared Data Banks," Communications of the ACM, June The above paper caused a major revolution in the field of Database management and earned Ted Codd the coveted ACM Turing Award. The relational Model of Data is based on the mathematical concept of Relation. Studied in Discrete Mathematics The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

2525 L3 Relational Data Model Definition of Relation A Relation is a mathematical concept based on the ideas of sets. Formal definition Relation R Given sets D 1, D 2, …, D n, a relation R is a subset of D 1 x D 2 x…x D n. Thus, a relation is a set of n-tuples ( a 1, a 2, …, a n ) where a i  D i Example: If customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city = {Sydney, Brisbane, Perth} then R = { (Jones, Main, Sydney), (Smith, North, Perth), (Curry, North, Brisbane), (Lindsay, Park, Sydney) } is a relation over customer-name x customer-street x customer-city

2626 L3 Relational Data Model Relation Schema and Instance A relation R has a relation schema: specifies name of relation, plus name and data type of each attribute. A 1, A 2, …, A n are attributes R = (A 1, A 2, …, A n ) is a relation schema e.g. Students (sid: string, name: string, login: string, age: integer, gpa: real). an relation instance: a table, with rows and columns. D 1, D 2, …, D n are the domains each attribute corresponds to one domain: dom(A i ) = D i, 1 <= i <= n R  D 1 x D 2 x…x D n #rows = cardinality, #fields = degree.

2727 L3 Relational Data Model Defining the Schema Mathematics of Relations Logical Database Design Today’s Agenda

2828 L3 Relational Data Model Logical DB Design: ER to Relational Entity sets to tables: CREATE TABLE Employee (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Employee ssn name lot ssnnamelot Employee

2929 L3 Relational Data Model Relationship Sets to Tables In translating a relationship set to a relation, attributes of the relation must include: Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation. All descriptive attributes. CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employee, FOREIGN KEY (did) REFERENCES Department) ssnnamelot diddname Employee Department budget since Works_In ssndid

3030 L3 Relational Data Model Review: Key Constraints Each dept has at most one manager, according to the key constraint on Manages. Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1 dname budget did since lot name ssn Manages Employee Department

3131 L3 Relational Data Model Translating ER Diagrams with Key Constraints Map relationship to a table: Note that did is the key now! Separate tables for Employees and Departments. Since each department has a unique manager, we could instead combine Manages and Departments. CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employee, FOREIGN KEY (did) REFERENCES Department) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employee) OR

3232 L3 Relational Data Model 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 table must appear in a row of the Manages table (with a non-null ssn value!) lot name dname budgetdid since name dname budgetdid since Manages since Department Employee ssn Works_In

3333 L3 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 Employee, ON DELETE NO ACTION)

3434 L3 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. lot name age pname Dependent Employee ssn Policy cost

3535 L3 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 Employee, ON DELETE CASCADE )

3636 L3 Relational Data Model Multi-valued attributes Resolution of a Multi-valued attribute Finite & non-critical: split the attribute into multiple attributes Video Store Phone# Customer Video Store Home Phone# Customer Office Phone# Infinite & critical: create a new entity Phone Company Phone# Customer Phone Company CustomerPhone Phone# Phone type Servicetype

3737 L3 Relational Data Model Mapping Finite Multi-valued Attributes LicenceNoNameContactAge PILOT Identifier Attribute Licence Number ENTITY Multi-valued Attribute PILOT Attribute NameAge Contact Aircraft Type Endorsement Endorsement 1Endorsement 2

3838 L3 Relational Data Model Mapping Infinite Multi-valued Attributes LicenceNoNameContactAge PILOT Licence Number ENTITYPILOT NameAge Contact Aircraft Type Endorsement LicenceNo ENDORSEMENT Has

3939 L3 Relational Data Model Review: ISA Hierarchies   As in C++, or other PLs, attributes are inherited.   If we declare A ISA B, every A entity is also considered to be a B entity. Overlap constraints: Can Joe be an Hourly_Emp as well as a Contract_Emp entity? (Allowed/disallowed) Covering constraints: Does every Employee entity also have to be an Hourly_Emp or a Contract_Emp entity? (Yes/no) Contract_Emp name ssn Employee lot hourly_wages ISA Hourly_Emp contractid hours_worked

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

4141 L3 Relational Data Model Review: Binary vs. Ternary Relationships What are the additional constraints in the 2nd diagram? age pname Dependent Covers name Employee ssn lot Policy policyid cost Beneficiary age pname Dependent policyid cost Policy Purchaser name Employee ssn lot Bad design Better design

4242 L3 Relational Data Model Binary vs. Ternary Relationships (Contd.) The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents. Participation constraints lead to NOT NULL constraints. What if Policies is a weak entity set? CREATE TABLE Policy ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employee, ON DELETE CASCADE) CREATE TABLE Dependent ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policy, ON DELETE CASCADE)

4343 L3 Relational Data Model Wrap-Up Relational data model SQL DML, especially for constraints Mathematics of Relations Logical Database Design Transformation from ER to Relational