database systems - application development
Post on 26-Jan-2015
934 Views
Preview:
DESCRIPTION
TRANSCRIPT
Application DevelopmentSQL
References
Database SystemsApplication Development
H. Turgut Uyar Sule Oguducu
2002-2014
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
License
c© 2002-2014 T. Uyar, S. OguducuYou are free to:
Share – copy and redistribute the material in any medium or format
Adapt – remix, transform, and build upon the material
Under the following terms:
Attribution – You must give appropriate credit, provide a link to the license,and indicate if changes were made.
NonCommercial – You may not use the material for commercial purposes.
ShareAlike – If you remix, transform, or build upon the material, you mustdistribute your contributions under the same license as the original.
For more information:https://creativecommons.org/licenses/by-nc-sa/4.0/
Read the full license:
https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Introduction
using the database language in conjunctionwith a general-purpose programming language
general-purpose language: host language
mismatch between SQL and the host language:
sets in SQLobjects and iteration constructs in the host language
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Introduction
using the database language in conjunctionwith a general-purpose programming language
general-purpose language: host language
mismatch between SQL and the host language:
sets in SQLobjects and iteration constructs in the host language
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Program Structure
connect
server, database, username, password
run statements as needed:
query operations return result sets → iterate over rowsupdate operations return number of affected rows
disconnect
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Program Structure
connect
server, database, username, password
run statements as needed:
query operations return result sets → iterate over rowsupdate operations return number of affected rows
disconnect
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Program Structure
connect
server, database, username, password
run statements as needed:
query operations return result sets → iterate over rowsupdate operations return number of affected rows
disconnect
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Approaches
application programming interface (API)
embedded SQL
ODBC
language standard interfaces
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Application Programming Interface
using the library functions of the SQL server
pros: fast
cons: specific to SQL product
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
API Example: PostgreSQL - C
#include <libpq-fe.h>
int main(int argc, char *argv[]){
/* connect *//* execute query *//* disconnect */
}
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
API Example: Connection
/* PGconn *conn; */conn = PQconnectdb("host=localhost dbname=imdb"
" user=itucs password=itucs");if (PQstatus(conn) == CONNECTION_BAD){
fprintf(stderr, "Connection failed.\n");exit(1);
}
/* execute query */
PQfinish(conn);
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
API Example: Query Execution
sprintf(query, "SELECT TITLE, SCORE"" FROM MOVIE WHERE (YR = %d)", year);
/* PGresult *result; */result = PQexec(conn, query);if (PQresultStatus(result) != PGRES_TUPLES_OK){
fprintf(stderr, "Query failed.\n");PQclear(result);PQfinish(conn);exit(1);
}
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
API Example: Result Set
for (i = 0; i < PQntuples(result); i++){
title = PQgetvalue(result, i, 0);score = PQgetvalue(result, i, 1);...
}
PQclear(result);
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL
mark SQL statements within host language code: EXEC SQL
preprocessor: embedded SQL directives → API calls
pros: fast, standard
cons: difficult, does not support most languages
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL
mark SQL statements within host language code: EXEC SQL
preprocessor: embedded SQL directives → API calls
pros: fast, standard
cons: difficult, does not support most languages
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL
skip Embedded SQL
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL
variable sharing
EXEC SQL BEGIN DECLARE SECTION;-- shared variablesEXEC SQL END DECLARE SECTION;
error control
EXEC SQL WHENEVER{ SQLERROR | SQLWARNING | NOT FOUND }{ STOP | CONTINUE | DO command | GOTO label }
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL
variable sharing
EXEC SQL BEGIN DECLARE SECTION;-- shared variablesEXEC SQL END DECLARE SECTION;
error control
EXEC SQL WHENEVER{ SQLERROR | SQLWARNING | NOT FOUND }{ STOP | CONTINUE | DO command | GOTO label }
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Query Results
cursors for iterating over query results
EXEC SQL DECLARE cursor_name CURSOR FORSELECT ...;
EXEC SQL OPEN cursor_name;EXEC SQL FETCH IN cursor_name INTO variables;EXEC SQL CLOSE cursor_name;
query is executed when cursor is opened
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL Example: Connect
EXEC SQL BEGIN DECLARE SECTION;int year;char *title = NULL, *score = NULL;EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT TO imdbUSER itucs IDENTIFIED BY itucs;
/* process query */
EXEC SQL DISCONNECT;
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL Example: Cursor
scanf("%d", &year);EXEC SQL DECLARE c_query CURSOR FOR
SELECT TITLE, SCORE FROM MOVIEWHERE (YR = :year);
EXEC SQL OPEN c_query;
/* execute query */
EXEC SQL CLOSE c_query;EXEC SQL COMMIT;
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Embedded SQL Example: Query
EXEC SQL WHENEVER NOT FOUND DO break;while (1){
EXEC SQL FETCH c_query INTO :title, :score;...
}
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
ODBC
ODBC: Open DataBase Connectivitya service layer between the application and the server
pros: standard
cons: slow
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
ODBC Architecture
application
driver manager
registers the ODBC driverstransfers requests from application to driver
driver
translates and transfers requests to data source
data source
processes instructions from the driver
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
ODBC Example: PHP
$conn = odbc_connect("imdb", "itucs", "itucs");$query = "SELECT TITLE, SCORE FROM MOVIE"
. " WHERE (YR = " . $year . ")";$result = odbc_exec($conn, $query);
/* process the result set */
odbc_close($conn);
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
ODBC Example: PHP
echo "<table>\n";while (odbc_fetch_row($result)) {
$title = odbc_result($result, "title");$score = odbc_result($result, "score");echo "<tr>\n";echo " <td>$title</td>\n";echo " <td>$score</td>\n";echo "</tr>\n";
}echo "</table>\n";
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC
JDBC: Java DataBase Connectivity
same architectural concepts as in ODBC
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
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 serverfor translating into API of data source
Type IV: direct translation via Java driver
communicate with DBMS through Java sockets
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
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 serverfor translating into API of data source
Type IV: direct translation via Java driver
communicate with DBMS through Java sockets
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
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 serverfor translating into API of data source
Type IV: direct translation via Java driver
communicate with DBMS through Java sockets
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
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 serverfor translating into API of data source
Type IV: direct translation via Java driver
communicate with DBMS through Java sockets
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Flow
get a connection object:Connection conn = DriverManager.getConnection();
create a statement object on the connectionStatement stmt = conn.createStatement();
execute the query
select: ResultSet results = stmt.executeQuery(query);insert, update, delete: stmt.executeUpdate(query);
process the results
close resources which are no longer needed(result sets, statements, connections)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Flow
get a connection object:Connection conn = DriverManager.getConnection();
create a statement object on the connectionStatement stmt = conn.createStatement();
execute the query
select: ResultSet results = stmt.executeQuery(query);insert, update, delete: stmt.executeUpdate(query);
process the results
close resources which are no longer needed(result sets, statements, connections)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Flow
get a connection object:Connection conn = DriverManager.getConnection();
create a statement object on the connectionStatement stmt = conn.createStatement();
execute the query
select: ResultSet results = stmt.executeQuery(query);insert, update, delete: stmt.executeUpdate(query);
process the results
close resources which are no longer needed(result sets, statements, connections)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Flow
get a connection object:Connection conn = DriverManager.getConnection();
create a statement object on the connectionStatement stmt = conn.createStatement();
execute the query
select: ResultSet results = stmt.executeQuery(query);insert, update, delete: stmt.executeUpdate(query);
process the results
close resources which are no longer needed(result sets, statements, connections)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Flow
get a connection object:Connection conn = DriverManager.getConnection();
create a statement object on the connectionStatement stmt = conn.createStatement();
execute the query
select: ResultSet results = stmt.executeQuery(query);insert, update, delete: stmt.executeUpdate(query);
process the results
close resources which are no longer needed(result sets, statements, connections)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Processing Results
ResultSet is an iterator
whether there are more rows: .hasNext()proceed to the next row: .next()
convert and transfer data in the row to variables
by column name: .getXXX(name)by column order: .getXXX(order)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Data Type Conversions
SQL type Java class ResultSet method
BIT Boolean .getBoolean()CHAR String .getString()VARCHAR String .getString()FLOAT Float .getDouble()INTEGER Integer .getInt()DATE java.sql.Date .getDate()TIME java.sql.Time .getTime()TIMESTAMP java.sql.TimeStamp .getTimestamp()
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Driver
try {Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {// PostgreSQL driver not installed
}
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Connect
try {conn = DriverManager.getConnection(
"jdbc:postgresql:imdb", "itucs", "itucs");
} catch (SQLException e) {// connection error
}
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Insert
query = "INSERT INTO MOVIE (TITLE, YR)"+ " VALUES (’Casablanca’, 1942)";
stmt = conn.createStatement();stmt.executeUpdate(query);stmt.close();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Security
don’t trust inputs from outside sources
Example
name = ...; // get from outside sourcequery = "INSERT INTO Students (NAME)"
+ " VALUES (’" + name + "’)";stmt = conn.createStatement();stmt.executeUpdate(query);stmt.close();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Little Bobby Tables
http://xkcd.com/327/
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Prepared Statements
prepared statements can be reused by changing parameter values
creating:PreparedStatement stmt = conn.prepareStatement(query);
placeholder for parameters in query: ?
values must be set before executing:stmt.setXXX(order, value);
executing:ResultSet results = stmt.executeQuery();stmt.executeUpdate();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Prepared Statement
query = "INSERT INTO MOVIE (TITLE, YR) VALUES (?, ?)";stmt = conn.prepareStatement(query);for (Movie movie : getMovies()) {
stmt.setString(1, movie.getTitle());stmt.setInt(2, movie.getYear());stmt.executeUpdate();
}stmt.close();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Result Set
query = String.format("SELECT TITLE FROM MOVIE WHERE (YR = %d)", year
);stmt = conn.createStatement();results = stmt.executeQuery(query);while (results.next()) {
title = results.getString("TITLE");System.out.println("Title: " + title),
}results.close();stmt.close();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Delete
query = "DELETE FROM MOVIE WHERE (ID = ?)";stmt = conn.prepareStatement(query);stmt.setInt(1, movie.getId());stmt.executeUpdate();stmt.close();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Update
query = "UPDATE MOVIE SET YR = ? WHERE (ID = ?)";stmt = conn.prepareStatement(query);stmt.setInt(1, movie.getYear());stmt.setInt(2, movie.getId());stmt.executeUpdate();stmt.close();
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
JDBC Example: Close Resources
Statement stmt = ...;ResultSet results = ...;try {
results = stmt.executeQuery(query);...
} catch (SQLException e) {...
} finally {results.close();stmt.close();
}
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
IntroductionEmbedded SQLODBCJDBC
Auto-Generated Identity Values
query = "INSERT INTO ... ";stmt = conn.prepareStatement(query,
Statement.RETURN_GENERATED_KEYS);stmt.executeUpdate();
ResultSet ids = stmt.getGeneratedKeys();// assuming there is one and only one resultids.next();int id = ids.getInt(1);
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Stored Procedures
implementing functionality in the database server
languages: SQL, PL/SQL, C, ...
not portable
not scalable
database servers are not optimized for business logic
not recommended→ implement business logic on the application server
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Stored Procedures
implementing functionality in the database server
languages: SQL, PL/SQL, C, ...
not portable
not scalable
database servers are not optimized for business logic
not recommended→ implement business logic on the application server
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Stored Procedures
implementing functionality in the database server
languages: SQL, PL/SQL, C, ...
not portable
not scalable
database servers are not optimized for business logic
not recommended→ implement business logic on the application server
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Stored Procedures
implementing functionality in the database server
languages: SQL, PL/SQL, C, ...
not portable
not scalable
database servers are not optimized for business logic
not recommended→ implement business logic on the application server
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Stored Procedures
implementing functionality in the database server
languages: SQL, PL/SQL, C, ...
not portable
not scalable
database servers are not optimized for business logic
not recommended→ implement business logic on the application server
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Functions
creating a function
CREATE FUNCTIONfunction_name(parameter_type [, ...])RETURNS return_typeAS function_bodyLANGUAGE language_name
first parameter $1, second parameter $2, ...
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
SQL Function Example
calculating new score:$1: old score, $2: old votes, $3: new vote
CREATE FUNCTION NEW_SCORE(float, int, int)RETURNS floatAS ’SELECT ($1 * $2 + $3) / ($2 + 1);’LANGUAGE ’sql’
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Triggers
trigger: a function that will be automatically invoked on an event
can be useful for maintaining integrity
creating a trigger:
CREATE TRIGGER trigger_name{ BEFORE | AFTER } { event [ OR ... ] }ON table_name[ FOR [ EACH ] { ROW | STATEMENT } ]EXECUTE PROCEDURE function_name(...)
PL/pgSQL:
old: tuple before the operationnew: tuple after the operation
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Triggers
trigger: a function that will be automatically invoked on an event
can be useful for maintaining integrity
creating a trigger:
CREATE TRIGGER trigger_name{ BEFORE | AFTER } { event [ OR ... ] }ON table_name[ FOR [ EACH ] { ROW | STATEMENT } ]EXECUTE PROCEDURE function_name(...)
PL/pgSQL:
old: tuple before the operationnew: tuple after the operation
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Trigger Example
let SCORE * VOTES be kept in the POINTS column
CREATE FUNCTION UPDATE_MOVIE_POINTS()RETURNS opaqueAS ’BEGIN
new.POINTS = new.SCORE * new.VOTES;RETURN new;END;’
LANGUAGE ’plpgsql’
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Trigger Example
calculate POINTS automatically on updates
CREATE TRIGGER UPDATE_MOVIEBEFORE INSERT OR UPDATE ON MOVIEFOR EACH ROWEXECUTE PROCEDURE UPDATE_MOVIE_POINTS()
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Views
presenting a derived table like a base table: view
isolating users and application programsfrom changes in database structure
creating a view:
CREATE VIEW view_name ASSELECT ...
SELECT will be executed every time the view is used
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Views
presenting a derived table like a base table: view
isolating users and application programsfrom changes in database structure
creating a view:
CREATE VIEW view_name ASSELECT ...
SELECT will be executed every time the view is used
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
View Example
identifiers, titles and years of new movies
CREATE VIEW NEW_MOVIE ASSELECT ID, TITLE, YR FROM MOVIEWHERE (YR > 1995)
SELECT * FROM NEW_MOVIE
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Updating Views
any change will have to performed on the base tables
rules need to be defined
creating a rule:
CREATE RULE rule_name ASON event TO view_name[ WHERE condition ]DO [ INSTEAD ] sql_statement
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Updating Views
any change will have to performed on the base tables
rules need to be defined
creating a rule:
CREATE RULE rule_name ASON event TO view_name[ WHERE condition ]DO [ INSTEAD ] sql_statement
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
View Rule Example
modify the title of a new movie
UPDATE NEW_MOVIE SET TITLE = ...WHERE (ID = ...)
rule for updating the base table
CREATE RULE UPDATE_TITLE ASON UPDATE TO NEW_MOVIEDO INSTEADUPDATE MOVIE SET TITLE = new.TITLEWHERE (ID = old.ID)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
View Rule Example
modify the title of a new movie
UPDATE NEW_MOVIE SET TITLE = ...WHERE (ID = ...)
rule for updating the base table
CREATE RULE UPDATE_TITLE ASON UPDATE TO NEW_MOVIEDO INSTEADUPDATE MOVIE SET TITLE = new.TITLEWHERE (ID = old.ID)
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Topics
1 Application DevelopmentIntroductionEmbedded SQLODBCJDBC
2 SQLStored ProceduresViewsIndexes
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Indexes
some operations require sorting:ORDER BY, DISTINCT, GROUP BY, UNION, ...
indexes speed up queries
slow down inserts and updates
creating an index
CREATE [ UNIQUE ] INDEX index_nameON table_name(column_name [, ...])
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
Stored ProceduresViewsIndexes
Indexes
some operations require sorting:ORDER BY, DISTINCT, GROUP BY, UNION, ...
indexes speed up queries
slow down inserts and updates
creating an index
CREATE [ UNIQUE ] INDEX index_nameON table_name(column_name [, ...])
H. Turgut Uyar, Sule Oguducu Database Systems
Application DevelopmentSQL
References
References
Required Reading: Date
Chapter 4: An Introduction to SQL
4.6. Embedded SQL
Chapter 9: Integrity
9.11. Triggers (a Digression)
Chapter 10: Views
Supplementary Reference: Ramakrishnan, Gehrke
Chapter 6: Database Application Development
H. Turgut Uyar, Sule Oguducu Database Systems
top related