get your moneys worth out of your database

115
1 [email protected] technology.amis.nl blog.bar-solutions.com

Upload: patrick-barel

Post on 09-Jan-2017

323 views

Category:

Presentations & Public Speaking


2 download

TRANSCRIPT

Page 2: Get your moneys worth out of your database

2

• Patrick Barel• Working with Oracle since 1997• Working with PL/SQL since 1999• Playing with APEX since 2003 (mod_plsql)• ACE since 2011• OCA since December 20th 2012• OCP since February 20th 2014• Developer Choice Award 2015

About me…

Page 3: Get your moneys worth out of your database

3

Read me…

http://blog.bar-solutions.com

http://technology.amis.nl

http://allthingsoracle.com

http://www.otechmag.com

Page 4: Get your moneys worth out of your database

4

Download me…

Plugins for PL/SQL Developerhttp://bar-solutions.com

Plugins for Apexhttp://apex-plugin.com

Page 5: Get your moneys worth out of your database

5

Watch me…

Page 6: Get your moneys worth out of your database

6

@patch72

Patrick Barel

3029156

[email protected]

[email protected]

[email protected]

[email protected]

Contact me…

Page 7: Get your moneys worth out of your database

7

Patrick Barel, AMIS June 2, 2016

Get Your Moneys Worth Out Of Your Database

Page 8: Get your moneys worth out of your database

8

It doesn’t matter how many resources you have.

If you don’t know how to use them,it will never be enough

Page 9: Get your moneys worth out of your database

9

Database

Page 10: Get your moneys worth out of your database

10

Data integrity/quality

Page 11: Get your moneys worth out of your database

11

Data integrity/quality

create table demo.emp_unprotected( empno number(4) not null, ename varchar2(10), sal number(7,2), deptno number(2))

Page 12: Get your moneys worth out of your database

12

Data integrity/quality

insert into demo.emp_unprotected values (7369, 'SMITH', 800, 20);insert into demo.emp_unprotected values (7499, 'ALLEN', 1600, 30);insert into demo.emp_unprotected values (7521, 'WARD', 1250, 30);insert into demo.emp_unprotected values (7566, 'JONES', 2975, 20);…insert into demo.emp_unprotected values (7900, 'JAMES', 950, 30);insert into demo.emp_unprotected values (7902, 'FORD', 3000, 20);insert into demo.emp_unprotected values (7934, 'MILLER', 1300, 10);

Page 13: Get your moneys worth out of your database

13

Data integrity/quality

update demo.emp_unprotected e set e.sal = 2 * e.sal

• Salary not over 7500• No duplicate

employee numbers• Existing department

update demo.emp_unprotected e set e.deptno = 50 where e.ename = 'SCOTT'

insert into demo.emp_unprotected (empno, ename, sal, deptno) values (7900, 'BAREL', 1000, 10)

Page 14: Get your moneys worth out of your database

14

Data integrity/quality

update demo.emp_protected e set e.sal = 2 * e.sal

• Salary not over 7500• No duplicate

employee numbers• Existing department

update demo.emp_protected e set e.deptno = 50 where e.ename = 'SCOTT'

insert into demo.emp_protected (empno, ename, sal, deptno) values (7900, 'BAREL', 1000, 10)

alter table demo.emp_protected add constraint sal_under_7500 check ((sal < 7500))

alter table demo.emp_protected add constraint pk_emp_protected primary key (empno)

alter table demo.emp_protected add constraint fk_emp_protected_dept foreign key (deptno) references demo.dept (deptno)

dataintegrity.sql

Page 15: Get your moneys worth out of your database

15

Flashback queries

Page 16: Get your moneys worth out of your database

16

Flashback queries

create table emp_fb( empno NUMBER(4) not null, ename VARCHAR2(10), sal NUMBER(7,2), deptno NUMBER(2))

Page 17: Get your moneys worth out of your database

17

Flashback queries

…insert into emp_fb values (7782, 'CLARK', 2450, 10);insert into emp_fb values (7788, 'SCOTT', 3000, 20);insert into emp_fb values (7839, 'KING', 5000, 10);insert into emp_fb values (7844, 'TURNER', 1500, 30);insert into emp_fb values (7876, 'ADAMS', 1100, 20);insert into emp_fb values (7900, 'JAMES', 950, 30);insert into emp_fb values (7902, 'FORD', 3000, 20);insert into emp_fb values (7934, 'MILLER', 1300, 10);

Page 18: Get your moneys worth out of your database

18

Flashback queries

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

Page 19: Get your moneys worth out of your database

19

Flashback queries

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

update emp_fb e set e.sal = e.sal + 200 where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2850.00 7839 KING 5400.00 7934 MILLER 1700.00

Page 20: Get your moneys worth out of your database

