SlideShare a Scribd company logo
Write Less
Code
With More
Oracle 12c New Features
Oren Nakdimon
www.db-oriented.com
 oren@db-oriented.com
 +972-54-4393763
@DBoriented
© Oren Nakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
WHO AM I?
A CHRONOLOGY BY “ORACLE YEARS”
Where: IAF
When: Oracle 6/7 [1991-1997]
What: Developer
Where: Golden Screens
When: Oracle 8 [1997-1998]
What: Server Group Manager
Where: TELEknowledge
When: Oracle 8i/9i [1998-2003]
What: DBA Group Manager
Where: Olista
When: Oracle 10g/11g [2004-2011]
What: VP R&D + Israel Site Manager
Where:
When: Oracle 11g/12c [2011-]
What: Freelance Consultant
Where:
When: 2015-
What: Database Expert
“An expert is a person who has made all the
mistakes that can be made in a very narrow
field” (Niels Bohr, 1885-1962)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
@DBORIENTED
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
HTTP://DB-ORIENTED.COM
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
Read a summary of this
presentation in the
article in
Oracle Scene Issue 58
MORE “WRITE LESS WITH MORE”
Download this presentation from
db-oriented.com/presentations
Read the blog post series
db-oriented.com/category/writelesswithmore/
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
AGENDA
 Go over some common use cases
 For each one
 A pre-12c solution
 A 12c solution, that allows us to write less
 We’ll see examples for features that allow writing less…
 …configuration
 …application code
 …code in SQL statements
 …“inappropriately-located” code
6
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
THE USUAL WARNING
 Before you decide to apply any of the presented
features in production, make sure:
 to thoroughly test them for your applications and
environments
 that you have the required license to use them
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
MEET THE DEMO TABLES
8
This presentation is available in http://db-oriented.com/presentations
9
1. Fill the PEOPLE table from a
text file
2. Set a unique number to
ASSIGNMENT_ID (implicitly)
for every new record
3. Delete obsolete projects (but keep
their assignment history)
4. Add a “validity period” to
PROJECT_ASSIGNMENTS
5. Write a query to get project
assignments with pagination
1/2
2/2
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
LOADING DATA FROM FILES
 Before 12c
 SQL*Loader
 Requires a control file
 External tables
 Require a “control file” within the CREATE TABLE
statement
 Then:
INSERT INTO <“real” table> …
SELECT FROM <external table>… ;
10
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
SQL*LOADER EXPRESS MODE
 No need to create a control file
 Defaults
 File name: <tableName>.dat, in current directory
 Record delimiter: newline
 Field delimiter: comma
 No enclosures
11
12c
@ldr
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
SQL*LOADER EXPRESS MODE
 Mandatory command line parameter
 TABLE
 Some optional command line parameters
 DATA (up to 6 names, wildcards supported)
 TERMINATED_BY
 CHARACTERSET
 CSV = WITH_EMBEDDED
 OPTIONALLY_ENCLOSED_BY
 DATE_FORMAT
 DEGREE_OF_PARALLELISM
 DIRECT 12
12c
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
SQL*LOADER EXPRESS MODE
 A log file is generated for future use, including:
 Control file
 CREATE EXTERNAL TABLE statement
 INSERT statement
 Data types of all table columns should be
numeric, string or datetime
13
12c
This presentation is available in http://db-oriented.com/presentations
14
1. Fill the PEOPLE table from a
text file
2. Set a unique number to
ASSIGNMENT_ID (implicitly)
for every new record
3. Delete obsolete projects (but keep
their assignment history)
4. Add a “validity period” to
PROJECT_ASSIGNMENTS
5. Write a query to get project
assignments with pagination
1/2
2/2
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
AUTO-INCREMENT COLUMNS
 Before 12c
 A sequence + a BEFORE INSERT trigger
15
CREATE SEQUENCE project_assignments_seq;
--
-- first option: ignore the input even if supplied
--
CREATE TRIGGER project_assignments_bir_tr
BEFORE INSERT ON project_assignments
FOR EACH ROW
BEGIN
:new.assignment_id := project_assignments_seq.nextval;
END;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
AUTO-INCREMENT COLUMNS
 Before 12c
 A sequence + a BEFORE INSERT trigger
16
CREATE SEQUENCE project_assignments_seq;
--
-- second option: only if input not supplied
--
CREATE TRIGGER project_assignments_bir_tr
BEFORE INSERT ON project_assignments
FOR EACH ROW
WHEN (new.assignment_id IS NULL)
BEGIN
:new.assignment_id := project_assignments_seq.nextval;
END;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
IDENTITY COLUMNS
 A column can be defined as “identity”
 Implicit sequence
 Implicit NOT NULL
 GENERATED…
 [always] as identity
 by default as identity
 by default on null as identity
 You need the CREATE SEQUENCE privilege
17
@idn1
12c
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
IDENTITY COLUMNS
 Configuring the implicit sequence
 Like in CREATE SEQUENCE
 START WITH LIMIT VALUE
 Restrictions
 Only for numeric data types
 Maximum one identity column per table
 Non-identity column cannot be modified to identity
