database theory

22
University of Sunderland COM 220 Lecture Two Slide 1 Database Theory Database Theory

Upload: shiloh

Post on 05-Jan-2016

59 views

Category:

Documents


2 download

DESCRIPTION

Database Theory. Objectives. Understanding of relational data model Basic concepts of set theory Learn how to use SQL query language. Relational Data Model. Devised by Codd in 1970, conforming to a simple set of rules Data stored as two dimensional tables ( relations ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Database Theory

University of Sunderland COM 220 Lecture Two Slide 1

Database TheoryDatabase Theory

Page 2: Database Theory

University of Sunderland COM 220 Lecture Two Slide 2

ObjectivesObjectives

• Understanding of relational data model

• Basic concepts of set theory

• Learn how to use SQL query language

Page 3: Database Theory

University of Sunderland COM 220 Lecture Two Slide 3

• Devised by Codd in 1970, conforming to a simple set of rules – Data stored as two dimensional tables (relations)– Every relation must have a unique name– Each column (attribute) has a unique name within

a table– Each value of an attribute is from the same

domain– Order of rows (tuples) is irrelevant– Order of columns (attributes) is irrelevant– Each value should be single-valued (atomicity)

Relational Data ModelRelational Data Model

Page 4: Database Theory

University of Sunderland COM 220 Lecture Two Slide 4

Relational Database - Relational Database - ExampleExample

• BRANCH relation

• STAFF relation

branchNo street city postcodeB005 22 Deer Rd London SW1 4EHB007 16 Argyll St Aberdeen AB2 3SUB003 163 Main St Glasgow G11 9QX

StaffNo Name Position Salary branchNoSL21 John White Manager 30000 B005SG37 Ann Beech Assistant 12000 B003SG14 David Ford Supervisor 18000 B003

Page 5: Database Theory

University of Sunderland COM 220 Lecture Two Slide 5

KeysKeys

• Primary Key:– all tables have an attribute or set of attributes

which uniquely identifies each separate row in the table

– e.g. branchNo in BRANCH table– staffNo in STAFF table– Note that we could not use the staffName

attribute as the primary key– why do you think this is?

Page 6: Database Theory

University of Sunderland COM 220 Lecture Two Slide 6

KeysKeys

• Foreign Key:– a set of attributes in a table which match the

primary key of some other table– e.g. branchNo in STAFF relation– Allows us to join tables together to relate

information in tables.

Page 7: Database Theory

University of Sunderland COM 220 Lecture Two Slide 7

Relational Data Relational Data ManipulationManipulation

• Codd’s definition of the relational model was based on a sound theoretical basis

• Set theory• Allows us to specify relations between sets, e.g.

– StaffNames = {‘John White’, ‘Ann Beech’, ‘David Ford’}

– StaffSalaries = {30000, 12000, 18000}

Page 8: Database Theory

University of Sunderland COM 220 Lecture Two Slide 8

Relational Data Relational Data ManipulationManipulation

• It is fine to be able to store data in a relational database

• But we also need to be able to retrieve from it too• The use of set theory provides a basis for all

relational query languages such as SQL• Relational algebra contains operations for

– RESTRICT - return rows satisfying condition– PROJECT - return specified columns– JOIN - join of two tables on primary key/foreign key

Page 9: Database Theory

University of Sunderland COM 220 Lecture Two Slide 9

Example Queries 1Example Queries 1

• SQL– select branchNo, city

from branch;

• Relational AlgebraThis is use of PROJECT

Page 10: Database Theory

University of Sunderland COM 220 Lecture Two Slide 10

Example Queries 2Example Queries 2

• SQL:– select *

from branch

where branchNo = ‘B003’;

• Relational Algebra:– Use of RESTRICT

Page 11: Database Theory

University of Sunderland COM 220 Lecture Two Slide 11

Example Queries 3Example Queries 3

• SQL:– select branchNo,staff_forename, staff_surname

from branch, staff

where branch.branchNo = staff.branchNo;

• Relational Algebra:– Use of JOIN

Page 12: Database Theory

University of Sunderland COM 220 Lecture Two Slide 12

• What is it?

• Historical Background

• Internal structure

• Commands

SQLSQL

Page 13: Database Theory

University of Sunderland COM 220 Lecture Two Slide 13

• Originally called SEQUEL– developed by IBM c1976

• Now called SQL– the ISO standard query language for relational databases

• DEVELOPED TO:– Reduce programmers workload– Make programs and data functionally independent– Make data structures more flexible– Allow end-user access to data

Historical Historical BackgroundBackground

Page 14: Database Theory

University of Sunderland COM 220 Lecture Two Slide 14

DIVIDED INTO 2 MAIN ELEMENTS

• Data Definition Language ( DDL )

• Data Manipulation Language ( DML )

Internal StructureInternal Structure

Page 15: Database Theory

University of Sunderland COM 220 Lecture Two Slide 15

SYNTAX• Commands may be on one or many lines• Command words cannot be split• SQL commands are not case sensitive• Only one statement can be current within the SQL

buffer

• SQL statements always end in a semi-colon ;

SQLSQL

Page 16: Database Theory

University of Sunderland COM 220 Lecture Two Slide 16

USED TO:• define the structure of data (CREATE)• which columns are in which tables• what indexes are maintained• change existing rows ( ALTER )• delete existing rows/tables ( DELETE)

(DROP)• grant/revoke access to ORACLE database &

its structures

Data Definition (DDL)Data Definition (DDL)

Page 17: Database Theory

University of Sunderland COM 220 Lecture Two Slide 17

USED TO:• retrieve/query data SELECT• insert data INSERT• delete data DELETE• protect data GRANT

Data Manipulation Data Manipulation (DML)(DML)

Page 18: Database Theory

University of Sunderland COM 220 Lecture Two Slide 18

RETRIEVAL

• THE SELECT COMMAND

The purpose of this command is to read data from the database and present it in user specified form.

By far the most complex of all SQL commands, with the ability to perform a wide range of tasks.

Data Manipulation Data Manipulation (DML)(DML)

Page 19: Database Theory

University of Sunderland COM 220 Lecture Two Slide 19

• Used when an SQL query requires data from more than one table in the database

• Rows in one table can be joined to rows in another according to common values.

• These common values are the primary key of one table and the foreign key of the other table

Table JoinsTable Joins

Page 20: Database Theory

University of Sunderland COM 220 Lecture Two Slide 20

Adding/Removing Adding/Removing RowsRows

• Use the INSERT or DELETE command• Examples:

INSERT INTO emp

VALUES (1234, ‘Barney Rubble’, ‘Manager’, 30000);

DELETE FROM emp

WHERE job = ‘Administrator’;

Page 21: Database Theory

University of Sunderland COM 220 Lecture Two Slide 21

Protecting DataProtecting Data

• Use GRANT command to set privileges on data

• e.g.

GRANT SELECT ON emp TO ALL;

GRANT UPDATE ON emp TO personnel;

Page 22: Database Theory

University of Sunderland COM 220 Lecture Two Slide 22

Further ReadingFurther Reading

• Relational Model– Connolly & Begg, 3rd and 4th edition, chapter 3

• SQL– Connolly & Begg, 3rd and 4th edition, chapter 5

• Next week:– Database Application Lifecycle