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.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
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.
COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model.
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.
The Relational Model Ramakrishnan & Gehrke Chapter 3.
The Relational Model CS 186, Fall 2006, Lecture 2 R & G, Chap. 3.
The Relational Model CS 186, Spring 2006, Lecture 2 R & G, Chap. 1 & 3.
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.
CAS CS 460 Relational Model Based on Slides created by Prof. Mitch Cherniack, Brandeis University and from Prof. Joe.
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 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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Levels of Abstraction in DBMS data Many views, single conceptual (logical) schema and physical schema. – Views describe how users see the data (possibly.
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)
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.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Jianping Fan.
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.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
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 CMU SCS /615 C. Faloutsos – A. Pavlo
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
Presentation transcript:

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 keys, primary keys lot name Employees ssn Works_In since dname budget did Departments

1.3 Review n Weak Entity sets, identifying relationship n Discriminator, total participation, one-to-many Loan lno lamt Loan_Pmt Payment pno pdate pamt

1.4 Review Generalization-specialization Aggregation E1 a1 a2 S2 c1 b1 Isa S1 superclass subclasses E1 E2 R1 R2 E3

1.5 Review Data models: framework for organizing and interpreting data E/R Model OO, Object relational, XML Relational Model  Intro  E/R to relational  SQL preview

1.6 Relational Data Model Introduced by Ted Codd (early 70’) (Turing Award, ‘81) Relational data model contributes: 1.Separation of logical and physical data models (data independence) 2.Declarative query languages 3.Formal semantics 4.Query optimization (key to commercial success) First prototypes:  Ingres -> postgres, informix (Stonebraker, UC Berkeley) Turing Award 2015!!!!!!  System R -> Oracle, DB2 (IBM) (Jim Gray, Turing Award 1998)

1.7 Relations account = Rows (tuples, records) Columns (attributes) Tables (relations) Why relations?

1.8 Relations Mathematical relations (from set theory): Given 2 sets R={ 1, 2, 3, 5}, S={3, 4}  R x S = {(1,3), (1, 4), (2, 3), (2,4), (3,3), (3,4), (5,3), (5,4)}  A relation between R and S is any subset of R x S e.g., {(1,3), (2,4), 5,3)} Database relations: Given attribute domains: bname = {Downtown, Brighton, ….} acct_no = { A-101, A-102, A-203, …} balance = { …, 400, 500, …} account subset of bname x acct_no x balance { (Downtown, A-101, 500), (Brighton, A-202, 450), (Brookline, A-312, 600)}

1.9 Storing Data in a Table Data about individual students One row per student How to represent course enrollment?

1.10 Storing More Data in Tables Students may enroll in more that one course Most efficient: keep enrollment in separate table Enrolled

1.11 Linking Data from Multiple Tables How to connect student data to enrollment? Need a Key Enrolled Students

1.12 Relational Data Model: Formal Definitions Relational database: a set of relations. Relation: made up of 2 parts:  Instance : a table, with rows and columns.  #rows = cardinality  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)  #fields = degree / arity Can think of a relation as a set of rows or tuples.  i.e., all rows are distinct

1.13 In other words... Data Model – a way to organize information Schema – one particular organization,  i.e., a set of fields/columns, each of a given type Relation  a name  a schema  a set of tuples/rows, each following organization specified in schema

1.14 Example Instance of Students Relation Cardinality = 3, arity (degree) = 5, all rows distinct

1.15 SQL - A language for Relational DBs SQL: standard language (based on SEQUEL in System R (IBM now DB2)) Data Definition Language (DDL)  create, modify, delete relations  specify constraints  administer users, security, etc. Data Manipulation Language (DML)  Specify queries to find tuples that satisfy criteria  add, modify, remove tuples

1.16 SQL Overview n CREATE TABLE (, … ) n INSERT INTO ( ) VALUES ( ) n DELETE FROM WHERE n UPDATE SET = WHERE n SELECT FROM WHERE

1.17 Creating Relations in SQL Creates the Students relation. Note: the type (domain) of each field is specified, and enforced by the DBMS  whenever tuples are added or modified. Another example: the Enrolled table holds information about courses 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.18 Adding and Deleting Tuples Can insert a single tuple using: INSERT INTO Students (sid, name, login, age, gpa) VALUES ( ‘ ’, ‘ Smith ’, ‘ ’, 18, 3.2) Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students S WHERE S.name = ‘ Smith ’  Powerful variants of these commands are available; more later!

1.19Keys Integrity Constraints (IC): conditions that restrict the data that can be stored in the database Keys are a way to associate tuples in different relations Keys are one form of integrity constraint (IC) Students Enrolled

1.20 Primary Keys - Definitions Key: A minimal set of attributes that uniquely identify a tuple A set of fields is a superkey if:  No two distinct tuples can have same values in all key fields A set of fields is a candidate key for a relation if :  It is a superkey  No subset of the fields is a superkey >1 candidate keys 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.21 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)) “ 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 storage of database instances that should be permitted! CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade))

1.22 Foreign Keys A Foreign Key is a field whose values are keys in another relation. Students Enrolled

1.23 Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation used to `refer’ to tuples in another relation.  Must correspond to primary key of the second relation.  Like a `logical pointer’. E.g. sid in Enrolled 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.)

1.24 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.25 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.26 E/R to Relations 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 )

1.27 More on relationships What about: Could have : since a 1 is the key for R1 (also for E1=(a 1, …., a n )) Another option is to merge E1 and R1  ignore R1  Add b1, c1, …., ck to E1 instead, i.e.  E1=(a 1, …., a n, b 1, c 1, …, c k ) E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m R1= ( a 1, b 1, c 1, …, c k )

1.28 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, b 1, c 1, …, c k ) E2 = ( b 1, …, b m ) E1 = ( a 1, …, a n ) E2 = ( b 1, …, b m, a 1, c 1, …, c k ) Treat as n:1 or 1:m

1.29 E/R to Relational 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 ) Emp ssn Multivalued Attributes name dept Emp = (ssn, name) Emp-Dept = (ssn, dept)

1.30 E/R to Relational 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 ) Q: When is method 2 not possible?

1.31 E/R to Relational Aggregation E1 E2 R1 R2 E3 a 1 …. a n b 1 …. b m c 1 …. c k d1…djd1…dj E1, R1, E2, E3 as before R2 = (c 1, a 1, b 1, d 1, …, d j )