47

Oracle Database Programming · Oracle Database Product Family Express Edition Edition One Standard Standard Edition Enterprise Edition Non-Oracle developers, open source developers,

  • Upload
    others

  • View
    33

  • Download
    0

Embed Size (px)

Citation preview

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Database Programming

Timo Leppänen Database Architect Oracle Finland Oy October 22, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

3

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Introduction to Oracle and Oracle Database

Programming in Oracle Database

PL/SQL

Developer Tools

Resources

1

2

3

4

4

5

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Introduction to Oracle and Oracle Database

Programming in Oracle Database

PL/SQL

Developer Tools

Resources

1

2

3

4

5

5

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Corporation

• $38.3B in revenue in FY14*

• 400,000 customers in 145 countries

• $34B in R&D since 2004

• $60B on more than 100 acquisitions

• More than 25,000 partners

• More than 120,000 employees

• 18,000 customer support specialists, speaking 29 languages

• 18,000 implementation consultants

• 2.5 million students supported annually

• #1 in 50 product/industry categories

• #2 software company in the world

• #2 cloud company in the world

• More than 15,000 patents worldwide

• 35,000 developers and engineers

• 15 million developers in Oracle online communities

• 900 independent Oracle user groups with 500,000 members

6

Scale Innovation

* GAAP revenue reported in USD as of May 31, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Product Stack

7

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Engineered Systems

Exadata Database Machine

Exalogic Elastic Cloud

Exalytics Big Data Appliance SuperCluster

OLTP, Data Warehouse, DB Consolidation

Web, Bespoke & Packaged

Application Tier

Business Analytics & Planning

Hadoop & Unstructured Data

For Database and Application Workloads

• Reduced management & risk

• One-stop support

Best Platforms for Oracle Software

• Software Lead Architecture

• Faster time to value

• Extreme performance

• Lower TCO

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Database Product Family Express

Edition Standard

Edition One

Standard

Edition

Enterprise

Edition

Non-Oracle developers, open source

developers, new DBAs, students,

non-Oracle ISVs, hw vendors

Low-price option for SMB/LOB

Deployments, ISVs who need a

supported Oracle database

Full-featured database for SMBs with

optional clustering support (up to 4

CPUs)

Large-scale Enterprises that

demand high-performance BI (ETL,

DW, OLTP), security, scalability,

availability, etc.

FREE

< 20 users < 400 users 400-1000 users > 1000 users

Uses 1 CPU

< 4GB DB size

1 instance per CPU

Use up to 1GB RAM

2 CPUs

< 500GB DB size

Single or clustered up to 4 CPUs

< 500 GB DB size

4+ CPUs

> 500 GB DB size

Free OTN

Community Forum

Fee-based Support

available

Fee-based Support

available

Fee-based Support

available

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Enterprise Edition Feature Highlights

This is not an exhaustive list of the features factored in Enterprise Edition. Please see the license documentation for a complete list.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Database EE Options and Packs

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Database Market Share

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Introduction to Oracle and Oracle Database

Programming in Oracle Database

PL/SQL

Developer Tools

Resources

1

2

3

4

13

5

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Client-Server Connectivity

• OCI (C), OCCI (C++)

• JDBC, UCP, JPublisher

• .NET

• PHP OCI8

• Ruby & Python

• Perl

• …

In Database

• Java

• PL/SQL

• APEX & REST Data Services

• R

• More coming …

14

Applications and Oracle Database

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Application Express

• Oracle’s primary tool for developing Web-based applications using SQL and PL/SQL

• Database-centric development tool for personal, Intranet, and Internet applications

• Develop Desktop and Mobile applications

• 100% Browser-based Development and Runtime

• Declarative framework for rapid application definition, maintenance, and enhancements.

• Application scalability inherited from the Oracle Database

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Application Express Architectural Overview

• Simple 2-Tier Architecture

• Pages dynamically rendered using database metadata

• No code generation or file based compilation

• Runs everywhere Oracle Database runs

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Strategy for R Provide high-performance, scalable R environment tightly integrated with Oracle RDBMS and Hadoop

• For R users

