Professional Documents
Culture Documents
Oracle9i Dfdev v1.3 Workshop Solutions
Oracle9i Dfdev v1.3 Workshop Solutions
Table of Contents
connect system/dave@angela
/* Is UNDO management active? */
select value from v$parameter where name = 'undo_management';
connect dave/dave@angela
-- Create a directory:
create or replace directory davedirectory as 'c:\oraclass';
(
RECORDS FIXED 231
FIELDS (
CUST_NO INTEGER
EXTERNAL(10),
LASTNAME
CHAR(20),
FIRSTNAME
CHAR(15),
MIDINIT CHAR(1),
STREET
CHAR(30),
CITY
CHAR(20),
STATE CHAR(2),
ZIP CHAR(5),
ZIP_4 CHAR(4),
AREA_CODE CHAR(3),
PHONE CHAR(8),
COMPANY_NAME CHAR(50)
)
)
LOCATION ('gold_cust.txt')
)
PARALLEL 1
REJECT LIMIT UNLIMITED;
-- 3. Insure that other users can query your new external table.
grant select on gold_cust_ext to public;
connect scott/tiger@class2
select cust_no, lastname from system.gold_cust_ext;
1. Merge
-- SkillBuilders
-- Oracle9i SQL MERGE Workshop
,CL.STREET
,CL.CITY
,CL.STATE
,CL.ZIP
,CL.ZIP_4
,CL.AREA_CODE
,CL.PHONE
,CL.COMPANY_NAME );
2. Join
-- SkillBuilders, 2004
-- SUPPLIER_NAME DESCRIPTION
-- ------------------------------ ------------------
-- Sharps Gift Supply Co. Long stem red rose
-- ACME Gift Supply Co. Long stem red rose
-- ABC Gift Supply Co. Long stem red rose
-- Sharps Gift Supply Co. 1 dozen roses
-- ACME Gift Supply Co. 1 dozen roses
3. SubQuery With
with
level_1_customer AS (select cust_no
from ord
group by cust_no
having sum(total_order_price) >= 100)
--
-- SkillBuilders Oracle9i New Features: CONNECT BY Workshop
--
-- Use the PARTS table to create the following report. (If you
do not have a
-- PARTS table, run the supplied script PARTS.SQL.)
-- Note the sort on the PART_NAME column.
/*
PARTS
-----------------------
**10 widgets
****3 wodgets
******5 weebles
******4 weedgets
******6 woobles
********7 werbless
****2 wudgets
7 rows selected.
*/
--
-- Solution:
--
5. Case
-- SkillBuilders, 2004
select
sum ( case when total_order_price between 1 and 50 then 1 else 0
end )
as top_1_50,
sum ( case when total_order_price between 51 and 100 then 1 else
0 end )
as top_51_100,
sum ( case when total_order_price between 101 and 150 then 1
else 0 end )
as top_101_150,
sum ( case when total_order_price between 151 and 200 then 1
else 0 end )
as top_151_200,
sum ( case when total_order_price > 200 then 1 else 0 end )
as top_GT_200
from ord
/
1. Associative Arrays
--
-- SkillBuilders Associative Array Workshop
-- Solutiuon:
DECLARE
TYPE product_count_type IS TABLE OF NUMBER(20) INDEX
BY VARCHAR2(75);
product_count product_count_type;
v_product_name product.description%TYPE;
v_product_count NUMBER(20);
v_index VARCHAR2(75);
CURSOR read_counts IS
SELECT description, count(ord_item.product_id)
FROM product, ord_item
WHERE product.product_id = ord_item.product_id (+)
GROUP BY description;
BEGIN
OPEN read_counts;
LOOP
FETCH read_counts INTO v_product_name,
v_product_count;
EXIT WHEN read_counts%NOTFOUND;
product_count(v_product_name) := v_product_count;
END LOOP;
CLOSE read_counts;
DBMS_OUTPUT.PUT_LINE (product_count.COUNT);
DBMS_OUTPUT.PUT_LINE (product_count.FIRST);
DBMS_OUTPUT.PUT_LINE (product_count.LAST);
v_index := product_count.FIRST; -- get subscript of first
element
WHILE v_index IS NOT NULL LOOP
DBMS_OUTPUT.PUT_LINE (v_index||'
'||product_count(v_index));
v_index := product_count.NEXT(v_index); -- get subscript
of next element
END LOOP;
END;
/
return;
END;
/
show errors
3. UTL_FILE
-- SkillBuilders, 2004
DECLARE
v_raw RAW(32767);
v_file_id UTL_FILE.FILE_TYPE;
BEGIN
/* Open the file for reading */
v_file_id:=
UTL_FILE.FOPEN('c:\','blob.jpg','r',32767);
/* Execute GET_RAW to retrieve the blob data */
UTL_FILE.GET_RAW (v_file_id,v_raw);
/* Load the data into the blob_table */
INSERT INTO blob_table VALUES (1,v_raw);
/* Close the file */
UTL_FILE.FCLOSE( v_file_id);
END;
/
drop table t;
create table t
(c1 timestamp with time zone,
c2 timestamp with local time zone)
/
-- 18-APR-03 02.05.41.114000 PM
</course>
</term>
<term>
<heading name="Spring 1999"/>
<course>
<course-name>Physics for Poets</course-name>
<grade>A</grade>
<credits>7</credits>
</course>
<course>
<course-name>Poetry for Physicists</course-
name>
<grade>C+</grade>
<credits>5</credits>
</course>
</term>
<summary>Student has earned 19 credits so far</summary>
<comments>
Student seems well-rounded. He occasionally withdraws into his
shell
but is usually a good egg.
</comments>
</transcript>')
);
COMMIT;
select extract(transcript,
'/transcript/student/home_address/text()' )
from student_transcript
/
select
extract(transcript,'/transcript/student/interests/interest/text()
')
from student_transcript;
declare
v_order_total order_summary.order_total%type;
begin
for i in 1..90000 loop
v_order_total := i * 1.10;
insert into order_summary values (i, v_order_total);
end loop;
end;
/
connect dave/dave@class2
select u1.username, r1.name, status, timeout
from dba_resumable r1, dba_users u1
where u1.user_id = r1.user_id;
disc
connect system/dave@class2
alter tablespace dave add datafile size 5m;
select u1.username, r1.name, status, timeout
from dba_resumable r1, dba_users u1
where u1.user_id = r1.user_id;
disc
-- SkillBuilders, 2004
-- DES3 Workshop
IF UPDATING THEN
dbms_obfuscation_toolkit.DES3GETKEY(which=>1,
seed_string=>v_seed,
key=>v_key);
dbms_obfuscation_toolkit.DES3ENCRYPT(input_string =>
RPAD(:NEW.ccn,16,' '),
key_string => v_key, encrypted_string => v_encrypted_string
);
:NEW.ccn := v_encrypted_string;
UPDATE cust_no_Key_table
SET key_value = v_key
WHERE cust_no = :OLD.cust_no;
END IF;
END;
/
1. Bitmap Join
-- SkillBuilders, 2004
-- Lesson 10 - Bitmap Join Index Solution
2. Skip Scan
-- SkillBuilders, 2004
-- Lesson 10 - Index Skip Scan Solution
-- Execution Plan
-- --------------------------------------------------------------
--------
-- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1
Bytes=9)
-- 1 0 TABLE ACCESS (FULL) OF 'EMPLOYEE' (Cost=2 Card=1
Bytes=9)
-- --------------------------------------------------------------
----------
-- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1
Bytes=9)
-- 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'EMPLOYEE'
(Cost=2 Card=1
-- Bytes=9)
--
-- 2 1 INDEX (SKIP SCAN) OF 'IX1' (NON-UNIQUE) (Cost=1
Card=1)
1. V$SQL_PLAN
see supplid v$sql_plan_query.SQL
2. DBMS_STATS
-- SkillBuilders, 2004
-- DBMS_STATS Workshop
-- No, many rows were returned revealing that index stats where
not collected.
SELECT table_name
FROM user_tables
WHERE last_analyzed IS NULL;
SELECT index_name
FROM user_indexes
WHERE last_analyzed IS NULL;
3. Dynamic Sampling
-- SkillBuilders, 2004
-- Execution Plan
-- --------------------------------------------------------------
----------
-- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=603
Card=11239 Bytes
-- =674340)
--
-- 1 0 MERGE JOIN (Cost=603 Card=11239 Bytes=674340)
-- 2 1 SORT (JOIN) (Cost=201 Card=11239 Bytes=427082)
-- 3 2 TABLE ACCESS (FULL) OF 'T2' (Cost=44
Card=11239 Bytes=
-- 427082)
--
-- 4 1 SORT (JOIN) (Cost=403 Card=29424 Bytes=647328)
-- 5 4 TABLE ACCESS (FULL) OF 'T1' (Cost=127
Card=29424 Bytes
-- =647328)
select
from t1, t2
where t1.object_id = t2.object_id;
-- Execution Plan
-- --------------------------------------------------------
-- 0 SELECT STATEMENT Optimizer=CHOOSE
-- 1 0 MERGE JOIN
-- 2 1 SORT (JOIN)
-- 3 2 TABLE ACCESS (FULL) OF 'T2'
-- 4 1 SORT (JOIN)
-- 5 4 TABLE ACCESS (FULL) OF 'T1'
4. Table Compression
-- SkillBuilders, 2004
@TABLE_COMPRESSION_PREP.SQL
-- BYTES BLOCKS
-- ---------- ----------
-- 4194304 1024
-- BYTES BLOCKS
-- ---------- ----------
-- 2097152 512
-- Statistics
-- ----------------------------------------------------------
-- 0 recursive calls
-- 0 db block gets
-- 831 consistent gets
-- 818 physical reads
-- 0 redo size
-- 1451 bytes sent via SQL*Net to client
-- 532 bytes received via SQL*Net from client
-- 5 SQL*Net roundtrips to/from client
-- 0 sorts (memory)
-- 0 sorts (disk)
-- 48 rows processed
-- Statistics
-- ----------------------------------------------------------
-- 0 recursive calls
-- 0 db block gets
-- 434 consistent gets
-- 426 physical reads
-- 0 redo size
-- 1451 bytes sent via SQL*Net to client
-- 532 bytes received via SQL*Net from client
-- 5 SQL*Net roundtrips to/from client
-- 0 sorts (memory)
-- 0 sorts (disk)
-- 48 rows processed
--@TABLE_COMPRESSION_RESET;