The Relational Model Relational Data Model

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.
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 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.
D ATABASE S YSTEMS I T HE R ELATIONAL D ATA M ODEL.
The Relational Model Ramakrishnan & Gehrke Chapter 3.
The Relational Model CS 186, Fall 2006, Lecture 2 R & G, Chap. 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.
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 Instructor: Mohamed Eltabakh
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.
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)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CMPT 258 Database Systems The Relationship 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.
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
These slides are based on the slides of your text book.
CS 186, Fall 2006, Lecture 2 R & G, Chap. 3
The Relational Model Lecture 3 INFS614, Fall
The Relational Model Chapter 3
The Relational Model Chapter 3
Database Applications (15-415) The Relational Model Lecture 3, August 28, 2016 Mohammad Hammoud.
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
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.
Database Systems (資料庫系統)
More on ER Model and the Relationship Model
The Relational Model Chapter 3
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
Presentation transcript:

The Relational Model Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) 1

Why Study the Relational Model? Most widely used model in Commercial DBMSs: Vendors: IBM, Microsoft, Oracle, … Competitors Object-oriented model e.g., O2, but also Oracle, DB2 XML Model e.g. Natix, but also IBM, Oracle Key-Value Stores/NonSQL e.g. BigTable, MongoDB, etc. 2

Example Instance of Students Relation relation = rows (tuples) and columns (attributes). example: Cardinality = 3, degree = 5. Set not an ordered collection. 4

Relational Query Languages (SQL) Developed by IBM (system R) in the 1970s Need for a standard since used by many vendors Standards: SQL-86 SQL-89 (minor revision) SQL-92 (major revision) SQL-99 (major extensions, current standard) . . .

Relational Query Languages (SQL) A major strength of the relational model: supports simple, powerful querying of data. DBMS is responsible for efficient evaluation. SQL = DDL + DML + …… (Chap 5) 9

DDL ---- CREATE TABLE CREATE TABLE Students. (sid: CHAR(20), DDL ---- CREATE TABLE 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)) 15

DDL --- Altering Relations DROP TABLE Students Destroys the relation Students. The schema information and the tuples are deleted. ALTER TABLE Students ADD COLUMN firstYear: integer The schema of Students is altered by adding a new field (null value assigned) 16

DML --- Query single relation To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18

DML --- Querying Multiple Relations SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=“A” we get:

DML --- Adding and Deleting Tuples Can insert a single tuple : INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2) Delete tuples: DELETE FROM Students S WHERE S.name = ‘Smith’ 10

Integrity Constraints (ICs) IC condition that must be true for any instance of the database Fundamental constraints : Key Foreign Key, Domain Constraints General constraints: Single table constraints and triggers 5

Primary and Candidate Keys in SQL One primary key, and Zero to many candidate keys (specified using UNIQUE), 6

Primary and Candidate Keys in SQL Candidate keys and primary key: CREATE TABLE Student (sid CHAR(20), ssn CHAR(20), name CHAR(20), gpa INTEGER, PRIMARY KEY (sid), UNIQUE (ssn) ) 6

Primary and Candidate Keys in SQL Candidate keys and primary key used carelessly : CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid) ) 6

Primary and Candidate Keys in SQL Candidate keys and primary key used carelessly: CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) 6

Primary and Candidate Keys in SQL Students can take only one course, and receive a single grade for that course. No two students in a course receive the same grade. CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) Used carelessly, an IC can prevent the storage of database instances that arise in practice! 6

Primary and Candidate Keys in SQL Candidate keys and primary key : CREATE TABLE Enrolled ( sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) ) 6

Primary and Candidate Keys in SQL “For a given student and course, there is a single grade.” CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) ) 6

Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to "refer" to a tuple in another relation. (Like a `logical pointer’.) Foreign key : FK in referencing relation must match PK of referenced relation. Match = same number of columns, compatible data types (column names can be different). 7

Foreign Keys, Referential Integrity Foreign key : Enrolled.sid  Students.sid Enrolled (referencing relation) Students (referenced relation) Primary Key Foreign Key 7

Foreign Keys in SQL Only students listed in 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 ) If all foreign key constraints are enforced, then referential integrity is achieved, i.e., no dangling references. 7

Enforcing Referential Integrity sid in Enrolled is a foreign key that references Students. Insertion: What if a new Student tuple is inserted? Insertion: What should be done if an Enrolled tuple with a non-existent student id is inserted? Reject it Enrolled (referencing relation) Students (referenced relation) Primary Key Foreign Key 13

Enforcing Referential Integrity Enrolled (referencing relation) Students (referenced relation) Deletion: What if an Enrolled tuple is deleted? Primary Key Foreign Key 13

Enforcing Referential Integrity Enrolled (referencing relation) Students (referenced relation) Deletion: What if a Student tuple is deleted? Cascading -- Also delete all Enrolled tuples that refer to it. No Action -- Disallow deletion of a Students tuple that is referred to. Set Default -- Set sid in Enrolled tuples that refer to it to a default sid. Set Null -- Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’. (Not always applicable) Similar if primary key of Students tuple is updated. Primary Key Foreign Key 13

Referential Integrity in SQL CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students (sid) ON DELETE CASCADE ON UPDATE SET DEFAULT ) 14

Referential Integrity in SQL 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) 14

Relational Model: Summary Tabular representation of data. Simple, intuitive and widely used Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks for violations. Two important ICs: primary and foreign keys Powerful ‘natural’ query language exist. 15