• Provide full access to Database and Hadoop objects, while using standard R interfaces

• Deliver high-performance and scalability for all R operations

• Deliver distributed parallel and scalable machine learning algorithms from R

• Deploy R scripts and store R calculation results in Database or Hadoop

• For Database and Big Data developers, using standard interfaces:

Execute embedded R scripts containing any R algorithm or calculation

Access stored R results in Database or Hadoop

Retrieve R computation results in graphical formats like XML or PNG

Integrate R results into BI Applications

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Advanced Analytics

• R-SQL Transparency Framework intercepts R functions for scalable in-database execution

• Function intercept for data transforms, statistical functions and advanced analytics

• Interactive display of graphical results and flow control as in standard R

• Submit entire R scripts for execution by database

• Scale to large datasets

• Access tables, views, and external tables, as well as data through DB LINKS

• Leverage database SQL parallelism

• Leverage new and existing in-database statistical and data mining capabilities

R Engine Other R

packages

Oracle R Enterprise packages

User R Engine on desktop

• Database can spawn multiple R engines for database-managed parallelism

• Efficient data transfer to spawned R engines

• Emulate map-reduce style algorithms and applications

• Enables “lights-out” execution of R scripts

1 User tables

Oracle Database SQL

Results

Database Compute Engine

2 R Engine Other R

packages

Oracle R Enterprise packages

R Engine(s) spawned by Oracle DB

R

Results

3

?x

R Open Source

R Enterprise Compute Engines R

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL, PL/SQL, and Java Interaction

19

SQL PL/SQL Java

JDBC

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Java

JDBC Calls

SQL

Data Shipping/Caching vs. Function Shipping

Why Java in Oracle Database

Java

Multiple Roundtrips

Java SE/EE in Mid-Tier

SQL

Java in the Database

Up to 10 times faster!

Java + any language

1 Roundtrip

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Rationales for Java in the Database

• Why Java?

– #1 programming language (TIOBE index)

• Java VM

– Ensures Java Portability

– Not just Java, any language that compiles to Java byte codes http://en.wikipedia.org/wiki/List_of_JVM_languages

• Why in the database

– In-place data processing

– Java methods as triggers and stored procedures

– Ability to extend database capabilities with Java libraries

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Why Not Only PL/SQL

• PL/SQL is better for direct manipulation of SQL types

• Java more effective for algorithmic (data logic)

• There are more Java developers

• Java has superior built-in functionalities

• Java has a wide range of libraries

• There are things you cannot do (easily) in PL/SQL

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Example: Java Stored Procedures

23

2. Load 1. Create/reuse

public class HelloWorld { public static String hello() { return ”Hello, world!”; } }

3. Publish

CREATE FUNCTION hello RETURN varchar2 AS LANGUAGE JAVA NAME ’HelloWorld.hello() return java.lang.String’;

4. Use

SELECT hello FROM dual; DECLARE greeting varchar2(256); BEGIN greeting := hello; … END;

loadjava -user usr/pwd HelloWorld.java

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Introduction to Oracle and Oracle Database

Programming in Oracle Database

PL/SQL

Developer Tools

Resources

1

2

3

4

24

5

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Why Use PL/SQL

• It is a ‘real’ language

– It is not a scripting language

– It is not a ‘toy’, it is used to code ‘real’ things

• It is the API to the database

• If your goal is to procedurally process data (after ensuring a single SQL statement cannot do your work!) then PL/SQL is simply the most productive language to do so

– SQL data types are PL/SQL data types (consider NUMBER(38) & Java)

– Tight coupling between the two languages

– Code short cuts (implicit cursors)

– Protected from many database changes

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

PL/SQL Architecture

• Two engines

– SQL engine in database server for SQL

– PL/SQL engine for PL/SQL • Resides either in DB or client (Forms)

• PL/SQL engine = Virtual Machine – In-memory

– M-code instructions

26

PL/S

QL

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

PL/SQL Language

• PL/SQL = ”Procedural Language extension to SQL”

• Ada-like syntax

• Block-structured

• PL/SQL syntax contains SQL, but

