ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.

Slides:



Advertisements
Similar presentations
primary key constraint foreign key constraint
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.
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.
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)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data 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.
The Relational Model Ramakrishnan & Gehrke, Chap. 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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 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.
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
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.
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.
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.
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 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 Content based on Chapter 3
The Relational Model Chapter 3
The Relational Model Relational Data Model
The Relational Model Chapter 3
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 Content based on Chapter 3
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
Presentation transcript:

ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow Lim -- University of Hawaii at Manoa

Review ER model models the application data at the conceptual level – it does not assume any data model at the logical level A rigorous way to reason about ER is using set theory / Venn diagrams – Entity sets are collections of entities – Relationship sets are collections of edges connecting entities of entity sets Relational model – logical database design 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa2

Relational Database: Definitions Relational database: a set of relations Relation: made up of 2 parts: – Instance : a table, with rows and columns. #Rows = cardinality, #fields = degree / arity. – Schema : specifies name of relation, plus name and type of each column. E.G. Students(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). 1/19/20103Lipyeow Lim -- University of Hawaii at Manoa

Example Instance of Students Relation Cardinality = 3, degree=5, all rows distinct Do all columns in a relation instance have to be distinct? 1/19/20104Lipyeow Lim -- University of Hawaii at Manoa

Relational Query Languages A major strength of the relational model: simple, powerful querying of data. Queries can be written intuitively, and the DBMS is responsible for efficient evaluation. – The key: precise semantics for relational queries. – Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change. The SQL query language was developed by IBM (system R) in the 1970s – Standards: SQL-86, SQL-89 (minor revision), SQL-92 (major revision), SQL-99 (major extensions, current standard) 1/19/20105Lipyeow Lim -- University of Hawaii at Manoa

The SQL Query Language Syntax A simple SQL query takes the following form: SELECT FROM WHERE Conditions can be a boolean combination using AND, OR, NOT SQL queries can be nested into the FROM and WHERE clauses Conceptually, results of a SQL query is also a relation 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa6

Example: SQL Query on Single Table To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 To find just names and logins: SELECT S.name, S.login FROM Students S WHERE S.age=18 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa7

Querying Multiple Relations What does the following query compute? SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=“A” Given the following instances of Enrolled and Students: we get:

Creating Relations in SQL Creates the Students 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 Students (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)) 1/19/20109Lipyeow Lim -- University of Hawaii at Manoa

Integrity Constraints (ICs) IC: condition that must be true for any instance of the database; e.g., domain constraints. – ICs are specified when schema is defined. – ICs are checked when relations are modified. A legal instance of a relation is one that satisfies all specified ICs. – DBMS should not allow illegal instances. If the DBMS checks ICs, stored data is more faithful to real-world meaning. – Avoids data entry errors, too! 1/19/201010Lipyeow Lim -- University of Hawaii at Manoa

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. 1/19/201011Lipyeow Lim -- University of Hawaii at Manoa

Primary and Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE ), one of which is chosen as the primary key. 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) ) 1/19/201012Lipyeow Lim -- University of Hawaii at Manoa “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!

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 Students: – 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! 1/19/201013Lipyeow Lim -- University of Hawaii at Manoa

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 Students 1/19/201014Lipyeow Lim -- University of Hawaii at Manoa

Logical DB Design: ER to Relational Customers Credit Cards has 1m Customers Menu Items Orders mn

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

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 Employees, FOREIGN KEY (did) REFERENCES Departments)

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 Employees Departments

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

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 Dependents Employees ssn Policy cost 1/19/201020Lipyeow Lim -- University of Hawaii at Manoa

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 ) 1/19/201021Lipyeow Lim -- University of Hawaii at Manoa

Hourly_ wages Schema Refinement 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa22 Hourly_Emps Suppose hourly wages are determined by rating Redundant storage : (8,10) stored multiple times Update anomaly : change hourly wages in row 1 Insertion anomaly : requires knowing hourly wages for the rating Deletion anomaly : deleting all (8,10) loses info SSNNameLotRatingHourly _wages Hours_ worked Attishoo Smiley Smethurst Guldu Madayan Hourly_Emps SSN Name Lot Rating Hours_ worked

Using Two Smaller Tables Notation: denote relation schema by listing the attributes SNLRWH Update anomaly : Can we change W for Attishoo? Insertion anomaly : What if we want to insert an employee and don’t know the hourly wage for his rating? Deletion anomaly : If we delete all employees with rating 5, do we lose the information about the wage for rating 5? 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa23 SSNNameLotRatingHours_ worked Attishoo Smiley Smethurst Guldu Madayan35840 RatingHourly_ wages Hourly_EmpsRatingWages Hourly _Emps Rating Wages m1

Decomposition Remove redundancy by decomposition – Since hourly wage is completely determined by rating, factor out hourly wage. Pros: less redundancy less anomalies Cons: retrieving the hourly wage of an employee requires a join 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa24 SSNNameLotRatingHours_ worked Attishoo Smiley Smethurst Guldu Madayan35840 RatingHourly_ wages Hourly_EmpsRatingWages

Functional Dependency A functional dependency X -> Y holds over relation R if, for every allowable instance r of R: – for all tuples t1,t2 in r, π X (t1) = π X (t2) implies π Y (t1) = π Y (t2) – i.e., given two tuples in r, if the X values agree, then the Y values must also agree. (X and Y are sets of attributes.) An FD is a statement about all allowable relations. – Must be identified based on semantics of application. – Given some allowable instance r1 of R, we can check if it violates some FD f, but we cannot tell if f holds over R! K is a candidate key for R means that K -> R – However, K -> R does not require K to be minimal! 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa25

FD Example Two FDs on Hourly_Emps: – ssn is the key: S -> SNLRWH – rating determines hourly_wages: R -> W 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa26 Hourly_Emps SSNNameLotRatingHourly_wagesHours_worked Attishoo Smiley Smethurst Guldu Madayan

Reasoning about FDs Given some FDs, we can usually infer additional FDs: – ssn -> did, did -> lot implies ssn -> lot Armstrong’s Axioms – Let X, Y, Z are sets of attributes: – Reflexivity: If X is a subset of Y, then Y -> X – Augmentation: If X -> Y, then XZ -> YZ for any Z – Transitivity: If X -> Y and Y -> Z, then X -> Z These are sound and complete inference rules for FDs! 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa27

Example: Armstrong’s Axioms Reflexivity: If X is a subset of Y, then Y -> X – SNLR is a subset of SNLRWH, SNLRWH -> SNLR Augmentation: If X -> Y, then XZ -> YZ for any Z – S -> N, then SLR -> NLR Transitivity: If X -> Y and Y -> Z, then X -> Z – S -> R, R -> W, then S -> W 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa28 Hourly_Emps SSNNameLotRatingHourly_WagesHours_worked Attishoo Smiley Smethurst Guldu Madayan

Preparations for next class Install DB2 Express-C edition on your laptops by Thursday’s class Bring your laptops to class on Thursday. 1/19/2010Lipyeow Lim -- University of Hawaii at Manoa29