Intro to Database Systems and Relational Model George Kollios - - PowerPoint PPT Presentation

intro to database systems and relational model
SMART_READER_LITE
LIVE PREVIEW

Intro to Database Systems and Relational Model George Kollios - - PowerPoint PPT Presentation

Intro to Database Systems and Relational Model George Kollios Boston University (based on the slides by Feifei Li, U of Utah) About the course Administrivia n Instructor: George Kollios, gkollios@cs.bu.edu MCS 104, Tue 12:30-2:00 and


slide-1
SLIDE 1

Intro to Database Systems and Relational Model

George Kollios Boston University (based on the slides by Feifei Li, U of Utah)

slide-2
SLIDE 2

About the course – Administrivia

n Instructor:

– George Kollios, gkollios@cs.bu.edu

MCS 104, Tue 12:30-2:00 and Wed 1:00-2:30

n Teaching Fellow:

– Xiao Zhou, zhouxiao@bu.edu – 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

slide-3
SLIDE 3

Textbook

Raghu Ramakrishnan and Johannes Gehrke, "Database Management Systems", McGraw-Hill, Third Edition. 2002.

slide-4
SLIDE 4

Grading (Tentative)

(Tentative Grading)

■ Written Homeworks: 15% ■ Midterm: 20% ■ Final: 25% ■ Programming Assignments: 40% ■ See next

slide-5
SLIDE 5

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, Recovery

n Some Written Homeworks n Exams – 1 Midterm & 1 Final

slide-6
SLIDE 6

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

slide-7
SLIDE 7

“Big Data” Buzz

slide-8
SLIDE 8

“Big Data” Buzz

100 200 300 400 500 600 700 800 900 1000 M a n u f a c t u r i n g G

  • v

e r n m e n t C

  • m

m u n i c a t i

  • n

s B a n k i n g H e a l t h c a r e R e t a i l E d u c a t i

  • n

T r a n s p

  • r

t a t i

  • n

966 848 715 619 434 364 269 227

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

slide-9
SLIDE 9

DB System Landscape

9

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

slide-10
SLIDE 10

What’s a database management system (DBMS)?

10

slide-11
SLIDE 11

What’s a database management system (DBMS)?

Application Interface SQL Interpreter Query Evaluator Plan Generator Plan Executor Plan Optimizer Data Access Transaction Manager Buffer Manager Security Manager File & Index Manager Database Data Files Index Files System & Metadata Files

Crash Recovery Concurrency Control

11

slide-12
SLIDE 12

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

slide-13
SLIDE 13

Data Models

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

  • f the world and bits stored in DBMS

n Many models exist n We will concentrate on the Relational

Model

10101 11101

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

13

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

SQL - A language for Relational DBs

n SQL: standard language for DBMS, structured query language n 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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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 S WHERE S.name = ‘Smith’

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

21

slide-22
SLIDE 22

Keys

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

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 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B

Enrolled Students

22

slide-23
SLIDE 23

Primary Keys

n A set of fields is a superkey if:

– No two distinct tuples can have same values in all key fields

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

  • ne 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

slide-24
SLIDE 24

Primary and Candidate Keys in SQL

n 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

slide-25
SLIDE 25

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 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 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B

Enrolled Students

25

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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 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 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B

Enrolled Students

27

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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 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 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B

Enrolled Students

30

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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 S WHERE 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.login FROM Students S WHERE S.age=18

name login Jones jones@cs Smith smith@ee

32

slide-33
SLIDE 33

Querying Multiple Relations

n What does the following query compute?

SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade='A'

S.name E.cid Smith Topology112

sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B

we get:

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

Students Enrolled

33

slide-34
SLIDE 34

Semantics of a Query

n A conceptual evaluation method for the previous query:

  • 1. do FROM clause: compute cross-product of Students and Enrolled
  • 2. do WHERE clause: Check conditions, discard tuples that fail
  • 3. 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

slide-35
SLIDE 35

Cross-product of Students and Enrolled Instances

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

slide-36
SLIDE 36

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