CS 440 Database Management Systems Stored procedures & OR mapping 1.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Fast Track to ColdFusion 9. Getting Started with ColdFusion Understanding Dynamic Web Pages ColdFusion Benchmark Introducing the ColdFusion Language Introducing.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
Lecture 4: Introduction to PHP 3 PHP & MySQL
1 CS6320 – Why Servlets? L. Grewe 2 What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are Java programs.
Oracle SQL*plus John Ortiz. Lecture 10SQL: Overview2 Overview  SQL: Structured Query Language, pronounced S. Q. L. or sequel.  A standard language for.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Database Management Systems (DBMS)
Session-01. Hibernate Framework ? Why we use Hibernate ?
The Client/Server Database Environment
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
Announcements Read JDBC Project Step 5, due Monday.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Database Technical Session By: Prof. Adarsh Patel.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Python MySQL Database Access
M1G Introduction to Database Development 6. Building Applications.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Object-Oriented Frameworks for Migrating Structured Data April 2004.
Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Effective Test Driven Database Development Gojko Adzic
Databases in Web Devolvement By Andy Larson Using Microsoft Sever 2000 with ASP Database Integrity Inserting data Stored procedures Views Triggers Tips.
Effective Test Driven Database Development Gojko Adzic
Advanced SQL: Cursors & Stored Procedures
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
1 Introduction to Oracle Chapter 1. 2 Before Databases Information was kept in files: Each field describes one piece of information about student Fields.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
XML and Database.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Ch- 8. Class Diagrams Class diagrams are the most common diagram found in modeling object- oriented systems. Class diagrams are important not only for.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
Chapter 5 Introduction To Form Builder. Lesson A Objectives  Display Forms Builder forms in a Web browser  Use a data block form to view, insert, update,
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Introduction – ORM, Helloworld Application
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Introduction to Database Programming with Python Gary Stewart
Chengyu Sun California State University, Los Angeles
Databases.
CS 440 Database Management Systems
CS320 Web and Internet Programming SQL and MySQL
Web Technologies IT230 Dr Mohamed Habib.
The Client/Server Database Environment
CS 440 Database Management Systems
CS3220 Web and Internet Programming SQL and MySQL
Object-Oriented Programming
CS3220 Web and Internet Programming SQL and MySQL
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS 440 Database Management Systems Stored procedures & OR mapping 1

Writing Large DB Programs How to to write large scale programs that use a database? SQL – cannot express all types of queries: e.g., recursive queries. – no support for advanced features, e.g., GUI. Embed SQL in a programming language to write programs that contain many queries – Libraries to submit SQL queries as strings to RDBMS – PHP, Java: JDBC, 2

Problems with Using SQL in PL The programs are hard to debug and maintain Programmers have to run their programs to find SQL compilation bugs. If a programmer adds an attribute to a table, she has to manually find and change many lines in the source code. Communication overhead – queries are sent to RDBMS per each call. 3

Stored Procedures Programming modules written in a procedural language. Stored in the database. Compiled and run by RDBMS. Most RDBMS provide their own language to write stored procedures. – Oracle: PL / SQL – SQL Server: Transact-SQL Some RDBMSs use widely used programming languages – VoltDB: Java 4

Example: PL/SQL Create a stored procedure that inserts the integer numbers between 1 and 100 into table NumOddEven(numVal, oddEven). CREATE OR REPLACE PROCEDURE sample IS BEGIN FOR i IN LOOP IF MOD(i,2) = 0 THEN -- i is even INSERT INTO numOddEven VALUES (i, ‘i is even’); ELSE INSERT INTO numOddEven VALUES (i, ‘i is odd’); END IF; END LOOP; COMMIT; END 5

Example: PL/SQL Called by user for from a program. > Execute sample Users can pass input parameters to stored procedures – For example, the number of values to insert in table NumOddEven. 6

Advantages of Stored Procedures They are more expressive than SQL. It is easy to use SQL in them – SQL queries are parts of the programming language. – many errors are detected in compilation phase. They run faster than using SQL in programming languages such as PHP – minimal communication overhead for submitting queries to RDBMS. and sometimes no communication overhead for results – queries are pre-compiled and may be pre-optimized. 7

Disadvantages of Stored Procedures They do not support some advanced features – e.g., graphical user interface. Each RDBMS has its own. – If we write a program over a RDBMS, we cannot run it over another one. They are harder to write, debug, and maintain than the programs written in Java or C++. 8

When to use Stored Procedures Generally Speaking: – They should be used for portions of the program whose performances matter a lot. ID generation – They should contain minimal amount of business logic keep them simple so they are easy to debug and maintain. 9

Other problems with using SQL in PL Programmers must deal with two different data models – Relational model create table Bars(name varchar(50), addr varchar(100) …) – object-oriented model public class Bars { private String name; private String addr; …} 10

Object Relational Mapping (ORM) ORM tools hide relational model from the programmers (as much as possible) Programmers deal with only object oriented data model. They write small number of SQL queries or no SQL query at all. 11

How an ORM tool work 1.Programmers write their programs in an object oriented language. 2.They let the ORM tool know which objects to store in DB (as tables). 3.The ORM tool generates the SQL queries to create tables and retrieve/manipulate the data. 12

Example Various tools for many programming languages. – Hibernate, Zend, … We use Hibernate ( for Java in our example. 13

Example We like to write a Java program that stores and retrieves information about bars, where each bar has name, address, and license. We write a class in Java for Bar. public class Bars implements Serializable { private String name; private String addr; private String license; …} 14 Interface for persistent classes

Example, cont’d We add setter and getter methods for member variables of the class. public class Bars implements Serializable{ private String name; private String addr; private String license; // Setter and getter methods public void setName(string name){ this.name = name; } public String getName(){ return name; } … } 15

Example, cont’d DBA creates table Bars(name, addr, license) in DB. (or programmer) create table Bars ( name varchar(50), addr varchar(100), license varchar(100) …) DBA writes the relationship between the relation and the Java class in a configuration file – Bars.name  Bars.name – Bars.addr  Bars.addr – Bars.license  Bars.license 16

Example: Cont. try{ SessionFactory factory = new Configur().configure().buildSessionFactory(); Session session = factory.openSession(); Bars JoeBar = new Bars(); JoeBar.setName(“Joe Bar”); JoeBar.setaddr(“12 Madison St. Corvallis,OR,97331”); JoeBar.setLicense(“license324”); session.save(JoeBar); … } catch(Exception ex){…} finally{ session.close(); } 17 Hibernate reads the configuration file, generate the insert query, and inserts a new tuple into the DB.

ORM Configuration file We can describe integrity constraints over a single table – Not NULL – Keys – … We can describe relationships between objects (tables) – Primary key to foreign key constraints – One to one, one to many 18

Advantages of ORM Tools Programmers write minimal amount of SQL queries. – DBA may do most of the RDBMS related work. Our programs will work over various RDBMSs. – Test the program over MySQL, deploy it over Oracle 19

Disadvantages of ORM Tools They are slower than stored procedures – they are outside DB – they may be slower than using SQL in PL – too many function calls. We cannot use customized and fast commands of an RDBMS. They are generally harder to learn than JDBC and PHP – API + configuration files 20

When to Use What There is no exact rule! Generally speaking: – small programs: SQL in programming language (JDBC, PHP) – small/moderate business logic + performance => stored procedures – complex business logic + large scale program: ORM We usually use a mixed of these techniques. – e.g., separate small parts of the program where performance is essential, and make them stored procedures and use ORM for other parts. 21