intro to database systems and relational modelrelational query languages na major strength of the...

36
Intro to Database Systems and Relational Model George Kollios Boston University (based on the slides by Feifei Li, U of Utah)

Upload: others

Post on 26-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Intro to Database Systemsand Relational Model

George KolliosBoston University

(based on the slides by Feifei Li, U of Utah)

Page 2: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

About the course – Administrivian Instructor:

– George Kollios, [email protected] 104, Tue 12:30-2:00 and Wed 1:00-2:30

n Teaching Fellow:– Xiao Zhou, [email protected]– Ugrad Lab, 730 Coom Ave, Room 302

Thu 3:15-5:15 and Fri 9:00-11:00

n Home Page:– http://www.cs.bu.edu/fac/gkollios/cs660f19

Check frequently! Syllabus, schedule, assignments, announcements…

– Piazza site for discussions, etc– Blackboard from Student link for grades

Page 3: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

TextbookRaghu Ramakrishnan and Johannes Gehrke, "DatabaseManagement Systems", McGraw-Hill, Third Edition. 2002.

Page 4: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Grading (Tentative)

(Tentative Grading)■ Written Homeworks: 15%■ Midterm: 20%■ Final: 25%■ Programming Assignments: 40%

■ See next

Page 5: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Workload

n Set of Projects:– “SimpleDB” projects from MIT/UW – Done individually or in pairs– Java-based implementations of key DBMS functions– 5(or 4) project phases:

• Files, Operators, Optimizer, Transactions, Recoveryn Some Written Homeworksn Exams – 1 Midterm & 1 Final

Page 6: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Why Study Databases??

Big Data Revolution:

“Between the dawn of civilization and 2003, we only created five exabytes of information; now we’re creating that amount every two days.” Eric Schmidt, Google

Page 7: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

“Big Data” Buzz

Page 8: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

“Big Data” Buzz

0

100

200

300

400

500

600

700

800

900

1000

Manufacturing

Government

Communications

Banking

Healthcare

Retail

Education

Transportation

966

848

715

619

434364

269227

Amount of Stored Data By Sector(in Petabytes, 2009)

Source:"Big Data: The Next Frontier for Innovation, Competition and Productivity."US Bureau of Labor Statistics | McKinsley Global Institute Analysis

Page 9: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

DB System Landscape

9

https://db-engines.com/en/ranking

Page 10: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

What’s a database management system (DBMS)?

10

Page 11: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

What’s a database management system (DBMS)?Application Interface

SQL InterpreterQuery Evaluator

Plan Generator

Plan Executor

Plan Optimizer

Data AccessTransaction

ManagerBuffer

ManagerSecurityManager

File & IndexManager

Database

Data Files

IndexFiles

System & MetadataFiles

Crash Recovery

Concurrency Control

11

Page 12: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Highlights

n Why use a DBMS? OS provides RAM and disk– Concurrency– Recovery– Abstraction, Data Independence– Query Languages (declarative language: specify what you want, but not how to do it)– Efficiency (for most tasks)– Security– Data Integrity

12

Page 13: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Data Models

n DBMS models real worldn Data Model is the link between user’s view

of the world and bits stored in DBMSn Many models existn We will concentrate on the Relational

Model

1010111101

Students(sid: string, name: string, login: string, age: integer, gpa:real)

13

Page 14: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Why Study the Relational Model?

n Most widely used model for structured data.– Vendors: IBM DB2, Microsoft SQL Server, Oracle, MySQL, PostgreSQL, etc.

n Have motivated the design of many other data models for structured/semi-structured data.– XML, JSON

n Is the basis for understanding many models for unstructured data.

14

Page 15: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

ANSI/SPARC Model

n Views describe how users see the data.

n Conceptual schema defines logical structure

n Physical schema describes the files and indexes used.

Physical Schema

Conceptual Schema

View 1 View 2 View 3

DB

Users

Page 16: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Relational Database: Definitions

n Relational database: a set of relations.n 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• type: specify the domain of a field (the set of possible valid values a field may take)

n Can think of a relation as a set of rows or tuples. – i.e., all rows are distinct

16

Page 17: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

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

• Cardinality = 3, arity = 5 , all rows distinct

• Do all values in each column of a relation instance have to be distinct?

17

Page 18: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

SQL - A language for Relational DBs

n SQL: standard language for DBMS, structured query languagen Data Definition Language (DDL)

– create, modify, delete relations– specify constraints– administer users, security, etc.

n Data Manipulation Language (DML)– Specify queries to find tuples that satisfy criteria– add, modify, remove tuples

18

Page 19: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

SQL Overview

n CREATE TABLE <name> ( <field> <domain>, … )

n INSERT INTO <name> (<field names>)VALUES (<field values>)

n DELETE FROM <name> WHERE <condition>

n UPDATE <name> SET <field name> = <value>WHERE <condition>

n SELECT <fields> FROM <name>WHERE <condition>

19

Page 20: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Creating Relations in SQL

n Creates the Students relation.n Note: the type (domain) of each field is specified, and enforced by the DBMS

– whenever tuples are added or modified. n 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 FLOAT)

CREATE TABLE Enrolled(sid CHAR(20), cid CHAR(20), grade CHAR(2))

20

Page 21: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Adding and Deleting Tuples

n Can insert a single tuple using:

n Can delete a single (or a set of) tuple(s) using:

INSERT INTO Students (sid, name, login, age, gpa)VALUES (‘53688’, ‘Smith’, ‘smith@ee’, 18, 3.2)

