You are on page 1of 5

EXTERNAL TABLES

You can user external table feature to access external files as if they are tabl
es inside the database.
When you create an external table, you define its structure and location with in
oracle.
When you query the table, oracle reads the external table and returns the result
s just as if the data had been stored with in the database.
ACCESSING EXTERNAL TABLE DATA
To access external files from within oracle, you must first use the create direc
tory command to define a directory object pointing to the external file location
Users who will access the external files must have the read and write privilege
on the directory.
Ex:
CREATING DIRECTORY AND OS LEVEL FILE
SQL> Sqlplus system/manager
SQL> Create directory saketh_dir as /Visdb/visdb/9.2.0/external ;
SQL> Grant all on directory saketh_dir to saketh;
SQL> Conn saketh/saketh
SQL> Spool dept.lst
SQL> Select deptno || , || dname || , || loc from dept;
SQL> Spool off
CREATING EXTERNAL TABLE
SQL> Create table dept_ext
(deptno number(2),
Dname varchar(14),
Loc varchar(13))
Organization external ( type oracle_loader
Default directory saketh_dir
Access parameters
( records delimited by newline
Fields terminated by ,
( deptno number(2),
Dname varchar(14),
Loc varchar(13)))
Location ( /Visdb/visdb/9.2.0/dept.lst ));
SELECTING DATA FROM EXTERNAL TABLE
SQL> select * from dept_ext;
This will read from dept.lst which is a operating system level file.
LIMITATIONS ON EXTERNAL TABLES
a) You can not perform insert, update, and delete operations
a) Indexing not possible
b) Constraints not possible
BENEFITS OF EXTERNAL TABLES
a) Queries of external tables complete very quickly even though a full table
scan id required with each access
b) You can join external tables to each other or to standard tables
-------------------------------------------------------------------
----Creating Source table -----------------------------------------
-------------------------------------------------------------------
Create table Test_table_a (
Trans_dt Date,
C1 Number,
C2 Number,
C3 Number,
C4 Number);
-------------------------------------------------------------------
----Inserting Some Sample Data for Testing ------------------------
-------------------------------------------------------------------
Insert Into Test_table_a Values ('23-JUL-05', 9001, 8654, 231, 6954);
Insert Into Test_table_a Values ('23-JUL-04', 432, 3823, 159, 7175);
Insert Into Test_table_a Values ('23-JUL-03', 9356, 2635, 5324, 6586);
Insert Into Test_table_a Values ('23-JUL-02', 7073, 279, 3574, 4816);
Insert Into Test_table_a Values ('23-JUL-01', 1510, 6416, 5987, 9003);
-------------------------------------------------------------------
----Creating Target table -----------------------------------------
-------------------------------------------------------------------
Create table Test_table_b (
Trans_dt Date,
c_name Varchar2(20),
c_val Number);
-------------------------------------------------------------------
----Insert Script to load data into Target ------------------------
-------------------------------------------------------------------
Insert Into Test_table_b select Trans_dt, 'c' || r c_name,
decode( r, 1, c1, 2, c2, 3, c3, 4, c4 ) c_val
from Test_table_a, (select rownum r
from all_objects where rownum <= 4);
-------------------------------------------------------------------
----Output (Select * from Test_tab_b;) ----------------------------
-------------------------------------------------------------------
23-JUL-05 c1 9005
23-JUL-04 c1 432
23-JUL-03 c1 9356
23-JUL-02 c1 7073
23-JUL-01 c1 1510
23-JUL-05 c2 8654
23-JUL-04 c2 3823
23-JUL-03 c2 2635
23-JUL-02 c2 279
23-JUL-01 c2 6416
23-JUL-05 c3 231
23-JUL-04 c3 159
23-JUL-03 c3 5324
23-JUL-02 c3 3574
23-JUL-01 c3 5987
23-JUL-05 c4 6954
23-JUL-04 c4 7175
23-JUL-03 c4 6586
23-JUL-02 c4 4816
23-JUL-01 c4 9003
-------------------------------------------------------------------
-------------------------------------------------------------------
CONVERTING ROW INTO COLUMN

