Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

39
Advanced SQL (part 2) and SQL in practice • CS263 Lecture 8
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    233
  • download

    2

Transcript of Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Page 1: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Advanced SQL (part 2) and SQL in practice

• CS263 Lecture 8

Page 2: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Ensuring transaction integrity

• User-defined transactions can improve system performance because transaction will be processed as sets rather than individually

• Some SQL systems have concurrency controls that handle the updating of a shared database by concurrent users

• These can journalise database changes, so that a database can be recovered after abnormal termination in the middle of a transaction

• Such controls are transparent to the user, no special programming is needed to ensure proper control of concurrent access to data

Page 3: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Data dictionary facilities

• Data dictionary = system tables that store metadata• Users usually can view some of these tables, using SELECT

statements that can generate reports about system usage, user privileges etc,

• Users are restricted from updating them, sine the DBMS maintains them and depends on them for its processing

• In Oracle there are over 100 data dictionary views• Tables that anyone can access begin with USER or ALL• Tables that only the Database Administrator can use begin

with DBA

Page 4: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Data dictionary facilities

– Examples:– DBA_TABLES – descriptions of all tables in database– DBA_CONSTRAINTS – description of constraints– DBA_USERS – information about the users of the

system– DBA_TAB_PRIVS – descriptions of grants on objects

in the database

Page 5: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

SQL-99 enhancements/extensions• Enhancements to SQL have been built into many

implementations (including Oracle)• User-defined data types (UDT) subclasses of standard

types or an object type• Analytical functions (for OLAP/Data visualisation) - many

mathematical/statistical and related functions• Persistent Stored Modules (SQL/PSM) - capability to

create and drop code modules. Persistent means that a module of code will be stored until dropped, making it available for execution across user sessions.

Page 6: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

SQL-99 enhancements/extensions

– New statements:• CASE, IF, LOOP, FOR, WHILE, REPEAT etc

• New statements and PSM introduce procedurality into SQL (statements are processed sequentially) whereas base SQL is a non-procedural language and no statement execution sequence is implied

• SQL-99 Standard not widely adopted yet• Oracle has propriety version called PL/SQL

Page 7: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Routines and triggers

• These are stored in the database and controlled by the DBMSThese are stored in the database and controlled by the DBMS• This promotes stronger data integrity and consistency of use This promotes stronger data integrity and consistency of use

within the databasewithin the database• Since they are stored once, code maintenance is simplifiedSince they are stored once, code maintenance is simplified• Both consist of blocks of procedural codeBoth consist of blocks of procedural code• Trigger code is stored in the database and runs automatically Trigger code is stored in the database and runs automatically

whenever the triggering event (such as an UPDATE) occurswhenever the triggering event (such as an UPDATE) occurs• Routines do not run automatically, they have to be called in to Routines do not run automatically, they have to be called in to

operate operate

Page 8: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Triggers

• Since triggers are stored and executed in the database, they execute against all applications that access the database

• Triggers can also cascade, causing other triggers to fire

• They can be used to ensure referential integrity, enforce business rules, create audit trails etc.

• Constraints can be thought of as a special case of triggers, as they are applied automatically as a result of data modification commands (though they are not as flexible as triggers)

• Triggers have 3 parts, the event, the condition and the action

• The following trigger will automatically insert the order number whenever a new order is added

Page 9: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Triggers

• BIR stands for Before Insert Row, also requires that a sequence ID_SEQUENCE has been previously defined

• CREATE TRIGGER ORDER_ID_BIR

• BEFORE INSERT ON ORDER_T

• FOR EACH ROW

• BEGIN

• SELECT ID_SEQUENCE.NEXTVAL

• INTO: NEW.ORDER_ID

• FROM DUAL;

• END ORDER_ID_BIR;

Page 10: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Triggers

• Triggers may occur either before or after the statement that aroused the trigger is executed

• They may occur on INSERT, UPDATE or DELETE commands

• They may fire once for each time a row is affected, or they may fire once per statement

• Care should be taken when using them, since they fire automatically the user will be unaware of them

• One trigger can cause another to fire, can easily end up with an endless loop of triggers

Page 11: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Routines

• Routines are Program modules that execute on demand• Functions – routines that return values and take input

parameters• Procedures – routines that do not return values and can

take input or output parameters

Page 12: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Routines - example procedure

• CREATE OR REPLACE PROCEDURE PRODUCT_LINE_SALE• AS BEGIN• UPDATE PRODUCT_T• SET SALE_PRICE = 0.90*STANDARD_PRICE• WHERE STANDARD_PRICE >= 400;• UPDATE PRODUCT_T• SET SALE_PRICE = 0.85*STANDARD_PRICE• WHERE STANDARD_PRICE < 400;

