SlideShare a Scribd company logo
MARIADB
Writing powerful stored
procedures in PL/SQL
Alton Dinsmore
Senior Solution Architect
MariaDB Corporation
Agenda
● Introduction to PL/SQL
○ History and Origin of PL/SQL
○ What is PL/SQL
■ Why Use PL/SQL
○ Evolution of PL/SQL
● MariaDB PL/SQL
○ Why Add PL/SQL to MariaDB
■ Migrations
■ Knowledge of PL/SQL
■ Missing Functions and UDF
○ Creating a Stored Procedure or Function
Agenda
● Advanced PL/SQL Procedures
○ Cursors, LOOP, Variables, Exceptions, and more
○ Packages
● Real World Experiences
○ Where PL/SQL can be problematic
○ PL/SQL and Database Load
● Q&A
Introduction to
PL/SQL
History and Origins of PL/SQL
How did we get here???
● Hierarchical/CODASYL Databases - DL/1, IMS, Total, IDMS
● Inverted List Databases - ADABASE, Model 204, Datacom/DB
○ B-Tree, Hashed, Bit Mapped Indexes
○ 4GL - Natural, User Language, Ideal
● IBM System R Research Project - 1974
● SQL/DS, DB2, Ingres, Informix, Oracle, Sybase
● Each RDBMS release unique Stored Procedure Language
● PL/SQL was released with Oracle V6.0 in July 1988
● Modeled after ADA and Pascal
● Oracle (Relational Software) first contract was with US Government
What is PL/SQL
● PL/SQL is Procedural Language for SQL
● Code for PL/SQL and resides in the Database
● Code is executed in the Database
● Code is normally executed dynamically and it makes calls to internal c routines
● Block Structured
● Easy to Learn Powerful
● Database Triggers Language
● Data never leaves the Database server except for results
Evolution of PL/SQL
● PL/SQL first release was very limited
○ No Stored Procedures
○ Weak Exception Handling
○ Virtually no Error Handling
● Now the foundation of many Oracle developed applications
MariaDB
PL/SQL
Why add PL/SQL to MariaDB
● MariaDB already had an excellent Procedural Language
● Migrations from Oracle to MariaDB
○ Cost Reduction
○ Time Reduction
○ Leverage Skill Sets
● Is It 100% the same - NO
○ Most Major structures are supported
○ Some Functions are Different
○ Easy to extend MariaDB using UDF (User Defined Functions)
Please attend Migration Presentation for more Information
Using PL/SQL in MariaDB
SET SQL_MODE=ORACLE;
Verify by:
SELECT @@SQL_MODE;
|PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KE
Y_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_C
REATE_USER,SIMULTANEOUS_ASSIGNMENT |
SET SQL_MODE=@@GLOBAL.SQL_MODE;
Let’s create a PL/SQL
Stored Procedure !
Creating a PL/SQL Stored Procedure in MariaDB
SET SQL_MODE=ORACLE;
DELIMITER //
BEGIN
Select 'Hello World';
END;
//
DELIMITER ;
What about a PL/SQL
Function?
Creating a PL/SQL Function in MariaDB
SET SQL_MODE=ORACLE;
DELIMITER //
CREATE OR REPLACE FUNCTION hello RETURN VARCHAR2 DETERMINISTIC
AS
BEGIN
DECLARE
vString VARCHAR2(255) := NULL;
BEGIN
SELECT 'Hello world from MariaDB PL/SQL Function!' INTO vString FROM dual;
RETURN vString;
END;
END hello;
//
Now let’s call the PL/SQL
Function?
Calling a PL/SQL Function in MariaDB
DECLARE
vString VARCHAR(255) := NULL;
BEGIN
vString := hello();
SELECT vString;
END;
//
DELIMITER ;
Can we do a PL/SQL
Trigger?
Creating a PL/SQL Trigger in MariaDB
CREATE TRIGGER tr1 BEFORE INSERT ON t1
FOR EACH ROW
DECLARE
cnt INT := 0;
BEGIN
IF :NEW.a IS NULL THEN cnt:=cnt+1; END IF;
IF :NEW.b IS NULL THEN cnt:=cnt+1; END IF;
IF :NEW.c IS NULL THEN :NEW.c:=cnt; END IF;
END;
//
Advance PL/SQL
Procedures
Cursor, Loop, and
Variables in a PL/SQL
Procedure!!!!
Cursors, Loops, and Variables
CREATE or REPLACE PROCEDURE t1demo() as
id int; ** Local Variable Integer
CURSOR c1 IS ** Cursor
SELECT a FROM t1; ** Select can be used instead of
DBMS_OUTPUT
BEGIN
OPEN c1;
LOOP ** Loop thru the records
one at a time
FETCH c1 INTO id;
SELECT id;
END LOOP;
CLOSE c1;
Is there Default
Exceptions???
Exceptions - Predefined
BEGIN
...
EXCEPTION
WHEN TOO_MANY_ROWS THEN ...
WHEN NO_DATA_FOUND THEN ...
WHEN OTHERS THEN NULL;
END;
What about User Defined
Exceptions???
Exceptions - User Defined
CREATE FUNCTION f1 (a INT) RETURN INT
AS
e1 EXCEPTION;
BEGIN
IF a < 0 THEN
RAISE e1;
END IF;
RETURN 0;
EXCEPTION
WHEN e1 THEN RETURN 1;
END;
PL/SQL Packages???
Packages
● Schema object that groups logically related
○ PL/SQL data types,
○ items (e.g. variables, cursors, exceptions)
○ and subprograms.
● Packages usually have two parts:
○ a specification
○ a body
● Sometimes the body is unnecessary.
Packages
CREATE OR REPLACE PACKAGE employee_tools AS
FUNCTION getSalary(eid INT) RETURN DECIMAL(10,2);
PROCEDURE raiseSalary(eid INT, amount DECIMAL(10,2));
PROCEDURE raiseSalaryStd(eid INT);
PROCEDURE hire(ename TEXT, esalary DECIMAL(10,2));
END;
Now for some of the
Package Body!!!!!!
Packages
CREATE PACKAGE BODY employee_tools AS
-- package body variables
stdRaiseAmount DECIMAL(10,2):=500;
-- private routines
PROCEDURE log (eid INT, ecmnt TEXT) AS
BEGIN
INSERT INTO employee_log (id, cmnt) VALUES (eid,
ecmnt);
END;
Packages
-- public routines
PROCEDURE hire(ename TEXT, esalary DECIMAL(10,2)) AS
eid INT;
BEGIN
INSERT INTO employee (name, salary) VALUES (ename,
esalary);
eid:= last_insert_id();
log(eid, 'hire ' || ename);
END;
Packages
FUNCTION getSalary(eid INT) RETURN DECIMAL(10,2) AS
nSalary DECIMAL(10,2);
BEGIN
SELECT salary INTO nSalary FROM employee WHERE
id=eid;
log(eid, 'getSalary id=' || eid || ' salary=' ||
nSalary);
RETURN nSalary;
END;
What about Oracle’s
Default Packages!!!!!!
Default Oracle Packages
SET SQL_MODE=ORACLE;
DELIMITER //
CREATE OR REPLACE PACKAGE DBMS_OUTPUT AS
PROCEDURE PUT_LINE(pString IN VARCHAR2);
END DBMS_OUTPUT;
//
Now the Package Body
Default Oracle Packages
CREATE OR REPLACE PACKAGE BODY DBMS_OUTPUT AS
PROCEDURE PUT_LINE(pString IN VARCHAR2) AS
BEGIN
SELECT pString;
END;
END DBMS_OUTPUT;
//
Now call the Package
Default Oracle Packages
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello world from MariaDB
DBMS_OUTPUT.PUT_LINE!');
END;
//
DELIMITER ;
Real World Experiences
PL/SQL
Where PL/SQL can be Problematic
● PL/SQL is an extension to SQL, not a replacement
● PL/SQL is sequential row by row processing, SQL is
Set Processing
● Pure SQL solution can be faster than SQL with PL/SQL
● Avoid external tables (CONNECT Engine in MariaDB)
PL/SQL and Database Load
● PL/SQL is executed in the Database Kernel
● Causes more CPU requirements in Database Server
● Causes need for Larger System
● Harder Horizontal Scaling
● No way to offload CPU cycles to Client Machines
Q&A
THANK YOU!
Blue Azure
#0E6488
Sea Fresh
#96DDCF
Granite
#424F62
Brand Guidelines
Colors and Type
Open Seas
#2F99A3
Deep Ocean
#003545
Electric Eel
#AC74A
Brand Guidelines
Logos
Icons
Miscellaneous
Icons
Company
MariaDB Server ColumnStore MaxScale
Icons
Miscellaneous (Circle)
Icons
Miscellaneous (Filled)
Icons
Social
Facebook Twitter LinkedIn YouTubeGoogle+