-------------------------------------------------------------------
----Creating Source table -----------------------------------------
-------------------------------------------------------------------
Create table Test_tab_a (
SName Varchar(20),
EMail Varchar2(20),
Section Varchar(10));
-------------------------------------------------------------------
----Inserting Some Sample Data for Testing ------------------------
-------------------------------------------------------------------
Insert Into test_tab_a Values ('Amit','a@gmail.com','Math');
Insert Into test_tab_a Values ('Amit', 'a@gmail.com', 'Science');
Insert Into test_tab_a Values ('Amit', 'a@gmail.com', 'Art');
Insert Into test_tab_a Values ('Kiran','k@gmail.com','Math');
Insert Into test_tab_a Values ('Kiran','k@gmail.com','Science');
Insert Into test_tab_a Values ('Kiran','k@gmail.com','Art');
Insert Into test_tab_a Values ('Lakshmi','l@gmail.com','Math');
Insert Into test_tab_a Values ('Lakshmi','l@gmail.com','Science');
Insert Into test_tab_a Values ('Lakshmi','l@gmail.com','Art');
Insert Into test_tab_a Values ('Mitha','m@gmail.com','Math');
Insert Into test_tab_a Values ('Mitha','m@gmail.com','Science');
Insert Into test_tab_a Values ('Mitha','m@gmail.com','Art');
Insert Into test_tab_a Values ('Nithila','n@gmail.com','Math');
Insert Into test_tab_a Values ('Nithila','n@gmail.com','Science');
Insert Into test_tab_a Values ('Nithila','n@gmail.com','Art');
Insert Into test_tab_a Values ('Varsha','v@gmail.com','Math');
Insert Into test_tab_a Values ('Varsha','v@gmail.com','Science');
Insert Into test_tab_a Values ('Varsha','v@gmail.com','Art');
-------------------------------------------------------------------
----Creating Target table -----------------------------------------
-------------------------------------------------------------------
Create table Test_tab_b (
SName Varchar(20),
EMail Varchar2(20),
Section_1 Varchar(10),
Section_2 Varchar(10),
Section_3 Varchar(10));
-------------------------------------------------------------------
----Insert Script to load data into Target ------------------------
-------------------------------------------------------------------
Insert Into Test_tab_b SELECT DISTINCT SNAME,EMAIL,
'MATH' SECTION_1,'Science' SECTION_2,'Art' SECTION_3
FROM TEST_TAB_A;
-------------------------------------------------------------------
----Output (Select * from Test_tab_b;) ----------------------------
-------------------------------------------------------------------
Amit a@gmail.com Math Science Art
Kiran k@gmail.com Math Science Art
Lakshmi k@gmail.com Math Science Art
Mitha k@gmail.com Math Science Art
Nithila k@gmail.com Math Science Art
Varsha k@gmail.com Math Science Art
-------------------------------------------------------------------
-------------------------------------------------------------------
COUNT only counts non-null values (except for count(*)). This can often be used
to answer questions like this by creating an expression that only returns someth
ing non-null for those rows we wish to count.
I have a table containing data for likes and dislikes on my "Facebook for Geeks"
website:
create table plch_likes (
nick_name varchar2(10)
, like_value integer
)
A like_value of 1 (positive one) means a like , where a like_value of -1 (negat
ive one) means a dislike .
I'd like a list showing for each nick_name the percentage of likes of the total
number of feedbacks.
Which of the choices can replace the /* PERCENT CALCULATION */ comment to make t
he query return this desired output:
NICK_NAME PERCENT_LIKED
---------- -------------
dba_tom 100
the_coder 60
sCr1pt0r 25
select nick_name
, 100 * count(case like_value when 1 then 1 end)
/ count(*) percent_liked
from plch_likes
group by nick_name
order by percent_liked desc
/

CREATE TABLE plch_employees


(
employee_id INTEGER
, last_name VARCHAR2 (20)
, salary NUMBER
)
My boss wants a list of employees and for each employee he wants to see how many
employees has a comparable salary (defined as earning same salary +/- 1000.)
Which of the choices produce this desired output:
LAST_NAME SALARY SIMILAR
-------------------- ---------- ----------
Jobs 200000 4
Ellison 300500 1
Gates 199500 3
Feuerstein 199400 3
Hansen 200600 2
SELECT last_name
, salary
, COUNT(*) OVER (
ORDER BY salary
RANGE BETWEEN 1000 PRECEDING AND 1000 FOLLOWING
) similar
FROM plch_employees
ORDER BY employee_id
Without analytic functions you could do it with a scalar subquery:
SELECT e.last_name
, e.salary
, ( SELECT COUNT(*)
FROM plch_employees e_similar
WHERE e_similar.salary BETWEEN e.salary - 1000 AND e.salary + 1000
) similar
FROM plch_employees e
ORDER BY e.employee_id;
I have these tables with these data:
create table plch_cars (
car_id integer primary key
, car_name varchar2(40)
)
/
create table plch_trucks (
truck_id integer primary key
, truck_name varchar2(30)
)
I'd like a list of both car and truck names. The list should first show all cars
in alphabetic order, then all trucks in alphabetic order.
Which of the choices contain a query that will return this desired result:
NAME
----------------------------------------
Ford
Mercedes
Volkswagen
Mack
Mercedes
Peterbilt
Scania

select name
from (
select 1 sort_order
, car_name name
from plch_cars
union all
select 2 sort_order
, truck_name name
from plch_trucks
)
order by sort_order
, name

You might also like