20

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2850.00 7839 KING 5400.00 7934 MILLER 1700.00

Flashback queries

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

update emp_fb e set e.sal = e.sal + 200 where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2850.00 7839 KING 5400.00 7934 MILLER 1700.00

commit

Page 21: Get your moneys worth out of your database

21

Flashback queries

…insert into emp_fb values (7782, 'CLARK', 2450, 10);insert into emp_fb values (7788, 'SCOTT', 3000, 20);insert into emp_fb values (7839, 'KING', 5000, 10);insert into emp_fb values (7844, 'TURNER', 1500, 30);insert into emp_fb values (7876, 'ADAMS', 1100, 20);insert into emp_fb values (7900, 'JAMES', 950, 30);insert into emp_fb values (7902, 'FORD', 3000, 20);insert into emp_fb values (7934, 'MILLER', 1300, 10);

truncate table emp_fb

Page 22: Get your moneys worth out of your database

22

Flashback queries

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

Page 23: Get your moneys worth out of your database

23

Flashback queries

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

update emp_fb e set e.sal = e.sal + 200 where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2850.00 7839 KING 5400.00 7934 MILLER 1700.00

Page 24: Get your moneys worth out of your database

24

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2850.00 7839 KING 5400.00 7934 MILLER 1700.00

Flashback queries

select e.empno, e.ename, e.sal from emp_fb e where e.deptno = 10

update emp_fb e set e.sal = e.sal + 200 where e.deptno = 10

commit

Page 25: Get your moneys worth out of your database

25

Flashback queries

select e.empno, e.ename, e.sal from demo.emp_fb as of timestamp to_timestamp(sysdate - 1/(24*60*60)) e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2850.00 7839 KING 5400.00 7934 MILLER 1700.00

Page 26: Get your moneys worth out of your database

26

Flashback queries

select e.empno, e.ename, e.sal from demo.emp_fb as of timestamp to_timestamp(sysdate - 1/(24*60*60)) e where e.deptno = 10

EMPNO ENAME SAL----- ---------- --------- 7782 CLARK 2650.00 7839 KING 5200.00 7934 MILLER 1500.00

select e.empno, e.ename, e.sal from demo.emp_fb as of scn 23352983 e where e.deptno = 10

select dbms_flashback.

get_system_change_number

from dual

flashback_fb1.sqlflashback_fb2.sqlflashback_fb3.sql

Page 27: Get your moneys worth out of your database

27

Temporal validity

Page 28: Get your moneys worth out of your database

28

Temporal validity

create table demo.customers( id number, name varchar2(30), address varchar2(30))

Page 29: Get your moneys worth out of your database

29

Temporal validity

insert into demo.customers (id, name, address) values (1, 'Patrick', '1 Oxford Street')insert into demo.customers (id, name, address) values (2, 'Steven', '57 Chicago Boulevard')

select * from demo.customers c order by c.id

ID NAME ADDRESS --- ------------------------- ------------------------- 1 Patrick 1 Oxford Street 2 Steven 57 Chicago Boulevard

Page 30: Get your moneys worth out of your database

30

Temporal validity

update demo.customers c set c.address = '1 Oracle Lane' where c.id = 1

select * from demo.customers c order by c.id

ID NAME ADDRESS--- ------------------------- ------------------------- 1 Patrick 1 Oracle Lane 2 Steven 57 Chicago Boulevard

Movehouse

Page 31: Get your moneys worth out of your database

31

Temporal validity

truncate table demo.customersinsert into demo.customers (id, name, address) values (1, 'Patrick', '1 Oxford Street')insert into demo.customers (id, name, address) values (2, 'Steven', '57 Chicago Boulevard')select * from demo.customers c order by c.id

ID NAME ADDRESS --- ------------------------- ------------------------- 1 Patrick 1 Oxford Street 2 Steven 57 Chicago Boulevard

alter table demo.customers add period for valid_time

Page 32: Get your moneys worth out of your database

32

Temporal validity

truncate table demo.customersinsert into demo.customers (id, name, address) values (1, 'Patrick', '1 Oxford Street')insert into demo.customers (id, name, address) values (2, 'Steven', '57 Chicago Boulevard')

ID NAME ADDRESS --- ------------------------- ------------------------- 1 Patrick 1 Oxford Street 2 Steven 57 Chicago Boulevard

Columns are not visible when the

table is describeddesc demo.customersName Type Nullable

------- ------------ --------

ID NUMBER Y

NAME VARCHAR2(30) Y

ADDRESS VARCHAR2(30) Y

select * from demo.customers c order by c.idalter table demo.customers add period for valid_time

Page 33: Get your moneys worth out of your database

33

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.id

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 2 Steven 57 Chicago Boulevard

Page 34: Get your moneys worth out of your database