column
 CTAS ignores the identity definition
18
@idn2
12c
This presentation is available in http://db-oriented.com/presentations
19
1. Fill the PEOPLE table from a
text file
2. Set a unique number to
ASSIGNMENT_ID (implicitly)
for every new record
3. Delete obsolete projects (but keep
their assignment history)
4. Add a “validity period” to
PROJECT_ASSIGNMENTS
5. Write a query to get project
assignments with pagination
1/2
2/2
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
LOGICAL DELETION OF RECORDS
20
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
LOGICAL DELETION OF RECORDS
 Before 12c
 Add an IS_DELETED column
 Use a view to hide “deleted” records
21
ALTER TABLE projects ADD
is_deleted NUMBER(1) DEFAULT 0 NOT NULL
CHECK (is_deleted IN (0,1));
CREATE VIEW projects AS
SELECT *
FROM all_projects
WHERE is_deleted=0;
RENAME projects TO all_projects;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
In-Database Archiving
 Enables to archive rows within a table by
marking them as “inactive”
 Inactive rows are still there, but are not visible
to the application (or visible, when we want)
22
@idar
12c
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
In-Database Archiving
 The table should be defined as ROW ARCHIVAL
 A hidden column is created:
ORA_ARCHIVE_STATE
 The session level parameter ROW ARCHIVAL
VISIBILITY controls if archived rows are visible
(ALL) or not (ACTIVE)
23
12c
This presentation is available in http://db-oriented.com/presentations
24
1. Fill the PEOPLE table from a
text file
2. Set a unique number to
ASSIGNMENT_ID (implicitly)
for every new record
3. Delete obsolete projects (but keep
their assignment history)
4. Add a “validity period” to
PROJECT_ASSIGNMENTS
5. Write a query to get project
assignments with pagination
1/2
2/2
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
VALIDITY PERIODS
 Before 12c
 Add PERIOD_START and PERIOD_END columns
 Add conditions to queries to filter only valid periods
25
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
TEMPORAL VALIDITY
 Enables to track time periods for real world
validity or effectivity
 Valid times can be set by users/application for
data
 Data can be selected by a specific valid time (or
range)
26
12c
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
TEMPORAL VALIDITY
 A valid time period consists of two date/time
columns (start/end)
 The columns can be created explicitly or
implicitly
 The table is defined with PERIOD FOR
27
12c
@vld
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
TEMPORAL VALIDITY
 Statement level visibility control
 SELECT … AS OF PERIOD FOR
 SELECT … VERSIONS PERIOD FOR
 Session level visibility control
 DBMS_FLASHBACK_ARCHIVE.enable_at_valid_time
 ALL
 CURRENT
 ASOF
28
12c
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
THERE ARE TWO SIDES TO EVERY COIN
 Hidden columns
 Hidden predicates
 “Hidden” =“Easy to Forget”
 “Hidden” ≠“Can be Ignored”
 And session-level control is session-level control
In Database
Archiving
Temporal
Validity
SELECT * FROM PROJECTS;
where most of the rows are “archived”
DELETE
PROJECT_ASSIGNMENTS;
where
enable_at_valid_time
is on
This presentation is available in http://db-oriented.com/presentations
30
1. Fill the PEOPLE table from a
text file
2. Set a unique number to
ASSIGNMENT_ID (implicitly)
for every new record
3. Delete obsolete projects (but keep
their assignment history)
4. Add a “validity period” to
PROJECT_ASSIGNMENTS
5. Write a query to get project
assignments with pagination
1/2
2/2
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
TOP-N AND PAGINATION QUERIES
 Before 12c
 Use inline views and ROWNUM or ROW_NUMBER( )
 Queries become quite cumbersome
31
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
TOP-N AND PAGINATION QUERIES
 All the records
 Top 8 records
 Records 5-8
32
SELECT project_id,
person_id,
assignment_id,
validity_period_start,
validity_period_end
FROM (
SELECT x.*
,rownum row_num
FROM (
SELECT project_id,
person_id,
assignment_id,
validity_period_start,
validity_period_end
FROM project_assignments
ORDER BY project_id, person_id
) x WHERE rownum <= 8
) WHERE row_num > 4;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
ROW LIMITING
 The Row Limiting clause is added to the end of
SELECT statement
 FETCH FIRST n ROWS WITH TIES
 FETCH FIRST n ROWS ONLY
 FETCH FIRST p PERCENT ROWS
 OFFSET m ROWS FETCH NEXT n ROWS
33
12c
@topn
This presentation is available in http://db-oriented.com/presentations
1/2
34
6. Write a procedure to update the
status of multiple projects
7. Write a query that shows the
number of assignments per
project in the last days
2/2
8. Write a query that shows all the
people with a valid date in their
GENERAL_INFO column
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
 Write a stored procedure that
 Gets a collection parameter of “project updates”,
