97
Application Development SQL Facilities Database Systems Application Development H. Turgut Uyar ¸ Sule ¨ ud¨ uc¨ u 2002-2010 H. Turgut Uyar, ¸ Sule ¨ ud¨ uc¨ u Database Systems

Database Systems - Application Development

Embed Size (px)

DESCRIPTION

Embedded SQL, ODBC, JDBC, stored procedures, triggers, views.

Citation preview

Page 1: Database Systems - Application Development

Application DevelopmentSQL Facilities

Database SystemsApplication Development

H. Turgut Uyar Sule Oguducu

2002-2010

H. Turgut Uyar, Sule Oguducu Database Systems

Page 2: Database Systems - Application Development

Application DevelopmentSQL Facilities

License

c©2002-2010 T. Uyar, S. Oguducu

You are free:

to Share — to copy, distribute and transmit the work

to Remix — to adapt the work

Under the following conditions:

Attribution — You must attribute the work in the manner specified by the author or licensor (but not inany way that suggests that they endorse you or your use of the work).

Noncommercial — You may not use this work for commercial purposes.

Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work onlyunder the same or similar license to this one.

H. Turgut Uyar, Sule Oguducu Database Systems

Page 3: Database Systems - Application Development

Application DevelopmentSQL Facilities

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 4: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 5: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Introduction

using the database language in conjunction with ageneral-purpose programming language

general-purpose language: host language

mismatch between SQL and the host language:

SQL operations on setsiteration constructs in programming languages

H. Turgut Uyar, Sule Oguducu Database Systems

Page 6: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Introduction

using the database language in conjunction with ageneral-purpose programming language

general-purpose language: host language

mismatch between SQL and the host language:

SQL operations on setsiteration constructs in programming languages

H. Turgut Uyar, Sule Oguducu Database Systems

Page 7: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Program Structure

connect

server, database, username, password

as necessary:

execute a queryiterate over the result set

disconnect

H. Turgut Uyar, Sule Oguducu Database Systems

Page 8: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Program Structure

connect

server, database, username, password

as necessary:

execute a queryiterate over the result set

disconnect

H. Turgut Uyar, Sule Oguducu Database Systems

Page 9: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Program Structure

connect

server, database, username, password

as necessary:

execute a queryiterate over the result set

disconnect

H. Turgut Uyar, Sule Oguducu Database Systems

Page 10: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Approaches

application programming interface (API)

embedded SQL

ODBC

language standard interfaces

H. Turgut Uyar, Sule Oguducu Database Systems

Page 11: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Application Programming Interface

using the library functions of the SQL server

pros and cons:

fastnot standard

H. Turgut Uyar, Sule Oguducu Database Systems

Page 12: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Application Programming Interface

using the library functions of the SQL server

pros and cons:

fastnot standard

H. Turgut Uyar, Sule Oguducu Database Systems

Page 13: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

API Example

Example (PostgreSQL - C)

#inc lude < l i b pq−f e . h>