34

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.idupdate demo.customers c set c.valid_time_end = to_date('06012016 235959' ,'MMDDYYYY HH24MISS') where c.id = 1

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 2 Steven 57 Chicago Boulevard

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 06-01-2016 2 Steven 57 Chicago Boulevard

Page 35: Get your moneys worth out of your database

35

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 06-01-2016 1 Patrick 1 Oracle Lane 06-02-2016 2 Steven 57 Chicago Boulevard

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.idupdate demo.customers c set c.valid_time_end = to_date('06012016 235959' ,'MMDDYYYY HH24MISS') where c.id = 1insert into demo.customers(id, name, address, valid_time_start) values (1,'Patrick','1 Oracle Lane',to_date('06022016','MMDDYYYY'))

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 06-01-2016 2 Steven 57 Chicago Boulevard

Page 36: Get your moneys worth out of your database

36

Enable temporal validity

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.idexec dbms_flashback_archive.enable_at_valid_time('CURRENT')

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oracle Lane 06-02-2016 2 Steven 57 Chicago Boulevard

Page 37: Get your moneys worth out of your database

37

Enable temporal validity

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.idexec dbms_flashback_archive.enable_at_valid_time('CURRENT')

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oracle Lane 06-02-2016 2 Steven 57 Chicago Boulevard

exec dbms_flashback_archive.enable_at_valid_time('ASOF' , to_date('06012016','MMDDYYYY'))

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 06-01-2016 2 Steven 57 Chicago Boulevard

Page 38: Get your moneys worth out of your database

38

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oracle Lane 06-02-2016 2 Steven 57 Chicago Boulevard

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 06-01-2016 2 Steven 57 Chicago Boulevard

Enable temporal validity

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.idexec dbms_flashback_archive.enable_at_valid_time('CURRENT')exec dbms_flashback_archive.enable_at_valid_time('ASOF' , to_date('06012016','MMDDYYYY'))exec dbms_flashback_archive.enable_at_valid_time('ASOF' , to_date('06022016','MMDDYYYY'))

Page 39: Get your moneys worth out of your database

39

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oxford Street 06-01-2016 1 Patrick 1 Oracle Lane 06-02-2016 2 Steven 57 Chicago Boulevard

ID NAME ADDRESS VALID_FROM VALID_TILL--- ------------------------- ------------------------- ---------- ---------- 1 Patrick 1 Oracle Lane 06-02-2016 2 Steven 57 Chicago Boulevard

Enable temporal validity

Temporal validity

select c.id , c.name , c.address , to_char(c.valid_time_start, 'MM-DD-YYYY') valid_from , to_char(c.valid_time_end, 'MM-DD-YYYY') valid_till from demo.customers c order by c.idexec dbms_flashback_archive.enable_at_valid_time('CURRENT')exec dbms_flashback_archive.enable_at_valid_time('ASOF' , to_date('06012016','MMDDYYYY'))exec dbms_flashback_archive.enable_at_valid_time('ASOF' , to_date('06022016','MMDDYYYY'))exec dbms_flashback_archive.enable_at_valid_time('ALL')

temporal_validity.sql

Page 40: Get your moneys worth out of your database

40

Data security

Page 41: Get your moneys worth out of your database

41

Data redaction

SensitiveData5555-5555-5555-4444

5105-1051-0510-51005111-1111-1111-1118

SensitiveData (Clear)5555-5555-5555-44445105-1051-0510-51005111-1111-1111-1118

SensitiveData (Redacted)XXXX-XXXX-XXXX-4444XXXX-XXXX-XXXX-5100XXXX-XXXX-XXXX-1118

Page 42: Get your moneys worth out of your database

42

Data redaction

create table SensitiveData (contract varchar2(23))insert into SensitiveData(contract) values ('5555-5555-5555-4444');insert into SensitiveData(contract) values ('5105-1051-0510-5100');insert into SensitiveData(contract) values ('5111-1111-1111-1118');

Page 43: Get your moneys worth out of your database

43

Data redaction

begin dbms_redact.add_policy( object_schema => 'DEMO' ,object_name => 'SENSITIVEDATA' ,policy_name => 'PARTIALMASKDEMO' ,column_name => 'CONTACT' ,function_type => DBMS_REDACT.PARTIAL ,function_parameters => 'VVVVFVVVVFVVVVFVVVV,VVVV-VVVV-VVVV-VVVV,X,1,12' ,expression => q'[SYS_CONTEXT ('USERENV', 'SESSION_USER') <> 'BU']');end;

Page 44: Get your moneys worth out of your database

44

Data redaction

select * from demo.SensitiveData select * from demo.SensitiveData

CONTRACT-----------------------5555-5555-5555-44445105-1051-0510-51005111-1111-1111-1118

