Database Management Systems. Chapter 3 Part 1

Similar documents
Relational data model

The Relational Data Model. Data Model

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

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

The Relational Model. Chapter 3

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

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

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

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

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

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

The Relational Model of Data (ii)

Database Applications (15-415)

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

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

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

Database Systems ( 資料庫系統 )

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

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

The Relational Model. Week 2

CIS 330: Applied Database Systems

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

The Relational Model

The Relational Model

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

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

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

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

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

Keys, SQL, and Views CMPSCI 645

Database Systems. Course Administration

The Relational Model. Database Management Systems

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL: The Query Language Part 1. Relational Query Languages

Database Management Systems. Session 2. Instructor: Vinnie Costa

DBMS. Relational Model. Module Title?

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

Review: Where have we been?

Modern Database Systems Lecture 1

CS425 Midterm Exam Summer C 2012

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Relational Query Languages

CS275 Intro to Databases

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

CS 2451 Database Systems: Relational Data Model

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

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

Modern Database Systems CS-E4610

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

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

The Relational Model (ii)

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

Relational Model. CSC 343 Winter 2018 MICHAEL LIUT

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

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

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

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

Introduction to Database Systems

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

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

CSEN 501 CSEN501 - Databases I

Outer Join, More on SQL Constraints

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

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

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

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

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL

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

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

IT360: Applied Database Systems. Slide Set: #2. Relational Model (Chapter 3 Kroenke) The relational model Relational model terminology

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

Introduction to Data Management. Lecture #1 (The Course Trailer )

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

Goals for Today. CS 133: Databases. Relational Model. Multi-Relation Queries. Reason about the conceptual evaluation of an SQL query

L3 The Relational Model. Eugene Wu Fall 2018

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

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

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

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

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

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

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

CSC 742 Database Management Systems

Databases. Jörg Endrullis. VU University Amsterdam

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

SQL. Chapter 5 FROM WHERE

CS5300 Database Systems

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Database Management Systems. Chapter 1

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

The Relational Model Constraints and SQL DDL

Database Systems ( 資料庫系統 ) Practicum in Database Systems ( 資料庫系統實驗 ) 9/20 & 9/21, 2006 Lecture #1

BBM371- Data Management. Lecture 1: Course policies, Introduction to DBMS

Introduction to Data Management. Lecture #1 (Course Trailer )

Introduction to Data Management. Lecture #1 (Course Trailer )

Transcription:

Database Management Systems Chapter 3 Part 1 The Relational Model Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. Simple data representation and it can express complex queries. Recent competitor: object-oriented model ObjectStore, Versant, Ontos A synthesis emerging: object-relational model Informix Universal Server, UniSQL, O2, Oracle, DB2 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Relational Database: Definitions Relational database: a set of relations with distinct names. Relation: made up of 2 parts: Instance : a table, with rows and columns. #Rows = cardinality, #fields = degree. Schema : specifies name of relation, plus name and domain/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). Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Example Instance of Students Relation sid name login dob gpa 53666 Jones jones@cs 2000-09-01 3.4 53688 Smith smith@eecs 2001-08-01 3.2 53650 Smith smith@math 2000-10-10 3.8 Cardinality = 3, degree = 5, all rows distinct The order of fields does not matter if the fields are named. A relation is a set of rows, so the order of rows is not important. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Relational Query Languages A major strength of the relational model: supports 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. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 The SQL Query Language Developed by IBM (system R) in the 1970s Need for a standard since it is used by many vendors Standards: SQL-86 SQL-89 (minor revision) SQL-92 (major revision) SQL-99 (major extensions, current standard) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

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. CREATE TABLE Students (sid CHAR(20), name CHAR(30), login CHAR(20), DOB DATE, gpa REAL); string type with different length Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Creating Relations in SQL As another example, the Course table holds information about courses that students take. CREATE TABLE Course (cid CHAR(20), cname CHAR (20), description CHAR( 100) DEFAULT Course Description ); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Creating Relations in SQL As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(10)); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Adding Tuples We can insert a single tuple into the Students table as follows: INSERT INTO Students (sid, name, login, DOB, gpa) VALUES ( 53666, Jones, jones@cs, 2000-09-01, 3.4); sid name login DOB gpa 53666 Jones jones@cs 2000-09-01 3.4 53688 Smith smith@eecs 2001-08-01 3.2 53650 Smith smith@math 2000-10-10 3.8 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Deleting Tuples We can delete all tuples satisfying some condition (e.g., name = Smith): DELETE S FROM Students S WHERE S.name = Smith ; sid name login dob gpa 53666 Jones jones@cs 2000-09-01 3.4 53688 Smith smith@eecs 2001-08-01 3.2 53650 Smith smith@math 2000-10-10 3.8 Powerful variants of these commands are available; more later! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Updating Tuples We can modify the column values in an existing row: UPDATE Students S SET S.gpa = S.gpa -1 WHERE S.sid = 53688; Old value sid name login Dob gpa 53666 Jones jones@cs 2000-09-01 3.4 53688 Smith smith@eecs 2001-08-01 3.2 53650 Smith smith@math 2000-10-10 3.8 2.2 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

