Top Banner
The Relational Model Suan Lee
26

The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Jul 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

The Relational Model

Suan Lee

Page 2: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Database Management System (DBMS)

• Used by all major commercial database systems

• Very simple model

• Query with high-level languages: simple yet expressive

• Efficient implementations

The Relational Model - Suan Lee 2

Page 3: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Database = set of named relations (or tables)

The Relational Model - Suan Lee 3

Page 4: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Database = set of named relations (or tables)

The Relational Model - Suan Lee 4

Student College

Page 5: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Database = set of named relations (or tables)

• Each relation has a set of named attributes (or columns)

The Relational Model - Suan Lee 5

ID Name GPA Photo Name State ENR

Student College

Page 6: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Database = set of named relations (or tables)

• Each relation has a set of named attributes (or columns)

• Each tuple (or row) has a value for each attribute

The Relational Model - Suan Lee 6

ID Name GPA Photo

1 Adam 3.9

2 Bill 3.4 NULL

3 Cathy NULL

Name State ENR

Stanford CA 15,000

Berkeley CA 36,000

MIT MA 10,000

Student College

… … … … … … …

Page 7: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Database = set of named relations (or tables)

• Each relation has a set of named attributes (or columns)

• Each tuple (or row) has a value for each attribute

• Each attribute has a type (or domain)

The Relational Model - Suan Lee 7

ID Name GPA Photo

1 Adam 3.9

2 Bill 3.4 NULL

3 Cathy NULL

Name State ENR

Stanford CA 15,000

Berkeley CA 36,000

MIT MA 10,000

Student College

… … … … … … …

Page 8: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Schema = structural description of relations in database

• Instance = actual contents at given point in time

The Relational Model - Suan Lee 8

ID Name GPA Photo

1 Adam 3.9

2 Bill 3.4 NULL

3 Cathy NULL

Name State ENR

Stanford CA 15,000

Berkeley CA 36,000

MIT MA 10,000

Student College

… … … … … … …

Page 9: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• NULL – special value for “unknown” or “undefined”

The Relational Model - Suan Lee 9

ID Name GPA Photo

1 Adam 3.9

2 Bill 3.4 NULL

3 Cathy NULL

Name State ENR

Stanford CA 15,000

Berkeley CA 36,000

MIT MA 10,000

Student College

… … … … … … …

Page 10: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Key – attribute whose value is unique in each tuple Or set of attributes whose combined values are unique

The Relational Model - Suan Lee 10

ID Name GPA Photo

1 Adam 3.9

2 Bill 3.4 NULL

3 Cathy NULL

Name State ENR

Stanford CA 15,000

Berkeley CA 36,000

MIT MA 10,000

Student College

… … … … … … …

Page 11: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Creating relations (tables) in SQL

The Relational Model - Suan Lee 11

CREATE TABLE Student (ID INTEGER, name STRING, GPA FLOAT, photo STRING)

CREATE TABLE College (name STRING, state CHAR(2), enrollment INTEGER)

Page 12: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

• Used by all major commercial database systems

• Very simple model

• Query with high-level languages: simple yet expressive

• Efficient implementations

The Relational Model - Suan Lee 12

Page 13: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Steps in creating and using a (relational) database

Querying Relational Databases - Suan Lee 13

Page 14: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Steps in creating and using a (relational) database

1. Design schema; create using DDL

Querying Relational Databases - Suan Lee 14

Page 15: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Steps in creating and using a (relational) database

1. Design schema; create using DDL

2. “Bulk load” initial data

Querying Relational Databases - Suan Lee 15

Page 16: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Steps in creating and using a (relational) database

1. Design schema; create using DDL

2. “Bulk load” initial data

3. Repeat: execute queries and modifications

Querying Relational Databases - Suan Lee 16

Page 17: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Steps in creating and using a (relational) database

1. Design schema; create using DDL

2. “Bulk load” initial data

3. Repeat: execute queries and modifications

Querying Relational Databases - Suan Lee 17

M

“ok”

Page 18: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Ad-hoc queries in high-level language

Querying Relational Databases - Suan Lee 18

Page 19: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Ad-hoc queries in high-level language

Querying Relational Databases - Suan Lee 19

All students with GPA > 3.7 applying to Stanford and MIT only

All engineering departments in CA with < 500 applicants

College with highest average accept rate over last 5 years

Page 20: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Ad-hoc queries in high-level language

• Some easy to pose; some a bit harder

• Some easy for DBMS to execute efficiently; some harder

• “Query language” also used to modify data

Querying Relational Databases - Suan Lee 20

All students with GPA > 3.7 applying to Stanford and MIT only

All engineering departments in CA with < 500 applicants

College with highest average accept rate over last 5 years

Page 21: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Queries return relations (“compositional”, “closed”)

Querying Relational Databases - Suan Lee 21

Q1

Page 22: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Queries return relations (“compositional”, “closed”)

Querying Relational Databases - Suan Lee 22

Q1

Q2

Page 23: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Query Languages

• Relational Algebra (formal)

• SQL (actual/implemented)

Querying Relational Databases - Suan Lee 23

Page 24: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Query Languages

• Relational Algebra (formal)

• SQL (actual/implemented)

IDs of students with GPA > 3.7 applying to Stanford

Querying Relational Databases - Suan Lee 24

Page 25: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Query Languages

• Relational Algebra (formal)

• SQL (actual/implemented)

IDs of students with GPA > 3.7 applying to Stanford

Querying Relational Databases - Suan Lee 25

ΠID(σGPA=3.7 ∧ College.Name=‘Stanford’(Student⋈ Apply))

Page 26: The Relational Modelsuanlab.com/assets/lectures/dbe/02.pdf · Database Management System (DBMS) •Used by all major commercial database systems •Very simple model •Query with

Query Languages

• Relational Algebra (formal)

• SQL (actual/implemented)

IDs of students with GPA > 3.7 applying to Stanford

Querying Relational Databases - Suan Lee 26

SELECT Student.IDFROM Student, ApplyWHERE Student.ID=Apply.IDAND GPA>3.7 AND College.name=‘Stanford’

ΠID(σGPA=3.7 ∧ College.Name=‘Stanford’(Student⋈ Apply))