CONTRACT-----------------------XXXX-XXXX-XXXX-4444XXXX-XXXX-XXXX-5100XXXX-XXXX-XXXX-1118

redaction.sql

Page 45: Get your moneys worth out of your database

45

Virtual private database

Page 46: Get your moneys worth out of your database

46

Virtual private database

Page 47: Get your moneys worth out of your database

47

Virtual private databasecreate or replace function vpdfunc(schemaname_in in varchar2,tablename_in in varchar2) return varchar2is l_returnvalue varchar2(2000);begin case user when 'BU' then l_returnvalue := 'DEPTNO in (10,30)'; when 'MP' then l_returnvalue := 'DEPTNO in (20,40)'; else l_returnvalue := '1=1'; end case; return l_returnvalue;end;

Page 48: Get your moneys worth out of your database

48

Virtual private databasebegin dbms_rls.add_policy(object_schema => 'DEMO' ,object_name => 'EMP_VPD' ,policy_name => 'VPDDEMO' ,function_schema => 'DEMO' ,policy_function => 'VPDFUNC');end;

Page 49: Get your moneys worth out of your database

49

Virtual private databasebegin dbms_rls.add_policy(object_schema => 'DEMO' ,object_name => 'EMP_VPD' ,policy_name => 'VPDDEMO' ,function_schema => 'DEMO' ,policy_function => 'VPDFUNC');end;

select empno, ename, deptno from demo.emp_vpdEMPNO ENAME DEPTNO----- ---------- ------ 7499 ALLEN 30… 7934 MILLER 109 rows selected

EMPNO ENAME DEPTNO----- ---------- ------ 7369 SMITH 20… 7902 FORD 205 rows selected

VirtualPrivateDatabase.sql

Page 50: Get your moneys worth out of your database

50

Advanced Security

Key VaultDatabase Vault

Data Maskingand Subsetting

Audit Vault andDatabase Firewall

Label Security

Unified andConditional

Auditing

Real

Application

Security

TraditionalDatabaseAuditing

Page 51: Get your moneys worth out of your database

51

Performance increase

Page 52: Get your moneys worth out of your database

52

Query result cache

create table QueryResultCacheTable( id number)

Page 53: Get your moneys worth out of your database

53

Query result cache

insert into QueryResultCacheTable(id) values (1);insert into QueryResultCacheTable(id) values (2);insert into QueryResultCacheTable(id) values (3);insert into QueryResultCacheTable(id) values (4);insert into QueryResultCacheTable(id) values (5);create or replace function veryslowfunction(id_in in queryresultcachetable.id%type) return queryresultcachetable.id%type asbegin dbms_lock.sleep(1); return id_in;end;set timing on

Page 54: Get your moneys worth out of your database

54

Query result cache

select VerySlowFunction(qrct.id) from QueryResultCacheTable qrct

select /*+ result_cache */ veryslowfunction(qrct.id) from queryresultcachetable qrct

select /*+ result_cache */ veryslowfunction(qrct.id) from queryresultcachetable qrct

Executed in 5.88 seconds

Executed in 11.664 seconds

Executed in 0.035 seconds

Page 55: Get your moneys worth out of your database

55

Query result cache

create or replace function veryslowfunction(id_in in queryresultcachetable.id%type) return queryresultcachetable.id%type deterministic asbegin dbms_lock.sleep(1); return id_in;end;

Page 56: Get your moneys worth out of your database

56

Query result cache

select /*+ result_cache */ veryslowfunction(qrct.id) from queryresultcachetable qrct

select /*+ result_cache */ veryslowfunction(qrct.id) from queryresultcachetable qrct

Executed in 5.507 seconds

Executed in 0.038 seconds

It is even

cross session!

Page 57: Get your moneys worth out of your database

57

Query result cache

create table FunctionResultCacheTable( id number)

Page 58: Get your moneys worth out of your database

58

Query result cache

insert into FunctionResultCacheTable(id) values (1);insert into FunctionResultCacheTable(id) values (2);insert into FunctionResultCacheTable(id) values (3);insert into FunctionResultCacheTable(id) values (1);insert into FunctionResultCacheTable(id) values (2);insert into FunctionResultCacheTable(id) values (3);insert into FunctionResultCacheTable(id) values (1);insert into FunctionResultCacheTable(id) values (2);insert into FunctionResultCacheTable(id) values (3);insert into FunctionResultCacheTable(id) values (1);

Page 59: Get your moneys worth out of your database

59

Query result cache

select veryslowfunction(frct.id) from FunctionResultCacheTable frct

Elapsed: 00:00:10.18

create or replace function veryslowfunction(id_in in FunctionResultCacheTable.id%type) return FunctionResultCacheTable.id%type asbegin dbms_lock.sleep(1); return id_in;end;

