Logical Layer The Relational Data Model + SQL
Abstraction Layers zConceptual yWhat data is held xAn Image and its meta-data xEntity-Relationship model (ERM) zLogical yHow data is organised in storage xBlock and Directory structure xTables, keys zPhysical yHow data is stored in bits xJPEG as a stream of bytes xA Database as files and records stored in a DBMS-specific format Abstraction Realisation (Refinement Reification) (Reverse Engineering) (Engineering, Model-Driven development
The Relational Data Model zThe Theory underlying Relational Databases – Access, Oracle, MySQL.. zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zAll Relational DBMSs depart from the basic model
Components zThe concepts available to represent the UoD yRelations (tables) of yTuples (rows), of yColumns (fields) containing values drawn from a Domain zBase Relations - facts zDerived Relations - yRelations constructed by extracting, combining base relations
EMP-DEPT example (from SQL workbook) Two relations: Department : DEPTDEPT Employee : EMPEMP
DEPT Table DeptnoDnameLocation 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston
EMP - table EmpnoEnameMgr Sal Deptno 7369SMITH7902£ ALLEN7698£1, WARD7698£1, JONES7839£2, MARTIN7698£1, BLAKE7839£2, CLARK7839£2, SCOTT7566£3, KING£5, TURNER7698£1, ADAMS7788£1, JAMES7698£ FORD7566£3, MILLER7782£1,
Relation DeptnoDnameLoc 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston Relation (table) Tuple (row) Column (field) domain string city name general specific integer 0<int<99
Relations are everything zThere is only one data structure in the relational data model - the relation yEvery relation in a database must have a distinct name. yEvery column in a relation must have a distinct name within the relation. yAll entries in a column must be of the same kind yThe ordering of columns in a relation is not significant. yEach row in a relation must be distinct xLeads to Primary Key in a Relational Database yThe ordering of rows is not significant. yEach cell or column/row intersection in a relation should contain only a so-called atomic value.
Relational Algebra zA group of operations on relations which yield only other relations – “Closed” yA single tuple (row) is a relation yA single value is also a relation zBase operations yRESTRICT, PROJECT, PRODUCT zConvenience operations yEQUI-JOIN, (Natural) JOIN, Outer Joins zSet operations yUNION, INTERSECTION, DIFFERENCE, DIVISION
The Relational Algebra Restrict Project Product Union Intersect
SQL (SeQueL) zThere are a number of languages for manipulating relations, but the one most commonly implemented is SQL zSQL zSQL ybetter support for Algebraic operations zSQL Post-Relational yrow and column types, stored procedures, triggers, references, large objects
SQL zDATA MANIPULATION (DML) - factbase yQUERY xSELECT yUPDATE xUPDATE xDELETE xINSERT zDATA DEFINITION (DDL) -schema yCREATE, ALTER, DROP zDATA CONTROL (DCL) - access control yGRANT,REVOKE
RESTRICT: SELECT * FROM EMP WHERE sal > 2000 EmpnoEnameMgr Sal Deptno 7369SMITH7902£ ALLEN7698£1, WARD7698£1, JONES7839£2, MARTIN7698£1, BLAKE7839£2, CLARK7839£2, SCOTT7566£3, KING£5, TURNER7698£1, ADAMS7788£1, JAMES7698£ FORD7566£3, MILLER7782£1,
Project: Select Empno,Mgr,Deptno from Emp EmpnoEnameMgr Sal Deptno 7369SMITH7902£ ALLEN7698£1, WARD7698£1, JONES7839£2, MARTIN7698£1, BLAKE7839£2, CLARK7839£2, SCOTT7566£3, KING£5, TURNER7698£1, ADAMS7788£1, JAMES7698£ FORD7566£3, MILLER7782£1,
EmpnoEnameMgr Sal Deptno 7369SMITH7902£ ALLEN7698£1, WARD7698£1, JONES7839£2, MARTIN7698£1, BLAKE7839£2, CLARK7839£2, SCOTT7566£3, KING£5, TURNER7698£1, ADAMS7788£1, JAMES7698£ FORD7566£3, MILLER7782£1, Restrict - Project: Select Empno,Mgr,Deptno from Emp where sal > 2000;
Restrict - Project: Select Empno,Mgr,Deptno from Emp where Sal > 2000 and Sal < 3000; EmpnoEnameMgr Sal Deptno 7369SMITH7902£ ALLEN7698£1, WARD7698£1, JONES7839£2, MARTIN7698£1, BLAKE7839£2, CLARK7839£2, SCOTT7566£3, KING£5, TURNER7698£1, ADAMS7788£1, JAMES7698£ FORD7566£3, MILLER7782£1,
Some queries to write: zhttp:// l/queryemp.phphttp:// l/queryemp.php zList the names of the employees whose manager is 7698 zList the empnos of the employees in Department no 20 whose salary is over $2500
EmpnoMgrDeptno DeptnoDnameLocation 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston Product: Select * from EmpX, Dept; EmpnoMgrDeptno DnameLocation AccountingNew York ResearchDallas SalesChicago OperationsBoston AccountingNew York ResearchDallas SalesChicago OperationsBoston AccountingNew York ResearchDallas SalesChicago OperationsBoston
Product : zDEPT has 4 records zEMPX has 3 records zso DEPT x EMPX has 12 records zbut not very useful
EmpnoMgrDeptnoDeptnoDnameLocation AccountingNew York ResearchDallas SalesChicago OperationsBoston AccountingNew York ResearchDallas SalesChicago OperationsBoston AccountingNew York ResearchDallas SalesChicago OperationsBoston Product – Project - Restrict Select * from EmpX,Dept where Emp.Deptno=Dept.Deptno;
EmpnoMgrDeptnoDeptnoDnameLocation AccountingNew York ResearchDallas SalesChicago OperationsBoston AccountingNew York ResearchDallas SalesChicago OperationsBoston AccountingNew York ResearchDallas SalesChicago OperationsBoston Product – Project - Restrict Select * 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;
Join queries zList the names of all staff in department number 10
SQL Functions vary with RDBMS zSTRINGS yLIKE, CONCAT, SUBSTR… zDATE ySYSDATE.. zSTATISTICAL FUNCTIONS yCOUNT, AVG, MIN, MAX, SUM yGENERATE AGGREGATE VALUES ySELECT SUM(sal) FROM emp; xshows total salary over all emps
zSorting Rows (tuples) ySelect ename, sal from emp order by sal; zGrouping Rows ySelect deptno, count(*) from emp group by deptno; zLimiting the number of Rows ySelect ename, sal from emp order by sal desc limit 1;
RDMS zRelational Database Management System yMaps Relations and values into the Physical Layer yInterprets SQL statements and executes the requested updates on the physical data yControls access to data, recovery from errors..
MySQL yFree – pay for support yCommand line interface or use PHPMyAdmin yInstalled for student use on shares – usual Unix login yPersonal copy easily be installed yMultiple databases can be created – you get just one ySeveral different file systems for physical storage (ISAM, INNODB) yISAM xThree files per table definition (schema).FRM Data.MYD Index.MYI
What would you expect to see at the physical layer?
Department Table data file ASCII String length Record header Dept no Big or Little Endian?
Learning SQL zWorkbook yMySQL, Oracle and MS Access yWe will be using MySQL with PHP yhttp:// zSQL/PHP Textbook yChapter 10 of Welling and Thomson