LAB
• http://www.oracle.com/technetwork/community/developer-
vm/index.html#dbapp
• => Search for "Database App Development VM"
• https://www.oracle.com/database/technologies/databaseappdev-
vm.html
• http://www.oracle.com/technetwork/server-
storage/virtualbox/overview/index.html
Making Connection to existing DB:
1. Click New connection
2. Fill form and click connect (oracle as password)
LAB0 : Subqueries
SUBQUERIES (single value using =)
select max(salary) from employees;
select *
from employees
where salary = (select max(salary) from employees);
select *
from employees
where salary = (select min(salary) from employees);
SUBQUERIES (multiple values using IN)
SELECT department_id
FROM departments
WHERE LOCATION_ID = 1700;
SELECT first_name, last_name, department_id
FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE LOCATION_ID = 1700);
SELECT first_name, last_name, department_id
FROM employees
WHERE department_id NOT IN (SELECT department_id
FROM departments
WHERE LOCATION_ID = 1700);
Co-Related Queries
SELECT EMPLOYEE_ID, salary, department_id
FROM employees t1
WHERE salary > (SELECT avg(salary)
FROM employees t2
WHERE t1.department_id = t2.department_id);
Subqueries (Multiple column)
SELECT min_salary, job_id
FROM jobs;
SELECT *
FROM employees
WHERE (salary, job_id) in (SELECT min_salary, job_id
FROM jobs);
Inline views
Eg1:
How many employees are assigned to each department?
SELECT department_id, count(*) emp_count FROM employees GROUP BY department_id;
You also want to display depart name? (You can use sub query)
Select * from (SELECT department_id, count(*) emp_count FROM employees GROUP BY department_id) emp join departments dept on
emp.department_id=dept.department_id;
Eg2:
select * from employees Where commission_pct is null Order by salary desc;
select * from (select * from employees where commission_pct is null Order by salary desc) top_salaries Where rownum <=3;
DOODB_LAB.pptx
Lab1: indexes
• Without index need to scan million of rows
DOODB_LAB.pptx
B-Tree Index
DOODB_LAB.pptx
Eg:
Creating Index
CREATE INDEX emp_sal_idx ON employees (salary);
select * from employees where salary = 24000;
select * from employees where salary between 24000 and 2500;
Composite index:
CREATE INDEX emp_idx2 ON employees (salary, hire_date);
select * from employees where salary between 24000 and 2500
and hire_date < sysdate-365;
drop index emp_idx2;
DOODB_LAB.pptx
Lab2: Data dictionary
Eg:
USE DICT if you don’t know metadata you are searching contained on which view …
select table_name from dict;
select table_name from dict
where table_name = 'ALL_TABLES';
select table_name from dict
where table_name like '%TRIGGERS%';
select table_name from dict
where table_name like '%INDEXES%';
select * from all_tables;
select * from user_tables;
select * from user_tables
where table_name = 'EMPLOYEES';
select * from user_tab_columns
where table_name = 'EMPLOYEES';
select * from user_ind_columns
where table_name = 'EMPLOYEES';
select username, program, machine from v$session;
select * from v$instance;
select * from v$lock;
select * from all_users;
DOODB_LAB.pptx
Lab: Concurrency
DOODB_LAB.pptx
Isolation and locks
Until COMMIT happen each connection to DB sees their own values
Eg: Among 2 concurrent one is update and one operation is select
Isolation Example
Connection1: TRANSACTION1
update employees set salary
= 25000 where employee_id
= 100;
select employee_id, salary
from employees where
employee_id=100;
=> o/p 25000
Connection2: TRANSACTION2
select employee_id, salary
from employees where
employee_id=100;
=> o/p: 25000
Isolation and locks
Eg: Among 2 concurrent both is update and one operation; so second
transaction will wait until first transaction COMMIT/ROLLBACK
Example LOCKS
Session1: TRANSACTION1
update employees set salary
= 30000 where employee_id
= 100;
=> 1 row updated will create
lock to row with emp_id =100
Session2: TRANSACTION2
update employees set salary =
35000 where employee_id = 100;
=> WAITING……
NOTE: If we COMMIT/ROLLBACK in transaction1, then transaction 2 will be completed
NOTE:: SEE all sessions connected to database: THERE ARE VIEWS CREATED ALREADY::
SELECT username, osuser, sid, serial#, lockwait, status, module, machine, program FROM
v$session;
=> You will see many of active session from internal of Oracle; Scroll and find session (above 2)
we generate
NOTE:: SEE all sessions connected to database: THERE ARE VIEWS CREATED
ALREADY::
SELECT username, osuser, sid, serial#, lockwait, status, module, machine,
program FROM v$session;
=> You will see many of active session from internal of Oracle; Scroll and find 2
session from SQL DEVELOPER we generate
DOODB_LAB.pptx
Lab: Oracle Schema Objects
Triggers
Eg use case:
Trigger:
EG1:
CREATE TABLE table1 (col1 number(5), col2 varchar2(50));
CREATE TABLE table2 (col1 number(5), col2 varchar2(50));
CREATE OR REPLACE TRIGGER test_trg1
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
insert into table2 values (:new.col1 , :new.col2);
END;
/
Eg2:
CREATE TABLE audit_table (op_time date, col1 number(5), col2_before varchar2(50),
col2_after varchar2(50));
CREATE OR REPLACE TRIGGER audit_trg
AFTER INSERT OR DELETE OR UPDATE ON table1
FOR EACH ROW
BEGIN
insert into audit_table values (sysdate, :new.col1 , :old.col2, :new.col2);
END;
/
Verify::
UPDATE TABLE1 set col2= 'Hello World 2' where col1 = 1;
SELECT * FROM audit_table;
PL/SQL
PL/SQL vs SQL
How to Execute? =>
PL/SQL : Procedure: Example
CREATE OR REPLACE PROCEDURE proc1 (emp_id IN NUMBER)
IS
emp_fname VARCHAR2(50);
BEGIN
SELECT first_name INTO emp_fname FROM employees where employee_id =
emp_id;
DBMS_OUTPUT.PUT_LINE('Employee name is: ' || emp_fname);
END;
SET SERVEROUTPUT ON
EXECUTE::
EXEC proc1(100)
Where and How it is executed?
=> It’s different than procedure in sence that it’s executed within SQL statement
OR; i.e.
PL/SQL Function: Example
CREATE OR REPLACE Function func1 (emp_id IN NUMBER)
RETURN VARCHAR2
IS
emp_fname VARCHAR2(50);
BEGIN
SELECT first_name INTO emp_fname FROM employees where employee_id =
emp_id;
RETURN emp_fname;
END;
SELECT func1(100) from dual;
SELECT func1(200) from dual;
PL/SQL Packages
Groups multiple procedures, functions, types, varibales, and so on that
share a business purpose
Has 2 mandatory parts:
1. Package Specification
2. Package Body
DOODB_LAB.pptx
DOODB_LAB.pptx
Views
CREATING VIEW::
create or replace view emp_sales
as
select * from employees where job_id = 'SA_MAN';
USES:
=> select * from emp_sales;
This means same as below
select * from (select * from employees where job_id = 'SA_MAN');
=> You can have all normal SQL query operation on views as in table
select * from emp_sales where salary > 12000;
REMOVE:: DROP VIEW emp_sales;
Complex views
CREATE OR REPLACE VIEW emp_sales
as
select employee_id, first_name, last_name from employees where job_id =
'SA_MAN';
SELECT emp_sales.first_name, departments.department_name
FROM emp_sales JOIN departments ON emp_sales.department_id = departments.department_id;
CREATE VIEW locations_view AS
SELECT d.department_id, d.department_name, l.location_id, l.city
FROM departments d, locations l
WHERE d.location_id = l.location_id;
=> SELECT * FROM locations_view;
DROP VIEW emp_sales;
DOODB_LAB.pptx
Lab: Table Partitioning
Table Partitioning/Partition:
=> Allows table to sub-divided into smaller pieces or element; Each these pieces
are known as table partition
=> Each table partition has it’s own name and is separate physical and logical
object
From application
view point tble
without partition
and with
partition are
equivalent but
partitioned table
has advantage
(next slide)
DOODB_LAB.pptx
DOODB_LAB.pptx
DOODB_LAB.pptx
DOODB_LAB.pptx
DOODB_LAB.pptx
CREATE TABLE sales_list (
salesrep_id NUMBER(5), salesrep_name VARCHAR2(40), sales_state VARCHAR2(30),
sales_value NUMBER(10), sales_date DATE) PARTITION BY LIST(sales_state)
( PARTITION sales_CA VALUES('CA'), PARTITION sales_NY VALUES ('NY'), PARTITION
sales_central VALUES('TX', 'IL'), PARTITION sales_other VALUES(DEFAULT) );
List:
1. Create partiton on table(see previous query/ in notes)
2. Insert values:
INSERT INTO sales_list VALUES (100, 'Picard', 'CA', 100, '01-JAN-2017’);
INSERT INTO sales_list VALUES (200, 'Janeway', 'NY', 500, '02-JAN-2017’);
INSERT INTO sales_list VALUES (300, 'Kirk', 'TX', 1000, '03-JAN-2017’);
INSERT INTO sales_list VALUES (100, 'Picard', 'IL', 500, '04-JAN-2017’);
3. Verify:
TOTAL COUNT: SELECT COUNT(*) FROM sales_list; -- 4
SELECT COUNT(*) FROM sales_list partition (sales_ca); -- 1
SELECT COUNT(*) FROM sales_list partition (sales_ny); -- 1
SELECT COUNT(*) FROM sales_list partition (sales_central); -- 2
SELECT COUNT(*) FROM sales_list partition (sales_other); -- 0
Split already existing partition in table:
Eg: Split existing partition sales_other into 2 new partitions sales_nv & sales_other
ALTER TABLE sales_list
SPLIT PARTITION sales_other VALUES ('NV')
INTO
(PARTITION sales_nv,
PARTITION sales_other);
SELECT count(*) FROM SALES_LIST PARTITION (sales_nv); -- 0
INSERT INTO sales_list VALUES (300, 'Kirk', 'NV', 2300, '01-JAN-2017');
SELECT count(*) FROM SALES_LIST PARTITION (sales_nv); -- 1
DOODB_LAB.pptx
RANGE PARTITION::
CREATE TABLE sales_range
(
salesrep_id NUMBER(5),
salesrep_name VARCHAR2(30),
sales_amount NUMBER(10),
sales_date DATE)
PARTITION BY RANGE(sales_date)
(
PARTITION sales_jan2017 VALUES LESS THAN(TO_DATE('01/02/2017','DD/MM/YYYY')),
PARTITION sales_feb2017 VALUES LESS THAN(TO_DATE('01/03/2017','DD/MM/YYYY')),
PARTITION sales_mar2017 VALUES LESS THAN(TO_DATE('01/04/2017','DD/MM/YYYY')),
PARTITION sales_apr2017 VALUES LESS THAN(TO_DATE('01/05/2017','DD/MM/YYYY'))
);
NOTE: ALTER TABLE sales_range ADD PARTITION sales_may2017 VALUES LESS THAN(TO_DATE('01/06/2017','DD/MM/YYYY'))
DOODB_LAB.pptx
Hash partition::
CREATE TABLE sales_hash
(
salesrep_id NUMBER(5),
salesrep_name VARCHAR2(30),
sales_amount NUMBER(10),
sale_no NUMBER(5))
PARTITION BY HASH(sale_no)
PARTITIONS 4 ; -- Just specify number; no explicit name is required
ANALYZE TABLE sales_hash COMPUTE STATISTICS;
SELECT table_name, partition_name, num_rows FROM user_tab_partitions
WHERE table_name = 'SALES_HASH';

More Related Content

PDF
Function Procedure Trigger Partition.pdf
PPTX
How to tune a query - ODTUG 2012
PPTX
OpenWorld Sep14 12c for_developers
PPT
MS SQL Server.ppt sql
PDF
kscope2013vst-130627142814-phpapp02.pdf
PPTX
Sql analytic queries tips
PDF
ILOUG 2019 - SQL features for Developers
DOC
SQLQueries
Function Procedure Trigger Partition.pdf
How to tune a query - ODTUG 2012
OpenWorld Sep14 12c for_developers
MS SQL Server.ppt sql
kscope2013vst-130627142814-phpapp02.pdf
Sql analytic queries tips
ILOUG 2019 - SQL features for Developers
SQLQueries

Similar to DOODB_LAB.pptx (20)

PPTX
PostgreSQL Database Slides
PPT
Less09 Data
PPTX
Web Developer make the most out of your Database !
DOCX
Oracle 11g developer on linux training in bangalore
DOCX
Oracle 11g developer on linux training in bangalore
PPTX
OpenWorld 2018 - Common Application Developer Disasters
PPTX
SQL techniques for faster applications
PPT
PPT
Oracle tips and tricks
PPTX
DOAG: Visual SQL Tuning
PDF
OOW19 - Ten Amazing SQL features
PDF
Flashback ITOUG
PDF
UKOUG 2019 - SQL features
PDF
SQL & Adv SQL - Basics and Advanced for Beginners
PDF
OOW19 - Flashback, not just for DBAs
PPT
MS SQL Server.ppt
PPT
Sql server introduction to sql server
PPTX
Sql server
PPTX
OOUG: VST , visual sql tuning diagrams
PPT
Transactions-in-Oracle-Evgenya-Kotzeva (2).ppt
PostgreSQL Database Slides
Less09 Data
Web Developer make the most out of your Database !
Oracle 11g developer on linux training in bangalore
Oracle 11g developer on linux training in bangalore
OpenWorld 2018 - Common Application Developer Disasters
SQL techniques for faster applications
Oracle tips and tricks
DOAG: Visual SQL Tuning
OOW19 - Ten Amazing SQL features
Flashback ITOUG
UKOUG 2019 - SQL features
SQL & Adv SQL - Basics and Advanced for Beginners
OOW19 - Flashback, not just for DBAs
MS SQL Server.ppt
Sql server introduction to sql server
Sql server
OOUG: VST , visual sql tuning diagrams
Transactions-in-Oracle-Evgenya-Kotzeva (2).ppt
Ad

Recently uploaded (20)

PDF
ahaaaa shbzjs yaiw jsvssv bdjsjss shsusus s
PPTX
Tapan_20220802057_Researchinternship_final_stage.pptx
PPT
expt-design-lecture-12 hghhgfggjhjd (1).ppt
PDF
REPORT CARD OF GRADE 2 2025-2026 MATATAG
PPTX
ai agent creaction with langgraph_presentation_
PPTX
1 hour to get there before the game is done so you don’t need a car seat for ...
PPT
statistics analysis - topic 3 - describing data visually
PPTX
PPT for Diseases.pptx, there are 3 types of diseases
PPTX
Fundementals of R Programming_Class_2.pptx
PDF
Grey Minimalist Professional Project Presentation (1).pdf
PPT
Chinku Sharma Internship in the summer internship project
PPTX
Hushh Hackathon for IIT Bombay: Create your very own Agents
PPTX
OJT-Narrative-Presentation-Entrep-group.pptx_20250808_102837_0000.pptx
PPTX
cp-and-safeguarding-training-2018-2019-mmfv2-230818062456-767bc1a7.pptx
PPTX
machinelearningoverview-250809184828-927201d2.pptx
PDF
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
PPTX
Machine Learning and working of machine Learning
PDF
©️ 02_SKU Automatic SW Robotics for Microsoft PC.pdf
PDF
Navigating the Thai Supplements Landscape.pdf
PPTX
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
ahaaaa shbzjs yaiw jsvssv bdjsjss shsusus s
Tapan_20220802057_Researchinternship_final_stage.pptx
expt-design-lecture-12 hghhgfggjhjd (1).ppt
REPORT CARD OF GRADE 2 2025-2026 MATATAG
ai agent creaction with langgraph_presentation_
1 hour to get there before the game is done so you don’t need a car seat for ...
statistics analysis - topic 3 - describing data visually
PPT for Diseases.pptx, there are 3 types of diseases
Fundementals of R Programming_Class_2.pptx
Grey Minimalist Professional Project Presentation (1).pdf
Chinku Sharma Internship in the summer internship project
Hushh Hackathon for IIT Bombay: Create your very own Agents
OJT-Narrative-Presentation-Entrep-group.pptx_20250808_102837_0000.pptx
cp-and-safeguarding-training-2018-2019-mmfv2-230818062456-767bc1a7.pptx
machinelearningoverview-250809184828-927201d2.pptx
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
Machine Learning and working of machine Learning
©️ 02_SKU Automatic SW Robotics for Microsoft PC.pdf
Navigating the Thai Supplements Landscape.pdf
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
Ad

DOODB_LAB.pptx

  • 1. LAB
  • 2. • http://www.oracle.com/technetwork/community/developer- vm/index.html#dbapp • => Search for "Database App Development VM" • https://www.oracle.com/database/technologies/databaseappdev- vm.html • http://www.oracle.com/technetwork/server- storage/virtualbox/overview/index.html
  • 3. Making Connection to existing DB: 1. Click New connection 2. Fill form and click connect (oracle as password)
  • 5. SUBQUERIES (single value using =) select max(salary) from employees; select * from employees where salary = (select max(salary) from employees); select * from employees where salary = (select min(salary) from employees);
  • 6. SUBQUERIES (multiple values using IN) SELECT department_id FROM departments WHERE LOCATION_ID = 1700; SELECT first_name, last_name, department_id FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE LOCATION_ID = 1700); SELECT first_name, last_name, department_id FROM employees WHERE department_id NOT IN (SELECT department_id FROM departments WHERE LOCATION_ID = 1700);
  • 7. Co-Related Queries SELECT EMPLOYEE_ID, salary, department_id FROM employees t1 WHERE salary > (SELECT avg(salary) FROM employees t2 WHERE t1.department_id = t2.department_id);
  • 8. Subqueries (Multiple column) SELECT min_salary, job_id FROM jobs; SELECT * FROM employees WHERE (salary, job_id) in (SELECT min_salary, job_id FROM jobs);
  • 9. Inline views Eg1: How many employees are assigned to each department? SELECT department_id, count(*) emp_count FROM employees GROUP BY department_id; You also want to display depart name? (You can use sub query) Select * from (SELECT department_id, count(*) emp_count FROM employees GROUP BY department_id) emp join departments dept on emp.department_id=dept.department_id; Eg2: select * from employees Where commission_pct is null Order by salary desc; select * from (select * from employees where commission_pct is null Order by salary desc) top_salaries Where rownum <=3;
  • 12. • Without index need to scan million of rows
  • 16. Eg:
  • 17. Creating Index CREATE INDEX emp_sal_idx ON employees (salary); select * from employees where salary = 24000; select * from employees where salary between 24000 and 2500; Composite index: CREATE INDEX emp_idx2 ON employees (salary, hire_date); select * from employees where salary between 24000 and 2500 and hire_date < sysdate-365; drop index emp_idx2;
  • 20. Eg:
  • 21. USE DICT if you don’t know metadata you are searching contained on which view … select table_name from dict; select table_name from dict where table_name = 'ALL_TABLES'; select table_name from dict where table_name like '%TRIGGERS%'; select table_name from dict where table_name like '%INDEXES%';
  • 22. select * from all_tables; select * from user_tables; select * from user_tables where table_name = 'EMPLOYEES'; select * from user_tab_columns where table_name = 'EMPLOYEES'; select * from user_ind_columns where table_name = 'EMPLOYEES'; select username, program, machine from v$session; select * from v$instance; select * from v$lock; select * from all_users;
  • 26. Isolation and locks Until COMMIT happen each connection to DB sees their own values Eg: Among 2 concurrent one is update and one operation is select
  • 27. Isolation Example Connection1: TRANSACTION1 update employees set salary = 25000 where employee_id = 100; select employee_id, salary from employees where employee_id=100; => o/p 25000 Connection2: TRANSACTION2 select employee_id, salary from employees where employee_id=100; => o/p: 25000
  • 28. Isolation and locks Eg: Among 2 concurrent both is update and one operation; so second transaction will wait until first transaction COMMIT/ROLLBACK
  • 29. Example LOCKS Session1: TRANSACTION1 update employees set salary = 30000 where employee_id = 100; => 1 row updated will create lock to row with emp_id =100 Session2: TRANSACTION2 update employees set salary = 35000 where employee_id = 100; => WAITING…… NOTE: If we COMMIT/ROLLBACK in transaction1, then transaction 2 will be completed NOTE:: SEE all sessions connected to database: THERE ARE VIEWS CREATED ALREADY:: SELECT username, osuser, sid, serial#, lockwait, status, module, machine, program FROM v$session; => You will see many of active session from internal of Oracle; Scroll and find session (above 2) we generate
  • 30. NOTE:: SEE all sessions connected to database: THERE ARE VIEWS CREATED ALREADY:: SELECT username, osuser, sid, serial#, lockwait, status, module, machine, program FROM v$session; => You will see many of active session from internal of Oracle; Scroll and find 2 session from SQL DEVELOPER we generate
  • 35. Trigger: EG1: CREATE TABLE table1 (col1 number(5), col2 varchar2(50)); CREATE TABLE table2 (col1 number(5), col2 varchar2(50)); CREATE OR REPLACE TRIGGER test_trg1 AFTER INSERT ON table1 FOR EACH ROW BEGIN insert into table2 values (:new.col1 , :new.col2); END; /
  • 36. Eg2: CREATE TABLE audit_table (op_time date, col1 number(5), col2_before varchar2(50), col2_after varchar2(50)); CREATE OR REPLACE TRIGGER audit_trg AFTER INSERT OR DELETE OR UPDATE ON table1 FOR EACH ROW BEGIN insert into audit_table values (sysdate, :new.col1 , :old.col2, :new.col2); END; / Verify:: UPDATE TABLE1 set col2= 'Hello World 2' where col1 = 1; SELECT * FROM audit_table;
  • 40. PL/SQL : Procedure: Example CREATE OR REPLACE PROCEDURE proc1 (emp_id IN NUMBER) IS emp_fname VARCHAR2(50); BEGIN SELECT first_name INTO emp_fname FROM employees where employee_id = emp_id; DBMS_OUTPUT.PUT_LINE('Employee name is: ' || emp_fname); END; SET SERVEROUTPUT ON EXECUTE:: EXEC proc1(100)
  • 41. Where and How it is executed? => It’s different than procedure in sence that it’s executed within SQL statement OR; i.e.
  • 42. PL/SQL Function: Example CREATE OR REPLACE Function func1 (emp_id IN NUMBER) RETURN VARCHAR2 IS emp_fname VARCHAR2(50); BEGIN SELECT first_name INTO emp_fname FROM employees where employee_id = emp_id; RETURN emp_fname; END; SELECT func1(100) from dual; SELECT func1(200) from dual;
  • 43. PL/SQL Packages Groups multiple procedures, functions, types, varibales, and so on that share a business purpose Has 2 mandatory parts: 1. Package Specification 2. Package Body
  • 46. Views CREATING VIEW:: create or replace view emp_sales as select * from employees where job_id = 'SA_MAN'; USES: => select * from emp_sales; This means same as below select * from (select * from employees where job_id = 'SA_MAN'); => You can have all normal SQL query operation on views as in table select * from emp_sales where salary > 12000; REMOVE:: DROP VIEW emp_sales;
  • 47. Complex views CREATE OR REPLACE VIEW emp_sales as select employee_id, first_name, last_name from employees where job_id = 'SA_MAN'; SELECT emp_sales.first_name, departments.department_name FROM emp_sales JOIN departments ON emp_sales.department_id = departments.department_id; CREATE VIEW locations_view AS SELECT d.department_id, d.department_name, l.location_id, l.city FROM departments d, locations l WHERE d.location_id = l.location_id; => SELECT * FROM locations_view; DROP VIEW emp_sales;
  • 50. Table Partitioning/Partition: => Allows table to sub-divided into smaller pieces or element; Each these pieces are known as table partition => Each table partition has it’s own name and is separate physical and logical object From application view point tble without partition and with partition are equivalent but partitioned table has advantage (next slide)
  • 56. CREATE TABLE sales_list ( salesrep_id NUMBER(5), salesrep_name VARCHAR2(40), sales_state VARCHAR2(30), sales_value NUMBER(10), sales_date DATE) PARTITION BY LIST(sales_state) ( PARTITION sales_CA VALUES('CA'), PARTITION sales_NY VALUES ('NY'), PARTITION sales_central VALUES('TX', 'IL'), PARTITION sales_other VALUES(DEFAULT) );
  • 57. List: 1. Create partiton on table(see previous query/ in notes) 2. Insert values: INSERT INTO sales_list VALUES (100, 'Picard', 'CA', 100, '01-JAN-2017’); INSERT INTO sales_list VALUES (200, 'Janeway', 'NY', 500, '02-JAN-2017’); INSERT INTO sales_list VALUES (300, 'Kirk', 'TX', 1000, '03-JAN-2017’); INSERT INTO sales_list VALUES (100, 'Picard', 'IL', 500, '04-JAN-2017’); 3. Verify: TOTAL COUNT: SELECT COUNT(*) FROM sales_list; -- 4 SELECT COUNT(*) FROM sales_list partition (sales_ca); -- 1 SELECT COUNT(*) FROM sales_list partition (sales_ny); -- 1 SELECT COUNT(*) FROM sales_list partition (sales_central); -- 2 SELECT COUNT(*) FROM sales_list partition (sales_other); -- 0
  • 58. Split already existing partition in table: Eg: Split existing partition sales_other into 2 new partitions sales_nv & sales_other ALTER TABLE sales_list SPLIT PARTITION sales_other VALUES ('NV') INTO (PARTITION sales_nv, PARTITION sales_other); SELECT count(*) FROM SALES_LIST PARTITION (sales_nv); -- 0 INSERT INTO sales_list VALUES (300, 'Kirk', 'NV', 2300, '01-JAN-2017'); SELECT count(*) FROM SALES_LIST PARTITION (sales_nv); -- 1
  • 60. RANGE PARTITION:: CREATE TABLE sales_range ( salesrep_id NUMBER(5), salesrep_name VARCHAR2(30), sales_amount NUMBER(10), sales_date DATE) PARTITION BY RANGE(sales_date) ( PARTITION sales_jan2017 VALUES LESS THAN(TO_DATE('01/02/2017','DD/MM/YYYY')), PARTITION sales_feb2017 VALUES LESS THAN(TO_DATE('01/03/2017','DD/MM/YYYY')), PARTITION sales_mar2017 VALUES LESS THAN(TO_DATE('01/04/2017','DD/MM/YYYY')), PARTITION sales_apr2017 VALUES LESS THAN(TO_DATE('01/05/2017','DD/MM/YYYY')) ); NOTE: ALTER TABLE sales_range ADD PARTITION sales_may2017 VALUES LESS THAN(TO_DATE('01/06/2017','DD/MM/YYYY'))
  • 62. Hash partition:: CREATE TABLE sales_hash ( salesrep_id NUMBER(5), salesrep_name VARCHAR2(30), sales_amount NUMBER(10), sale_no NUMBER(5)) PARTITION BY HASH(sale_no) PARTITIONS 4 ; -- Just specify number; no explicit name is required ANALYZE TABLE sales_hash COMPUTE STATISTICS; SELECT table_name, partition_name, num_rows FROM user_tab_partitions WHERE table_name = 'SALES_HASH';

Editor's Notes

  • #4: Password is: oracle ISSUE OPENING IN mac irtualBox: $ sudo kextload -b org.virtualbox.kext.VBoxDrv
  • #27: update employees set salary = 25000 where employee_id = 100; select employee_id, salary from employees where employee_id=100; commit;
  • #29: update employees set salary = 25000 where employee_id = 100; select employee_id, salary from employees where employee_id=100; commit;
  • #30: SEE all sessions connected to database: SELECT username, osuser, sid, serial#, lockwait, status, module, machine, program FROM v$session;
  • #36: 1.Create trigger 2. See initial values SELECT * FROM table1; SELECT * FROM table2; 3. insert INSERT INTO table1 VALUES (1,'Hello World!’); 4. Verify values in table SELECT * FROM table1; SELECT * FROM table2;
  • #51: Benefits: Partition Pruning Separate DBA operation per partition
  • #57: LIST PARTITIONING (specify specific matched value) Group based on same value in certain column Good for columns having few values and all values are known
  • #58: Create partiton on table:: CREATE TABLE sales_list ( salesrep_id NUMBER(5), salesrep_name VARCHAR2(40), sales_state VARCHAR2(30), sales_value NUMBER(10), sales_date DATE) PARTITION BY LIST(sales_state) ( PARTITION sales_CA VALUES('CA'), PARTITION sales_NY VALUES ('NY'), PARTITION sales_central VALUES('TX', 'IL'), PARTITION sales_other VALUES(DEFAULT) );
  • #60: RANGE (specify range to matched value) => partiton based on range of values
  • #61: Create table (Above query) Insert values INSERT INTO sales_range VALUES (100, 'Picard', 100, '01-JAN-2017'); INSERT INTO sales_range VALUES (100, 'Picard', 100, '01-FEB-2017’); Verify SELECT COUNT(*) FROM sales_range partition (sales_jan2017); SELECT COUNT(*) FROM sales_range partition (sales_feb2017); INSERT INTO sales_range VALUES (100, 'Picard', 100, '01-MAY-2017'); SELECT COUNT(*) FROM sales_range partition (sales_may2017); ALTER TABLE sales_range ADD PARTITION sales_may2017 VALUES LESS THAN(TO_DATE('01/06/2017','DD/MM/YYYY'))
  • #62: HASH => we don’t specify explicit partition names and values when creating your table => Useful when you want to divide table for performance but don’t know how to partition or don’t have visual pertitioning idea Oracle Db uses hash to evenly distribute data Suitble for tables where no natural distribution you know
  • #63: Useful when you want to divide table for performance but don’t know how to partition or don’t have visual pertitioning idea Create Analyse SELECT table_name, partition_name, num_rows FROM user_tab_partitions WHERE table_name = 'SALES_HASH'; INSERT INTO sales_hash VALUES (100,'Picard', 500, 1423); ANALYZE TABLE sales_hash COMPUTE STATISTICS; SELECT table_name, partition_name, num_rows FROM user_tab_partitions WHERE table_name = 'SALES_HASH’; 3. Insert INSERT INTO sales_hash VALUES (100,'Picard', 500, 1423); INSERT INTO sales_hash VALUES (100,'Picard', 600, 1424); INSERT INTO sales_hash VALUES (100,'Picard', 200, 1425); INSERT INTO sales_hash VALUES (100,'Picard', 100, 1426); INSERT INTO sales_hash VALUES (100,'Picard', 500, 1427); INSERT INTO sales_hash VALUES (100,'Picard', 600, 1428); INSERT INTO sales_hash VALUES (100,'Picard', 200, 1429); INSERT INTO sales_hash VALUES (100,'Picard', 100, 1430); 4. Analyze again ANALYZE TABLE sales_hash COMPUTE STATISTICS; SELECT table_name, partition_name, num_rows FROM user_tab_partitions WHERE table_name = 'SALES_HASH';