DELETE FROM Students SWHERE S.name = ‘Smith’

✦ Powerful variants of these commands are available; more later!

21

Page 22: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Keys

n Keys are a way to associate tuples in different relationsn Keys are one form of integrity constraint (IC)

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B

EnrolledStudents

22

Page 23: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Primary Keysn A set of fields is a superkey if:

– No two distinct tuples can have same values in all key fieldsn A set of fields is a key for a relation if :

– It is a superkey– No proper subset of the fields is a superkey

n >1 key for a relation?– one of the keys is chosen (by DBA) to be the primary key.

n E.g.– sid is a key for Students. – What about name?– The set {sid, gpa} is a superkey.

23

Page 24: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Primary and Candidate Keys in SQLn 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.” CREATE TABLE Enrolled

(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY(sid,cid),UNIQUE (cid, grade))

24

Page 25: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Foreign Keys

n A Foreign Key is a field whose values are keys in another relation (think about them as “pointers”).

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B

EnrolledStudents

25

Page 26: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Foreign Keys, Referential Integrity

n 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’.

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

26

Page 27: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Foreign Keys in SQL

n 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 name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B

EnrolledStudents

27

Page 28: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Integrity Constraints (ICs)

n IC: condition that must be true for any instance of the database; e.g., domain constraints (i.e., types).

– ICs are specified when schema is defined.– ICs are checked when relations are modified.

n A legal instance of a relation is one that satisfies all specified ICs. – DBMS should not allow illegal instances.

n If the DBMS checks ICs, stored data is more faithful to real-world meaning.– Avoids data entry errors, too!

28

Page 29: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Where do ICs Come From?

n ICs are based upon the semantics of the real-world that is being described in the database relations.

n We can check a database instance to see if an IC is violated, but we can NEVER infer that an IC is true by looking at an instance.

– An IC is a statement about all possible instances!– From example, we know name is not a key, but the assertion that sid is a key is given to us.

n Key and foreign key ICs are the most common; more general ICs supported too.

29

Page 30: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Enforcing Referential Integrity

n Remember Students and Enrolled; sid in Enrolled is a foreign key that references Students.

n What should be done if an Enrolled tuple with a non-existent student id is inserted? – (Reject it!)

n What should be done if a Students tuple is deleted?– Also delete all Enrolled tuples that refer to it.

– Disallow deletion of a Students tuple that is referred to.– Set sid in Enrolled tuples that refer to it to a default sid.– (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting

`unknown’ or `inapplicable’.)n Similar if primary key of Students tuple is updated.

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B

Enrolled Students

30

Page 31: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Relational Query Languages

n A major strength of the relational model: supports simple, powerful querying of data.

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

– Declarative language: specify what you want, and don’t have to worry about how to get the results (e.g., simply say sort, instead of specifying quicksort vs. mergesort)

31

Page 32: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

The SQL Query Language

n The most widely used relational query language. – Current std is SQL2011– To find all 18 year old students, we can write:

SELECT *FROM Students SWHERE S.age=18

To find just names and logins, replace the first line:

sid name login age gpa

53666 Jones jones@cs 18 3.4

53688 Smith smith@ee 18 3.2

SELECT S.name, S.loginFROM Students SWHERE S.age=18

name login

Jones jones@cs

Smith smith@ee

32

Page 33: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Querying Multiple Relationsn What does the following query compute?

SELECT S.name, E.cidFROM Students S, Enrolled E

WHERE S.sid=E.sid AND E.grade='A'

S.name E.cidSmith Topology112

sid cid grade53831 Carnatic101 C53831 Reggae203 B53650 Topology112 A53666 History105 B

we get:

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

Students Enrolled

33

Page 34: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Semantics of a Queryn A conceptual evaluation method for the previous query:

1. do FROM clause: compute cross-product of Students and Enrolled2. do WHERE clause: Check conditions, discard tuples that fail3. do SELECT clause: Delete unwanted fields

n Remember, this is conceptual. Actual evaluation will be much more efficient, but must produce the same answers.

34

Page 35: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Cross-product of Students and Enrolled InstancesS.sid S.name S.login S.age S.gpa E.sid E.cid E.grade 53666 Jones jones@cs 18 3.4 53831 Carnatic101 C 53666 Jones jones@cs 18 3.4 53832 Reggae203 B 53666 Jones jones@cs 18 3.4 53650 Topology112 A 53666 Jones jones@cs 18 3.4 53666 History105 B 53688 Smith smith@ee 18 3.2 53831 Carnatic101 C 53688 Smith smith@ee 18 3.2 53831 Reggae203 B 53688 Smith smith@ee 18 3.2 53650 Topology112 A 53688 Smith smith@ee 18 3.2 53666 History105 B 53650 Smith smith@math 19 3.8 53831 Carnatic101 C 53650 Smith smith@math 19 3.8 53831 Reggae203 B 53650 Smith smith@math 19 3.8 53650 Topology112 A 53650 Smith smith@math 19 3.8 53666 History105 B

35

Page 36: Intro to Database Systems and Relational ModelRelational Query Languages nA major strength of the relational model: supports simple, powerful queryingof data. nQueries can be written

Relational Model: Summary

n A tabular representation of data.n Simple and intuitive, currently the most widely used

– Other models are commonly used too (XML, JSON, key-value pairs)n Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks

for violations. – Two important ICs: primary and foreign keys– In addition, we always have domain constraints.

n Powerful and natural query languages exist.

36