Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 14, 2016
0 parents
commit 98dcabf
Showing
403 changed files
with
13,985 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2010 Karen Morton, Robyn Sands, Jared Still, Riyaj Shamsudeen, and Kerry Osborne | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select * from scott.dept; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* Listing 1-2 */ | ||
|
||
select salary | ||
from hr.employees | ||
where employee_id = 108; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Listing 1-8 */ | ||
|
||
select empno, deptno from scott.emp where ename = 'SMITH' ; | ||
|
||
select empno, deptno from scott.emp where ename = 'SMITH' | ||
; | ||
|
||
select empno, deptno from scott.emp where ename = 'SMITH' | ||
/ | ||
|
||
select empno, deptno from scott.emp where ename = 'SMITH' | ||
|
||
/ | ||
|
||
select empno, deptno from scott.emp where ename = 'SMITH'/ | ||
|
||
l | ||
|
||
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* Listing 1-9 */ | ||
|
||
@list_depts | ||
|
||
start list_depts | ||
|
||
l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* Listing 1-11 */ | ||
|
||
select c.customer_id, count(o.order_id) as orders_ct | ||
from oe.customers c | ||
join oe.orders o | ||
on c.customer_id = o.customer_id | ||
where c.gender = 'F' | ||
group by c.customer_id | ||
having count(o.order_id) > 4 | ||
order by orders_ct, c.customer_id | ||
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* Listing 1-12 */ | ||
|
||
select c.customer_id cust_id, o.order_id ord_id, c.gender | ||
from oe.customers c | ||
join oe.orders o | ||
on c.customer_id = o.customer_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* Listing 1-13 */ | ||
|
||
select c.customer_id, count(o.order_id) as orders_ct | ||
from oe.customers c | ||
join oe.orders o | ||
on c.customer_id = o.customer_id | ||
where c.gender = 'F' | ||
group by c.customer_id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* Listing 1-14 */ | ||
|
||
select c.customer_id, c.cust_first_name||' '||c.cust_last_name, | ||
(select e.last_name | ||
from hr.employees e | ||
where e.employee_id = c.account_mgr_id) acct_mgr | ||
from oe.customers c; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* Listing 1-15 */ | ||
|
||
select c.customer_id, count(o.order_id) as orders_ct | ||
from oe.customers c | ||
join oe.orders o | ||
on c.customer_id = o.customer_id | ||
where c.gender = 'F' | ||
group by c.customer_id | ||
having count(o.order_id) > 4 | ||
order by orders_ct, c.customer_id | ||
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* Listing 1-16 */ | ||
|
||
insert into hr.jobs (job_id, job_title, min_salary, max_salary) | ||
values ('IT_PM', 'Project Manager', 5000, 11000) ; | ||
|
||
|
||
insert into scott.bonus (ename, job, sal) | ||
select ename, job, sal * .10 | ||
from scott.emp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Listing 1-17 */ | ||
|
||
create table small_customers | ||
(customer_id number, | ||
sum_orders number) | ||
; | ||
|
||
|
||
create table medium_customers | ||
(customer_id number, | ||
sum_orders number) | ||
; | ||
|
||
|
||
create table large_customers | ||
(customer_id number, | ||
sum_orders number) | ||
; | ||
|
||
|
||
select * from small_customers ; | ||
|
||
select * from medium_customers ; | ||
|
||
select * from large_customers ; | ||
|
||
insert all | ||
when sum_orders < 10000 then | ||
into small_customers | ||
when sum_orders >= 10000 and sum_orders < 100000 then | ||
into medium_customers | ||
else | ||
into large_customers | ||
select customer_id, sum(order_total) sum_orders | ||
from oe.orders | ||
group by customer_id ; | ||
|
||
|
||
select * from small_customers ; | ||
|
||
select * from medium_customers ; | ||
|
||
select * from large_customers ; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* Listing 1-18 */ | ||
|
||
|
||
create table employees2 as select * from hr.employees ; | ||
|
||
alter table employees2 | ||
add constraint emp2_emp_id_pk primary key (employee_id) ; | ||
|
||
select employee_id, last_name, salary | ||
from hr.employees where department_id = 90 ; | ||
|
||
update employees2 | ||
set salary = salary * 1.10 -- increase salary by 10% | ||
where department_id = 90 ; | ||
|
||
commit ; | ||
|
||
select employee_id, last_name, salary | ||
from employees2 where department_id = 90 ; | ||
|
||
update hr.employees employees | ||
set salary = (select employees2.salary | ||
from employees2 | ||
where employees2.employee_id = employees.employee_id | ||
and employees.salary != employees2.salary) | ||
where department_id = 90 ; | ||
|
||
select employee_id, last_name, salary | ||
from hr.employees where department_id = 90 ; | ||
|
||
rollback ; | ||
|
||
update hr.employees | ||
set salary = salary * 1.10 | ||
where department_id in | ||
(select department_id | ||
from departments | ||
where department_name = 'Executive') ; | ||
|
||
select employee_id, last_name, salary | ||
from hr.employees | ||
where department_id in | ||
(select department_id | ||
from departments | ||
where department_name = 'Executive') ; | ||
|
||
rollback ; | ||
|
||
update (select e1.salary, e2.salary new_sal | ||
from hr.employees e1, employees2 e2 | ||
where e1.employee_id = e2.employee_id | ||
and e1.department_id = 90) | ||
set salary = new_sal; | ||
|
||
select employee_id, last_name, salary, commission_pct | ||
from hr.employees where department_id = 90 ; | ||
|
||
rollback ; | ||
|
||
update hr.employees employees | ||
set (salary, commission_pct) = (select employees2.salary, .10 comm_pct | ||
from employees2 | ||
where employees2.employee_id = employees.employee_id | ||
and employees.salary != employees2.salary) | ||
where department_id = 90 ; | ||
|
||
select employee_id, last_name, salary, commission_pct | ||
from hr.employees where department_id = 90 ; | ||
|
||
rollback ; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* Listing 1-19 */ | ||
|
||
select employee_id, department_id, last_name, salary | ||
from employees2 | ||
where department_id = 90; | ||
|
||
delete from employees2 | ||
where department_id = 90; | ||
|
||
select employee_id, department_id, last_name, salary | ||
from employees2 | ||
where department_id = 90; | ||
|
||
rollback; | ||
|
||
select employee_id, department_id, last_name, salary | ||
from employees2 | ||
where department_id = 90; | ||
|
||
delete from (select * from employees2 where department_id = 90); | ||
|
||
select employee_id, department_id, last_name, salary | ||
from employees2 | ||
where department_id = 90; | ||
|
||
rollback; | ||
|
||
select employee_id, department_id, last_name, salary | ||
from employees2 | ||
where department_id = 90; | ||
|
||
delete from employees2 | ||
where department_id in (select department_id | ||
from hr.departments | ||
where department_name = 'Executive'); | ||
|
||
select employee_id, department_id, last_name, salary | ||
from employees2 | ||
where department_id = 90; | ||
|
||
rollback; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* Listing 1-20 */ | ||
|
||
create table dept60_bonuses | ||
(employee_id number | ||
,bonus_amt number); | ||
|
||
insert into dept60_bonuses values (103, 0); | ||
|
||
insert into dept60_bonuses values (104, 100); | ||
|
||
insert into dept60_bonuses values (105, 0); | ||
|
||
commit; | ||
|
||
select employee_id, last_name, salary | ||
from hr.employees | ||
where department_id = 60 ; | ||
|
||
select * from dept60_bonuses; | ||
|
||
merge into dept60_bonuses b | ||
using ( | ||
select employee_id, salary, department_id | ||
from hr.employees | ||
where department_id = 60) e | ||
on (b.employee_id = e.employee_id) | ||
when matched then | ||
update set b.bonus_amt = e.salary * 0.2 | ||
where b.bonus_amt = 0 | ||
delete where (e.salary > 7500) | ||
when not matched then | ||
insert (b.employee_id, b.bonus_amt) | ||
values (e.employee_id, e.salary * 0.1) | ||
where (e.salary < 7500); | ||
|
||
select * from dept60_bonuses; | ||
|
||
rollback ; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Listing 2-1 | ||
connect to hr demo schema | ||
*/ | ||
|
||
|
||
select * from employees where department_id = 60; | ||
|
||
SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID = 60; | ||
|
||
select /* a_comment */ * from employees where department_id = 60; | ||
|
||
select sql_text, sql_id, child_number, hash_value, address, executions | ||
from v$sql where upper(sql_text) like '%EMPLOYEES%'; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Listing 2-2 */ | ||
|
||
variable v_dept number | ||
|
||
exec :v_dept := 10 | ||
|
||
select * from hr.employees where department_id = :v_dept; | ||
|
||
exec :v_dept := 20 | ||
|
||
select * from hr.employees where department_id = :v_dept; | ||
|
||
exec :v_dept := 30 | ||
|
||
select * from hr.employees where department_id = :v_dept; | ||
|
||
select sql_text, sql_id, child_number, hash_value, address, executions | ||
from v$sql where sql_text like '%v_dept'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* Listing 2-3 */ | ||
|
||
|
||
alter system set events 'immediate trace name flush_cache'; | ||
|
||
alter system flush shared_pool; | ||
|
||
set autotrace traceonly statistics | ||
|
||
select * from hr.employees where department_id = 60; | ||
|
||
set autotrace off | ||
|
||
alter system set events 'immediate trace name flush_cache'; | ||
|
||
set autotrace traceonly statistics | ||
|
||
select * from hr.employees where department_id = 60; | ||
|
||
select * from hr.employees where department_id = 60; | ||
|
||
set autotrace off | ||
|
Oops, something went wrong.