Elapsed: 00:00:10.28

Page 60: Get your moneys worth out of your database

60

create or replace function veryslowfunction(id_in in FunctionResultCacheTable.id%type) return FunctionResultCacheTable.id%type deterministic asbegin dbms_lock.sleep(1); return id_in;end;

Query result cache

select veryslowfunction(frct.id) from FunctionResultCacheTable frct

Elapsed: 00:00:10.18Elapsed: 00:00:10.28

Elapsed: 00:00:04.16Elapsed: 00:00:04.09

Page 61: Get your moneys worth out of your database

61

create or replace function veryslowfunction(id_in in FunctionResultCacheTable.id%type) return FunctionResultCacheTable.id%type result_cache asbegin dbms_lock.sleep(1); return id_in;end;

Query result cache

select veryslowfunction(frct.id) from FunctionResultCacheTable frct

Elapsed: 00:00:10.18Elapsed: 00:00:10.28

Elapsed: 00:00:04.16Elapsed: 00:00:04.09

Elapsed: 00:00:00.00Elapsed: 00:00:03.15

Elapsed: 00:00:00.00

VirtualPrivateDatabase.sql

Page 62: Get your moneys worth out of your database

62

Page 63: Get your moneys worth out of your database

63

Application Upgrade

Page 64: Get your moneys worth out of your database

64

Application Upgrade

Page 65: Get your moneys worth out of your database

65

Application Upgrade

Page 66: Get your moneys worth out of your database

66

Application Upgrade

Page 67: Get your moneys worth out of your database

67

Application Upgrade

Page 68: Get your moneys worth out of your database

68

Application Upgrade

Creation of new objectsChanging existing objectsDrop redundant objectsConvert / MigrateResume normal operation

Page 69: Get your moneys worth out of your database

69

Edition BasedRedefinition

Page 70: Get your moneys worth out of your database

70

(Non)Editionable ObjectsEditionable Non-EditionablePackages TablesProcedures Materialized viewsFunctions DB LinksTriggersViewsTypesSynonyms

Page 71: Get your moneys worth out of your database

71

Special SQL features

Page 72: Get your moneys worth out of your database

72

Retrieve the top-3 earning employees

Special SQL features

select e.empno ,e.ename ,e.sal from demo.emp e where rownum < 4 order by e.sal desc

EMPNO ENAME SAL----- ---------- --------- 7499 ALLEN 1600.00 7521 WARD 1250.00 7369 SMITH 800.00

Page 73: Get your moneys worth out of your database

73

Retrieve the top-3 earning employees

Special SQL features

select e.empno ,e.ename ,e.sal from (select e2.empno ,e2.ename ,e2.sal from demo.emp e2 order by e2.sal desc) e where rownum < 4

EMPNO ENAME SAL----- ---------- --------- 7839 KING 5000.00 7788 SCOTT 3000.00 7902 FORD 3000.00

select e.empno ,e.ename ,e.sal from demo.emp e where rownum < 4 order by e.sal desc

EMPNO ENAME SAL----- ---------- --------- 7499 ALLEN 1600.00 7521 WARD 1250.00 7369 SMITH 800.00

Page 74: Get your moneys worth out of your database

74

Retrieve the top-3 earning employees

Special SQL features

with orderedbysal as (select e.empno ,e.ename ,e.sal

from demo.emp e order by e.sal desc)select obs.empno ,obs.ename ,obs.sal from orderedbysal obs where rownum < 4

select e.empno ,e.ename ,e.sal from demo.emp e order by e.sal desc fetch first 3 rows only

EMPNO ENAME SAL----- ---------- --------- 7839 KING 5000.00 7788 SCOTT 3000.00 7902 FORD 3000.00

EMPNO ENAME SAL----- ---------- --------- 7839 KING 5000.00 7788 SCOTT 3000.00 7902 FORD 3000.00

select e.empno ,e.ename ,e.sal from (select e2.empno ,e2.ename ,e2.sal from demo.emp e2 order by e2.sal desc) e where rownum < 4

Syntax Candythis

is rewritten as

Page 75: Get your moneys worth out of your database

75

Retrieve the next 3 topearning employees

Special SQL features

with orderedbysal as (select e.empno ,e.ename ,e.sal ,rownum rn from demo.emp e order by e.sal desc)select obs.empno ,obs.ename ,obs.sal from orderedbysal obs where obs.rn between 4 and 6 order by obs.rn

EMPNO ENAME SAL----- ---------- --------- 7566 JONES 2975.00 7654 MARTIN 1250.00 7698 BLAKE 2850.00

Page 76: Get your moneys worth out of your database

76

Retrieve the next 3 topearning employees

Special SQL features

