SQL Stored Routines Draft 2017-06-09
Total Page:16
File Type:pdf, Size:1020Kb
SQL Stored Routines Draft 2017-06-09 www.DBTechNet.org SQL Stored Routines Procedural Extensions of SQL and External Routines in Transactional Context Authors: Martti Laiho, Fritz Laux, Ilia Petrov, Dimitris Dervos Parts of application logic can be stored at database server-side as “stored routines”, that is as stored procedures, user-defined-functions, and triggers. The client-side application logic makes use of these routines in the context of database transactions, except that the application does not have control over triggers, but triggers control the data access actions of the application. The tutorial introduces the procedural extension of the SQL language, the concepts of stored routines in SQL/PSM of ISO SQL standard, and the implementations in some mainstream DBMS products available for hands-on experimentations in the free virtual database laboratory of DBTechNet.org. licensed on Creative Commons license for non-commercial use SQL Stored Routines www.DBTechNet.org SQL Stored Routines - Procedural Extensions of SQL in Transactional Context This is a companion tutorial to the “SQL Transactions – Theory and hands-on labs” of DBTech VET project available at www.dbtechnet.org/download/SQL-Transactions_handbook_EN.pdf . For more information or corrections please email to martti.laiho(at)gmail.com. You can contact us also via the LinkedIn group DBTechNet. Disclaimer Don’t believe what you read . This tutorial may provide you some valuable technology details, but you need to verify everything by yourself! Things may change in every new software version. The writers don’t accept any responsibilities on damages resulting from this material. The trademarks of mentioned products belong to the trademark owners. Permission is granted to copy this material as long as the original authors are mentioned. Acknowledgements This material is not funded by EU LLP programme, but without the EU LLP projects and the inspiring co- operation with the DBTechNet community and friends this tutorial would never have been written. Special thanks to Ken North on raising the topics to us, Peter Gulutzan and Trudy Pelzer for their exciting books and presentations at the SQL 2012 workshop in Helia, to Riitta Blomster, Timo Leppänen, and to Dr. Malcolm Crowe for their contributions to this paper. Preface The SQL Transactions handbook was aimed for basic training on database transactions at the vocational education level and for self-study by application developers. The book was limited to Client/Server data access using SQL only, and more advanced topics were left out. The purpose of this tutorial is to fill the gap and present these topics for interested learners. With database transactions in mind, we move to the server-side topics of stored procedures, user-defined functions, and triggers extending the built-in constraints of SQL. We look at what ISO SQL says, and experiment with the SQL dialects of free DBMS products which we have in our free virtual database laboratory available from www.dbtechnet.org/download/DebianDBVM06_4.zip. The appendices extend the introduction to even more advanced topics and proceed to client-side application development calling the stored procedures from Java/JDBC. Our examples are minimalistic, and the coverage of the topics is only on introductory level. However, we hope that learners get at least an overview of these exciting topics and get interested in more information on these rich topics. The learners are encouraged to get the product manuals available on the websites of the product vendors and experiment with their products. All examples have been tested in our virtual database lab, but don’t trust on what you read – verify things yourself. licensed on Creative Commons license for non-commercial use SQL Stored Routines Contents Part 1 ISO SQL/PSM ..................................................................................................................................... 1 1.1 Stored Procedures .............................................................................................................................. 2 SQL/PSM procedure language ............................................................................................................... 6 Exceptions and Condition handlers ....................................................................................................... 7 Benefits of stored procedures ............................................................................................................. 10 1.2 Stored Functions ............................................................................................................................... 11 1.3 Triggers ............................................................................................................................................. 14 Part 2 Implementations of Stored Routines .............................................................................................. 18 2.1 DB2 SQL PL ....................................................................................................................................... 20 Simple myProc and myFun tests .......................................................................................................... 22 Exception handling and BankTransfer examples ................................................................................. 24 Triggers ................................................................................................................................................. 26 RVV example on optimistic locking ...................................................................................................... 26 2.2 Oracle PL/SQL ................................................................................................................................... 27 Simple myProc and myFun tests .......................................................................................................... 29 Exception handling and BankTransfer examples ................................................................................. 32 On Oracle Triggers ............................................................................................................................... 34 RVV example on optimistic locking ...................................................................................................... 35 2.3 SQL Server Transact-SQL ................................................................................................................... 37 Simple myProc and myFun tests .......................................................................................................... 37 Exception handling and BankTransfer examples ................................................................................. 39 Triggers and RVV example on optimistic locking ................................................................................. 40 2.4 MySQL/InnoDB ................................................................................................................................. 41 Simple myProc and myFun tests .......................................................................................................... 41 Trigger workaround for the missing support of CHECK constraints: ................................................... 42 Exception handling and BankTransfer examples ................................................................................. 43 Triggers ................................................................................................................................................. 44 RVV example on optimistic locking ...................................................................................................... 44 2.5 PostgreSQL PL/pgSQL ....................................................................................................................... 45 Simple myProc and myFun tests .......................................................................................................... 45 Exception handling and BankTransfer examples ................................................................................. 46 Triggers and RVV example on optimistic locking ................................................................................. 47 2.6 Pyrrho DBMS ..................................................................................................................................... 47 licensed on Creative Commons license for non-commercial use SQL Stored Routines Simple myProc and myFun tests .......................................................................................................... 48 Exception handling and BankTransfer examples ................................................................................. 49 Triggers and RVV example on optimistic locking ................................................................................. 51 Part 3 Concurrency Problems due to Side-effects ..................................................................................... 54 3.1 Side-effects due to Foreign Keys ...................................................................................................... 54 SQL Server Transact-SQL ...................................................................................................................... 55 DB2 SQL PL ........................................................................................................................................... 56 Oracle PL/SQL ......................................................................................................................................