• END (To run this procedure we would use: EXEC

PRODUCT_LINE_SALE)

Page 13: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Triggers contrasted with routines

Procedures are called explicitly

Triggers are event-driven

Page 14: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Oracle PL/SQL trigger syntax

SQL-99 Create routine syntax

Page 15: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Embedded and dynamic SQL

• Embedded SQL - including hard-coded SQL statements in a program written in another language such as C or Java = more efficient processing than interactive SQL

• Dynamic SQL - ability for an application program to generate SQL code on the fly, as the application is running - central to many internet applications (discussed in a later

lecture)

Page 16: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

SQL in practice

• Following material illustrates moving from ER diagram to SQL code

• Following Fig. Shows a simple ER diagram

• Other slides show how to populate and query tables using SQL code

Page 17: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Identify entities and attributes

Emp

Dept

EmpNo

Salary Comm

Hire DateName

Job

DeptNo

Location

Name

Page 18: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Identify relationships

Emp

Manage

Managed by

DeptWorks in

Assigned

Each employee may be managed by one other employee

Each employee may manage one or more other employees

Each employee must work in a single department

Each department may be assigned one or more employees

Integrity Rules – derived from ER Diagram:

Page 19: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Create a relational schema

Emp (EmpNo, Name, Job, Sal, Comm, HireDate, Mgr, DeptNo)

Dept (DeptNo, Name, Location)

DeptNo Number(2)Name Varchar2(14)Location Varchar2(13)

EmpNo Number(4)Name Varchar2(10)Job Varchar2(9)Sal Number(7,2)Comm Number(7,2)HireDate DateMgr Number(4)DeptNo Number(2)

EmpDept

Page 20: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Create relational tables

To create a relation in SQL the following ‘Create Table’ command is required:

create table R (A1 D1, A2 D2, … An Dn, integrity constraint1, integrity constarint2)

Where: R = Relation (table) nameA = Attribute nameD = Attribute domain

create table Dept (deptno number(2), name varchar2(14), location varchar2(13),constraint DeptPK primary key (deptno));

create table Emp (empno number(4), name varchar2(10), job varchar2(9), sal number(7,2), comm number(7,2), hiredate date, mgr number(4), deptno number(2), constraint EmpPK primary key (empno), constraint EmpFK1 foreign key (mgr) references Emp, constraint EmpFK2 foreign key (deptno) references Dept);

Page 21: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Populate relational tablesTo create a tuple in SQL the following ‘Insert’ command is required:

insert into R (attribute1, attribute2, … attributen )values (value1, value2, … valuen)

insert into Emp (empno, name, job, sal, comm, hiredate, mgr, deptno ) values (7839, ‘King’, ‘President’, 5000, NULL, ‘17-Nov-81’, NULL, 10)

insert into Emp (empno, name, job, sal, comm, hiredate, mgr, deptno ) values (7698, ‘Blake’, ‘Manager’, 1600, NULL, ’01-May-81’, 7839, 30)

insert into Dept (deptno, name, location ) values (10, ‘Accounting’, ‘New York’)

insert into Dept (deptno, name, location ) values (30, ‘Sales’, ‘Chicago)

The insert order matters in terms of referential integrity

constraints!

Page 22: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tablesTo query a relation in SQL the following ‘Select’ command is required:

SELECT [ALL | DISTINCT] attribute1, attribute2, … attributen

FROM relation1, relation2, … relationn

[WHERE condition-expression][GROUP BY attribute1, attribute2, … attributen ][HAVING condition-expression][ORDER BY attribute1, attribute2, … attributen ]

Simple Example: list all Employees and the departments they work in

select empno, name, deptno from Emp;

7839 King 107698 Blake 30

Page 23: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables

Simple Example: list all Employees that work in department 30

select empno, namefrom Empwhere deptno = 30;

7698 Blake

Simple Example: list all Employees that work in either department 10 or 30

select empno, namefrom Empwhere deptno = 10 or deptno = 30;

7839 King7698 Blake

Page 24: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables - JoinExample: list Employee and Department names of all employees that work

in either department 10 or 30

select emp.name, dept.namefrom Emp, Deptwhere (emp.deptno = 10

or emp.deptno = 30);

King AccountingKing SalesBlake AccountingBlake Sales

select emp.name, dept.namefrom Emp, Deptwhere (emp.deptno = dept.deptno) and (emp.deptno = 10 or emp.deptno = 30)

