29
Introduction to Relational Database Systems 1 Introduction to Relational Database Systems Lecture 4

Introduction to Relational Database Systems 1 Lecture 4

Embed Size (px)

Citation preview

Page 1: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

1

Introduction to Relational Database Systems

Lecture 4

Page 2: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

2

Relational database systems

data objects relations / tables

operators applied to tables generate tables

Page 3: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

3

Relations / tables

explicit data values extensionally defined

atomic keys integrity design

includes how to organise data in tables

Page 4: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

4

Data about departments

Dept_id Dept_name Budget

D1 Marketing 10M

D2 Development 12M

D3 Research 5M

Depts

Page 5: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

5

Data about employees

E_id E_name Dept_id Salary

E1 Smith D1 40K

E2 John D1 42KE3 Stella D2 30KE4 Art D3 35K

Emps

Page 6: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

6

Atomic values

E_id E_name Dept_id Salary AddressE1 John Smith D1 25 18A Cowley SE14 6AJ

E2 John Wiles D1 45 7 Big Road, Kent, US257BB

E3 Mary Kent D2 30 149 Rue d’Enfent,13556 Paris, France

E4 Jenny Wolf D3 35 4 New Cross Road

no access to individual items

Page 7: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

7

Primary and foreign keys

E_id E_name Dept_id Salary

E1 Smith D1 40K

E2 John D1 42KE3 Stella D2 30KE4 Art D3 35K

Dept_id Dept_name Budget

D1 Marketing 10M

D2 Development 12M

D3 Research 5Mprimary

foreign

Page 8: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

8

Integrity

restrictions on data defined by users on individual tables

• age > 18; salary < 100k

on more than one table• if budget < 10M then salary < 50k

implicit in the data model

Page 9: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

9

Primary key integrity

E_id E_name Dept_id Salary

E100A Smith D1 40K

E120A John D1 42KE120A Stella D2 30KE110C Art D3 35K

incorrect model

Page 10: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

10

Foreign key integrity

E_id E_name Dept_id Salary

E1 Smith D1 40K

E2 John D1 42KE3 Stella D5 30KE4 Art D3 35K

Dept_id Dept_name Budget

D1 Marketing 10M

D2 Development 12M

D3 Research 5M

incorrect model

?

Page 11: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

11

Relational operators

characteristics set at a time base and derived tables ‘closed’ with respect to relations / tables

• nested expressions

include RESTRICT PROJECT JOIN

Page 12: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

12

RESTICT

RESTRICT Depts WHERE Budget > 8M

Dept_id Dept_name Budget

D1 Marketing 10M

D2 Development 12M

Page 13: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

13

PROJECT

PROJECT Depts OVER Dept_id, Budget

Dept_id Budget

D1 10M

D2 12M

D3 5M

Page 14: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

14

JOIN

JOIN Depts AND Emps OVER Dept_id

Dept_id Dept_name Budget E_id E_name Salary

D1 Marketing 10M E1 Smith 40K

D1 Marketing 10M E2 John 42K

D2 Development 12M E3 Stella 30K

D3 Research 5M E4 Art 35K

Page 15: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

15

Nested statements

“the members of all departments that have the budget greater than 7M”

JOIN (RESTRICT Depts WHERE Budget > 7M) AND Emps OVER Dept_id

Dept_id Dept_name Budget E_id E_name Salary

D1 Marketing 10M E1 Smith 40K

D1 Marketing 10M E2 John 42K

D2 Development 12M E3 Stella 30K

Page 16: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

16

Relational model

a data model in which all data is modelled as relations a way of looking at data

a prescription for a way of representing data manipulating data representing integrity constraints

Page 17: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

17

Relational database systems

relational DBMS implements the relational model

• not in its entirety• may add new features

relational database system a database application developed in the relational

model and implemented in a relational DBMS physical details hidden from the user

Page 18: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

18

Relational DBMS - features

views security

the optimiser the data catalogue / data dictionary

Page 19: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

19

Views (in relational systems)

named derived table the definition stored in the catalogue evaluated only when used

optimisation

used as if it were a real table problems with updates

views ANSI/SPARC relational

Page 20: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

20

Views

CREATE VIEW TopEmp AS PROJECT (SELECT Emps WHERE Salary > 33K) OVER E_name, Salary

Page 21: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

21

Views - usage

SELECT TopEmp WHERE Salary <= 40

SELECT PROJECT SELECT Emps WHERE Salary > 33 OVER E_name, Salary WHERE Salary <= 40

Page 22: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

22

Page 23: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

23

Security and views

how would you use the view mechanism in conjunction with the security system?

DEFINE SECURITY RULE AS ...

Page 24: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

24

The optimiser

operators - set level the DBMS decides how to best perform the

operations, based on• strategies of evaluation• information about the DB (in the catalogue)

Page 25: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

25

The best evaluation strategy

PROJECT SELECT Emps WHERE E_id = E2 OVER E_name, Salary

Page 26: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

26

The catalogue

information about the database• schemas• mappings• integrity rules• views definition• security rules ...

other modules that need it• the optimiser• the security system ...

Page 27: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

27

The system table “Tables”

Tabname Colcount Rowcount …

Depts 3 3 …

Emps 4 4 …

… … … …

Tables … … …

PROJECT Tables OVER Coulcount, Rowcount

Page 28: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

28

The system table “Columns”

Tabname Colname … …

Depts Dept_id … …Depts Dept_nameDepts BudgetEmps E_id … …Emps E_nameEmps DeptEmps Salary… … … …Tables Tabname … …

Page 29: Introduction to Relational Database Systems 1 Lecture 4

Introduction to Relational Database Systems

29

Summary

relational model• relations• operators• integrity

relational DBMSs• implement the relational model• views + security• the optimiser• the catalogue

next lecture : SQL