Updating Tuples Where clause is applied first and determines which rows to be modified. UPDATE Students S SET S.gpa = S.gpa - 0.1 WHERE S.gpa >= 3.3; sid nam e login age gpa 53666 Jones jones@ cs 18 3.4 53688 Sm ith sm ith@ eecs 18 3.2 53650 Sm ith sm ith@ m ath 19 3.8 3.3 3.7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Integrity Constraints (ICs) An IC is a condition specified on a database schema and restricts the data that can be stored in an instance of the database, such as 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! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Primary Key Constraints A set of fields is a candidate key for a relation if : 1. No two distinct tuples can have same values in all key fields, and 2. No subset of the set of fields in a key is a unique identifier for a tuple. Part 2 false? A superkey (a set of fields containing a key). 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. Every relation is guaranteed to have a key. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Primary and Candidate Keys in SQL UNIQUE: Candidate keys (you could have many) PRIMARY KEY: one of candidate keys is chosen as the primary key. CREATE TABLE Students (sid CHAR(20), name CHAR(30), login CHAR(20), age INTEGER, gpa REAL, UNIQUE (name, age), PRIMARY KEY (sid)); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Name a constraint We could name a constraint in a table. If the constraint is violated, the constraint name is returned and can be used to identify the error. CONSTRAINT constraint-name CREATE TABLE Students (sid CHAR(20), name CHAR(30), login CHAR(20), dob DATE, gpa REAL, UNIQUE (name, dob), CONSTRAINT StudentsKey PRIMARY KEY (sid)); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Primary and Candidate Keys in SQL 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) ); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18

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. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 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(sid) ); Enrolled sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Students 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 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

Enforcing Integrity Constraints ICs are specified when a relation is created and enforced when a relation is modified. Violating the primary key constraint, the transaction will be rejected. Inserting a tuple with an existing sid. INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, Mike, mike@ee, 17, 3.4); Inserting a tuple with primary key as null. INSERT INTO Students (sid, name, login, age, gpa) VALUES (null, Mike, mike@ee, 17, 3.4); Updating a tuple with an existing sid. UPDATE Students S SET S.sid = 50000 WHERE S.sid=53688; Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 Enforcing Integrity Constraints Deletion of Enrolled tuples do not violate referential integrity, but insertions could. Inserting a tuple with an un-exist sid in Students. INSERT INTO Enrolled (sid, cid, grade) VALUES (51111, Hindi101, B ); Insertion of Students tuples do not violate referential integrity, but deletions could. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22

Ways to handle foreign key violations If an Enrolled row with un-existing sid is inserted, it is rejected. If a Students row is deleted/updated, Option 1: Delete/Update all Enrolled rows that refer to the deleted sid in Students (CASCADE). Both are affected Option 2: Reject the deletion/updating of the Students row if an Enrolled row refers to it (NO ACTION ). [The default action for SQL]. None is affected. Option 3: Set the sid of Enrolled to some existing (default) sid value in Students for every involved Enrolled row (SET NULL / SET DEFAULT ). Both are affected. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23 Referential Integrity in SQL When a Students row is deleted, all Enrolled rows that refer to it are to be deleted as well. When a Students sid is modified, the update is to be rejected if an Enrolled row refers to the modified Students row. 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 NO ACTION ); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24

The SQL Query Language A relational database query is a question about the data, and the answer consists of a new relation containing the result. To find information about all 17 years old students : SELECT * FROM Students S WHERE year(s.dob)=2000; To find just names and logins: sid name login Dob gpa 53666 Jones jones@cs 2000-09-01 3.4 53650 Smith smith@math 2000-10-10 3.8 SELECT S.name, S.login FROM Students S WHERE year(s.dob)=2000; name Jones Smith login jones@cs smith@ee Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25 Destroying and Altering Relations Destroys the relation Students. The schema information and the tuples are deleted. DROP TABLE Students; The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. ALTER TABLE Students ADD COLUMN firstyear: integer; Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26

Views A view is just a relation, but we store a definition, rather than a set of tuples. 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; Views can be dropped using the DROP VIEW command. How to handle DROP TABLE if there s a view on the table? View becomes invalid if its base table is dropped. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27 Views and Security Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s). Given YoungStudents, but not Students or Enrolled, we can find students s who have are enrolled, but not the cid s of the courses they are enrolled in. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 28