i n t main ( void ){

/∗ connect ∗//∗ e x e cu t e query ∗//∗ d i s c o nn e c t ∗/

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 14: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

API Example

Example (connecting)

/∗ PGconn ∗ conn ; ∗/

conn = PQconnectdb ( ”hos t=l o c a l h o s t dbname=imdb ”” u s e r=i t u c s password=i t u c s ” ) ;

i f ( PQstatus ( conn ) == CONNECTION BAD) {f p r i n t f ( s t d e r r , ”Connect ion f a i l e d .\ n ”) ;e x i t ( 1 ) ;

}/∗ e x e cu t e query ∗/PQf i n i s h ( conn ) ;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 15: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

API Example

Example (executing a query)

/∗ PGre su l t ∗ r e s u l t ; ∗/

s p r i n t f ( query , ”SELECT TITLE ,SCORE”” FROM MOVIE WHERE (YR = %d) ” , y ea r ) ;

r e s u l t = PQexec ( conn , query ) ;i f ( PQre su l t S t a t u s ( r e s u l t ) != PGRES TUPLES OK) {

f p r i n t f ( s t d e r r , ”Query f a i l e d .\ n ”) ;PQclear ( r e s u l t ) ;PQf i n i s h ( conn ) ;e x i t ( 1 ) ;

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 16: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

API Example

Example (processing the result set)

f o r ( i = 0 ; i < PQntuples ( r e s u l t ) ; i++)p r i n t f ( ”%s %s \n ” ,

PQgetva lue ( r e s u l t , i , 0 ) ,PQgetva lue ( r e s u l t , i , 1 ) ) ;

PQclear ( r e s u l t ) ;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 17: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 18: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL

stages:1 mark SQL statements within host language code:

EXEC SQL2 embedded SQL preprocessor:

embedded SQL directives → API calls3 host language compiler

pros and cons:

fast, standarddifficult, does not support most languages

skip Embedded SQL

H. Turgut Uyar, Sule Oguducu Database Systems

Page 19: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL

stages:1 mark SQL statements within host language code:

EXEC SQL2 embedded SQL preprocessor:

embedded SQL directives → API calls3 host language compiler

pros and cons:

fast, standarddifficult, does not support most languages

skip Embedded SQL

H. Turgut Uyar, Sule Oguducu Database Systems

Page 20: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL

H. Turgut Uyar, Sule Oguducu Database Systems

Page 21: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL Standard

sharing variables with the host language

error control

adapting query results

H. Turgut Uyar, Sule Oguducu Database Systems

Page 22: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Variable Sharing

Syntax

EXEC SQL BEGIN DECLARE SECTION ;s ha r ed v a r i a b l e sEXEC SQL END DECLARE SECTION ;

in SQL statements: ’:’ in front of host language variables

H. Turgut Uyar, Sule Oguducu Database Systems

Page 23: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Error Control

Error Processing

EXEC SQL WHENEVER{ SQLERROR | SQLWARNING | NOT FOUND }{ STOP | CONTINUE | DO command | GOTO l a b e l }

H. Turgut Uyar, Sule Oguducu Database Systems

Page 24: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Adapting Query Results

Cursors

EXEC SQL DECLARE curso r name CURSOR FOR SELECT . . .EXEC SQL OPEN curso r name ;EXEC SQL FETCH IN curso r name INTO v a r i a b l e s ;EXEC SQL CLOSE curso r name ;

query is not executed when cursor is defined

it is executed when cursor is opened

cursor points to first element in the result set

H. Turgut Uyar, Sule Oguducu Database Systems

Page 25: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Adapting Query Results

Cursors

EXEC SQL DECLARE curso r name CURSOR FOR SELECT . . .EXEC SQL OPEN curso r name ;EXEC SQL FETCH IN curso r name INTO v a r i a b l e s ;EXEC SQL CLOSE curso r name ;

query is not executed when cursor is defined

it is executed when cursor is opened

cursor points to first element in the result set

H. Turgut Uyar, Sule Oguducu Database Systems

Page 26: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL Example

Example (connecting)

EXEC SQL BEGIN DECLARE SECTION ;i n t y r ;char ∗ t i t l e = NULL , ∗ s c o r e = NULL ;EXEC SQL END DECLARE SECTION ;

EXEC SQL CONNECT TO i t u c sUSER i t u c s IDENTIFIED BY i t u c s ;

/∗ p r o c e s s query ∗/

EXEC SQL DISCONNECT;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 27: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL Example

Example (processing query)

s c an f ( ”%d ” , &y r ) ;EXEC SQL DECLARE c que r y CURSOR FOR

SELECT TITLE , SCORE FROM MOVIEWHERE (YR = : y r ) ;

EXEC SQL OPEN c que r y ;

/∗ e x e cu t e query ∗/

EXEC SQL CLOSE c que r y ;EXEC SQL COMMIT;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 28: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Embedded SQL Example

Example (executing query)

EXEC SQL WHENEVER NOT FOUND DO break ;whi le (1 ) {

EXEC SQL FETCH c que r y INTO : t i t l e , : s c o r e ;p r i n t f ( ”%s %s \n ” , t i t l e , s c o r e ) ;t i t l e = s c o r e = NULL ;

}f r e e ( t i t l e ) ;f r e e ( s c o r e ) ;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 29: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 30: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC

ODBC: Open DataBase Connectivity:a service layer between the application and the server

pros and cons:

standardvery slow

H. Turgut Uyar, Sule Oguducu Database Systems

Page 31: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC

ODBC: Open DataBase Connectivity:a service layer between the application and the server

pros and cons:

standardvery slow

H. Turgut Uyar, Sule Oguducu Database Systems

Page 32: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC Architecture

application

driver manager

registers the ODBC driverstransfers requests from application to driver

driver

translates and transfers requests to data source

data source

processes instructions from the driver

H. Turgut Uyar, Sule Oguducu Database Systems

Page 33: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC Architecture

application

driver manager

registers the ODBC driverstransfers requests from application to driver

driver

translates and transfers requests to data source

data source

processes instructions from the driver

H. Turgut Uyar, Sule Oguducu Database Systems

Page 34: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC Architecture

application

driver manager

registers the ODBC driverstransfers requests from application to driver

driver

translates and transfers requests to data source

data source

processes instructions from the driver

H. Turgut Uyar, Sule Oguducu Database Systems

Page 35: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC Architecture

application

driver manager

registers the ODBC driverstransfers requests from application to driver

driver

translates and transfers requests to data source

data source

processes instructions from the driver

H. Turgut Uyar, Sule Oguducu Database Systems

Page 36: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC Example

Example (PHP)

$conn = odbc connect ( ”imdb ” , ” i t u c s ” , ” i t u c s ” ) ;$query = ”SELECT TITLE , SCORE”

” FROM MOVIE WHERE (YR = ” . $yea r . ”) ” ;$ r e s u l t = odbc exec ( $conn , $query ) ;

/∗ p r o c e s s the r e s u l t s e t ∗/

odbc close ( $conn ) ;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 37: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

ODBC Example

Example (processing the result set)

echo ”<t ab l e >\n ”;whi le ( odbc fetch row ( $ r e s u l t ) ) {

$ t i t l e = odbc resu l t ( $ r e s u l t , ” t i t l e ” ) ;$ s c o r e = odbc resu l t ( $ r e s u l t , ”s c o r e ” ) ;echo ”<t r >\n ”;echo ” <td>$ t i t l e </td>\n ”;echo ” <td>$sco re </td>\n ”;echo ”</t r >\n ”;

}echo ”</t ab l e >\n ”;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 38: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 39: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC

JDBC: Java DataBase Connectivity

same architectural concepts as in ODBC

different types of drivers

JDBC URL for connection

jdbc:<subprotocol>:<otherParameters>

matching Java and SQL data types

H. Turgut Uyar, Sule Oguducu Database Systems

Page 40: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC

JDBC: Java DataBase Connectivity

same architectural concepts as in ODBC

different types of drivers

JDBC URL for connection

jdbc:<subprotocol>:<otherParameters>

matching Java and SQL data types

H. Turgut Uyar, Sule Oguducu Database Systems

Page 41: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC

JDBC: Java DataBase Connectivity

same architectural concepts as in ODBC

different types of drivers

JDBC URL for connection

jdbc:<subprotocol>:<otherParameters>

matching Java and SQL data types

H. Turgut Uyar, Sule Oguducu Database Systems

Page 42: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC

JDBC: Java DataBase Connectivity

same architectural concepts as in ODBC

different types of drivers

JDBC URL for connection

jdbc:<subprotocol>:<otherParameters>

matching Java and SQL data types

H. Turgut Uyar, Sule Oguducu Database Systems

Page 43: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Drivers

Type I: bridges

translate into non-native calls (for example ODBC)

Type II: direct translation via non-Java driver

translate into API of data source (for example C++)

Type III: network bridges

connect to middleware server for translating into API of datasource

Type IV: direct translation via Java driver

communicate with DBMS through Java sockets

H. Turgut Uyar, Sule Oguducu Database Systems

Page 44: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Drivers

Type I: bridges

translate into non-native calls (for example ODBC)

Type II: direct translation via non-Java driver

translate into API of data source (for example C++)

Type III: network bridges

connect to middleware server for translating into API of datasource

Type IV: direct translation via Java driver

communicate with DBMS through Java sockets

H. Turgut Uyar, Sule Oguducu Database Systems

Page 45: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Drivers

Type I: bridges

translate into non-native calls (for example ODBC)

Type II: direct translation via non-Java driver

translate into API of data source (for example C++)

Type III: network bridges

connect to middleware server for translating into API of datasource

Type IV: direct translation via Java driver

communicate with DBMS through Java sockets

H. Turgut Uyar, Sule Oguducu Database Systems

Page 46: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Drivers

Type I: bridges

translate into non-native calls (for example ODBC)

Type II: direct translation via non-Java driver

translate into API of data source (for example C++)

Type III: network bridges

connect to middleware server for translating into API of datasource

Type IV: direct translation via Java driver

communicate with DBMS through Java sockets

H. Turgut Uyar, Sule Oguducu Database Systems

Page 47: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Package

import the JDBC package:import java . sql .∗

exception about SQL operations:SQLException

H. Turgut Uyar, Sule Oguducu Database Systems

Page 48: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Package

import the JDBC package:import java . sql .∗

exception about SQL operations:SQLException

H. Turgut Uyar, Sule Oguducu Database Systems

Page 49: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Connection

DriverManager: connection manager

getConnection (static)

Connection: database connection

createStatement

H. Turgut Uyar, Sule Oguducu Database Systems

Page 50: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Connection

DriverManager: connection manager

getConnection (static)

Connection: database connection

createStatement

H. Turgut Uyar, Sule Oguducu Database Systems

Page 51: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Queries

Statement: SQL statement

executeQuery: for query operationsexecuteUpdate: for insert/update/delete operations

PreparedStatement: precompiled SQL statement

can be used multiple timesplaceholder for parameters: ?set value before invoking query

ResultSet: set of query results

next: next element in the result setmethods for getting the data in appropriate type

H. Turgut Uyar, Sule Oguducu Database Systems

Page 52: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Queries

Statement: SQL statement

executeQuery: for query operationsexecuteUpdate: for insert/update/delete operations

PreparedStatement: precompiled SQL statement

can be used multiple timesplaceholder for parameters: ?set value before invoking query

ResultSet: set of query results

next: next element in the result setmethods for getting the data in appropriate type

H. Turgut Uyar, Sule Oguducu Database Systems

Page 53: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Queries

Statement: SQL statement

executeQuery: for query operationsexecuteUpdate: for insert/update/delete operations

PreparedStatement: precompiled SQL statement

can be used multiple timesplaceholder for parameters: ?set value before invoking query

ResultSet: set of query results

next: next element in the result setmethods for getting the data in appropriate type

H. Turgut Uyar, Sule Oguducu Database Systems

Page 54: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

Java and SQL Data Types

SQL type Java class ResultSet method

BIT Boolean getBoolean()

CHAR String getString()

VARCHAR String getString()

DOUBLE Double getDouble()

FLOAT Float getDouble()

INTEGER Integer getInt()

REAL Double getFloat()

DATE java.sql.Date getDate()

TIME java.sql.Time getTime()

TIMESTAMP java.sql.TimeStamp getTimestamp()

H. Turgut Uyar, Sule Oguducu Database Systems

Page 55: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Example

Example (checking the database driver)

t ry {C l a s s . forName ( ”org . p o s t g r e s q l . D r i v e r ” ) ;

} catch ( C la s sNotFoundExcept ion e ) {// PostgreSQL d r i v e r not i n s t a l l e d

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 56: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Example

Example (connecting)

t ry {Connect ion conn = Dr iverManager . g e tConnec t i on (

”jdbc : p o s t g r e s q l : imdb ” , ” i t u c s ” , ” i t u c s ”) ;

} catch ( SQLException e ) {// connec t i on e r r o r

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 57: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Example

Example (executing query)

Statement stmt = conn . c r e a t eS ta t emen t ( ) ;S t r i n g query = ”SELECT TITLE , SCORE FROM MOVIE”

+ ” WHERE (YR = 1990) ”;R e s u l t S e t r e s u l t = stmt . executeQuery ( query ) ;whi le ( r e s u l t . nex t ( ) )

System . out . p r i n t l n ( r e s u l t . g e t S t r i n g (1 )+ ” ” + r e s u l t . g e t F l o a t ( 2 ) ) ;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 58: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

JDBC Example

Example (prepared statement)

S t r i n g query = ”DELETE FROM MOVIE” +” WHERE (SCORE < ?) ”;

PreparedStatement stmt =conn . p r epa r eS ta t ement ( query ) ;

stmt . s e t F l o a t (1 , s c o r e ) ;stmt . executeUpdate ( ) ;

H. Turgut Uyar, Sule Oguducu Database Systems

Page 59: Database Systems - Application Development

Application DevelopmentSQL Facilities

IntroductionEmbedded SQLODBCJDBCReferences

References

Required text: Ramakrishnan, Gehrke

Chapter 6: Database Application Development

6.1. Accessing Databases from Applications6.2. An Introduction to JDBC6.3. JDBC Classes and Interfaces

Supplementary text: Date

Chapter 4: An Introduction to SQL

4.6. Embedded SQL

H. Turgut Uyar, Sule Oguducu Database Systems

Page 60: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 61: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Stored Procedures

implementing functionality in the database server

languages: SQL, PL/SQL, C, ...

not really recommended

not portabledatabase servers are not optimized for business logic

→ implement business logic on the application server

H. Turgut Uyar, Sule Oguducu Database Systems

Page 62: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Stored Procedures

implementing functionality in the database server

languages: SQL, PL/SQL, C, ...

not really recommended

not portabledatabase servers are not optimized for business logic

→ implement business logic on the application server

H. Turgut Uyar, Sule Oguducu Database Systems

Page 63: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Functions

SQL Statement

CREATE FUNCTION f unc t i on name ( [ type [ , . . . ] ] )RETURNS r e t u r n t y p eAS f u n c t i o n bodyLANGUAGE language name

first parameter $1, second parameter $2, ...

H. Turgut Uyar, Sule Oguducu Database Systems

Page 64: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Functions

SQL Statement

CREATE FUNCTION f unc t i on name ( [ type [ , . . . ] ] )RETURNS r e t u r n t y p eAS f u n c t i o n bodyLANGUAGE language name

first parameter $1, second parameter $2, ...

H. Turgut Uyar, Sule Oguducu Database Systems

Page 65: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

SQL Function Example

Example (calculating new score)

$1: old score, $2: old votes, $3: new vote

CREATE FUNCTION NEW SCORE( f l oa t , int , i n t )RETURNS f l o a tAS ’SELECT ( $1∗$2+$3 ) / ( $2+1); ’LANGUAGE ’ s q l ’

H. Turgut Uyar, Sule Oguducu Database Systems

Page 66: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Triggers

Definition

trigger:a function that will be automatically activated on an event

can be useful for maintaining integrity

H. Turgut Uyar, Sule Oguducu Database Systems

Page 67: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Triggers

SQL Statement

CREATE TRIGGER t r i g g e r n ame{ BEFORE | AFTER } { even t [ OR . . . ] }ON tab l e name[ FOR [ EACH ] { ROW | STATEMENT } ]EXECUTE PROCEDURE f unc t i on name ( . . . )

PL/pgSQL:

old: tuple before the operationnew: tuple after the operation

H. Turgut Uyar, Sule Oguducu Database Systems

Page 68: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Triggers

SQL Statement

CREATE TRIGGER t r i g g e r n ame{ BEFORE | AFTER } { even t [ OR . . . ] }ON tab l e name[ FOR [ EACH ] { ROW | STATEMENT } ]EXECUTE PROCEDURE f unc t i on name ( . . . )

PL/pgSQL:

old: tuple before the operationnew: tuple after the operation

H. Turgut Uyar, Sule Oguducu Database Systems

Page 69: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Trigger Example

Example (keep a ”points”column)

CREATE FUNCTION UPDATE MOVIE POINTS( )RETURNS opaqueAS ’BEGIN

new . POINTS = new .SCORE ∗ new .VOTES;RETURN new ;END; ’

LANGUAGE ’ p l p g s q l ’

H. Turgut Uyar, Sule Oguducu Database Systems

Page 70: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Trigger Example

Example (keep a ”points”column)

CREATE TRIGGER UPDATE MOVIEBEFORE INSERT OR UPDATE ON MOVIEFOR EACH ROWEXECUTE PROCEDURE UPDATE MOVIE POINTS( )

H. Turgut Uyar, Sule Oguducu Database Systems

Page 71: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 72: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Views

presenting a derived table like a base table

isolating application programs from changes in databasestructure

denormalizing after database refactoring

H. Turgut Uyar, Sule Oguducu Database Systems

Page 73: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Views

presenting a derived table like a base table

isolating application programs from changes in databasestructure

denormalizing after database refactoring

H. Turgut Uyar, Sule Oguducu Database Systems

Page 74: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Views

SQL Statement

CREATE VIEW view name ASSELECT . . .

the SELECT query will be executed every time the view is used

H. Turgut Uyar, Sule Oguducu Database Systems

Page 75: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Views

SQL Statement

CREATE VIEW view name ASSELECT . . .

the SELECT query will be executed every time the view is used

H. Turgut Uyar, Sule Oguducu Database Systems

Page 76: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

View Example

Example

CREATE VIEW NEW MOVIE ASSELECT ID , TITLE , YR FROM MOVIE

WHERE (YR > 1995)

SELECT ∗ FROM NEW MOVIE

H. Turgut Uyar, Sule Oguducu Database Systems

Page 77: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

View Example

Example

CREATE VIEW NEW MOVIE ASSELECT ID , TITLE , YR FROM MOVIE

WHERE (YR > 1995)

SELECT ∗ FROM NEW MOVIE

H. Turgut Uyar, Sule Oguducu Database Systems

Page 78: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Updating Views

changes have to performed on the base tables

rules are needed

Creating Rules

CREATE RULE ru l e name ASON even t TO view name[ WHERE c o n d i t i o n ]DO [ INSTEAD ] s q l s t a t emen t

H. Turgut Uyar, Sule Oguducu Database Systems

Page 79: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Updating Views

changes have to performed on the base tables

rules are needed

Creating Rules

CREATE RULE ru l e name ASON even t TO view name[ WHERE c o n d i t i o n ]DO [ INSTEAD ] s q l s t a t emen t

H. Turgut Uyar, Sule Oguducu Database Systems

Page 80: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

View Rule Example

Example

UPDATE NEW MOVIE SET TITLE=’ . . ’WHERE ( ID = 1)

CREATE RULE UPDATE TITLE ASON UPDATE TO NEW MOVIEDO INSTEAD

UPDATE MOVIE SET TITLE = new . TITLEWHERE ( ID = o ld . ID )

H. Turgut Uyar, Sule Oguducu Database Systems

Page 81: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

View Rule Example

Example

UPDATE NEW MOVIE SET TITLE=’ . . ’WHERE ( ID = 1)

CREATE RULE UPDATE TITLE ASON UPDATE TO NEW MOVIEDO INSTEAD

UPDATE MOVIE SET TITLE = new . TITLEWHERE ( ID = o ld . ID )

H. Turgut Uyar, Sule Oguducu Database Systems

Page 82: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Snapshots

similar to view but creates a new base table

the query is executed only once when snapshot is created

changes to base tables do not effect the snapshot

no updates on the snapshot

H. Turgut Uyar, Sule Oguducu Database Systems

Page 83: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Snapshots

similar to view but creates a new base table

the query is executed only once when snapshot is created

changes to base tables do not effect the snapshot

no updates on the snapshot

H. Turgut Uyar, Sule Oguducu Database Systems

Page 84: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Creating Snapshots

SQL Statement

CREATE SNAPSHOT snapshot name ASSELECT . . .

H. Turgut Uyar, Sule Oguducu Database Systems

Page 85: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 86: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Permissions

subject: active entities (user, group)

object: passive entities (table, column, view, ...)

owner of object determines permissions of other subjects:discretionary access control

H. Turgut Uyar, Sule Oguducu Database Systems

Page 87: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Permissions

subject: active entities (user, group)

object: passive entities (table, column, view, ...)

owner of object determines permissions of other subjects:discretionary access control

H. Turgut Uyar, Sule Oguducu Database Systems

Page 88: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

SQL Permissions

Granting Permissions

GRANT pe rmi s s i on name [ , . . . ]ON ob jec t name TO sub j ec t name[ WITH GRANT OPTION ]

Revoking Permissions

REVOKE pe rmi s s i on nameON ob jec t name FROM sub j ec t name

H. Turgut Uyar, Sule Oguducu Database Systems

Page 89: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

SQL Permissions

Granting Permissions

GRANT pe rmi s s i on name [ , . . . ]ON ob jec t name TO sub j ec t name[ WITH GRANT OPTION ]

Revoking Permissions

REVOKE pe rmi s s i on nameON ob jec t name FROM sub j ec t name

H. Turgut Uyar, Sule Oguducu Database Systems

Page 90: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Permission Examples

Example (granting permissions on a table)

GRANT SELECT , INSERT , UPDATE ON MOVIE TO ’ i t u c s ’

Example (granting permissions on a view)

GRANT SELECT ON NEW MOVIES TO ’ i t u c s ’

Example (revoking permissions on a table)

REVOKE INSERT ON MOVIE FROM ’ i t u c s ’

H. Turgut Uyar, Sule Oguducu Database Systems

Page 91: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Permission Examples

Example (granting permissions on a table)

GRANT SELECT , INSERT , UPDATE ON MOVIE TO ’ i t u c s ’

Example (granting permissions on a view)

GRANT SELECT ON NEW MOVIES TO ’ i t u c s ’

Example (revoking permissions on a table)

REVOKE INSERT ON MOVIE FROM ’ i t u c s ’

H. Turgut Uyar, Sule Oguducu Database Systems

Page 92: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Permission Examples

Example (granting permissions on a table)

GRANT SELECT , INSERT , UPDATE ON MOVIE TO ’ i t u c s ’

Example (granting permissions on a view)

GRANT SELECT ON NEW MOVIES TO ’ i t u c s ’

Example (revoking permissions on a table)

REVOKE INSERT ON MOVIE FROM ’ i t u c s ’

H. Turgut Uyar, Sule Oguducu Database Systems

Page 93: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Topics

1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC

2 SQL FacilitiesStored ProceduresViewsPermissionsPerformance

H. Turgut Uyar, Sule Oguducu Database Systems

Page 94: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Indexes

some operations require sorting:ORDER BY, DISTINCT, GROUP BY, UNION, ...

creating indexes can speed up queries

slows and insert and update operationsevery key definition creates an index

H. Turgut Uyar, Sule Oguducu Database Systems

Page 95: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Indexes

some operations require sorting:ORDER BY, DISTINCT, GROUP BY, UNION, ...

creating indexes can speed up queries

slows and insert and update operationsevery key definition creates an index

H. Turgut Uyar, Sule Oguducu Database Systems

Page 96: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

Indexes

SQL Statement

CREATE [ UNIQUE ] INDEX index nameON tab l e name ( column name [ , . . . ] )

H. Turgut Uyar, Sule Oguducu Database Systems

Page 97: Database Systems - Application Development

Application DevelopmentSQL Facilities

Stored ProceduresViewsPermissionsPerformanceReferences

References

Required text: Date

Chapter 9: Integrity

9.11. Triggers (a Digression)

Chapter 10: Views

Chapter 17: Security

17.1. Introduction17.6. SQL Facilities

H. Turgut Uyar, Sule Oguducu Database Systems