Title: Oracle Quiz on SQL and PLSQL See water burning Alex Nuijten
1Oracle Quiz on SQL and PL/SQL See water
burning Alex Nuijten Lucas JellemaAMIS, The
Netherlands
2Journey to the Mythical Heart of the Matter...
- We will look at some lesser known Oracle SQL and
PL/SQL features - And see some ..ahem alternative usages of well
known features - In the form of a Quiz, you can show off
- Multiple choice, no modifications, play fair!
- Winner gets to select any book from the bookstall
3What is the name of Scotts cat?
1
4The Demo-tables The Infamous SCOTT schema
5What has been done to have this query return
this result?
- Data in EMP have been manipulated
- 10g Package DBMS_ADVANCED_REWRITE has been
(ab)used - Pre-parsed cursor fetch has been rolled over by
buffer- cache checkpoint high water mark - 9iR2 and beyond query randomizer at work
2
6What is the result of this query?
- OPERATIONS
- New York
- 04
- SELAS
3
7Which query implements this question?
- For all employees, return the name and
- If they are manager
- And their salary is 2950 or up, return label
High - Salary under 2950, return label Low
- Else, in case they are not a manager, return
- label Low if they earn under 1000
- label Medium Odd if they have an odd salary
over 1000 - label Medium if their salary is over 1000 and
it is an even number
8Which query implements this question?
B.
A.
4
C.
D.
9CASE offers a much more attractive solution
10DECODE vs. CASE
- Why not DECODE
- Hard to read, impossible to maintain
- Not part of the ANSI standards for SQL
- CASE expressions are a lot more powerful
- When/Where CASE
- In SQL
- select, where, having, order by
- In PL/SQL
- In Check Constraints
- In Index definitions
11How can we implement this business rule?
- A department may not have more than one CLERK
- Unique Constraint - on EMP (DEPTNO, JOB)
- Check Constraint with CASE expression
- Unique Index
- Insert and Update trigger on EMP (column DEPTNO
and JOB)
5
12Function Based Unique Index
- An index can be defined as UNIQUE
- A UNIQUE index ignores NULL-clashes
- UNIQUE (deptno, job) accepts (NULL,NULL) and
(NULL,NULL) - A Function Based Index can be defined for an
expression or PL/SQL function call, for example a
CASE expression - A department may not have more than one CLERK
13How do we make a random selection of
approximately 10 of our Employee records?
B.
A.
6
C.
D.
14FROM SAMPLE (8i)
- FROM lttablegt sample (percentage)
- The sample_clause lets you instruct Oracle to
select from a random sample of rows from the
table, rather than from the entire table. - The sample percentage is between 0.000001 and 100
- The percentage indicates the chance for each
record to be included in the sample
select emp. , dept.dname from emp sample
(10) -- 10 , deptwhere emp.deptno
dept.deptno
15Whoops Management
- I perform this update
- update emp set sal (1 dbms_random.value)
sal - commit
- Whoops.production system Oh Oh.
- How can I best correct this situation?
- Flashback Database and Roll Forward until just
prior to the update - Select old values from Flashback Query and update
- Restore a database backup, export table and
import - Use the 10g Undo Transaction statement
7
16Flashback Query
- select from table AS OF TIMESTAMP
- Select values as they were at a certain moment in
time - Depends on UNDO data
- In actual fact is every Oracle query (due to the
read-consistency) some sort of flashback query)
17The Top-3 Earning Employees
- What can you state about the query below with
regard to the quest for the Top 3 earning
employees?
- Returns the correct answer
- May sometimes return the correct answer
- Okay as long as there are no duplicate salaries
- Not correct
8
18In-Line Views
- In line views have been around since 7.2 (and in
7.1 an undocumented feature)
19Can we select the Top 3 Earners PER DEPARTMENT?
A.
B.
Can not be done in a single Query
9
C.
D.
20Analytical Functions
21SELECT ANALYTICAL FUNCTION (8i/9i)
- Functions for advanced analysis
- OLAP inspired
- Very efficient compared to traditional SQL
- Since 8.1.7 EE and 9iR2 SE
- Typical Analytical operations
- Aggregation
- Ranking
- Deduplication
- Inter-row comparisons and calculations
- Spreadsheet-ish
- Statistical Analysis
22Which of these queries contains an invalid Oracle
9iR2 SQL-statement
B.
A.
10
C.
D.
23User Defined Aggregate
select avg(hiredate) from emp
ORA-00932 inconsistent datatypes expected
NUMBER got DATE
- Oracle Data Cartridge allows us to define our own
Aggregates - For example SUM_VARCHAR2, AVG_DATE,COUNT_CHARA
CTERS
24User defined sum_varchar2 aggregate
- Implement AmisVarchar2SumImpl object
- Create function sum_varchar2 as aggregate using
object AmisVarchar2SumImpl
select deptno , sum_varchar2(ename)
employees from emp group by deptno
25What is going on here?
Plaatje EXCEL
- Table Function and UTL_FILE
- External Table
- Materialized View
- REDO LogFile
11
26EXTERNAL TABLE (9i)
- Data in an external text-file can be published as
table in the database - accessed in SQL and PL/SQL like a normal table
- Define table with ORGANIZATION EXTERNAL
CREATE TABLE emp_ext ( empcode NUMBER(4),
empname VARCHAR2(25), deptname VARCHAR2(25),
hiredate date ) ORGANIZATION EXTERNAL ...
select from where
27Language!?
28What (or who) is behind all this?
- View on Table Function that generates of
transforms values - NLS_RESOURCE_BUNDLE settings
- Virtual Private Database policy function on
table - View with PL/SQL Function Call in the WHERE-clause
12
29FROM TABLE FUNCTION (9i)
- (result returned by) PL/SQL Function can be used
as data-source in query - As if it were a table
- Function must return a Server NESTED TABLE Type
- CREATE TYPE NUM_TABLE IS TABLE OF NUMBER
- Functions can be nested, parallelized and/or
pipelined
... from table( function(ltparameters) )where
...
30PieCharts in SQL
- Can you have a SQL query return apie chart?
- For example to review the salary sumsper JOB
catagory
- No. Are you out of your mind?
- I would be surprised. I assume with Table
Functions - Piece of cake (pun intentional) use the new 10g
EXTRACT_PIE function - Yeah, you can do that, but only on Windows
13
31PieChart in SQL
32Final scores.
- How many correct answers did you give?
33 and the winner is
34How often will this trigger fire for this update
statement?
- No more than 3 times
- Up to 5 times
- At least three with a maximum of 6 times
- Unlimited
35What is happening?
Session 1
Session 2
update i1, i2 and begin with i3 run into lock
held by session 2
updates are rolled-back and a select-for update
is initiated
The lock on i3 is released now i equals 6!
36Which Country is The Netherlands?