each one with:
 PROJECT_ID
 UPDATE_TIME
 STATUS
 Updates the PROJECTS table with the latest status of
each project
35
PROJECT_ID STATUS
1 2
2 3
3 4
4 2
3
2
4
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
PROJECT
ID
UPDATE
TIME STATUS
1 10:00 2
1 14:00 3
2 09:00 1
3 09:00 4
2 08:00 3
3 08:00 2
2 10:00 2
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
36
CREATE OR REPLACE PACKAGE projects_dl AS
TYPE proj_update_t IS RECORD(
project_id projects.project_id%TYPE,
update_time DATE,
status projects.status%TYPE);
TYPE proj_update_tt IS TABLE OF proj_update_t;
PROCEDURE update_status(
i_proj_update_list IN proj_update_tt
);
END projects_dl;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
37
CREATE OR REPLACE PACKAGE BODY projects_dl AS
PROCEDURE update_status(
i_proj_update_list IN proj_update_tt) IS
BEGIN
MERGE INTO projects p
USING (
SELECT project_id,
MAX(status) keep(dense_rank LAST
ORDER BY update_time) latest_status
FROM TABLE(i_proj_update_list)
GROUP BY project_id
) i
ON (p.project_id = i.project_id)
WHEN MATCHED THEN UPDATE
SET p.status = i.latest_status;
END update_status;
END projects_dl;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
SELECT FROM COLLECTION VARIABLES
 Before 12c
 Impossible to SELECT FROM package-level collections
38
PL/SQL: SQL Statement ignored
ORA-22905:
cannot access rows from a non-nested table item
PLS-00642:
local collection types not allowed in SQL statements
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
SELECT FROM COLLECTION VARIABLES
 Before 12c
 Schema-level collection types must be created and used,
even if needed only in the scope of a single package
39
CREATE OR REPLACE TYPE proj_update_t AS OBJECT (
project_id INTEGER,
update_time DATE,
status INTEGER)
CREATE TYPE proj_update_tt AS TABLE OF proj_update_t
CREATE OR REPLACE PACKAGE projects_dl AS
PROCEDURE update_status(
i_proj_update_list IN proj_update_tt
);
END projects_dl;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
SELECT FROM COLLECTION VARIABLES
 Now it is possible to SELECT FROM package-level
collections
40
12c
This presentation is available in http://db-oriented.com/presentations
1/2
41
6. Write a procedure to update the
status of multiple projects
7. Write a query that shows the
number of assignments per
project in the last days
2/2
8. Write a query that shows all the
people with a valid date in their
GENERAL_INFO column
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
PROJECT
NAME
LAST DAYS TO SHOW
IN REPORTS
Project A 2
Project B 3
Project C 4
PROJECT
NAME DATE
NUM OF
ASSIGNMENTS
Project A 02/03/2016 3
Project A 03/03/2016 3
Project B 01/03/2016 5
Project B 02/03/2016 4
Project B 03/03/2016 2
Project C 29/02/2016 1
Project C 01/03/2016 0
Project C 02/03/2016 0
Project C 03/03/2016 1
@ltrl
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
LATERAL INLINE VIEWS
 An inline view in a query can be defined as
LATERAL
 It allows it to refer to other tables that appear to
its left in the FROM clause
43
12c
This presentation is available in http://db-oriented.com/presentations
1/2
44
6. Write a procedure to update the
status of multiple projects
7. Write a query that shows the
number of assignments per
project in the last days
2/2
8. Write a query that shows all the
people with a valid date in their
GENERAL_INFO column
©OrenNakdimon
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
CALLING PL/SQL FROM SQL
 Before 12c
 A PL/SQL function must be stored in the database
 And it must be public (either standalone or exposed
in a package spec)
45
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
CALLING PL/SQL FROM SQL
 Before 12c
46
CREATE OR REPLACE FUNCTION is_date(i_info IN VARCHAR2)
RETURN NUMBER AS
l_date DATE;
BEGIN
IF i_info IS NULL THEN
RETURN 0;
ELSE
l_date := to_date(i_info, 'dd/mm/yyyy');
RETURN 1;
END IF;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
SELECT * FROM people WHERE is_date(general_info) = 1;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
PL/SQL in the WITH Clause
 Before 12c, the WITH clause included only
subquery factoring
 In 12c, it can include also PL/SQL declarations
 Functions, that can be used in the query
 Procedures, that can be used in the functions
 Name resolution
 statement-level function names have precedence
over schema-level stored functions
47
12c
@with
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon
PL/SQL in the WITH Clause
 Not yet supported in static SQL in PL/SQL
 If used as subquery, the top-level statement
must be hinted with WITH_PLSQL (or be itself a
SELECT with PL/SQL declaration)
 Considerations for statement-level vs. schema-
level functions
 Ad-hoc vs. common functionality
 Performance
 Consider also PRAGMA UDF
