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

Similar documents
Relational data model

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

The Relational Data Model. Data Model

The Relational Model. Chapter 3

Database Management Systems. Chapter 3 Part 1

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model of Data (ii)

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Database Applications (15-415)

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

The Relational Model. Week 2

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

The Relational Model 2. Week 3

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Database Systems ( 資料庫系統 )

The Relational Model

CIS 330: Applied Database Systems

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

The Relational Model

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

The Relational Model. Database Management Systems

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Keys, SQL, and Views CMPSCI 645

DBMS. Relational Model. Module Title?

Database Systems. Course Administration

Review: Where have we been?

Relational Query Languages

SQL: The Query Language Part 1. Relational Query Languages

CS425 Midterm Exam Summer C 2012

CS 2451 Database Systems: Relational Data Model

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

Database Management Systems. Session 2. Instructor: Vinnie Costa

Introduction to Data Management. Lecture #4 E-R Model, Still Going

CS275 Intro to Databases

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

Modern Database Systems Lecture 1

A Few Tips and Suggestions. Database System II Preliminaries. Applications (contd.) Applications

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

Relational Model. CSC 343 Winter 2018 MICHAEL LIUT

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

L3 The Relational Model. Eugene Wu Fall 2018

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

CSEN 501 CSEN501 - Databases I

Modern Database Systems CS-E4610

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

A Few Tips and Suggestions. Database System I Preliminaries. Applications (contd.) Applications

A Few Tips and Suggestions. Database System II Preliminaries. Applications (contd.) Applications

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

CMPT 354: Database System I. Lecture 3. SQL Basics

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Introduction to Database Systems

Lecture 2 08/26/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Introduction to Databases

CS5300 Database Systems

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Introduction to Database Systems CSE 414

The Relational Model Constraints and SQL DDL

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

Data! CS 133: Databases. Goals for Today. So, what is a database? What is a database anyway? From the textbook:

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

Page 1. Quiz 18.1: Flow-Control" Goals for Today" Quiz 18.1: Flow-Control" CS162 Operating Systems and Systems Programming Lecture 18 Transactions"

Outer Join, More on SQL Constraints

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 377 Database Systems

CSC 453 Database Technologies. Tanu Malik DePaul University

CS430/630 Database Management Systems Spring, Betty O Neil University of Massachusetts at Boston

The Relational Model (ii)

Score. 1 (10) 2 (10) 3 (8) 4 (13) 5 (9) Total (50)

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

The Relational Data Model and Relational Database Constraints

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Introduction to Data Management CSE 344

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

CSC 742 Database Management Systems

Chapter 5. Relational Model Concepts 5/2/2008. Chapter Outline. Relational Database Constraints

Chapter 5. Relational Model Concepts 9/4/2012. Chapter Outline. The Relational Data Model and Relational Database Constraints

Chapter 1 SQL and Data

Databases. Jörg Endrullis. VU University Amsterdam

CMPT 354: Database System I. Lecture 2. Relational Model

CS2300: File Structures and Introduction to Database Systems

Transcription:

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

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 sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 relation = rows (tuples) and columns (attributes). example: Cardinality = 3, degree = 5. Set not an ordered collection. 3

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

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

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

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

DML --- Query single relation To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@ee 18 3.2 10

DML --- Querying Multiple Relations SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade= A sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B we get: S.name Smith E.cid Topology112 11

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 12

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 13

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

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

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

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

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! 20

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

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

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

Foreign Keys, Referential Integrity Foreign key : Enrolled.sid Students.sid Enrolled (referencing relation) sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Foreign Key Primary Key Students (referenced relation) sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 25

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. 26

Enforcing Referential Integrity sid in Enrolled is a foreign key that references Students. Enrolled (referencing relation) sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Foreign Key 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 Primary Key Students (referenced relation) sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 27

Enforcing Referential Integrity Enrolled (referencing relation) sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Foreign Key Primary Key Students (referenced relation) sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Deletion: What if an Enrolled tuple is deleted? 28

Enforcing Referential Integrity Enrolled (referencing relation) sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Foreign Key Primary Key Students (referenced relation) sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 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. 29

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

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

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. 35