More Related Content

What's hot (20)

PDF
The Complete MariaDB Server tutorial
Colin Charles
 
PDF
The Full MySQL and MariaDB Parallel Replication Tutorial
Jean-François Gagné
 
DOC
Schema replication using oracle golden gate 12c
uzzal basak
 
PDF
データベース技術の羅針盤
Yoshinori Matsunobu
 
PDF
SQL Server パフォーマンスカウンター
Masayuki Ozawa
 
PPTX
Fluids and electrolytes Archer NCLEX content review webinar
Archer Review USMLE and NCLEX
 
PPTX
Sharding Methods for MongoDB
MongoDB
 
PPTX
Azure Data Engineering.pptx
priyadharshini626440
 
PPT
ASM
VINAY PANDEY
 
PDF
Oracle Client Failover - Under The Hood
Ludovico Caldara
 
PDF
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
PPT
LiquiBase
Mike Willbanks
 
PDF
Making Cassandra more capable, faster, and more reliable (at ApacheCon@Home 2...
Scalar, Inc.
 
PDF
Introduction of Oracle Database Architecture
Ryota Watabe
 
PDF
MySQL Replication
Mark Swarbrick
 
PPTX
35- Copy Activity in Azure Data Factory.pptx
BRIJESH KUMAR
 
PDF
MongoDB Backups and PITR
Igor Donchovski
 
PPTX
ここからはじめる SQL Server の状態取得
Masayuki Ozawa
 
PPTX
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Reactive.IO
 
PPSX
Solving the DB2 LUW Administration Dilemma
Randy Goering
 
The Complete MariaDB Server tutorial
Colin Charles
 
The Full MySQL and MariaDB Parallel Replication Tutorial
Jean-François Gagné
 
Schema replication using oracle golden gate 12c
uzzal basak
 
データベース技術の羅針盤
Yoshinori Matsunobu
 
SQL Server パフォーマンスカウンター
Masayuki Ozawa
 
Fluids and electrolytes Archer NCLEX content review webinar
Archer Review USMLE and NCLEX
 
Sharding Methods for MongoDB
MongoDB
 
Azure Data Engineering.pptx
priyadharshini626440
 
Oracle Client Failover - Under The Hood
Ludovico Caldara
 
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
LiquiBase
Mike Willbanks
 
Making Cassandra more capable, faster, and more reliable (at ApacheCon@Home 2...
Scalar, Inc.
 
Introduction of Oracle Database Architecture
Ryota Watabe
 
MySQL Replication
Mark Swarbrick
 
35- Copy Activity in Azure Data Factory.pptx
BRIJESH KUMAR
 
MongoDB Backups and PITR
Igor Donchovski
 
ここからはじめる SQL Server の状態取得
Masayuki Ozawa
 
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Reactive.IO
 
Solving the DB2 LUW Administration Dilemma
Randy Goering
 

Similar to Writing powerful stored procedures in PL/SQL (20)

PDF
MariaDB stored procedures and why they should be improved
Federico Razzoli
 
PDF
Plpgsql russia-pgconf
Pavel Stěhule
 
PDF
How to migrate from Oracle Database with ease
MariaDB plc
 
PDF
Migrations from PLSQL and Transact-SQL - m18
Wagner Bianchi
 
PDF
M|18 Migrating from Oracle and Handling PL/SQL Stored Procedures
MariaDB plc
 
PPTX
ScyllaDB's Avi Kivity on UDF, UDA, and the Future
ScyllaDB
 
PDF
COUG_AAbate_Oracle_Database_12c_New_Features
Alfredo Abate
 
PPTX
Unit 3(rdbms)
Jay Patel
 
PPTX
Unit 3(rdbms)
Jay Patel
 
PDF
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Tim Callaghan
 
PDF
Meet MariaDB 10.3 Debconf 2017
Vicentiu Ciorbaru
 
PDF
2008 Collaborate IOUG Presentation
Biju Thomas
 
PDF
Improving PySpark performance: Spark Performance Beyond the JVM
Holden Karau
 
PDF
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
Cuneyt Goksu
 
PPTX
Oracle 10g
Svetlin Nakov
 
PDF
Introduction to Spark Datasets - Functional and relational together at last
Holden Karau
 
PDF
Fast federated SQL with Apache Calcite
Chris Baynes
 
PPTX
PL/SQL Tips and Techniques Webinar Presentation
Embarcadero Technologies
 
PDF
Stumbling stones when migrating from Oracle
EDB
 
PDF
MariaDB 10.4 New Features
FromDual GmbH
 
MariaDB stored procedures and why they should be improved
Federico Razzoli
 
Plpgsql russia-pgconf
Pavel Stěhule
 
How to migrate from Oracle Database with ease
MariaDB plc
 
Migrations from PLSQL and Transact-SQL - m18
Wagner Bianchi
 
M|18 Migrating from Oracle and Handling PL/SQL Stored Procedures
MariaDB plc
 
ScyllaDB's Avi Kivity on UDF, UDA, and the Future
ScyllaDB
 
COUG_AAbate_Oracle_Database_12c_New_Features
Alfredo Abate
 
Unit 3(rdbms)
Jay Patel
 
Unit 3(rdbms)
Jay Patel
 
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Tim Callaghan
 
Meet MariaDB 10.3 Debconf 2017
Vicentiu Ciorbaru
 
2008 Collaborate IOUG Presentation
Biju Thomas
 
Improving PySpark performance: Spark Performance Beyond the JVM
Holden Karau
 
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
Cuneyt Goksu
 
Oracle 10g
Svetlin Nakov
 
Introduction to Spark Datasets - Functional and relational together at last
Holden Karau
 
Fast federated SQL with Apache Calcite
Chris Baynes
 
PL/SQL Tips and Techniques Webinar Presentation
Embarcadero Technologies
 
Stumbling stones when migrating from Oracle
EDB
 
MariaDB 10.4 New Features
FromDual GmbH
 
Ad

More from MariaDB plc (20)

PDF
MariaDB Berlin Roadshow Slides - 8 April 2025
MariaDB plc
 
PDF
MariaDB München Roadshow - 24 September, 2024
MariaDB plc
 
PDF
MariaDB Paris Roadshow - 19 September 2024
MariaDB plc
 
PDF
MariaDB Amsterdam Roadshow: 19 September, 2024
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - Newpharma
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - Cloud
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - Performance Optimization
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - MaxScale
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - novadys presentation
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - DARVA presentation
MariaDB plc
 
PDF
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB plc
 
PDF
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB plc
 
PDF
Einführung : MariaDB Tech und Business Update Hamburg 2023
MariaDB plc
 
PDF
Hochverfügbarkeitslösungen mit MariaDB
MariaDB plc
 
PDF
Die Neuheiten in MariaDB Enterprise Server
MariaDB plc
 
PDF
Global Data Replication with Galera for Ansell Guardian®
MariaDB plc
 
PDF
Introducing workload analysis
MariaDB plc
 
PDF
Under the hood: SkySQL monitoring
MariaDB plc
 
MariaDB Berlin Roadshow Slides - 8 April 2025
MariaDB plc
 
MariaDB München Roadshow - 24 September, 2024
MariaDB plc
 
MariaDB Paris Roadshow - 19 September 2024
MariaDB plc
 
MariaDB Amsterdam Roadshow: 19 September, 2024
MariaDB plc
 
MariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB plc
 
MariaDB Paris Workshop 2023 - Newpharma
MariaDB plc
 
MariaDB Paris Workshop 2023 - Cloud
MariaDB plc
 
MariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB plc
 
MariaDB Paris Workshop 2023 - Performance Optimization
MariaDB plc
 
MariaDB Paris Workshop 2023 - MaxScale
MariaDB plc
 
MariaDB Paris Workshop 2023 - novadys presentation
MariaDB plc
 
MariaDB Paris Workshop 2023 - DARVA presentation
MariaDB plc
 
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB plc
 
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB plc
 
Einführung : MariaDB Tech und Business Update Hamburg 2023
MariaDB plc
 
Hochverfügbarkeitslösungen mit MariaDB
MariaDB plc
 
Die Neuheiten in MariaDB Enterprise Server
MariaDB plc
 
Global Data Replication with Galera for Ansell Guardian®
MariaDB plc
 
Introducing workload analysis
MariaDB plc
 
Under the hood: SkySQL monitoring
MariaDB plc
 
Ad

Recently uploaded (20)

PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 

Writing powerful stored procedures in PL/SQL

  • 1. MARIADB Writing powerful stored procedures in PL/SQL Alton Dinsmore Senior Solution Architect MariaDB Corporation
  • 2. Agenda ● Introduction to PL/SQL ○ History and Origin of PL/SQL ○ What is PL/SQL ■ Why Use PL/SQL ○ Evolution of PL/SQL ● MariaDB PL/SQL ○ Why Add PL/SQL to MariaDB ■ Migrations ■ Knowledge of PL/SQL ■ Missing Functions and UDF ○ Creating a Stored Procedure or Function
  • 3. Agenda ● Advanced PL/SQL Procedures ○ Cursors, LOOP, Variables, Exceptions, and more ○ Packages ● Real World Experiences ○ Where PL/SQL can be problematic ○ PL/SQL and Database Load ● Q&A
  • 5. History and Origins of PL/SQL How did we get here??? ● Hierarchical/CODASYL Databases - DL/1, IMS, Total, IDMS ● Inverted List Databases - ADABASE, Model 204, Datacom/DB ○ B-Tree, Hashed, Bit Mapped Indexes ○ 4GL - Natural, User Language, Ideal ● IBM System R Research Project - 1974 ● SQL/DS, DB2, Ingres, Informix, Oracle, Sybase ● Each RDBMS release unique Stored Procedure Language ● PL/SQL was released with Oracle V6.0 in July 1988 ● Modeled after ADA and Pascal ● Oracle (Relational Software) first contract was with US Government
  • 6. What is PL/SQL ● PL/SQL is Procedural Language for SQL ● Code for PL/SQL and resides in the Database ● Code is executed in the Database ● Code is normally executed dynamically and it makes calls to internal c routines ● Block Structured ● Easy to Learn Powerful ● Database Triggers Language ● Data never leaves the Database server except for results
  • 7. Evolution of PL/SQL ● PL/SQL first release was very limited ○ No Stored Procedures ○ Weak Exception Handling ○ Virtually no Error Handling ● Now the foundation of many Oracle developed applications
  • 9. Why add PL/SQL to MariaDB ● MariaDB already had an excellent Procedural Language ● Migrations from Oracle to MariaDB ○ Cost Reduction ○ Time Reduction ○ Leverage Skill Sets ● Is It 100% the same - NO ○ Most Major structures are supported ○ Some Functions are Different ○ Easy to extend MariaDB using UDF (User Defined Functions) Please attend Migration Presentation for more Information
  • 10. Using PL/SQL in MariaDB SET SQL_MODE=ORACLE; Verify by: SELECT @@SQL_MODE; |PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KE Y_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_C REATE_USER,SIMULTANEOUS_ASSIGNMENT | SET SQL_MODE=@@GLOBAL.SQL_MODE;
  • 11. Let’s create a PL/SQL Stored Procedure !
  • 12. Creating a PL/SQL Stored Procedure in MariaDB SET SQL_MODE=ORACLE; DELIMITER // BEGIN Select 'Hello World'; END; // DELIMITER ;
  • 13. What about a PL/SQL Function?
  • 14. Creating a PL/SQL Function in MariaDB SET SQL_MODE=ORACLE; DELIMITER // CREATE OR REPLACE FUNCTION hello RETURN VARCHAR2 DETERMINISTIC AS BEGIN DECLARE vString VARCHAR2(255) := NULL; BEGIN SELECT 'Hello world from MariaDB PL/SQL Function!' INTO vString FROM dual; RETURN vString; END; END hello; //
  • 15. Now let’s call the PL/SQL Function?
  • 16. Calling a PL/SQL Function in MariaDB DECLARE vString VARCHAR(255) := NULL; BEGIN vString := hello(); SELECT vString; END; // DELIMITER ;
  • 17. Can we do a PL/SQL Trigger?
  • 18. Creating a PL/SQL Trigger in MariaDB CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW DECLARE cnt INT := 0; BEGIN IF :NEW.a IS NULL THEN cnt:=cnt+1; END IF; IF :NEW.b IS NULL THEN cnt:=cnt+1; END IF; IF :NEW.c IS NULL THEN :NEW.c:=cnt; END IF; END; //
  • 20. Cursor, Loop, and Variables in a PL/SQL Procedure!!!!
  • 21. Cursors, Loops, and Variables CREATE or REPLACE PROCEDURE t1demo() as id int; ** Local Variable Integer CURSOR c1 IS ** Cursor SELECT a FROM t1; ** Select can be used instead of DBMS_OUTPUT BEGIN OPEN c1; LOOP ** Loop thru the records one at a time FETCH c1 INTO id; SELECT id; END LOOP; CLOSE c1;
  • 23. Exceptions - Predefined BEGIN ... EXCEPTION WHEN TOO_MANY_ROWS THEN ... WHEN NO_DATA_FOUND THEN ... WHEN OTHERS THEN NULL; END;
  • 24. What about User Defined Exceptions???
  • 25. Exceptions - User Defined CREATE FUNCTION f1 (a INT) RETURN INT AS e1 EXCEPTION; BEGIN IF a < 0 THEN RAISE e1; END IF; RETURN 0; EXCEPTION WHEN e1 THEN RETURN 1; END;
  • 27. Packages ● Schema object that groups logically related ○ PL/SQL data types, ○ items (e.g. variables, cursors, exceptions) ○ and subprograms. ● Packages usually have two parts: ○ a specification ○ a body ● Sometimes the body is unnecessary.
  • 28. Packages CREATE OR REPLACE PACKAGE employee_tools AS FUNCTION getSalary(eid INT) RETURN DECIMAL(10,2); PROCEDURE raiseSalary(eid INT, amount DECIMAL(10,2)); PROCEDURE raiseSalaryStd(eid INT); PROCEDURE hire(ename TEXT, esalary DECIMAL(10,2)); END;
  • 29. Now for some of the Package Body!!!!!!
  • 30. Packages CREATE PACKAGE BODY employee_tools AS -- package body variables stdRaiseAmount DECIMAL(10,2):=500; -- private routines PROCEDURE log (eid INT, ecmnt TEXT) AS BEGIN INSERT INTO employee_log (id, cmnt) VALUES (eid, ecmnt); END;
  • 31. Packages -- public routines PROCEDURE hire(ename TEXT, esalary DECIMAL(10,2)) AS eid INT; BEGIN INSERT INTO employee (name, salary) VALUES (ename, esalary); eid:= last_insert_id(); log(eid, 'hire ' || ename); END;
  • 32. Packages FUNCTION getSalary(eid INT) RETURN DECIMAL(10,2) AS nSalary DECIMAL(10,2); BEGIN SELECT salary INTO nSalary FROM employee WHERE id=eid; log(eid, 'getSalary id=' || eid || ' salary=' || nSalary); RETURN nSalary; END;
  • 34. Default Oracle Packages SET SQL_MODE=ORACLE; DELIMITER // CREATE OR REPLACE PACKAGE DBMS_OUTPUT AS PROCEDURE PUT_LINE(pString IN VARCHAR2); END DBMS_OUTPUT; //
  • 36. Default Oracle Packages CREATE OR REPLACE PACKAGE BODY DBMS_OUTPUT AS PROCEDURE PUT_LINE(pString IN VARCHAR2) AS BEGIN SELECT pString; END; END DBMS_OUTPUT; //
  • 37. Now call the Package
  • 38. Default Oracle Packages BEGIN DBMS_OUTPUT.PUT_LINE('Hello world from MariaDB DBMS_OUTPUT.PUT_LINE!'); END; // DELIMITER ;
  • 40. Where PL/SQL can be Problematic ● PL/SQL is an extension to SQL, not a replacement ● PL/SQL is sequential row by row processing, SQL is Set Processing ● Pure SQL solution can be faster than SQL with PL/SQL ● Avoid external tables (CONNECT Engine in MariaDB)
  • 41. PL/SQL and Database Load ● PL/SQL is executed in the Database Kernel ● Causes more CPU requirements in Database Server ● Causes need for Larger System ● Harder Horizontal Scaling ● No way to offload CPU cycles to Client Machines
  • 42. Q&A
  • 44. Blue Azure #0E6488 Sea Fresh #96DDCF Granite #424F62 Brand Guidelines Colors and Type Open Seas #2F99A3 Deep Ocean #003545 Electric Eel #AC74A