SQL zSeQueL 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
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
Relation DeptnoDnameLoc 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston Relation tuple 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 yThe ordering of rows is not significant. yEach cell or column/row intersection in a relation should contain only a so-called atomic value.
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
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
The Relational Algebra Restrict Project Product Union Intersect
EMP-DEPT example (from SQL workbook) Two relations: Department : DEPT Employee : EMP DEPT EMP manager worksFor
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,
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,
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;
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
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 KING Distinct(Ename)
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;
Learning SQL zWorkbook zSQL Textbook zChapter 10 of Welling and Thomson