with orderedbysal as (select e.empno ,e.ename ,e.sal from demo.emp e order by e.sal desc), orderedwithrownum as (select obs.empno , obs.ename , obs.sal , rownum rn from orderedbysal obs)select obr.empno ,obr.ename ,obr.sal from orderedwithrownum obr where obr.rn between 4 and 6 order by obr.rn

select e.empno ,e.ename ,e.sal from demo.emp e order by e.sal desc offset 3 rows fetch next 3 rows only

EMPNO ENAME SAL----- ---------- --------- 7566 JONES 2975.00 7698 BLAKE 2850.00 7782 CLARK 2450.00

EMPNO ENAME SAL----- ---------- --------- 7566 JONES 2975.00 7698 BLAKE 2850.00 7782 CLARK 2450.00

with orderedbysal as (select e.empno ,e.ename ,e.sal ,rownum rn from demo.emp e order by e.sal desc)select obs.empno ,obs.ename ,obs.sal from orderedbysal obs where obs.rn between 4 and 6 order by obs.rn

EMPNO ENAME SAL----- ---------- --------- 7566 JONES 2975.00 7654 MARTIN 1250.00 7698 BLAKE 2850.00

paginating.sql

Page 77: Get your moneys worth out of your database

77

Build PL/SQL in SQL

Special SQL features

create or replace function wavey(string_in in varchar2) return varchar2is l_returnvalue varchar2(30);begin for indx in 1 .. length(string_in) loop l_returnvalue := l_returnvalue || case mod(indx, 2) when 0 then upper(substr(string_in, indx, 1)) else lower(substr(string_in, indx, 1)) end; end loop; return l_returnvalue;end;

Function created

Page 78: Get your moneys worth out of your database

78

Build PL/SQL in SQL

Special SQL features

select e.empno, e.ename, wavey(e.ename) wavey_ename, e.sal from demo.emp e

EMPNO ENAME WAVEY_ENAME SAL----- ---------- ------------ --------- 7369 SMITH sMiTh 800.00 7499 ALLEN aLlEn 1600.00 7521 WARD wArD 1250.00 7566 JONES jOnEs 2975.00 7654 MARTIN mArTiN 1250.00 7698 BLAKE bLaKe 2850.00 7782 CLARK cLaRk 2450.00 7788 SCOTT sCoTt 3000.00 7839 KING kInG 5000.00 7844 TURNER tUrNeR 1500.00 7876 ADAMS aDaMs 1100.00 7900 JAMES jAmEs 950.00 7902 FORD fOrD 3000.00 7934 MILLER mIlLeR 1300.0014 rows selected

Page 79: Get your moneys worth out of your database

79

Special SQL featureswith function wavey(string_in in varchar2) return varchar2 is l_returnvalue varchar2(30); begin for indx in 1 .. length(string_in) loop l_returnvalue := l_returnvalue || case mod(indx, 2) when 1 then upper(substr(string_in, indx, 1)) else lower(substr(string_in, indx, 1)) end; end loop; return l_returnvalue; end;select e.empno, e.ename, wavey(e.ename) wavey_ename, e.sal from demo.emp e

EMPNO ENAME WAVEY_ENAME SAL----- ---------- ------------ --------- 7369 SMITH SmItH 800.00 7499 ALLEN AlLeN 1600.00… 7934 MILLER MiLlEr 1300.0014 rows selected

plsql_in_with.sql

Page 80: Get your moneys worth out of your database

80

Page 81: Get your moneys worth out of your database

81

Virtual Columns

Page 82: Get your moneys worth out of your database

82

Virtual Columns

create table demo.emp_income( empno number(4) not null, ename varchar2(10), sal number(7,2), comm number(7,2), deptno number(2), income number(7,2) generated always as (demo.emp_income.sal + coalesce(demo.emp_income.comm,0)) virtual)

Page 83: Get your moneys worth out of your database

83

Virtual Columns

insert into demo.emp_income(empno, ename, sal, deptno, comm) values (7499, 'ALLEN', 1600, 30, 300);insert into demo.emp_income(empno, ename, sal, deptno, comm) values (7521, 'WARD', 1250, 30, 500);insert into demo.emp_income(empno, ename, sal, deptno, comm) values (7654, 'MARTIN', 1250, 30, 1400);insert into demo.emp_income(empno, ename, sal, deptno, comm) values (7698, 'BLAKE', 2850, 30, null);insert into demo.emp_income(empno, ename, sal, deptno, comm) values (7844, 'TURNER', 1500, 30, 0);insert into demo.emp_income(empno, ename, sal, deptno, comm) values (7900, 'JAMES', 950, 30, null);

DEMO@demo> insert into demo.emp_income

2 (empno, ename, sal, deptno, comm, income)

3 values 4 (2912, 'BAREL', 3000, 30, 2000, 5000)