– SQL dialect of PL/SQL is not exactly same as SQL dialect of DB • SELECT count(*) INTO v_sum FROM …

• All database objects (tables, views, columns, …) are “inherently” visible in PL/SQL

– No special syntax when referring to a PL/SQL or DB object

– Precedence of column names > precedence of variable names !

27

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

[DECLARE

…]

BEGIN

[EXCEPTION

…]

END;

• DECLARE (optional): declaration of types, constants, variables, cursors, etc.

• BEGIN: start of executable section

– Can contain any number of PL/SQL and SQL statements

• EXCEPTION (optional): exception handling

• END: end of the block

• Blocks can be nested

PL/SQL Block Structure (Anonymous Block)

28

DECLARE

v_count NUMBER;

BEGIN

SELECT count(*)

INTO v_count

FROM t;

DBMS_OUTPUT.

PUT_LINE(

'Count='||

v_count);

END;

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Procedure

PROCEDURE myProc

IS

[-- declarations]

BEGIN

-- statements

[EXCEPTION

-- error handling]

END;

Function

FUNCTION myFunc

RETURN someType

IS

[-- declarations]

BEGIN

-- statements

[EXCEPTION

-- error handling]

END;

Anonymous

[DECLARE

-- declarations]

BEGIN

-- statements

[EXCEPTION

-- error handling]

END;

Block Types

29

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

• Almost same as database data types

• Scalar:

– SQL data types

– REF CURSOR, BOOLEAN, PLS_INTEGER, BINARY_INTEGER

– User-define subtypes

• Collections (associative array, VARRAY, nested table)

• RECORD

• %TYPE and %ROWTYPE attribute to refer type of another object

DECLARE

v_flag BOOLEAN := false;

v_act VARCHAR2(16);

SUBTYPE Balance IS NUMBER(8,2);

v_bal Balance NOT NULL := 0.0;

TYPE arrType IS TABLE OF NUMBER

INDEX BY VARCHAR(32);

v_arr arrType;

TYPE pointType IS RECORD (

X NUMBER NOT NULL := 0,

Y NUMBER NOT NULL DEFAULT 0

);

v_point pointType;

v_amount v_bal%TYPE := 0.0;

v_email hr.employees.email%TYPE;

v_emp hr.employees%ROWTYPE;

Data Types

30

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

• SQL single row functions

• SQL statements

• Control structures:

– IF – THEN – ELIF – ELSE – END IF;

– CASE – WHEN – ELSE – END CASE;

– LOOP – EXIT [WHEN] – END LOOP;

– WHILE – LOOP – END LOOP;

– FOR – IN – LOOP – END LOOP;

– CONTINUE

BEGIN

SELECT balance

INTO v_bal

FROM accounts

WHERE acct_no = '1234567890' FOR UPDATE;

v_bal := v_bal +

CASE v_act WHEN 'deposit' THEN v_amount

WHEN 'withdrawal' THEN –v_amount

ELSE 0

END;

IF v_bal < 0 THEN

DBMS_OUTPUT.PUT_LINE('No money!');

ELSE

CASE WHEN v_bal < 10000

THEN v_lvl := 'bronze';

WHEN v_bal < 100000

THEN v_lvl := 'silver';

ELSE v_lvl := 'gold';

END CASE;

FOR i IN 1..TRUNC(v_bal/100) LOOP

DBMS_OUTPUT.PUT_LINE('Century bill');

END LOOP;

END IF;

Procedural Statements

31

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Explicit DECLARE

CURSOR c_emp IS SELECT last_name

FROM employees;

v_name employees.last_name%TYPE;

BEGIN

OPEN c_emp;

LOOP

FETCH c_emp INTO v_name;

EXIT WHEN c_emp%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(v_name);

END LOOP;

CLOSE c_emp;

END;

Implicit BEGIN

FOR item IN

(SELECT last_name FROM employees)

LOOP

DBMS_OUTPUT.PUT_LINE(item.last_name);

END LOOP;

END;

Cursors

32

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

CREATE OR REPLACE PROCEDURE del_emp(

p_id IN employees.employee_id%TYPE,

p_ok OUT BOOLEAN)

IS BEGIN

