SlideShare une entreprise Scribd logo
1  sur  97
Télécharger pour lire hors ligne
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Introduction
                                           Embedded SQL
               Application Development
                                           ODBC
                          SQL Facilities
                                           JDBC
                                           References


Embedded SQL




                          ¸    ¨ g¨ u u
          H. Turgut Uyar, Sule O˘ud¨c¨     Database Systems
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

Contenu connexe

En vedette

The Dynamite of Next Generation (Y) Attack
The Dynamite of Next Generation (Y) AttackThe Dynamite of Next Generation (Y) Attack
The Dynamite of Next Generation (Y) AttackPrathan Phongthiproek
 
Worshipping_Rituals
Worshipping_RitualsWorshipping_Rituals
Worshipping_RitualsNeha Sharma
 
Codesign Athens Course Gentes
Codesign Athens Course GentesCodesign Athens Course Gentes
Codesign Athens Course GentesAnnie Gentes
 
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012TDR d.o.o Rovinj
 
Latino voters in Arizona and SB1070
Latino voters in Arizona and SB1070Latino voters in Arizona and SB1070
Latino voters in Arizona and SB1070Latino Decisions
 
Kahvakuulaharjoittelun Perusteet 2010
Kahvakuulaharjoittelun Perusteet 2010Kahvakuulaharjoittelun Perusteet 2010
Kahvakuulaharjoittelun Perusteet 2010Marko Suomi
 
Acta agrupamento hostelería vigo (emenda)
Acta agrupamento hostelería vigo (emenda)Acta agrupamento hostelería vigo (emenda)
Acta agrupamento hostelería vigo (emenda)oscargaliza
 

En vedette (20)

Hulu
HuluHulu
Hulu
 
1 15210 89532
1 15210 895321 15210 89532
1 15210 89532
 
Triangle Gives Back October Webinar
Triangle Gives Back October WebinarTriangle Gives Back October Webinar
Triangle Gives Back October Webinar
 
The Art of Grey-Box Attack
The Art of Grey-Box AttackThe Art of Grey-Box Attack
The Art of Grey-Box Attack
 
The Operation CloudBurst Attack
The Operation CloudBurst AttackThe Operation CloudBurst Attack
The Operation CloudBurst Attack
 
Tablas merchan
Tablas merchanTablas merchan
Tablas merchan
 
The Dynamite of Next Generation (Y) Attack
The Dynamite of Next Generation (Y) AttackThe Dynamite of Next Generation (Y) Attack
The Dynamite of Next Generation (Y) Attack
 
การเงิน การธนาคารและการคลัง
การเงิน การธนาคารและการคลังการเงิน การธนาคารและการคลัง
การเงิน การธนาคารและการคลัง
 
Worshipping_Rituals
Worshipping_RitualsWorshipping_Rituals
Worshipping_Rituals
 
Codesign Athens Course Gentes
Codesign Athens Course GentesCodesign Athens Course Gentes
Codesign Athens Course Gentes
 
Peri porsi
Peri porsiPeri porsi
Peri porsi
 
Convegno Ordine dei dottori commercialisti di Catania - Personal Branding
Convegno Ordine dei dottori commercialisti di Catania - Personal BrandingConvegno Ordine dei dottori commercialisti di Catania - Personal Branding
Convegno Ordine dei dottori commercialisti di Catania - Personal Branding
 
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
 
TEMA 1A Vocabulary
TEMA 1A VocabularyTEMA 1A Vocabulary
TEMA 1A Vocabulary
 
Digi Conv
Digi ConvDigi Conv
Digi Conv
 
Pajaros
PajarosPajaros
Pajaros
 
Madness
MadnessMadness
Madness
 
Latino voters in Arizona and SB1070
Latino voters in Arizona and SB1070Latino voters in Arizona and SB1070
Latino voters in Arizona and SB1070
 
Kahvakuulaharjoittelun Perusteet 2010
Kahvakuulaharjoittelun Perusteet 2010Kahvakuulaharjoittelun Perusteet 2010
Kahvakuulaharjoittelun Perusteet 2010
 
Acta agrupamento hostelería vigo (emenda)
Acta agrupamento hostelería vigo (emenda)Acta agrupamento hostelería vigo (emenda)
Acta agrupamento hostelería vigo (emenda)
 

Similaire à Application Development SQL Facilities

Java database programming with jdbc
Java database programming with jdbcJava database programming with jdbc
Java database programming with jdbcsriram raj
 
Lap Around Sql Azure
Lap Around Sql AzureLap Around Sql Azure
Lap Around Sql AzureAnko Duizer
 
Modified cloud based sql query processor
Modified cloud based sql query processorModified cloud based sql query processor
Modified cloud based sql query processorSekhar Vrs
 
