logical layer

30
Logical Layer The Relational Data Model + SQL

Upload: colman

Post on 19-Jan-2016

37 views

Category:

Documents


2 download

DESCRIPTION

Logical Layer. The Relational Data Model + SQL. Abstraction Layers. WHAT. Conceptual What data is held An Image and its meta-data Entity-Relationship model (ERM) Logical How data is organised in storage Block and Directory structure Tables, keys Physical How data is stored in bits - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Logical Layer

Logical Layer

The Relational Data Model

+ SQL

Page 2: Logical Layer

Abstraction Layers

Conceptual What data is held

An Image and its meta-dataEntity-Relationship model (ERM)

Logical How data is organised in storage

Block and Directory structureTables, keys

Physical How data is stored in bits

JPEG as a stream of bytesA Database as files and records stored in a DBMS-

specific format

Abstraction

Realisation

(Refinement

Reification)

(Reverse Engineering)

(Engineering, Model-Driven development

Page 3: Logical Layer

The Relational Data Model

The Theory underlying Relational Databases – Access, Oracle, MySQL..

E.F Codd A Relational Data Model for Large Shared Data Banks (1970)

All Relational DBMSs depart from the basic model

Page 4: Logical Layer

ComponentsThe concepts available to represent the UoD

Relations (tables) of Tuples (rows) , of Columns (fields) containing values drawn from a

DomainBase Relations - factsDerived Relations -

Relations constructed by extracting, combining base relations

Page 5: Logical Layer

EMP-DEPT example (from SQL workbook)

Two relations:

Department : DEPT

Employee : EMP

Page 6: Logical Layer

DEPT Table

Deptno Dname Location

10 Accounting New York

20 Research Dallas

30 Sales Chicago

40 Operations Boston

Page 7: Logical Layer

EMP - table

Empno Ename Mgr Sal Deptno7369 SMITH 7902 £ 800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 20

7900 JAMES 7698 £ 950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Page 8: Logical Layer

Relation

Deptno Dname Loc10 Accounting New York20 Research Dallas30 Sales Chicago40 Operations Boston

Relation (table)

Relation (table)

Tuple (row)

Tuple (row)

Column (field)Column (field)

domaindomainstring

city name

general

specific

integer

0<int<99

Page 9: Logical Layer

Relations are everything

There is only one data structure in the relational data model - the relation Every relation in a database must have a

distinct name. Every column in a relation must have a distinct

name within the relation. All entries in a column must be of the same kind The ordering of columns in a relation is not

significant. Each row in a relation must be distinct

Leads to Primary Key in a Relational Database The ordering of rows is not significant. Each cell or column/row intersection in a

relation should contain only a so-called atomic value.

Page 10: Logical Layer

Relational Algebra

A group of operations on relations which yield only other relations – “Closed” A single tuple (row) is a relation A single value is also a relation

Base operations RESTRICT, PROJECT, PRODUCT

Convenience operations EQUI-JOIN, (Natural) JOIN, Outer Joins

Set operations UNION, INTERSECTION, DIFFERENCE, DIVISION

Page 11: Logical Layer

The Relational Algebra

Restrict

Project

Product

Union

Intersect

Page 12: Logical Layer

SQL (SeQueL)

There are a number of languages for manipulating relations, but the one most commonly implemented is SQL

SQL1 - 1986 SQL2 - 1992

better support for Algebraic operationsSQL3 - 1999 Post-Relational

row and column types, stored procedures, triggers, references, large objects

Page 13: Logical Layer

SQLDATA MANIPULATION (DML) - factbase

QUERYSELECT

UPDATEUPDATEDELETEINSERT

DATA DEFINITION (DDL) -schema CREATE, ALTER, DROP

DATA CONTROL (DCL) - access control GRANT,REVOKE

Page 14: Logical Layer

RESTRICT:SELECT * FROM EMP WHERE sal > 2000

Empno Ename Mgr Sal Deptno7369 SMITH 7902 £ 800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 20

7900 JAMES 7698 £ 950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Page 15: Logical Layer

Project:Select Empno,Mgr,Deptno from Emp

Empno Ename Mgr Sal Deptno7369 SMITH 7902 £ 800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 20

7900 JAMES 7698 £ 950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Page 16: Logical Layer

Empno Ename Mgr Sal Deptno7369 SMITH 7902 £ 800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 20

7900 JAMES 7698 £ 950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Restrict - Project:Select Empno,Mgr,Deptno from Emp where sal > 2000;

Page 17: Logical Layer

Restrict - Project:Select Empno,Mgr,Deptno from Emp where Sal > 2000 and Sal < 3000;

Empno Ename Mgr Sal Deptno7369 SMITH 7902 £ 800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 20

7900 JAMES 7698 £ 950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Page 18: Logical Layer

Some queries to write:

http://www.cems.uwe.ac.uk/~cjwallac/sql/mysql/queryemp.php

List the names of the employees whose manager is 7698

List the empnos of the employees in Department no 20 whose salary is over $2500

Page 19: Logical Layer

Empno Mgr Deptno

7566 7839 20

7698 7839 30

7782 7839 10

Deptno Dname Location

10 Accounting New York

20 Research Dallas

30 Sales Chicago

40 Operations Boston

Product:Select * from EmpX , Dept;

Empno Mgr Deptno Deptno Dname Location

7566 7839 20 10 Accounting New York

7566 7839 20 20 Research Dallas

7566 7839 20 30 Sales Chicago

7566 7839 20 40 Operations Boston

7698 7839 30 10 Accounting New York

7698 7839 30 20 Research Dallas

7698 7839 30 30 Sales Chicago

7698 7839 30 40 Operations Boston

7782 7839 10 10 Accounting New York

7782 7839 10 20 Research Dallas

7782 7839 10 30 Sales Chicago

7782 7839 10 40 Operations Boston

Page 20: Logical Layer

Product :

DEPT has 4 recordsEMPX has 3 records

so DEPT x EMPX has 12 records

but not very useful

Page 21: Logical Layer

Empno Mgr Deptno Deptno Dname Location7566 7839 20 10 Accounting New York7566 7839 20 20 Research Dallas7566 7839 20 30 Sales Chicago7566 7839 20 40 Operations Boston7698 7839 30 10 Accounting New York7698 7839 30 20 Research Dallas7698 7839 30 30 Sales Chicago7698 7839 30 40 Operations Boston7782 7839 10 10 Accounting New York7782 7839 10 20 Research Dallas7782 7839 10 30 Sales Chicago7782 7839 10 40 Operations Boston

Product – Project - RestrictSelect * from EmpX ,Dept where Emp.Deptno=Dept.Deptno;

Page 22: Logical Layer

Empno Mgr Deptno Deptno Dname Location7566 7839 20 10 Accounting New York7566 7839 20 20 Research Dallas7566 7839 20 30 Sales Chicago7566 7839 20 40 Operations Boston7698 7839 30 10 Accounting New York7698 7839 30 20 Research Dallas7698 7839 30 30 Sales Chicago7698 7839 30 40 Operations Boston7782 7839 10 10 Accounting New York7782 7839 10 20 Research Dallas7782 7839 10 30 Sales Chicago7782 7839 10 40 Operations Boston

Product – Project - RestrictSelect * from EmpX natural join Dept;Restrict – Project – Product – Restrict – Project :Select Empno,Mgr,Deptno,Dname from Emp Natural Join Dept where Sal > 2000 and Sal < 3000;

Page 23: Logical Layer

Join queries

List the names of all staff in department number 10

Page 24: Logical Layer

SQL Functionsvary with RDBMS

STRINGS LIKE, CONCAT, SUBSTR…

DATE SYSDATE..

STATISTICAL FUNCTIONS COUNT, AVG, MIN, MAX, SUM GENERATE AGGREGATE VALUES SELECT SUM(sal) FROM emp;

shows total salary over all emps

Page 25: Logical Layer

Sorting Rows (tuples) Select ename, sal from emp order by sal;

Grouping Rows Select deptno, count(*) from emp group

by deptno;Limiting the number of Rows

Select ename, sal from emp order by sal desc limit 1;

Page 26: Logical Layer

RDMS

Relational Database Management System Maps Relations and values into the

Physical Layer Interprets SQL statements and executes

the requested updates on the physical data

Controls access to data, recovery from errors..

Page 27: Logical Layer

MySQL

Free – pay for support Command line interface or use PHPMyAdmin Installed for student use on shares – usual Unix

login Personal copy easily be installed Multiple databases can be created – you get just

one Several different file systems for physical storage

(ISAM, INNODB) ISAM

Three files per table• definition (schema) .FRM• Data .MYD• Index .MYI

Page 28: Logical Layer

What would you expect to see at the physical layer?

Page 29: Logical Layer

Department Table data file

ASCII String

length

Record header

Dept no

Big or Little Endian?

Page 30: Logical Layer

Learning SQL

Workbook MySQL, Oracle and MS Access We will be using MySQL with PHP http://www.cems.uwe.ac.uk/~cjwallac/sql/

mysql

SQL/PHP Textbook Chapter 10 of Welling and Thomson