Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL zSeQueL zSQL1 - 1986 zSQL2 - 1992 ybetter support for Algebraic operations zSQL3 - 1999 Post-Relational yrow and column types, stored procedures, triggers,

Similar presentations


Presentation on theme: "SQL zSeQueL zSQL1 - 1986 zSQL2 - 1992 ybetter support for Algebraic operations zSQL3 - 1999 Post-Relational yrow and column types, stored procedures, triggers,"— Presentation transcript:

1 SQL zSeQueL zSQL1 - 1986 zSQL2 - 1992 ybetter support for Algebraic operations zSQL3 - 1999 Post-Relational yrow and column types, stored procedures, triggers, references, large objects

2 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

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 Relation DeptnoDnameLoc 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston Relation tuple Column (field) domain string city name general specific integer 0<int<99

6 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 yThe ordering of rows is not significant. yEach cell or column/row intersection in a relation should contain only a so-called atomic value.

7 Primary Keys, Foreign Keys and Domains Each relation must have a primary key. This is to enforce the property that duplicate rows are forbidden in a relation. A primary key is one or more columns of a table whose values are used to uniquely identify each of the rows in a table. Foreign keys are the means of interconnecting the data stored in a series of disparate tables. A foreign key is a column or group of columns of some table which draws its values from the same domain as the primary key of some related table in the database. Domains are pools of values from which actual values appearing in the columns of a table are drawn. A special character is used in relational systems to indicate incomplete or unknown information - the character null. This character which is distinct from zero or space is particularly useful in the context of primary-foreign key links

8 Relational Algebra zA group of operations on relations which yield other relations – “Closed” yA single tuple (row) is a relation yA single value is a relation zBase operations yRESTRICT, PROJECT, PRODUCT zConvenience operations yEQUI-JOIN, (Natural) JOIN, Outer Joins zSet operations yUNION, INTERSECTION, DIFFERENCE, DIVISION

9 The Relational Algebra Restrict Project Product Union Intersect

10 EMP-DEPT example (from SQL workbook) Two relations: Department : DEPT Employee : EMP DEPT EMP manager worksFor

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

12 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

13 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

14 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

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

16 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

17 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

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

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

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

21 EmpnoEname 7369SMITH 7499ALLEN 7521WARD 7566JONES 7654MARTIN 7698BLAKE 7782CLARK 7788SCOTT 7839KING 7844TURNER 7876ADAMS 7900JAMES 7902FORD 7934MILLER Select Mgr from Emp where Sal between 2000 and 3000 Select Empno, Ename from Emp Find the names of the managers of employees who earn between 2000 and 3000 E M EmpnoMgrDeptno 7566783920 7698783930 7782783910

22 Find the names of the managers of employees who earn between 2000 and 3000 Select Ename From Emp E, Emp M Where E.mgr=M.empno and E.sal between 2000 and 3000 Mgr Empno Ename 78397839KING Distinct(Ename)

23 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

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

25 Learning SQL zWorkbook zSQL Textbook zChapter 10 of Welling and Thomson


Download ppt "SQL zSeQueL zSQL1 - 1986 zSQL2 - 1992 ybetter support for Algebraic operations zSQL3 - 1999 Post-Relational yrow and column types, stored procedures, triggers,"

Similar presentations


Ads by Google