Sql Azure Sql Saturday Nyc
Sql Azure   Sql Saturday NycSql Azure   Sql Saturday Nyc
Sql Azure Sql Saturday NycAaron King
 
Sql azure data services OData
Sql azure data services ODataSql azure data services OData
Sql azure data services ODataEduardo Castro
 
Rapid SQL Datasheet - The Intelligent IDE for SQL Development
Rapid SQL Datasheet - The Intelligent IDE for SQL DevelopmentRapid SQL Datasheet - The Intelligent IDE for SQL Development
Rapid SQL Datasheet - The Intelligent IDE for SQL DevelopmentEmbarcadero Technologies
 
CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...
CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...
CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...Juarez Junior
 
Sql azure introduction
Sql azure introductionSql azure introduction
Sql azure introductionSuherman .
 
Sql Azure - Columbus SQL PASS
Sql Azure - Columbus SQL PASSSql Azure - Columbus SQL PASS
Sql Azure - Columbus SQL PASSAaron King
 
SQL Azure Federation and Scalability
SQL Azure Federation and ScalabilitySQL Azure Federation and Scalability
SQL Azure Federation and ScalabilityEduardo Castro
 
Owasp Backend Security Project 1.0beta
Owasp Backend Security Project 1.0betaOwasp Backend Security Project 1.0beta
Owasp Backend Security Project 1.0betaSecurity Date
 
Sql Azure - Sql Saturday Chicago
Sql Azure - Sql Saturday ChicagoSql Azure - Sql Saturday Chicago
Sql Azure - Sql Saturday ChicagoAaron King
 
DWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual Threads
DWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ThreadsDWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual Threads
DWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ThreadsJuarez Junior
 

Similaire à Application Development SQL Facilities (20)

SQL Azure Overview
SQL Azure OverviewSQL Azure Overview
SQL Azure Overview
 
Java database programming with jdbc
Java database programming with jdbcJava database programming with jdbc
Java database programming with jdbc
 
Lap Around Sql Azure
Lap Around Sql AzureLap Around Sql Azure
Lap Around Sql Azure
 
Jdbc_ravi_2016
Jdbc_ravi_2016Jdbc_ravi_2016
Jdbc_ravi_2016
 
Modified cloud based sql query processor
Modified cloud based sql query processorModified cloud based sql query processor
Modified cloud based sql query processor
 
Sql Azure Sql Saturday Nyc
Sql Azure   Sql Saturday NycSql Azure   Sql Saturday Nyc
Sql Azure Sql Saturday Nyc
 
Sql azure data services OData
Sql azure data services ODataSql azure data services OData
Sql azure data services OData
 
Why you should(n't) run your databases in the cloud
Why you should(n't) run your databases in the cloudWhy you should(n't) run your databases in the cloud
Why you should(n't) run your databases in the cloud
 
Rapid SQL Datasheet - The Intelligent IDE for SQL Development
Rapid SQL Datasheet - The Intelligent IDE for SQL DevelopmentRapid SQL Datasheet - The Intelligent IDE for SQL Development
Rapid SQL Datasheet - The Intelligent IDE for SQL Development
 
Oracle sql demo
Oracle sql demoOracle sql demo
Oracle sql demo
 
CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...
CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...
CloudLand - Revolutionize Java DB AppDev with Reactive Streams and Virtual Th...
 
Sql azure introduction
Sql azure introductionSql azure introduction
Sql azure introduction
 
Sql Azure - Columbus SQL PASS
Sql Azure - Columbus SQL PASSSql Azure - Columbus SQL PASS
Sql Azure - Columbus SQL PASS
 
Day2
Day2Day2
Day2
 
SQL Azure Federation and Scalability
SQL Azure Federation and ScalabilitySQL Azure Federation and Scalability
SQL Azure Federation and Scalability
 
Owasp Backend Security Project 1.0beta
Owasp Backend Security Project 1.0betaOwasp Backend Security Project 1.0beta
Owasp Backend Security Project 1.0beta
 
Sql Azure - Sql Saturday Chicago
Sql Azure - Sql Saturday ChicagoSql Azure - Sql Saturday Chicago
Sql Azure - Sql Saturday Chicago
 
DWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual Threads
DWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ThreadsDWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual Threads
DWX23 - Revolutionize Java DB AppDev with Reactive Streams and Virtual Threads
 
sohini ppt
sohini pptsohini ppt
sohini ppt
 
Jooq java object oriented querying
Jooq java object oriented queryingJooq java object oriented querying
Jooq java object oriented querying
 

Dernier

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 

Dernier (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 

Application Development SQL Facilities

  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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