0
Application Development
                SQL Facilities




             Database Systems
           Application Developmen...
Application Development
                                          SQL Facilities



License



                           ...
Application Development
                                SQL Facilities



Topics

  1   Application Development
        In...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                              Embedded SQL
                  Application Development
        ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                                  Embedded SQL
                      Application Development
...
Introduction
                                                   Embedded SQL
                       Application Developmen...
Introduction
                                                  Embedded SQL
                      Application Development
...
Introduction
                                                  Embedded SQL
                      Application Development
...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                           Embedded SQL
               Application Development
              ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                             Embedded SQL
                 Application Development
          ...
Introduction
                                             Embedded SQL
                 Application Development
          ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                                  Embedded SQL
                      Application Development
...
Introduction
                                                   Embedded SQL
                       Application Developmen...
Introduction
                                                 Embedded SQL
                     Application Development
  ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                                Embedded SQL
                    Application Development
    ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                              Embedded SQL
                  Application Development
        ...
Introduction
                                              Embedded SQL
                  Application Development
        ...
Introduction
                                              Embedded SQL
                  Application Development
        ...
Introduction
                                              Embedded SQL
                  Application Development
        ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Introduction
                                            Embedded SQL
                Application Development
            ...
Introduction
                                                   Embedded SQL
                       Application Developmen...
Introduction
                                                   Embedded SQL
                       Application Developmen...
Introduction
                                                  Embedded SQL
                      Application Development
...
Introduction
                                                   Embedded SQL
                       Application Developmen...
Introduction
                                               Embedded SQL
                   Application Development
      ...
Stored Procedures
                                                 Views
                     Application Development
    ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                 Views
                     Application Development
    ...
Stored Procedures
                                                 Views
                     Application Development
    ...
Stored Procedures
                                                 Views
                     Application Development
    ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                             Views
                 Application Development
            ...
Stored Procedures
                                             Views
                 Application Development
            ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                              Views
                  Application Development
          ...
Stored Procedures
                                              Views
                  Application Development
          ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                                 Views
                     Application Development
    ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                  Views
                      Application Development
  ...
Stored Procedures
                                                  Views
                      Application Development
  ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                 Views
                     Application Development
    ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                               Views
                   Application Development
        ...
Stored Procedures
                                                Views
                    Application Development
      ...
Stored Procedures
                                                Views
                    Application Development
      ...
Upcoming SlideShare
Loading in...5
×

Database Systems - Application Development

533