King AccountingBlake Sales

Page 25: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Order by

select emp.name, dept.namefrom Emp, Deptwhere (emp.deptno = dept.deptno) and (emp.deptno = 10 or emp.deptno = 30)order by emp.name asc;

Blake SalesKing Accounting

select emp.name, dept.namefrom Emp, Deptwhere (emp.deptno = dept.deptno) and (emp.deptno = 10 or emp.deptno = 30)order by dept.name desc;

Blake SalesKing Accounting

select namefrom Deptorder by name;

AccountingSales

Remember in relations Remember in relations neither tuples nor attributes neither tuples nor attributes

have any intrinsic order!have any intrinsic order!

Page 26: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Example relations

EMPNO NAME JOB MGR HIREDATE SAL COMM DEPTNO7369 SMITH CLERK 7902 17-DEC-80 800 207499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 307521 WARD SALESMAN 7698 22-FEB-81 1250 500 307566 JONES MANAGER 7839 02-APR-81 2975 207654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 307698 BLAKE MANAGER 7839 01-MAY-81 2850 307782 CLARK MANAGER 7839 09-JUN-81 2450 107788 SCOTT ANALYST 7566 19-APR-87 3000 207839 KING PRESIDENT 17-NOV-81 5000 107844 TURNER SALESMAN 7698 08-SEP-81 1500 0 307876 ADAMS CLERK 7788 23-MAY-87 1100 207900 JAMES CLERK 7698 03-DEC-81 950 307902 FORD ANALYST 7566 03-DEC-81 3000 207934 MILLER CLERK 7782 23-JAN-82 1300 10

DEPTNO NAME LOCATION10 ACCOUNTING NEW YORK20 RESEARCH DALLAS30 SALES CHICAGO 40 OPERATIONS BOSTON

Dept

Emp

Page 27: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables - Outer Join

Example: list all departments and the names of staff that work in them.

select dept.name, emp.namefrom Emp, Deptwhere (emp.deptno (+) = dept.deptno)order by dept.name, emp.name;

Accounting Clark Accounting King Accounting MillerOperationsResearch AdamsResearch FordResearch JonesResearch ScottResearch SmithSales AllenSales BlakeSales JamesSales MartinSales TurnerSales Ward

Page 28: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Group by

Example: List employee’s departments giving a count of employees in each.

select deptnofrom Emporder by deptno;

1010102020202020303030303030

select deptnofrom Empgroup by deptno;

102030

select deptno, count(*)from Empgroup by deptno;

10 320 530 6

Aggregate Function!

Select queries can contain functions and calculations as well as attribute names in the select condition!

Page 29: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Group by

select deptno, sum(sal), sum(sal)/count(*)from Empgroup by deptno;

As we are dealing with groups of tuples, rather than individual tuples, there are only certain types of data that can be selected: 1) attributes named in the group by clause; 2) group aggregate functions; 3) expressions involving combinations of 1) and 2).

10 8750 2916.666720 10875 217530 9400 1566.6667

select deptno, count(*)from Empgroup by deptnohaving count(*) > 4;

20 530 6

Example: List employee’s departments giving a count of employees in each, provided that there are over four employees in the department.

The The havinghaving clause is used in Group clause is used in Group Selections in the same way that the Selections in the same way that the wherewhere clause is used in standard clause is used in standard tuple selections. tuple selections.

Page 30: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Group by

10 8750 2916.666720 10875 2175

select deptno, sum(sal), sum(sal)/count(*)from Empgroup by deptnohaving sum(sal)/count(*) > 2000order by sum(sal)/count(*) desc;

Combined Example: List, in reverse order of average department salary, the total salary and average salary for each department that has employees, and where the average salary of each department is over £2000

The result of an SQL Select is, as shown, a new (unnamed) relation. The attributes of this new relation are named on the basis of the select statement.

DEPTNO SUM(SAL) SUM(SAL)/COUNT(*)10 8750 2916.666720 10875 2175

DEPTNO Total Salary Avg Salary10 8750 2916.666720 10875 2175

ALIASALIAS:- select deptno, sum(sal) “Total Salary”, sum(sal)/count(*) “Avg Salary”

Page 31: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Inner SelectsAs the result of an SQL Select on a relation(s) is itself a relation, it follows that, as with Relational Algebra, the result of one select can be used as the input into another SQL Select!

However, rather than create, and name, a separate relation to contain the output of the first select and then use this relation in the second select, it is possible to ‘pipe’ the output of the first select directly into the second select!