5 / (empno, ename, sal, deptno, comm, income)

*

ERROR at line 2:ORA-54013: INSERT operation disallowed on virtual columns

Page 84: Get your moneys worth out of your database

84

Virtual Columns

EMPNO ENAME SAL COMM DEPTNO INCOME------ ---------- --------- --------- ------- --------- 7499 ALLEN 1600.00 300.00 30 1900.00 7521 WARD 1250.00 500.00 30 1750.00 7654 MARTIN 1250.00 1400.00 30 2650.00 7698 BLAKE 2850.00 30 2850.00 7844 TURNER 1500.00 .00 30 1500.00 7900 JAMES 950.00 30 950.00

6 rows selected.

select * from demo.emp_income e

VirtualColumnIncome.sql

Page 85: Get your moneys worth out of your database

85

Virtual Columns

create table demo.emp_nuke( empno number(4) not null, ename varchar2(10), sal number(7,2), deptno number(2), blowitup number generated always as (1/0) virtual)

Page 86: Get your moneys worth out of your database

86

Virtual Columns

insert into demo.emp_nuke(empno, ename, sal, deptno) values (7499, 'ALLEN', 1600, 30);insert into demo.emp_nuke(empno, ename, sal, deptno) values (7521, 'WARD', 1250, 30);insert into demo.emp_nuke(empno, ename, sal, deptno) values (7654, 'MARTIN', 1250, 30);insert into demo.emp_nuke(empno, ename, sal, deptno) values (7698, 'BLAKE', 2850, 30);insert into demo.emp_nuke(empno, ename, sal, deptno) values (7844, 'TURNER', 1500, 30);insert into demo.emp_nuke(empno, ename, sal, deptno) values (7900, 'JAMES', 950, 30);

Page 87: Get your moneys worth out of your database

87

Virtual Columns

select * *ERROR at line 1:ORA-01476: divisor is equal to zero

select * from demo.emp_nuke e

Page 88: Get your moneys worth out of your database

88

Virtual Columns

EMPNO ENAME SAL DEPTNO------ ---------- --------- ------- 7499 ALLEN 1600.00 30 7521 WARD 1250.00 30 7654 MARTIN 1250.00 30 7698 BLAKE 2850.00 30 7844 TURNER 1500.00 30 7900 JAMES 950.00 30

6 rows selected.

select e.empno, e.ename, e.sal, e.deptno from demo.emp_nuke e

VirtualColumnPreventSelectStar.sql

Page 89: Get your moneys worth out of your database

90

Identity columns (12c)

Page 90: Get your moneys worth out of your database

91

Identity columns (12c)

create table tablewithuniquecolumn( theuniquecolumn number(10) not null, foo varchar2(30), bar varchar2(30))

Page 91: Get your moneys worth out of your database

92

Identity columns (12c)

create sequence sequenceforuniquecolumn start with 1 nocache

Page 92: Get your moneys worth out of your database

93

Identity columns (12c)

create or replace trigger makesureitsfilled before insert or update on tablewithuniquecolumn for each rowbegin if :new.theuniquecolumn is null then select sequenceforuniquecolumn.nextval into :new.theuniquecolumn from dual; end if;end;

Page 93: Get your moneys worth out of your database

94

Identity columns (12c)

create or replace trigger makesureitsfilled before insert or update on tablewithuniquecolumn for each rowbegin if :new.theuniquecolumn is null then

:new.theuniquecolumn := sequenceforuniquecolumn.nextval;

end if;end;

Page 94: Get your moneys worth out of your database

95

Identity columns (12c)

create table tablewithidentitycolumn( theidentitycolumn number(10) generated always as identity, foo varchar2(30), bar varchar2(30))

identity_columns.sql

Page 95: Get your moneys worth out of your database

96

Identity columns (12c)

Page 96: Get your moneys worth out of your database

97

Invisible columns (12c)

Page 97: Get your moneys worth out of your database

98

Invisible columns (12c)

create table Show( I varchar2(30), am varchar2(30), the varchar2(30), invisible varchar2(30), man varchar2(30))

Page 98: Get your moneys worth out of your database

99

Invisible columns (12c)

select * from show

I AM THE INVISIBLE MAN- -- --- --------- ---I am the invisible man

Page 99: Get your moneys worth out of your database

100

Invisible columns (12c)

select * from show

I AM THE INVISIBLE MAN- -- --- --------- ---I am the invisible man

alter table show modify invisible invisible

Page 100: Get your moneys worth out of your database

101

Invisible columns (12c)

select * from show

I AM THE INVISIBLE MAN- -- --- --------- ---I am the invisible man

alter table show modify invisible invisible

I AM THE MAN- -- --- ---I am the man

Page 101: Get your moneys worth out of your database

102

