Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logical Layer The Relational Data Model + SQL. Abstraction Layers zConceptual yWhat data is held xAn Image and its meta-data xEntity-Relationship model.

Similar presentations


Presentation on theme: "Logical Layer The Relational Data Model + SQL. Abstraction Layers zConceptual yWhat data is held xAn Image and its meta-data xEntity-Relationship model."— Presentation transcript:

1 Logical Layer The Relational Data Model + SQL

2 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

3 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

4 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

5 EMP-DEPT example (from SQL workbook) Two relations: Department : DEPTDEPT Employee : EMPEMP

6 DEPT Table DeptnoDnameLocation 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston

7 EMP - table EmpnoEnameMgr Sal Deptno 7369SMITH7902£ 800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698£ 950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010

8 Relation DeptnoDnameLoc 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston Relation (table) Tuple (row) Column (field) domain string city name general specific integer 0<int<99

9 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.

10 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

11 The Relational Algebra Restrict Project Product Union Intersect

12 SQL (SeQueL) zThere are a number of languages for manipulating relations, but the one most commonly implemented is SQL zSQL1 - 1986 zSQL2 - 1992 ybetter support for Algebraic operations zSQL3 - 1999 Post-Relational yrow and column types, stored procedures, triggers, references, large objects

13 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

14 RESTRICT: SELECT * FROM EMP WHERE sal > 2000 EmpnoEnameMgr Sal Deptno 7369SMITH7902£ 800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698£ 950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010

15 Project: Select Empno,Mgr,Deptno from Emp EmpnoEnameMgr Sal Deptno 7369SMITH7902£ 800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698£ 950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010

16 EmpnoEnameMgr Sal Deptno 7369SMITH7902£ 800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698£ 950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010 Restrict - Project: Select Empno,Mgr,Deptno from Emp where sal > 2000;

17 Restrict - Project: Select Empno,Mgr,Deptno from Emp where Sal > 2000 and Sal < 3000; EmpnoEnameMgr Sal Deptno 7369SMITH7902£ 800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698£ 950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010

18 Some queries to write: zhttp://www.cems.uwe.ac.uk/~cjwallac/sql/mysq l/queryemp.phphttp://www.cems.uwe.ac.uk/~cjwallac/sql/mysq 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

19 EmpnoMgrDeptno 7566783920 7698783930 7782783910 DeptnoDnameLocation 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston Product: Select * from EmpX, Dept; EmpnoMgrDeptno DnameLocation 7566783920 10AccountingNew York 7566783920 ResearchDallas 7566783920 30SalesChicago 7566783920 40OperationsBoston 7698783930 10AccountingNew York 7698783930 20ResearchDallas 7698783930 SalesChicago 7698783930 40OperationsBoston 7782783910 AccountingNew York 7782783910 20ResearchDallas 7782783910 30SalesChicago 7782783910 40OperationsBoston

20 Product : zDEPT has 4 records zEMPX has 3 records zso DEPT x EMPX has 12 records zbut not very useful

21 EmpnoMgrDeptnoDeptnoDnameLocation 756678392010AccountingNew York 756678392020ResearchDallas 756678392030SalesChicago 756678392040OperationsBoston 769878393010AccountingNew York 769878393020ResearchDallas 769878393030SalesChicago 769878393040OperationsBoston 778278391010AccountingNew York 778278391020ResearchDallas 778278391030SalesChicago 778278391040OperationsBoston Product – Project - Restrict Select * from EmpX,Dept where Emp.Deptno=Dept.Deptno;

22 EmpnoMgrDeptnoDeptnoDnameLocation 756678392010AccountingNew York 756678392020ResearchDallas 756678392030SalesChicago 756678392040OperationsBoston 769878393010AccountingNew York 769878393020ResearchDallas 769878393030SalesChicago 769878393040OperationsBoston 778278391010AccountingNew York 778278391020ResearchDallas 778278391030SalesChicago 778278391040OperationsBoston 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;

23 Join queries zList the names of all staff in department number 10

24 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

25 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;

26 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..

27 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

28 What would you expect to see at the physical layer?

29 Department Table data file ASCII String length Record header Dept no Big or Little Endian?

30 Learning SQL zWorkbook yMySQL, Oracle and MS Access yWe will be using MySQL with PHP yhttp://www.cems.uwe.ac.uk/~cjwallac/sql/mysql zSQL/PHP Textbook yChapter 10 of Welling and Thomson


Download ppt "Logical Layer The Relational Data Model + SQL. Abstraction Layers zConceptual yWhat data is held xAn Image and its meta-data xEntity-Relationship model."

Similar presentations


Ads by Google