+ SQL
Abstraction Layers
Conceptual
What data is held
Realisation
(Refinement Reification)
Logical
Physical
JPEG as a stream of bytes A Database as files and records stored in a DBMS-specific format
Components
The concepts available to represent the UoD
Relations (tables) of Tuples (rows) , of Columns (fields) containing values drawn from a Domain
Base Relations - facts Derived Relations Relations constructed by extracting, combining base relations
Employee : EMP
DEPT Table
Deptno
10 20 30 40
Dname
Accounting Research Sales Operations
Location
New York Dallas Chicago Boston
EMP - table
Empno
7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934
Ename
SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER
Mgr
7902 7698 7698 7839 7698 7839 7839 7566 7698 7788 7698 7566 7782
Sal
800.00 1,600.00 1,250.00 2,975.00 1,250.00 2,850.00 2,450.00 3,000.00 5,000.00 1,500.00 1,100.00 950.00 3,000.00 1,300.00
Deptno
20 30 30 20 30 30 10 20 10 30 20 30 20 10
Relation
Relation (table)
Column (field)
Deptno 10 20 30 40
Tuple (row)
general
integer domain 0<int<99 string city name
specific
Relational Algebra
A group of operations on relations which yield only other relations Closed Base operations
A single tuple (row) is a relation A single value is also a relation
Convenience operations
Set operations
RESTRICT, PROJECT, PRODUCT EQUI-JOIN, (Natural) JOIN, Outer Joins UNION, INTERSECTION, DIFFERENCE, DIVISION
Intersect
Product
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 operations
SQL
DATA MANIPULATION (DML) - factbase
QUERY
SELECT
UPDATE
UPDATE DELETE INSERT
Ename
SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER
Mgr
7902 7698 7698 7839 7698 7839 7839 7566
Sal
800.00 1,600.00 1,250.00 2,975.00 1,250.00 2,850.00 2,450.00 3,000.00 5,000.00 1,500.00 1,100.00 950.00 3,000.00 1,300.00
Deptno
20 30 30 20 30 30 10 20 10 30 20 30 20 10
Empno
7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934
Ename
SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER
Mgr
7902 7698 7698 7839 7698 7839 7839 7566
Sal
800.00 1,600.00 1,250.00 2,975.00 1,250.00 2,850.00 2,450.00 3,000.00 5,000.00 1,500.00 1,100.00 950.00 3,000.00 1,300.00
Deptno
20 30 30 20 30 30 10 20 10 30 20 30 20 10
Restrict - Project: Select Empno,Mgr,Deptno from Emp where sal > 2000;
Empno
7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934
Ename
SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER
Mgr
7902 7698 7698 7839 7698 7839 7839 7566 7698 7788 7698 7566 7782
Sal
800.00 1,600.00 1,250.00 2,975.00 1,250.00 2,850.00 2,450.00 3,000.00 5,000.00 1,500.00 1,100.00 950.00 3,000.00 1,300.00
Deptno
20 30 30 20 30 30 10 20 10 30 20 30 20 10
Restrict - Project: Select Empno,Mgr,Deptno from Emp where Sal > 2000 and Sal < 3000;
Empno
7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934
Ename
SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER
Mgr
7902 7698 7698 7839 7698 7839 7839 7566
Sal
800.00 1,600.00 1,250.00 2,975.00 1,250.00 2,850.00 2,450.00 3,000.00 5,000.00 1,500.00 1,100.00 950.00 3,000.00 1,300.00
Deptno
20 30 30 20 30 30 10 20 10 30 20 30 20 10
Deptno
Dname
Deptno 20 30 10
Deptno 20 20 20 20 30 30 30 30 Deptno 10 20 30 40 10 20 30 40
Location New York Dallas Chicago Boston New York Dallas Chicago Boston
7782
7782 7782 7782
7839
7839 7839 7839
10
10 10 10
10
20 30 40
Accounting
Research Sales Operations
New York
Dallas Chicago Boston
Product :
DEPT has 4 records EMPX has 3 records so DEPT x EMPX has 12 records but not very useful
Empno Mgr 7566 7839 7566 7839 7566 7839 7566 7839 7698 7839 7698 7839 7698 7839 7698 7839 7782 7839 7782 7839 7782 7839 7782 7839
Deptno Deptno Dname 20 10 Accounting 20 20 Research 20 30 Sales 20 40 Operations 30 10 Accounting 30 20 Research 30 30 Sales 30 40 Operations 10 10 Accounting 10 20 Research 10 30 Sales 10 40 Operations
Location New York Dallas Chicago Boston New York Dallas Chicago Boston New York Dallas Chicago Boston
Restrict Product Restrict Project : Product Project - Restrict Select Empno,Mgr,Deptno,Dname * from EmpX natural join Dept; from Emp Natural Join Dept where Sal > 2000 and Sal < 3000;
Empno Mgr 7566 7839 7566 7839 7566 7839 7566 7839 7698 7839 7698 7839 7698 7839 7698 7839 7782 7839 7782 7839 7782 7839 7782 7839 Deptno Deptno Dname 20 10 Accounting 20 20 Research 20 30 Sales 20 40 Operations 30 10 Accounting 30 20 Research 30 30 Sales 30 40 Operations 10 10 Accounting 10 20 Research 10 30 Sales 10 40 Operations Location New York Dallas Chicago Boston New York Dallas Chicago Boston New York Dallas Chicago Boston
Join queries
List the names of all staff in department number 10
DATE
SYSDATE..
STATISTICAL FUNCTIONS
COUNT, AVG, MIN, MAX, SUM GENERATE AGGREGATE VALUES SELECT SUM(sal) FROM emp;
shows total salary over all emps
Grouping Rows
Select deptno, count(*) from emp group by deptno;
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..
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
ASCII String
Record header
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