48
12c
This presentation is available in http://db-oriented.com/presentations
49
6. Write a procedure to update the
status of multiple projects
7. Write a query that shows the
number of assignments per
project in the last days
2/2
8. Write a query that shows all the
people with a valid date in their
GENERAL_INFO column
1. Fill the PEOPLE table from a
text file
2. Set a unique number to
ASSIGNMENT_ID (implicitly)
for every new record
3. Delete obsolete projects (but keep
their assignment history)
4. Add a “validity period” to
PROJECT_ASSIGNMENTS
5. Write a query to get project
assignments with pagination
1/2
©OrenNakdimon
THANK YOU
Oren Nakdimon
www.db-oriented.com
 oren@db-oriented.com
 +972-54-4393763
@DBoriented
© Oren Nakdimon

More Related Content

What's hot (19)

PPTX
New PLSQL in Oracle Database 12c
Connor McDonald
 
PPTX
ZekeLabs PLSQL slides
zekeLabs Technologies
 
PPT
06 Using More Package Concepts
rehaniltifat
 
PDF
18(ish) Things You'll Love About Oracle Database 18c
Chris Saxon
 
PPT
ORACLE PL SQL
Srinath Maharana
 
PPT
Oracle PLSQL Step By Step Guide
Srinimf-Slides
 
PPTX
PLSQL Advanced
Quang Minh Đoàn
 
PDF
Controlling execution plans 2014
Enkitec
 
PDF
JAX-RS and CDI Bike the (Reactive) Bridge
José Paumard
 
PDF
JAX RS and CDI bike the reactive bridge
José Paumard
 
PDF
Agile Database Development with JSON
Chris Saxon
 
PPTX
Why You Should Use TAPIs
Jeffrey Kemp
 
PDF
Oracle sql & plsql
Sid Xing
 
PPTX
PL/SQL Fundamentals I
Nick Buytaert
 
PPT
11 Understanding and Influencing the PL/SQL Compilar
rehaniltifat
 
PPT
1 - Introduction to PL/SQL
rehaniltifat
 
PPTX
Plsql guide 2
Vinay Kumar
 
PDF
Oren nakdimon - Design Patterns for PL/SQL and SQL - UKOUGtogether21
Oren Nakdimon
 
New PLSQL in Oracle Database 12c
Connor McDonald
 
ZekeLabs PLSQL slides
zekeLabs Technologies
 
06 Using More Package Concepts
rehaniltifat
 
18(ish) Things You'll Love About Oracle Database 18c
Chris Saxon
 
ORACLE PL SQL
Srinath Maharana
 
Oracle PLSQL Step By Step Guide
Srinimf-Slides
 
PLSQL Advanced
Quang Minh Đoàn
 
Controlling execution plans 2014
Enkitec
 
JAX-RS and CDI Bike the (Reactive) Bridge
José Paumard
 
JAX RS and CDI bike the reactive bridge
José Paumard
 
Agile Database Development with JSON
Chris Saxon
 
Why You Should Use TAPIs
Jeffrey Kemp
 
Oracle sql & plsql
Sid Xing
 
PL/SQL Fundamentals I
Nick Buytaert
 
11 Understanding and Influencing the PL/SQL Compilar
rehaniltifat
 
1 - Introduction to PL/SQL
rehaniltifat
 
Plsql guide 2
Vinay Kumar
 
Oren nakdimon - Design Patterns for PL/SQL and SQL - UKOUGtogether21
Oren Nakdimon
 

Viewers also liked (18)

PDF
MongoDB NoSQL database a deep dive -MyWhitePaper
Rajesh Kumar
 
PDF
Tracxn Research - Mobile Advertising Landscape, February 2017
Tracxn
 
PPTX
Tugas4 1412510602 dewi_apriliani
dewiapril1996
 
PPTX
Webinar: Fighting Fraud with Graph Databases
DataStax
 
PPTX
Oracle Database 12c features for DBA
Karan Kukreja
 
PDF
Oracle 12c and its pluggable databases
Gustavo Rene Antunez
 
PDF
Mongo db a deep dive of mongodb indexes
Rajesh Kumar
 
PPTX
Tugas 4 0317-imelda felicia-1412510545
imeldafelicia
 
PDF
Tracxn Research - Construction Tech Landscape, February 2017
Tracxn
 