DELETE FROM employees

WHERE employee_id = p_id;

IF SQL%ROWCOUNT = 0 THEN

p_ok := FALSE;

ELSE

p_ok := TRUE;

END IF;

END del_emp;

DECLARE

v_ok BOOLEAN;

BEGIN

del_emp(1234,p_ok => v_ok);

END;

CREATE FUNCTION get_emp_count RETURN NUMBER

IS

v_count NUMBER;

BEGIN

SELECT COUNT(*)

INTO v_count

FROM employees;

RETURN v_count;

END;

SELECT get_emp_count FROM dual;

Procedures and Functions

33

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

• Package is a collection of logically related PL/SQL types, variables, and subprograms

• Package specification = interface

• Package body = implementation

– May contain private objects

CREATE OR REPLACE PACKAGE p AS

v_x NUMBER := 0;

PROCEDURE setX(newX NUMBER);

END p;

/

CREATE OR REPLACE PACKAGE BODY p AS

PROCEDURE setX(newX NUMBER) IS

BEGIN

v_x := newX;

END;

END;

BEGIN

p.setX(123);

DBMS_OUTPUT.PUT_LINE('X='||p.v_x);

END;

Packages

34

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

PL/SQL Dependencies

• PL/SQL objects may depend on SQL objects (views/tables/columns) or other PL/SQL objects

• Change in any object invalidates the objects that depends on it

– Needs recompilation to validate again

• Edition-based redefinition allows upgrades without breaking runnign system

35

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

• In 12cR1 265+ documented packages

• For example:

– DBMS_AQ* - Advanced Queue

– DBMS_DATA_MINING* - Data Mining

– DBMS_CUBE* - OLAP Cubes

– DBMS_DDL - Some DDL for PL/SQL procedures

– DBMS_FLASHBACK* - Flash back the DB

– DBMS_JAVA - RDBMS functionality for Java

– DBMS_JOB - Job queue management

– DBMS_LDAP* - LDAP API for PL/SQL

– DBMS_LOCK - Lock management services

– DBMS_OUTPUT - Messages to/from PL/SQL

– DBMS_PROFILER - PL/SQL performance profiler

– DBMS_SCHEDULER - Scheduler functionality

– DBMS_SERVICE - DB service management

– DBMS_XA - XA API for PL/SQL (2PC)

– DBMS_XML* - XML functionality for PL/SQL

– OWA_* - HTTP server functionality

– SDO_* - Spatial data functionality

– SEM_* - RDF data functionality

– UTL_FILE - File I/O

– UTL_MAIL - eMail sending

– UTL_TCP - TCP/IP functionality

Oracle Supplied PL/SQL Packages

36

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Introduction to Oracle and Oracle Database

Programming in Oracle Database

PL/SQL

Developer Tools

Resources

1

2

3

4

37

5

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle SQL Developer Overview

de facto Oracle IDE/GUI

3,300,000+ users worldwide

Windows, OS X, *NIX

Installed with Oracle Database

MOS via DB license

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Support for Other Oracle Database Products

Times Ten

Data Miner

XML DB

Spatial & Graph

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL Developer Data Modeling Included for free, also available as a separate download/program

Strategy and Analysis

Relational

Database Design

Data Type

Star Schema Physical

Multidimensional

ERD DFD

Logical

Reporting

Import Models

Domains

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Introduction to Oracle and Oracle Database

Programming in Oracle Database

PL/SQL

Developer Tools

Resources

1

2

3

4

42

5

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Technology Network (OTN)

• Technical information about all Oracle products

• Documentation, download, …

• http://www.oracle.com/technetwork/indexes/products/index.html

http://www.oracle.com/technetwork/index.html

43

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Oracle Learning Library

• Oracle by Example (OBE) hands-ons

• Training videos

• Free tutorials

https://apex.oracle.com/pls/apex/f?p=44785:1

44

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Free Oracle Application Express Workspace

• Obtain free APEX workspace and learn about APEX

• See also http://www.oracle.com/technetwork/developer-tools/apex/application-express/apex-getting-started-1863613.html

http://apex.oracle.com/

45

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 46