Published on

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

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
533
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Database Systems - Application Development"

  1. 1. Application Development SQL Facilities Database Systems Application Development H. Turgut Uyar ¨ g¨ u u Sule O˘ud¨c¨ ¸ 2002-2010 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  2. 2. Application Development SQL Facilities License ¸ ¨ g¨ u u c 2002-2010 T. Uyar, S. O˘ud¨c¨ 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 in any 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 only under the same or similar license to this one. ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  3. 3. Application Development SQL Facilities Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  4. 4. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  5. 5. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Introduction using the database language in conjunction with a general-purpose programming language general-purpose language: host language mismatch between SQL and the host language: SQL operations on sets iteration constructs in programming languages ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  6. 6. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Introduction using the database language in conjunction with a general-purpose programming language general-purpose language: host language mismatch between SQL and the host language: SQL operations on sets iteration constructs in programming languages ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  7. 7. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Program Structure connect server, database, username, password as necessary: execute a query iterate over the result set disconnect ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  8. 8. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Program Structure connect server, database, username, password as necessary: execute a query iterate over the result set disconnect ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  9. 9. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Program Structure connect server, database, username, password as necessary: execute a query iterate over the result set disconnect ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  10. 10. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Approaches application programming interface (API) embedded SQL ODBC language standard interfaces ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  11. 11. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Application Programming Interface using the library functions of the SQL server pros and cons: fast not standard ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  12. 12. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Application Programming Interface using the library functions of the SQL server pros and cons: fast not standard ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  13. 13. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References API Example Example (PostgreSQL - C) #i n c l u d e <l i b p q −f e . h> i n t main ( v o i d ) { /∗ c o n n e c t ∗/ /∗ e x e c u t e q u e r y ∗/ /∗ d i s c o n n e c t ∗/ } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  14. 14. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References API Example Example (connecting) /∗ PGconn ∗ conn ; ∗/ conn = PQconnectdb ( ”h o s t=l o c a l h o s t dbname=imdb ” ” u s e r=i t u c s p a s s w o r d=i t u c s ” ) ; i f ( P Q s t a t u s ( conn ) == CONNECTION BAD) { f p r i n t f ( s t d e r r , ”C o n n e c t i o n f a i l e d . n ” ) ; exit (1); } /∗ e x e c u t e q u e r y ∗/ P Q f i n i s h ( conn ) ; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  15. 15. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References API Example Example (executing a query) /∗ P G r e s u l t ∗ r e s u l t ; ∗/ s p r i n t f ( q u e r y , ”SELECT TITLE , SCORE ” ” FROM MOVIE WHERE (YR = %d ) ” , y e a r ) ; r e s u l t = PQexec ( conn , q u e r y ) ; i f ( P Q r e s u 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 ) ; P Q f i n i s h ( conn ) ; exit (1); } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  16. 16. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References API Example Example (processing the result set) f o r ( i = 0 ; i < P Q n t u p l e s ( r e s u l t ) ; i ++) p r i n t f ( ”%s %s n ” , PQgetvalue ( r e s u l t , i , 0) , PQgetvalue ( r e s u l t , i , 1 ) ) ; PQclear ( r e s u l t ) ; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  17. 17. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  18. 18. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL stages: 1 mark SQL statements within host language code: EXEC SQL 2 embedded SQL preprocessor: embedded SQL directives → API calls 3 host language compiler pros and cons: fast, standard difficult, does not support most languages skip Embedded SQL ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  19. 19. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL stages: 1 mark SQL statements within host language code: EXEC SQL 2 embedded SQL preprocessor: embedded SQL directives → API calls 3 host language compiler pros and cons: fast, standard difficult, does not support most languages skip Embedded SQL ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  20. 20. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  21. 21. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL Standard sharing variables with the host language error control adapting query results ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  22. 22. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Variable Sharing Syntax EXEC SQL BEGIN DECLARE SECTION ; shared v a r i a b l e s EXEC SQL END DECLARE SECTION ; in SQL statements: ’:’ in front of host language variables ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  23. 23. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Error Control Error Processing EXEC SQL WHENEVER { SQLERROR | SQLWARNING | NOT FOUND } { STOP | CONTINUE | DO command | GOTO l a b e l } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  24. 24. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Adapting Query Results Cursors EXEC SQL DECLARE c u r s o r n a m e CURSOR FOR SELECT . . . EXEC SQL OPEN c u r s o r n a m e ; EXEC SQL FETCH IN c u r s o r n a m e INTO v a r i a b l e s ; EXEC SQL CLOSE c u r s o r n a m e ; query is not executed when cursor is defined it is executed when cursor is opened cursor points to first element in the result set ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  25. 25. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Adapting Query Results Cursors EXEC SQL DECLARE c u r s o r n a m e CURSOR FOR SELECT . . . EXEC SQL OPEN c u r s o r n a m e ; EXEC SQL FETCH IN c u r s o r n a m e INTO v a r i a b l e s ; EXEC SQL CLOSE c u r s o r n a m e ; query is not executed when cursor is defined it is executed when cursor is opened cursor points to first element in the result set ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  26. 26. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL Example Example (connecting) EXEC SQL BEGIN DECLARE SECTION ; int yr ; 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 s USER i t u c s IDENTIFIED BY i t u c s ; /∗ p r o c e s s q u e r y ∗/ EXEC SQL DISCONNECT ; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  27. 27. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL Example Example (processing query) s c a n f ( ”%d ” , &y r ) ; EXEC SQL DECLARE c q u e r y CURSOR FOR SELECT TITLE , SCORE FROM MOVIE WHERE (YR = : y r ) ; EXEC SQL OPEN c q u e r y ; /∗ e x e c u t e q u e r y ∗/ EXEC SQL CLOSE c q u e r y ; EXEC SQL COMMIT; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  28. 28. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Embedded SQL Example Example (executing query) EXEC SQL WHENEVER NOT FOUND DO break ; while (1) { EXEC SQL FETCH c q u e 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 ; } free ( t i t l e ); free ( score ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  29. 29. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  30. 30. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC ODBC: Open DataBase Connectivity: a service layer between the application and the server pros and cons: standard very slow ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  31. 31. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC ODBC: Open DataBase Connectivity: a service layer between the application and the server pros and cons: standard very slow ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  32. 32. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC Architecture application driver manager registers the ODBC drivers transfers requests from application to driver driver translates and transfers requests to data source data source processes instructions from the driver ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  33. 33. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC Architecture application driver manager registers the ODBC drivers transfers requests from application to driver driver translates and transfers requests to data source data source processes instructions from the driver ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  34. 34. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC Architecture application driver manager registers the ODBC drivers transfers requests from application to driver driver translates and transfers requests to data source data source processes instructions from the driver ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  35. 35. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC Architecture application driver manager registers the ODBC drivers transfers requests from application to driver driver translates and transfers requests to data source data source processes instructions from the driver ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  36. 36. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC Example Example (PHP) $conn = odbc connect ( ”imdb ” , ” i t u c s ” , ” i t u c s ” ) ; $ q u e r y = ”SELECT TITLE , SCORE ” ” FROM MOVIE WHERE (YR = ” . $ y e a r . ”) ” ; $ r e s u l t = odbc exec ( $conn , $ q u e r y ) ; /∗ p r o c e s s t h e r e s u l t s e t ∗/ o d b c c l o s e ( $conn ) ; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  37. 37. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References ODBC Example Example (processing the result set) echo ”<t a b l e >n ” ; w h i l e ( odbc fetch row ( $ r e s u l t ) ) { $ t i t l e = odbc result ( $result , ” t i t l e ”) ; $ s c o r e = o d b c r e s u 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>$ s c o r e </td >n ” ; echo ”</ t r >n ” ; } echo ”</ t a b l e >n ” ; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  38. 38. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  39. 39. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  40. 40. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  41. 41. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  42. 42. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  43. 43. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  44. 44. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  45. 45. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  46. 46. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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 data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  47. 47. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Package import the JDBC package: import java . sql .∗ exception about SQL operations: SQLException ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  48. 48. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Package import the JDBC package: import java . sql .∗ exception about SQL operations: SQLException ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  49. 49. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Connection DriverManager: connection manager getConnection (static) Connection: database connection createStatement ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  50. 50. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Connection DriverManager: connection manager getConnection (static) Connection: database connection createStatement ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  51. 51. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Queries Statement: SQL statement executeQuery: for query operations executeUpdate: for insert/update/delete operations PreparedStatement: precompiled SQL statement can be used multiple times placeholder for parameters: ? set value before invoking query ResultSet: set of query results next: next element in the result set methods for getting the data in appropriate type ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  52. 52. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Queries Statement: SQL statement executeQuery: for query operations executeUpdate: for insert/update/delete operations PreparedStatement: precompiled SQL statement can be used multiple times placeholder for parameters: ? set value before invoking query ResultSet: set of query results next: next element in the result set methods for getting the data in appropriate type ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  53. 53. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Queries Statement: SQL statement executeQuery: for query operations executeUpdate: for insert/update/delete operations PreparedStatement: precompiled SQL statement can be used multiple times placeholder for parameters: ? set value before invoking query ResultSet: set of query results next: next element in the result set methods for getting the data in appropriate type ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  54. 54. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References 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() ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  55. 55. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Example Example (checking the database driver) try { C l a s s . forName ( ”o r g . p o s t g r e s q l . D r i v e r ” ) ; } catch ( C l a s s N o t F o u n d E x c e p t i o n e ) { // PostgreSQL d r i v e r n o t i n s t a l l e d } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  56. 56. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Example Example (connecting) try { C o n n e c t i o n conn = D r i v e r M a n a g e r . g e t C o n n e c t i o n ( ”j d b c : p o s t g r e s q l : imdb ” , ” i t u c s ” , ” i t u c s ” ); } catch ( SQLExce ptio n e ) { // c o n n e c t i o n e r r o r } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  57. 57. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Example Example (executing query) S t a t e m e n t st mt = conn . c r e a t e S t a t e m e n t ( ) ; S t r i n g q u e r y = ”SELECT TITLE , SCORE FROM MOVIE ” + ” WHERE (YR = 1 9 9 0 ) ” ; R e s u l t S e t r e s u l t = stmt . executeQuery ( query ) ; while ( r e s u l t . next ( ) ) System . o u t . p r i n t l n ( r e s u l t . g e t S t r i n g ( 1 ) + ” ” + result . getFloat (2)); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  58. 58. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References JDBC Example Example (prepared statement) S t r i n g q u e r y = ”DELETE FROM MOVIE ” + ” WHERE (SCORE < ? ) ” ; PreparedStatement stmt = conn . p r e p a r e S t a t e m e n t ( q u e r y ) ; stmt . s e t F l o a t (1 , s c o r e ) ; stmt . executeUpdate ( ) ; ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  59. 59. Introduction Embedded SQL Application Development ODBC SQL Facilities JDBC References References Required text: Ramakrishnan, Gehrke Chapter 6: Database Application Development 6.1. Accessing Databases from Applications 6.2. An Introduction to JDBC 6.3. JDBC Classes and Interfaces Supplementary text: Date Chapter 4: An Introduction to SQL 4.6. Embedded SQL ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  60. 60. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  61. 61. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not really recommended not portable database servers are not optimized for business logic → implement business logic on the application server ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  62. 62. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not really recommended not portable database servers are not optimized for business logic → implement business logic on the application server ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  63. 63. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Functions SQL Statement CREATE FUNCTION f u n c t i o n n a m e ( [ type [ , ...]]) RETURNS r e t u r n t y p e AS f u n c t i o n b o d y LANGUAGE l a n g u a g e n a m e first parameter $1, second parameter $2, ... ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  64. 64. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Functions SQL Statement CREATE FUNCTION f u n c t i o n n a m e ( [ type [ , ...]]) RETURNS r e t u r n t y p e AS f u n c t i o n b o d y LANGUAGE l a n g u a g e n a m e first parameter $1, second parameter $2, ... ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  65. 65. Stored Procedures Views Application Development Permissions SQL Facilities Performance References SQL Function Example Example (calculating new score) $1: old score, $2: old votes, $3: new vote CREATE FUNCTION NEW SCORE( f l o a t , i n t , i n t ) RETURNS f l o a t AS ’ SELECT ( $1 ∗ $2+$3 ) / ( $2 +1); ’ LANGUAGE ’ s q l ’ ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  66. 66. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Triggers Definition trigger: a function that will be automatically activated on an event can be useful for maintaining integrity ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  67. 67. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Triggers SQL Statement CREATE TRIGGER t r i g g e r n a m e { BEFORE | AFTER } { e v e n t [ OR . . . ] } ON t a b l e n a m e [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE f u n c t i o n n a m e ( . . . ) PL/pgSQL: old: tuple before the operation new: tuple after the operation ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  68. 68. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Triggers SQL Statement CREATE TRIGGER t r i g g e r n a m e { BEFORE | AFTER } { e v e n t [ OR . . . ] } ON t a b l e n a m e [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE f u n c t i o n n a m e ( . . . ) PL/pgSQL: old: tuple before the operation new: tuple after the operation ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  69. 69. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Trigger Example Example (keep a ”points” column) CREATE FUNCTION UPDATE MOVIE POINTS ( ) RETURNS opaque AS ’ BEGIN new . POINTS = new . SCORE ∗ new . VOTES ; RETURN new ; END ; ’ LANGUAGE ’ p l p g s q l ’ ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  70. 70. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Trigger Example Example (keep a ”points” column) CREATE TRIGGER UPDATE MOVIE BEFORE INSERT OR UPDATE ON MOVIE FOR EACH ROW EXECUTE PROCEDURE UPDATE MOVIE POINTS ( ) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  71. 71. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  72. 72. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Views presenting a derived table like a base table isolating application programs from changes in database structure denormalizing after database refactoring ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  73. 73. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Views presenting a derived table like a base table isolating application programs from changes in database structure denormalizing after database refactoring ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  74. 74. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Views SQL Statement CREATE VIEW view name AS SELECT . . . the SELECT query will be executed every time the view is used ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  75. 75. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Views SQL Statement CREATE VIEW view name AS SELECT . . . the SELECT query will be executed every time the view is used ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  76. 76. Stored Procedures Views Application Development Permissions SQL Facilities Performance References View Example Example CREATE VIEW NEW MOVIE AS SELECT ID , TITLE , YR FROM MOVIE WHERE (YR > 1 9 9 5 ) SELECT ∗ FROM NEW MOVIE ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  77. 77. Stored Procedures Views Application Development Permissions SQL Facilities Performance References View Example Example CREATE VIEW NEW MOVIE AS SELECT ID , TITLE , YR FROM MOVIE WHERE (YR > 1 9 9 5 ) SELECT ∗ FROM NEW MOVIE ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  78. 78. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Updating Views changes have to performed on the base tables rules are needed Creating Rules CREATE RULE r u l e n a m e AS ON e v e n t TO view name [ WHERE c o n d i t i o n ] DO [ INSTEAD ] s q l s t a t e m e n t ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  79. 79. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Updating Views changes have to performed on the base tables rules are needed Creating Rules CREATE RULE r u l e n a m e AS ON e v e n t TO view name [ WHERE c o n d i t i o n ] DO [ INSTEAD ] s q l s t a t e m e n t ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  80. 80. Stored Procedures Views Application Development Permissions SQL Facilities Performance References View Rule Example Example UPDATE NEW MOVIE SET TITLE= ’ . . ’ WHERE ( ID = 1 ) CREATE RULE UPDATE TITLE AS ON UPDATE TO NEW MOVIE DO INSTEAD UPDATE MOVIE SET TITLE = new . TITLE WHERE ( ID = o l d . ID ) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  81. 81. Stored Procedures Views Application Development Permissions SQL Facilities Performance References View Rule Example Example UPDATE NEW MOVIE SET TITLE= ’ . . ’ WHERE ( ID = 1 ) CREATE RULE UPDATE TITLE AS ON UPDATE TO NEW MOVIE DO INSTEAD UPDATE MOVIE SET TITLE = new . TITLE WHERE ( ID = o l d . ID ) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  82. 82. Stored Procedures Views Application Development Permissions SQL Facilities Performance References 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 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  83. 83. Stored Procedures Views Application Development Permissions SQL Facilities Performance References 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 ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  84. 84. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Creating Snapshots SQL Statement CREATE SNAPSHOT s n a p s h o t n a m e AS SELECT . . . ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  85. 85. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  86. 86. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Permissions subject: active entities (user, group) object: passive entities (table, column, view, ...) owner of object determines permissions of other subjects: discretionary access control ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  87. 87. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Permissions subject: active entities (user, group) object: passive entities (table, column, view, ...) owner of object determines permissions of other subjects: discretionary access control ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  88. 88. Stored Procedures Views Application Development Permissions SQL Facilities Performance References SQL Permissions Granting Permissions GRANT p e r m i s s i o n n a m e [ , . . . ] ON o b j e c t n a m e TO s u b j e c t n a m e [ WITH GRANT OPTION ] Revoking Permissions REVOKE p e r m i s s i o n n a m e ON o b j e c t n a m e FROM s u b j e c t n a m e ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  89. 89. Stored Procedures Views Application Development Permissions SQL Facilities Performance References SQL Permissions Granting Permissions GRANT p e r m i s s i o n n a m e [ , . . . ] ON o b j e c t n a m e TO s u b j e c t n a m e [ WITH GRANT OPTION ] Revoking Permissions REVOKE p e r m i s s i o n n a m e ON o b j e c t n a m e FROM s u b j e c t n a m e ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  90. 90. Stored Procedures Views Application Development Permissions SQL Facilities Performance References 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 ’ ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  91. 91. Stored Procedures Views Application Development Permissions SQL Facilities Performance References 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 ’ ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  92. 92. Stored Procedures Views Application Development Permissions SQL Facilities Performance References 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 ’ ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  93. 93. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Facilities Stored Procedures Views Permissions Performance ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  94. 94. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Indexes some operations require sorting: ORDER BY, DISTINCT, GROUP BY, UNION, ... creating indexes can speed up queries slows and insert and update operations every key definition creates an index ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  95. 95. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Indexes some operations require sorting: ORDER BY, DISTINCT, GROUP BY, UNION, ... creating indexes can speed up queries slows and insert and update operations every key definition creates an index ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  96. 96. Stored Procedures Views Application Development Permissions SQL Facilities Performance References Indexes SQL Statement CREATE [ UNIQUE ] INDEX i n d e x n a m e ON t a b l e n a m e ( column name [ , . . . ] ) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  97. 97. Stored Procedures Views Application Development Permissions SQL Facilities Performance References References Required text: Date Chapter 9: Integrity 9.11. Triggers (a Digression) Chapter 10: Views Chapter 17: Security 17.1. Introduction 17.6. SQL Facilities ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×