DPA Lecture 4
DPA Lecture 4
Dr Ismail Alarab
[email protected]
www.bournemouth.ac.uk
Recap
Scan Me!
Lecture Outline
➢Data Retrieval
➢Complex queries
➢Joins
➢Views
➢Security
➢Indexes
➢Data Dictionaries
➢Transactions
Complex Queries
Complex Queries
• Correlated subqueries
– Subquery depends on a value from parent query – cannot run independently
Subqueries
► The subquery (inner query) executes once before the main query.
► The result of the subquery is used by the main query (outer query).
Using a Subquery
Subquery Rules
• The Oracle server returns results into the HAVING clause of the main
query.
Multiple Row Subqueries
– JOIN
– IN
– EXISTS
Obtaining Data From Multiple Tables
Cartesian Product
JOINS
“JOIN”
Select all names and numbers of all salesmen and all employees
from departments based in Chicago
UNION (same to OR, can be faster)
select empno, ename from emp where job='SALESMAN‘
union
select empno, ename from emp, dept where
dept.deptno=emp.deptno and loc='CHICAGO'
More below:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions001.htm
Managing a Database
• Creating views
• Security
• Indexes
• Data dictionary
Views
Views
• Views are “virtual” tables, which are created on the runtime using
the stored query.
• Views themselves do not contain data. They just act as a window
through which certain (not all) contents of a table can be viewed.
Create View
• Rows can normally be inserted into, updated or deleted from a base table
using a view, subject to the following restrictions:
• If the view query contained SET, DISTINCT or GROUP BY or an aggregate
or analytic function, the base tables cannot be modified using the view.
• If the view query contains WITH CHECK OPTION, a row cannot be
inserted or updated if the view is unable to select the row.
• If there is a NOT NULL column, a row can only be inserted if a value is
supplied for that column in the INSERT command, or if a DEFAULT value
is specified in the CREATE TABLE command.
Sequences
Insert into emp (empno, ename, job, doj, sal, deptno) Values (Empcode.nextval, ‘Mini’,
‘Manager’, ’12-MARO4’, 7000,10) ;
Security
Security
User Jones must be able to retrieve data from the SalesRep table:
grant select on SalesRep to Jones
Users Smith and Brown must be able to add new parts:
grant insert on Part to Smith, Brown
Permit sales rep 3 (Mary Jones) to access any data concerning her customers
but do not permit her to access data concerning any other customers:
Definition
– "A collection of operations that performs a single
logical function in a database application." (Korth)
Atomicity
– A transaction is treated as indivisible.
– Other users never see the results of a partially completed
transaction.
Consistency
– A transaction takes a database from one consistent state to
another.
– During a transaction, the database does not need to be
consistent.
Ideal Properties of a transaction (ACID)
Isolation / Independence
– Transactions are kept isolated from all other transactions until
they either commit or rollback.
Durability
– Changes made during a transaction are permanent once the
transaction commits.
Further Reading
Oracle documentation
https://docs.oracle.com/database/121/CNCPT/sqllangu.htm#CNCPT015
Questions?