Upload
myrtle-harper
View
235
Download
0
Embed Size (px)
Citation preview
1Oracle Quiz on SQL and PL/SQL – See water burning
Oracle Quiz on SQL and PL/SQL
See water burning
Alex Nuijten & Lucas JellemaAMIS, The Netherlands
2Oracle Quiz on SQL and PL/SQL – See water burning
Journey 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
3Oracle Quiz on SQL and PL/SQL – See water burning
What is the name of Scott’s cat?
A. King
B. Tiger
C. Big Red
D. Bruce
1
4Oracle Quiz on SQL and PL/SQL – See water burning
The Demo-tables – The Infamous SCOTT schema
5Oracle Quiz on SQL and PL/SQL – See water burning
What has been done to have this query return this result?
A. Data in EMP have been manipulated
B. 10g Package DBMS_ADVANCED_REWRITE has been (ab)used
C. Pre-parsed cursor fetch has been rolled over by buffer- cache checkpoint high water mark
D. 9iR2 and beyond query randomizer at work
2
6Oracle Quiz on SQL and PL/SQL – See water burning
What is the result of this query?
A. OPERATIONS
B. New York
C. 04
D. SELAS
3
7Oracle Quiz on SQL and PL/SQL – See water burning
Which 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
8Oracle Quiz on SQL and PL/SQL – See water burning
Which query implements this question?
A. B.
D.C.
4
9Oracle Quiz on SQL and PL/SQL – See water burning
CASE offers a much more attractive solution:
10Oracle Quiz on SQL and PL/SQL – See water burning
DECODE 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
11Oracle Quiz on SQL and PL/SQL – See water burning
How can we implement this business rule?
A department may not have more than one CLERK
A. Unique Constraint - on EMP (DEPTNO, JOB)
B. Check Constraint with CASE expression
C. Unique Index
D. Insert and Update trigger on EMP (column DEPTNO and JOB)
5
12Oracle Quiz on SQL and PL/SQL – See water burning
Function 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:
13Oracle Quiz on SQL and PL/SQL – See water burning
How do we make a random selection of approximately 10% of our Employee records?
A. B.
D.C.
6
14Oracle Quiz on SQL and PL/SQL – See water burning
FROM … SAMPLE (8i)
FROM <table> 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.dnamefrom emp sample (10) -- 10%, deptwhere emp.deptno = dept.deptno
15Oracle Quiz on SQL and PL/SQL – See water burning
Whoops 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?
A. Flashback Database and Roll Forward until just prior to the update
B. Select old values from Flashback Query and update
C. Restore a database backup, export table and import
D. Use the 10g ‘Undo Transaction’ statement
7
16Oracle Quiz on SQL and PL/SQL – See water burning
Flashback 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)
17Oracle Quiz on SQL and PL/SQL – See water burning
The Top-3 Earning Employees
What can you state about the query below with regard to the quest for the “Top 3 earning employees”?
A. Returns the correct answer
B. May sometimes return the correct answer
C. Okay as long as there are no duplicate salaries
D. Not correct
8
18Oracle Quiz on SQL and PL/SQL – See water burning
In-Line Views
In line views have been around since 7.2 (and in 7.1 an undocumented feature)
19Oracle Quiz on SQL and PL/SQL – See water burning
Can we select the Top 3 Earners PER DEPARTMENT?
A. B.
D.
C.
Can not be done in a single Query
9
20Oracle Quiz on SQL and PL/SQL – See water burning
Analytical Functions
21Oracle Quiz on SQL and PL/SQL – See water burning
SELECT … 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
22Oracle Quiz on SQL and PL/SQL – See water burning
Which of these queries contains an invalid Oracle 9iR2 SQL-statement
A. B.
D.C.
10
23Oracle Quiz on SQL and PL/SQL – See water burning
User Defined Aggregate
Oracle Data Cartridge allows us to define our own Aggregates For example:
SUM_VARCHAR2, AVG_DATE,COUNT_CHARACTERS
select avg(hiredate) from emp
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
24Oracle Quiz on SQL and PL/SQL – See water burning
User defined sum_varchar2 aggregate
Implement AmisVarchar2SumImpl object Create function sum_varchar2 as aggregate
using object AmisVarchar2SumImpl
select deptno, sum_varchar2(ename) employeesfrom empgroupby deptno
25Oracle Quiz on SQL and PL/SQL – See water burning
What is going on here?
A. Table Function and UTL_FILE
B. External Table
C. Materialized View
D. REDO LogFile
Plaatje EXCEL
11
26Oracle Quiz on SQL and PL/SQL – See water burning
CREATE TABLE emp_ext ( empcode NUMBER(4), empname VARCHAR2(25), deptname VARCHAR2(25), hiredate date ) ORGANIZATION EXTERNAL ...
EXTERNAL 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
ExternalTables select *
from …where …
27Oracle Quiz on SQL and PL/SQL – See water burning
Language!?
28Oracle Quiz on SQL and PL/SQL – See water burning
What (or who) is behind all this?
A. View on Table Function that generates of transforms values
B. NLS_RESOURCE_BUNDLE settings
C. Virtual Private Database – policy function on table
D. View with PL/SQL Function Call in the WHERE-clause
12
29Oracle Quiz on SQL and PL/SQL – See water burning
FROM … 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(<parameters) )where ...
30Oracle Quiz on SQL and PL/SQL – See water burning
PieCharts in SQL
Can you have a SQL query return apie chart? For example to review the salary sums
per JOB catagory
A. No. Are you out of your mind?
B. I would be surprised. I assume with Table Functions…
C. Piece of cake (pun intentional) : use the new 10g EXTRACT_PIE function
D. Yeah, you can do that, but only on Windows
13
31Oracle Quiz on SQL and PL/SQL – See water burning
PieChart in SQL
32Oracle Quiz on SQL and PL/SQL – See water burning
Final scores….
How many correct answers did you give?
A, C or D
13
12
11
101
2
3
4
5
6
7
8
9
B
B
D
A
C or D
B or D
B (a or c)
B or D
D (c)
A
B or A
B
33Oracle Quiz on SQL and PL/SQL – See water burning
… and the winner is…
34Oracle Quiz on SQL and PL/SQL – See water burning
How often will this trigger fire for this update statement?
A. No more than 3 times
B. Up to 5 times
C. At least three with a maximum of 6 times
D. Unlimited
35Oracle Quiz on SQL and PL/SQL – See water burning
What is happening?
Session 1 Session 2
update i=1, i=2 and begin with i=3; run into lock held
by session 2
updates are rolled-back and a select-
for update is initiated
The lock on i=3 is released now; i
equals 6!
36Oracle Quiz on SQL and PL/SQL – See water burning
Which Country is The Netherlands?