PPTX
Introducing NoSQL and MongoDB to complement Relational Databases (AMIS SIG 14...
Lucas Jellema
 
PPTX
Best New Features of Oracle Database 12c
Pini Dibask
 
PDF
Oracle Exadata 1Z0-485 Certification
Exadatadba
 
PDF
Oracle 12c Multi Process Multi Threaded
Markus Flechtner
 
PDF
Senior .Net engineer
Adam Sowter
 
PDF
System Architecture of Cloud-based Web GIS for Real-Time Macroeconomic Loss E...
Reza Nourjou, Ph.D.
 
PDF
Google Home
Malhar Pandhare
 
DOCX
Oracle 12c RAC On your laptop Step by Step Implementation Guide 1.0
Yury Velikanov
 
PPT
Avaya anixter event
Odilo Alvarez
 
MongoDB NoSQL database a deep dive -MyWhitePaper
Rajesh Kumar
 
Tracxn Research - Mobile Advertising Landscape, February 2017
Tracxn
 
Tugas4 1412510602 dewi_apriliani
dewiapril1996
 
Webinar: Fighting Fraud with Graph Databases
DataStax
 
Oracle Database 12c features for DBA
Karan Kukreja
 
Oracle 12c and its pluggable databases
Gustavo Rene Antunez
 
Mongo db a deep dive of mongodb indexes
Rajesh Kumar
 
Tugas 4 0317-imelda felicia-1412510545
imeldafelicia
 
Tracxn Research - Construction Tech Landscape, February 2017
Tracxn
 
Introducing NoSQL and MongoDB to complement Relational Databases (AMIS SIG 14...
Lucas Jellema
 
Best New Features of Oracle Database 12c
Pini Dibask
 
Oracle Exadata 1Z0-485 Certification
Exadatadba
 
Oracle 12c Multi Process Multi Threaded
Markus Flechtner
 
Senior .Net engineer
Adam Sowter
 
System Architecture of Cloud-based Web GIS for Real-Time Macroeconomic Loss E...
Reza Nourjou, Ph.D.
 
Google Home
Malhar Pandhare
 
Oracle 12c RAC On your laptop Step by Step Implementation Guide 1.0
Yury Velikanov
 
Avaya anixter event
Odilo Alvarez
 
Ad

Similar to Write Less (code) With More (Oracle Database 12c New Features) (20)

PPTX
More than 12 More things about Oracle Database 12c
Guatemala User Group
 
PPTX
Less08_Schema Advanced Databases and Management.pptx
MurtazaMughal13
 
PPT
Less07 schema
Imran Ali
 
PPTX
Data modeling tips from the trenches
Terry Bunio
 
PPT
oodb.ppt
ISHAAGARWAL75
 
PDF
CIS 336 Final Exam (Feb 2016)p
critterc06
 
PPT
Database administration and management chapter 12
saniaafzalf1f2f3
 
PPTX
An AMIS Overview of Oracle database 12c (12.1)
Marco Gralike
 
PDF
An AMIS overview of database 12c
Getting value from IoT, Integration and Data Analytics
 
PDF
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Citus Data
 
PPTX
Chapter 1 Concepts for Object-oriented Databases.pptx
haymanottaddess2015m
 
PDF
CIS 336 PAPERS Lessons in Excellence--cis336papers.com
thomashard82
 
PPTX
Database Management System
Nishant Munjal
 
PDF
database management system - overview of entire dbms
vikramkagitapu
 
PPT
CH11.ppt
ssuser5c874e
 
PDF
Overview of Oracle database12c for developers
Getting value from IoT, Integration and Data Analytics
 
PDF
Oracle 12c New Features for Developers
CompleteITProfessional
 
PPTX
12 things about Oracle 12c
Connor McDonald
 
PDF
Aioug vizag oracle12c_new_features
AiougVizagChapter
 
More than 12 More things about Oracle Database 12c
Guatemala User Group
 
Less08_Schema Advanced Databases and Management.pptx
MurtazaMughal13
 
Less07 schema
Imran Ali
 
Data modeling tips from the trenches
Terry Bunio
 
oodb.ppt
ISHAAGARWAL75
 
CIS 336 Final Exam (Feb 2016)p
critterc06
 
Database administration and management chapter 12
saniaafzalf1f2f3
 
An AMIS Overview of Oracle database 12c (12.1)
Marco Gralike
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Citus Data
 
Chapter 1 Concepts for Object-oriented Databases.pptx
haymanottaddess2015m
 
CIS 336 PAPERS Lessons in Excellence--cis336papers.com
thomashard82
 
Database Management System
Nishant Munjal
 
database management system - overview of entire dbms
vikramkagitapu
 
CH11.ppt
ssuser5c874e
 
Overview of Oracle database12c for developers
Getting value from IoT, Integration and Data Analytics
 
Oracle 12c New Features for Developers
CompleteITProfessional
 
12 things about Oracle 12c
Connor McDonald
 
Aioug vizag oracle12c_new_features
AiougVizagChapter
 
Ad

Recently uploaded (20)

PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Digital Circuits, important subject in CS
contactparinay1
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 

Write Less (code) With More (Oracle Database 12c New Features)

  • 1. Write Less Code With More Oracle 12c New Features Oren Nakdimon www.db-oriented.com  [email protected]  +972-54-4393763 @DBoriented © Oren Nakdimon
  • 2. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon WHO AM I? A CHRONOLOGY BY “ORACLE YEARS” Where: IAF When: Oracle 6/7 [1991-1997] What: Developer Where: Golden Screens When: Oracle 8 [1997-1998] What: Server Group Manager Where: TELEknowledge When: Oracle 8i/9i [1998-2003] What: DBA Group Manager Where: Olista When: Oracle 10g/11g [2004-2011] What: VP R&D + Israel Site Manager Where: When: Oracle 11g/12c [2011-] What: Freelance Consultant Where: When: 2015- What: Database Expert “An expert is a person who has made all the mistakes that can be made in a very narrow field” (Niels Bohr, 1885-1962)
  • 3. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon @DBORIENTED
  • 4. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon HTTP://DB-ORIENTED.COM
  • 5. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon Read a summary of this presentation in the article in Oracle Scene Issue 58 MORE “WRITE LESS WITH MORE” Download this presentation from db-oriented.com/presentations Read the blog post series db-oriented.com/category/writelesswithmore/
  • 6. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon AGENDA  Go over some common use cases  For each one  A pre-12c solution  A 12c solution, that allows us to write less  We’ll see examples for features that allow writing less…  …configuration  …application code  …code in SQL statements  …“inappropriately-located” code 6
  • 7. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon THE USUAL WARNING  Before you decide to apply any of the presented features in production, make sure:  to thoroughly test them for your applications and environments  that you have the required license to use them
  • 8. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon MEET THE DEMO TABLES 8
  • 9. This presentation is available in http://db-oriented.com/presentations 9 1. Fill the PEOPLE table from a text file 2. Set a unique number to ASSIGNMENT_ID (implicitly) for every new record 3. Delete obsolete projects (but keep their assignment history) 4. Add a “validity period” to PROJECT_ASSIGNMENTS 5. Write a query to get project assignments with pagination 1/2 2/2 ©OrenNakdimon
  • 10. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon LOADING DATA FROM FILES  Before 12c  SQL*Loader  Requires a control file  External tables  Require a “control file” within the CREATE TABLE statement  Then: INSERT INTO <“real” table> … SELECT FROM <external table>… ; 10
  • 11. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon SQL*LOADER EXPRESS MODE  No need to create a control file  Defaults  File name: <tableName>.dat, in current directory  Record delimiter: newline  Field delimiter: comma  No enclosures 11 12c @ldr
  • 12. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon SQL*LOADER EXPRESS MODE  Mandatory command line parameter  TABLE  Some optional command line parameters  DATA (up to 6 names, wildcards supported)  TERMINATED_BY  CHARACTERSET  CSV = WITH_EMBEDDED  OPTIONALLY_ENCLOSED_BY  DATE_FORMAT  DEGREE_OF_PARALLELISM  DIRECT 12 12c
  • 13. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon SQL*LOADER EXPRESS MODE  A log file is generated for future use, including:  Control file  CREATE EXTERNAL TABLE statement  INSERT statement  Data types of all table columns should be numeric, string or datetime 13 12c
  • 14. This presentation is available in http://db-oriented.com/presentations 14 1. Fill the PEOPLE table from a text file 2. Set a unique number to ASSIGNMENT_ID (implicitly) for every new record 3. Delete obsolete projects (but keep their assignment history) 4. Add a “validity period” to PROJECT_ASSIGNMENTS 5. Write a query to get project assignments with pagination 1/2 2/2 ©OrenNakdimon
  • 15. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon AUTO-INCREMENT COLUMNS  Before 12c  A sequence + a BEFORE INSERT trigger 15 CREATE SEQUENCE project_assignments_seq; -- -- first option: ignore the input even if supplied -- CREATE TRIGGER project_assignments_bir_tr BEFORE INSERT ON project_assignments FOR EACH ROW BEGIN :new.assignment_id := project_assignments_seq.nextval; END;
  • 16. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon AUTO-INCREMENT COLUMNS  Before 12c  A sequence + a BEFORE INSERT trigger 16 CREATE SEQUENCE project_assignments_seq; -- -- second option: only if input not supplied -- CREATE TRIGGER project_assignments_bir_tr BEFORE INSERT ON project_assignments FOR EACH ROW WHEN (new.assignment_id IS NULL) BEGIN :new.assignment_id := project_assignments_seq.nextval; END;
  • 17. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon IDENTITY COLUMNS  A column can be defined as “identity”  Implicit sequence  Implicit NOT NULL  GENERATED…  [always] as identity  by default as identity  by default on null as identity  You need the CREATE SEQUENCE privilege 17 @idn1 12c
  • 18. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon IDENTITY COLUMNS  Configuring the implicit sequence  Like in CREATE SEQUENCE  START WITH LIMIT VALUE  Restrictions  Only for numeric data types  Maximum one identity column per table  Non-identity column cannot be modified to identity column  CTAS ignores the identity definition 18 @idn2 12c
  • 19. This presentation is available in http://db-oriented.com/presentations 19 1. Fill the PEOPLE table from a text file 2. Set a unique number to ASSIGNMENT_ID (implicitly) for every new record 3. Delete obsolete projects (but keep their assignment history) 4. Add a “validity period” to PROJECT_ASSIGNMENTS 5. Write a query to get project assignments with pagination 1/2 2/2 ©OrenNakdimon
  • 20. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon LOGICAL DELETION OF RECORDS 20
  • 21. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon LOGICAL DELETION OF RECORDS  Before 12c  Add an IS_DELETED column  Use a view to hide “deleted” records 21 ALTER TABLE projects ADD is_deleted NUMBER(1) DEFAULT 0 NOT NULL CHECK (is_deleted IN (0,1)); CREATE VIEW projects AS SELECT * FROM all_projects WHERE is_deleted=0; RENAME projects TO all_projects;
  • 22. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon In-Database Archiving  Enables to archive rows within a table by marking them as “inactive”  Inactive rows are still there, but are not visible to the application (or visible, when we want) 22 @idar 12c
  • 23. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon In-Database Archiving  The table should be defined as ROW ARCHIVAL  A hidden column is created: ORA_ARCHIVE_STATE  The session level parameter ROW ARCHIVAL VISIBILITY controls if archived rows are visible (ALL) or not (ACTIVE) 23 12c
  • 24. This presentation is available in http://db-oriented.com/presentations 24 1. Fill the PEOPLE table from a text file 2. Set a unique number to ASSIGNMENT_ID (implicitly) for every new record 3. Delete obsolete projects (but keep their assignment history) 4. Add a “validity period” to PROJECT_ASSIGNMENTS 5. Write a query to get project assignments with pagination 1/2 2/2 ©OrenNakdimon
  • 25. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon VALIDITY PERIODS  Before 12c  Add PERIOD_START and PERIOD_END columns  Add conditions to queries to filter only valid periods 25
  • 26. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon TEMPORAL VALIDITY  Enables to track time periods for real world validity or effectivity  Valid times can be set by users/application for data  Data can be selected by a specific valid time (or range) 26 12c
  • 27. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon TEMPORAL VALIDITY  A valid time period consists of two date/time columns (start/end)  The columns can be created explicitly or implicitly  The table is defined with PERIOD FOR 27 12c @vld
  • 28. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon TEMPORAL VALIDITY  Statement level visibility control  SELECT … AS OF PERIOD FOR  SELECT … VERSIONS PERIOD FOR  Session level visibility control  DBMS_FLASHBACK_ARCHIVE.enable_at_valid_time  ALL  CURRENT  ASOF 28 12c
  • 29. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon THERE ARE TWO SIDES TO EVERY COIN  Hidden columns  Hidden predicates  “Hidden” =“Easy to Forget”  “Hidden” ≠“Can be Ignored”  And session-level control is session-level control In Database Archiving Temporal Validity SELECT * FROM PROJECTS; where most of the rows are “archived” DELETE PROJECT_ASSIGNMENTS; where enable_at_valid_time is on
  • 30. This presentation is available in http://db-oriented.com/presentations 30 1. Fill the PEOPLE table from a text file 2. Set a unique number to ASSIGNMENT_ID (implicitly) for every new record 3. Delete obsolete projects (but keep their assignment history) 4. Add a “validity period” to PROJECT_ASSIGNMENTS 5. Write a query to get project assignments with pagination 1/2 2/2 ©OrenNakdimon
  • 31. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon TOP-N AND PAGINATION QUERIES  Before 12c  Use inline views and ROWNUM or ROW_NUMBER( )  Queries become quite cumbersome 31
  • 32. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon TOP-N AND PAGINATION QUERIES  All the records  Top 8 records  Records 5-8 32 SELECT project_id, person_id, assignment_id, validity_period_start, validity_period_end FROM ( SELECT x.* ,rownum row_num FROM ( SELECT project_id, person_id, assignment_id, validity_period_start, validity_period_end FROM project_assignments ORDER BY project_id, person_id ) x WHERE rownum <= 8 ) WHERE row_num > 4;
  • 33. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon ROW LIMITING  The Row Limiting clause is added to the end of SELECT statement  FETCH FIRST n ROWS WITH TIES  FETCH FIRST n ROWS ONLY  FETCH FIRST p PERCENT ROWS  OFFSET m ROWS FETCH NEXT n ROWS 33 12c @topn
  • 34. This presentation is available in http://db-oriented.com/presentations 1/2 34 6. Write a procedure to update the status of multiple projects 7. Write a query that shows the number of assignments per project in the last days 2/2 8. Write a query that shows all the people with a valid date in their GENERAL_INFO column ©OrenNakdimon
  • 35. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon  Write a stored procedure that  Gets a collection parameter of “project updates”, each one with:  PROJECT_ID  UPDATE_TIME  STATUS  Updates the PROJECTS table with the latest status of each project 35 PROJECT_ID STATUS 1 2 2 3 3 4 4 2 3 2 4 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2 PROJECT ID UPDATE TIME STATUS 1 10:00 2 1 14:00 3 2 09:00 1 3 09:00 4 2 08:00 3 3 08:00 2 2 10:00 2
  • 36. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon 36 CREATE OR REPLACE PACKAGE projects_dl AS TYPE proj_update_t IS RECORD( project_id projects.project_id%TYPE, update_time DATE, status projects.status%TYPE); TYPE proj_update_tt IS TABLE OF proj_update_t; PROCEDURE update_status( i_proj_update_list IN proj_update_tt ); END projects_dl;
  • 37. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon 37 CREATE OR REPLACE PACKAGE BODY projects_dl AS PROCEDURE update_status( i_proj_update_list IN proj_update_tt) IS BEGIN MERGE INTO projects p USING ( SELECT project_id, MAX(status) keep(dense_rank LAST ORDER BY update_time) latest_status FROM TABLE(i_proj_update_list) GROUP BY project_id ) i ON (p.project_id = i.project_id) WHEN MATCHED THEN UPDATE SET p.status = i.latest_status; END update_status; END projects_dl;
  • 38. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon SELECT FROM COLLECTION VARIABLES  Before 12c  Impossible to SELECT FROM package-level collections 38 PL/SQL: SQL Statement ignored ORA-22905: cannot access rows from a non-nested table item PLS-00642: local collection types not allowed in SQL statements
  • 39. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon SELECT FROM COLLECTION VARIABLES  Before 12c  Schema-level collection types must be created and used, even if needed only in the scope of a single package 39 CREATE OR REPLACE TYPE proj_update_t AS OBJECT ( project_id INTEGER, update_time DATE, status INTEGER) CREATE TYPE proj_update_tt AS TABLE OF proj_update_t CREATE OR REPLACE PACKAGE projects_dl AS PROCEDURE update_status( i_proj_update_list IN proj_update_tt ); END projects_dl;
  • 40. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon SELECT FROM COLLECTION VARIABLES  Now it is possible to SELECT FROM package-level collections 40 12c
  • 41. This presentation is available in http://db-oriented.com/presentations 1/2 41 6. Write a procedure to update the status of multiple projects 7. Write a query that shows the number of assignments per project in the last days 2/2 8. Write a query that shows all the people with a valid date in their GENERAL_INFO column ©OrenNakdimon
  • 42. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon PROJECT NAME LAST DAYS TO SHOW IN REPORTS Project A 2 Project B 3 Project C 4 PROJECT NAME DATE NUM OF ASSIGNMENTS Project A 02/03/2016 3 Project A 03/03/2016 3 Project B 01/03/2016 5 Project B 02/03/2016 4 Project B 03/03/2016 2 Project C 29/02/2016 1 Project C 01/03/2016 0 Project C 02/03/2016 0 Project C 03/03/2016 1 @ltrl
  • 43. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon LATERAL INLINE VIEWS  An inline view in a query can be defined as LATERAL  It allows it to refer to other tables that appear to its left in the FROM clause 43 12c
  • 44. This presentation is available in http://db-oriented.com/presentations 1/2 44 6. Write a procedure to update the status of multiple projects 7. Write a query that shows the number of assignments per project in the last days 2/2 8. Write a query that shows all the people with a valid date in their GENERAL_INFO column ©OrenNakdimon
  • 45. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon CALLING PL/SQL FROM SQL  Before 12c  A PL/SQL function must be stored in the database  And it must be public (either standalone or exposed in a package spec) 45
  • 46. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon CALLING PL/SQL FROM SQL  Before 12c 46 CREATE OR REPLACE FUNCTION is_date(i_info IN VARCHAR2) RETURN NUMBER AS l_date DATE; BEGIN IF i_info IS NULL THEN RETURN 0; ELSE l_date := to_date(i_info, 'dd/mm/yyyy'); RETURN 1; END IF; EXCEPTION WHEN OTHERS THEN RETURN 0; END; SELECT * FROM people WHERE is_date(general_info) = 1;
  • 47. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon PL/SQL in the WITH Clause  Before 12c, the WITH clause included only subquery factoring  In 12c, it can include also PL/SQL declarations  Functions, that can be used in the query  Procedures, that can be used in the functions  Name resolution  statement-level function names have precedence over schema-level stored functions 47 12c @with
  • 48. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon PL/SQL in the WITH Clause  Not yet supported in static SQL in PL/SQL  If used as subquery, the top-level statement must be hinted with WITH_PLSQL (or be itself a SELECT with PL/SQL declaration)  Considerations for statement-level vs. schema- level functions  Ad-hoc vs. common functionality  Performance  Consider also PRAGMA UDF 48 12c
  • 49. This presentation is available in http://db-oriented.com/presentations 49 6. Write a procedure to update the status of multiple projects 7. Write a query that shows the number of assignments per project in the last days 2/2 8. Write a query that shows all the people with a valid date in their GENERAL_INFO column 1. Fill the PEOPLE table from a text file 2. Set a unique number to ASSIGNMENT_ID (implicitly) for every new record 3. Delete obsolete projects (but keep their assignment history) 4. Add a “validity period” to PROJECT_ASSIGNMENTS 5. Write a query to get project assignments with pagination 1/2 ©OrenNakdimon
  • 50. THANK YOU Oren Nakdimon www.db-oriented.com  [email protected]  +972-54-4393763 @DBoriented © Oren Nakdimon