select name, salfrom Empwhere sal > (select AVG(sal) from Emp)order by name;

Example: List employee’s who earn more than the average company salary.

NAME SALBLAKE 2850CLARK 2450FORD 3000JONES 2975KING 5000SCOTT 3000

2

1 2073.2143, is substituted for this select statement

Page 32: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Inner SelectsRemember: Inner Selects (also called sub-selects or sub-queries) are full-bodied SQL Select statements: Therefore, they can, when required to do so, return more than a single value (one tuple, one attribute) relation.

Example: List employees who earn the maximum salary in each of the departments.

select name, deptno, salfrom Empwhere (deptno, sal) in (select deptno, max(sal)

from emp group by deptno)

order by deptno;

NAME DEPT SALKING 10 5000SCOTT 20 3000FORD 20 3000BLAKE 30 2850

DEPT MAX(SAL)10 500020 300030 2850

This relation is substituted for the inner select statement

Page 33: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Query relational tables – Inner SelectsNote: It is sometimes necessary to make reference to an attribute value from the outer select, within the Where Clause of the inner select. This can be achieved by using a Relation Alias!

Example: List employee’s who earn more than the average salary in the department they work in.

select name, deptno, salfrom Emp E1where sal > (select AVG(sal) from Emp where deptno = E1.deptno

group by deptno)order by deptno;

NAME DEPT SALKING 10 5000JONES 20 2975SCOTT 20 3000FORD 20 3000ALLEN 30 1600BLAKE 30 2850

The deptno attribute value of the current tuple will be substituted here! If The deptno attribute value of the current tuple will be substituted here! If there are 14 employee tuples there will be 14 separate substitutions (even there are 14 employee tuples there will be 14 separate substitutions (even though there are only three departments)!though there are only three departments)!

Relation Alias

Page 34: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Example: list all staff that work in either of two departments (each dept. has a separate database), showing their staff number, and date of birth.

select staffno, dob from DepAUNIONselect staffno, dob from DepB;

SQL set operations – Union

staffno dobSL10 14-02-64SA51 21-11-82DS40 01-01-40

staffno dobCC15 11-03-66SA51 21-11-82

DepA DepB

staffno dobSL10 14-02-64SA51 21-11-82DS40 01-01-40CC15 11-03-66

Page 35: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Example: list all staff that work in both departments (each dept. has a separate database), showing their staff number, and date of birth.

select staffno, dob from DepAINTERSECTselect staffno, dob from DepB;

SQL set operations – Intersection

staffno dobSL10 14-02-64SA51 21-11-82DS40 01-01-40

staffno dobCC15 11-03-66SA51 21-11-82

DepA DepB

staffno dobSA51 21-11-82

Page 36: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

Example: list all staff that only work in department A (each dept. has a separate database), showing their staff number, and date of birth.

select staffno, dob from DepAMINUSselect staffno, dob from DepB;

SQL set operations – difference

staffno dobSL10 14-02-64SA51 21-11-82DS40 01-01-40

staffno dobCC15 11-03-66SA51 21-11-82

DepA DepB

staffno dobSL10 14-02-64DS40 01-01-40

Page 37: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

SQL - Group Insert statementTo create a set of tuples in SQL the following ‘Insert’ command can be used:

insert into R (attribute1, attribute2, … attributen )select (attribute1, attribute2, … attributen) from relation1, relation2, … relationn

[where condition-expression] [group by attribute1, attribute2, … attributen ] [having condition-expression] [order by attribute1, attribute2, … attributen ]

insert into DepA (staffno, name, job, hiredate) select empno, name, job, hiredate from Emp where deptno = 10;

Example: copy details of all employees that work in department 10 from the Emp relation into the DepA relation.

corresponding attributes have to be of the same type

Each tuple to be inserted has to be unique!

Page 38: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

SQL - Delete statement

delete from R[where condition-expression]

To delete a set of tuples in SQL the following ‘Delete’ command is used:

Example: remove details of all employees that work in department 10 from the Emp relation.

Delete from Empwhere deptno = 10;

If the where clause is omitted then all tuples in the relation will be removed!

Page 39: Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.

SQL - Update statement

To alter a set of tuples in SQL the following ‘Update’ command is used:

update Rset attribute1 = datavalue1, attribute2 = datavalue2, ... attributen = datavaluen

[where condition-expression]

Example: increase the salary of all employees that work in department 10 by 10%.

update Empset sal = sal *1.1where deptno = 10;

If the where clause is omitted then all tuples in the relation will be altered!