Invisible columns (12c)

select * from show

I AM THE INVISIBLE MAN- -- --- --------- ---I am the invisible man

alter table show modify invisible invisible

I AM THE MAN- -- --- ---I am the man

select I, am, the, invisible, man from show

IAmTheInvisibleColumn.sql

Page 102: Get your moneys worth out of your database

103

Whitelisting PL/SQL program units

Page 103: Get your moneys worth out of your database

104

Whitelisting PL/SQL program units

Page 104: Get your moneys worth out of your database

105

Whitelisting PL/SQL program units

create or replace procedure onlywhitelisted accessible by (normalprocedure)isbegin dbms_output.put_line('You are allowed');end;

Page 105: Get your moneys worth out of your database

106

Whitelisting PL/SQL program units

create or replace procedure onlywhitelisted accessible by (normalprocedure)isbegin dbms_output.put_line('You are allowed');end;

create or replace procedure normalprocedureisbegin dbms_output.put_line ('normalprocedure calls onlywhitelisted'); onlywhitelisted;end;

Page 106: Get your moneys worth out of your database

107

Whitelisting PL/SQL program units

create or replace procedure onlywhitelisted accessible by (normalprocedure)isbegin dbms_output.put_line('You are allowed');end;

create or replace procedure normalprocedureisbegin dbms_output.put_line ('normalprocedure calls onlywhitelisted'); onlywhitelisted;end;

create or replace procedure evilprocedureisbegin dbms_output.put_line ('evilprocedure calls onlywhitelisted'); onlywhitelisted;end;

Page 107: Get your moneys worth out of your database

108

Whitelisting PL/SQL program units

create or replace procedure onlywhitelisted accessible by (normalprocedure)isbegin dbms_output.put_line('You are allowed');end;

create or replace procedure normalprocedureisbegin dbms_output.put_line ('normalprocedure calls onlywhitelisted'); onlywhitelisted;end;

create or replace procedure evilprocedureisbegin dbms_output.put_line ('evilprocedure calls onlywhitelisted'); onlywhitelisted;end;

create or replace procedure evilprocedureisbegin dbms_output.put_line ('evilprocedure calls normalprocedure'); normalprocedure;end;

whitelisting_12c.sql

Page 108: Get your moneys worth out of your database

109

Q&A

Page 109: Get your moneys worth out of your database

110

Page 110: Get your moneys worth out of your database

111

• Data integrity/quality• http://www.riparteilfuturo.it/assets/articles/images/

429e6a270817213e33647f1344cb121f.jpeg

• No Matter How Much– https://pbs.twimg.com/profile_images/

439295781602734080/WwHn8tUM.jpeg

Credits

• Database– https://www.oracle.com/engineered-systems/

products.html

Page 111: Get your moneys worth out of your database

112

• Flashback queries– https://insiderhema.files.wordpress.com/2015/07/the-

time-desktop-background-496046.jpg

Credits

• Data security– http://tr1.cbsistatic.com/hub/i/2016/02/25/f1cab87f-0b42-

4700-945e-898a5f90898b/32c0dc0f18610d2a9eddc85b19bdce81/hacker6.jpg

• Temporal validity– http://blogs.discovermagazine.com/crux/files/2014/01/

time-travel.jpg

Page 112: Get your moneys worth out of your database

113

• Performance increase– http://www.adelhi.com/wp-content/uploads/2015/10/

result.adelhi.jpg

Credits

• Edition Based Redefinition– http://www.psychedelictribe.com/wp-content/uploads/

parallel-universe-image.jpg

• Why Downtime matters?– http://image.slidesharecdn.com/

wed1240yahooshahegashira-150424124356-conversion-gate01/95/oozie-towards-zero-downtime-12-638.jpg?cb=1429897523

Page 113: Get your moneys worth out of your database

114

• Special SQL features– https://geofrikphotos.files.wordpress.com/2012/12/

paisaje-marte.jpg

Credits

• Virtual Columns– http://wallpaper.zone/img/1667098.jpg

• Pros Cons– https://secure.surveymonkey.com/wp-content/uploads/

2014/10/Pros_Cons_NPS.jpg

Page 114: Get your moneys worth out of your database

115

• Identity columns (12c)– http://www.cambridgedove.com/Pages/images/

020/020title2.gif

Credits

• Whitelisting PL/SQL program units– http://www.pinpointe.com/blog/wp-content/uploads/

what-is-whitelisting-780x421.png

• Invisible columns (12c)– https://hollywoodhatesme.files.wordpress.com/2012/07/

invisible-man.jpg

Page 115: Get your moneys worth out of your database

116

• Whitelisting PL/SQL program units– http://www.ccs-inc.com/images/uploads/

Yes_No_Shutterstock_83547358.jpg

Credits