Download presentation
Presentation is loading. Please wait.
1
Database Management Systems
DBMS Database Management Systems
2
Outline Introduction to relational databases
Structured Query Language (SQL) Introduction to JDBC Connecting to a database Querying a database
3
Relational Databases Based on Mathematical Relations Table = Relation
Row = Tuple Operators take relations as operands and produce relations as results
4
Basic SQL Data Types char[(n)] character[(n)]
String of length n (right padded if necessary). Long strings are truncated. Default length is 1. varchar(n) character varying(n) String with max length n. Long strings truncated. smallint 16 bit signed integer int integer 32 bit signed integer numeric(p [,s]) Fixed point number with decimal precision p (number of digits) and scale s (digits to right of decimal point). decimal(p [,s]) Same as numeric, but may be represented internally with larger scale. float[(p)] Floating point number with maximum binary precision (bits) p. real double Floating point numbers with implementation dependent precision. Double more precise than real. date Month, day, and year. time Hour, minute, second. timestamp Combination of date and time.
5
Sample Database Design
People ssn fname lname city state 1 * Autos vin make year Accidents accident_date damages 0..1 * * * drives owns * handles Agents salary photo 0..1
6
Sample Database Tables
People (ssn char(11), fname varchar(12), lname varchar(15), city varchar(12), state char(2)) Agents (ssn char(11), salary float, photo blob) Autos (vin varchar(12), make varchar(10), year int, agent_ssn char(11)) Accidents (accident_date date, damages float, vin char(12), driver_ssn char(11)) Owns (ssn char(11), vin char(12))
7
Basic SQL Statements CREATE TABLE INSERT SELECT UPDATE DELETE
SQL is not sensitive to spacing and line breaks. SQL is not case sensitive in regards to keywords and identifiers (e.g. table names, columns names, etc.). SQL is case sensitive in regards to character data (e.g. inside quotation marks). SQL statements are not terminated with semicolons!
8
CREATE TABLE Statement
CREATE TABLE table_name (attribute type, … ) CREATE TABLE autos( vin varchar(12) primary key, make varchar(10), year integer, agent_ssn char(11))
9
INSERT Statement INSERT INTO table_name (attribute, …) VALUES (value, …) INSERT INTO people (lname, ssn) VALUES (‘SMITH’, ‘ ’) INSERT INTO people VALUES (‘ ’, ‘JANE’, ‘DOE’, ‘BOSTON’, ‘MA’) Attribute names may be omitted only when all attributes are supplied – in the same order as in the table definition.
10
SELECT Statement SELECT attribute, … FROM table_name, … WHERE condition GROUP BY attribute, … HAVING condition ORDER BY attribute, … Only the select and from clauses are required Examples: SELECT ssn, fname, lname FROM people WHERE state = ‘MA’ ORDER BY lname, fname SELECT * FROM people The * means all attributes (columns) No where clause means all rows
11
SELECT DISTINCT fname, lname FROM people
The keyword distinct after select, ensures that there are no duplicate rows in the output. SELECT DISTINCT fname, lname FROM people There may be two rows with the same value for fname, or the same value for lname, but no two rows that are completely identical.
12
UPDATE Statement UPDATE table SET attribute = value, … WHERE condition
UPDATE people SET city = ‘Boston’, state = ‘MA’ WHERE ssn = ‘ ’ If there is no where clause, all rows in the table will be updated!
13
DELETE Statement DELETE FROM table_name WHERE condition
DELETE from people WHERE ssn = ‘ ’ WHERE state <> ‘MA’ If there is no where clause, all rows are deleted!!!
14
Aggregate Functions min max sum avg count
SELECT avg(damages) FROM accidents WHERE accident_date > ‘ ’
15
Aggregate Functions (2)
Number of accidents in the database: select count(*) from accidents Number of accidents where the damaged vehicle had a driver: select count(driver_ssn) from accidents Number of different people who have been drivers in accidents: select count (distinct driver_ssn) from accidents select distinct count(driver_ssn) from accidents
16
Grouping After rows are filtered by the where clause, they may be grouped with a group by clause. The output will include only one row for each group. Only the attributes by which rows are grouped and aggregate values can be selected. Groups may be filtered with a having clause (similar to how rows are filtered by the where clause).
17
Grouping (2) SELECT city, state, count(ssn) FROM people WHERE lname < ‘G’ GROUP BY city, state HAVING count(ssn) > 5
18
Selecting from multiple tables
If two or more tables are listed in the from clause, the tables are first joined by taking their cartesian product. The rows are concatenated in all possible combinations. The cartesian product of a table of i columns and m rows with a table of j columns and n rows produces a table of i+j columns and m*n rows.
19
Multiple tables (2) Often times we want a natural join, where attributes with the same names must match values. SELECT fname, lname, vin FROM people, owns WHERE people.ssn = owns.ssn Without the where clause, every name would appear with every car.
20
Tuple Variables (renaming tables)
Sometimes useful just to save typing: SELECT fname, lname, vin FROM people p, owns o WHERE p.ssn = o.ssn Sometimes necessary (e.g. find vin’s of cars that are jointly owned by more than one person): SELECT o1.vin FROM owns o1, owns o2 WHERE o1.vin = o2.vin AND o1.ssn <> o2.ssn
21
Joins Form a new relation by combining tuples from two other relations. Typically used in from clause. Join condition defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.
22
Join Conditions Natural Using (A1, A2, ... ) On <predicate>
Tuples match if attributes with the same names have the same values. Two attributes with the same name are combined into a single column. Using (A1, A2, ... ) Each of the listed attributes (A1, A2, ... ) must appear in both relations. Like natural join, except that only the listed attributes have to match, instead of all attributes with the same names. The result will have only one column for each of the listed attributes. On <predicate> The predicate can specify any arbitrary join condition. Every column from both relations is retained, even if the predicate specifies that some attributes must be equal.
23
Join Type inner join left outer join right outer join
Each row of the result combines matching tuples from the two joined relations. If a tuple from one of the relations doesn't match any tuple in the other relation, it does not appear in the result. The keyword inner is optional. (MySQL does not allow the keyword inner.) left outer join If a tuple from the relation on the left hand side does not match any tuple from the relation on the right hand side, it is included in the result with null values supplied for attributes of the right hand relation. right outer join If a tuple from the relation on the right hand side does not match any tuple from the relation on the left hand side, it is included in the result with null values supplied for attributes of the left hand relation. full outer join (not supported in MySQL) All tuples from both joined relations are included in the result, with null values supplied where neccessary.
24
Join Examples (for MySQL omit optional keywords inner and outer)
Find names of car owners and the vin numbers of the cars they own: SELECT fname, lname, vin FROM people NATURAL INNER JOIN owns SELECT fname, lname, vin FROM people INNER JOIN OWNS USING (ssn) SELECT fname, lname, vin FROM people INNER JOIN OWNS ON people.ssn = owns.ssn
25
To include all people (even if they don't own any cars):
SELECT fname, lname, vin FROM people NATURAL LEFT OUTER JOIN owns SELECT fname, lname, vin FROM owns NATURAL RIGHT OUTER JOIN people
26
Find number of agents who live in each state
Find number of agents who live in each state. Include states with zero agents: SELECT state, count(agents.ssn) FROM agents NATURAL RIGHT JOIN people GROUP BY state Find names of people who do not own cars: SELECT fname, lname FROM people NATURAL LEFT OUTER JOIN owns WHERE vin IS NULL Find ssn's of people who either own cars or have been the driver in an accident, but not both: SELECT ssn, driver_ssn FROM owns FULL OUTER JOIN accidents ON ssn = driver_ssn WHERE ssn IS NULL OR driver_ssn IS NULL
27
NULL Values The value NULL, indicates that the attribute is missing in the database. It means the value is unknown. It does not mean “zero”. It does not mean the attribute has no value. Aggregate functions ignore rows where the aggregated attribute is NULL. Except in the expression: count(*) which counts all rows.
28
NULL Values (2) SQL uses a 3 valued boolean logic with values true, false, and unknown: AND T F U OR T F U
29
NULL Values (3) Explain:
select count(ssn) from agents where salary > ==> 20 select count(ssn) from agents where salary <= ==> 10 select count(ssn) from agents where salary > OR salary <= ==> 30 select count(ssn) from agents ==> 32 select count(ssn) from agents where salary = NULL ==> 0
30
JDBC Can be used by applications and applets(with restrictions)
Provides an interface to SQL
31
Java application JDBC Driver Manager Bridge JDBC/ODBC Driver Driver JDBC ODBC DB DB
32
Basic Steps in Accessing a DB
Load the JDBC drivers Use the DriverManager to create a Connection Use the Connection object to create a Statement object Use the Statement object to execute queries and obtain ResultSet objects Close the Connection
33
Connecting to a Database
Connection con; Statement stmt; try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); String url = "jdbc:odbc:myDB.mdb"; String user = "user"; String password = "****"; con = DriverManager.getConnection( url, user, password ); } catch (SQLException e) { // … }
34
Creating a Statement try { stmt = con.createStatement();
} catch( SQLException ex ) { // … }
35
Executing a Query try { ResultSet rs; rs = stmt.executeQuery(
”select * from student” ); while( rs.next() ){ System.out.println( “ID: “ + rs.getString(1)); “Name: “ + rs.getString(2)); “Major: “ + rs.getString(3) + “\n”); } rs.close(); } catch (SQLException e) { … }
36
Executing an Update TextField id; TextField name; // ...
stmt.executeUpdate( ”insert into student (sid, name) ” + “values (‘“ + id.getText() + “’, ‘“ + name.getText() + “’)” );
37
public interface Connection
A Connection represents a session with a specific database. Within the context of a Connection, SQL statements are executed and results are returned. A Connection's database is able to provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, etc. This information is obtained with the getMetaData method.
38
createStatement( ) prepareStatement ( String sql)
public abstract Statement createStatement() throws SQLException SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it is more efficient to use: public abstract PreparedStatement prepareStatement ( String sql) throws SQLException A SQL statement can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.
39
Automatic Commit For a connection in auto-commit mode, all SQL statements are executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by either commit() or rollback(). By default, new connections are in auto-commit mode. The commit occurs when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a ResultSet, the statement completes when the last row of the ResultSet has been retrieved or the ResultSet has been closed.
40
Automatic Commit (2) public abstract void setAutoCommit( boolean autoCommit ) throws SQLException Sets autocommit. public abstract boolean getAutoCommit( ) throws SQLException Returns true if autocommit set, returns false otherwise.
41
commit( ) & rollback( ) public abstract void commit( ) throws SQLException Commit makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by the Connection. This method should only be used when auto commit has been disabled. public abstract void rollback( ) throws SQLException Rollback drops all changes made since the previous commit/rollback and releases any database locks currently held by the Connection. This method should only be used when auto commit has been disabled.
42
close( ) & isClosed( ) public abstract void close() throws SQLException This method provides immediate release of Connection's database and JDBC resources instead of waiting for them to be automatically released. A Connection is automatically closed when it is garbage collected. public abstract boolean isClosed() throws SQLException Returns true if Connection is closed.
43
getMetaData( ) public abstract DatabaseMetaData getMetaData( ) throws SQLException A Connection's database can provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, etc. This information is made available through a DatabaseMetaData object.
44
public interface Statement
A Statement object is used for executing a static SQL statement and obtaining the results produced by it. Only one ResultSet per Statement can be open at any point in time. Therefore, if the reading of one ResultSet is interleaved with the reading of another, each must have been generated by different Statements. All statement execute methods implicitly close a statement's current ResultSet if an open one exists.
45
Executing SQL statements
public abstract ResultSet executeQuery( String sql ) throws SQLException Execute a SQL statement that returns a single ResultSet public abstract int executeUpdate( String sql ) throws SQLException Execute a SQL INSERT, UPDATE or DELETE statement.
46
Controlling the size of the ResultSet
public abstract int getMaxRows( ) throws SQLException public abstract void setMaxRows( int max ) throws SQLException The maxRows limit is the maximum number of rows that a ResultSet can contain. If the limit is exceeded, the excess rows are silently dropped.
47
Query time-out public abstract void setQueryTimeout( int seconds ) throws SQLException The queryTimeout limit is the number of seconds the driver will wait for a Statement to execute. If the limit is exceeded, a SQLException is thrown. If seconds == 0 wait forever. public abstract int getQueryTimeout( ) throws SQLException If zero returned : unlimited wait time
48
public interface ResultSet
A ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. Within a row its column values can be accessed in any order. A ResultSet maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The 'next' method moves the cursor to the next row.
49
next( ) public abstract boolean next() throws SQLException
A ResultSet is initially positioned before its first row. The first call to next makes the first row the current row. The second call makes the second row the current row, etc. Returns true if the new current row is valid; false if there are no more rows
50
getXXX Methods The getXXX methods retrieve column values for the current row. Values can be retrieved by index number of the column (more efficient), or by the name of the column. Columns are numbered from 1. Column names used as input to getXXX methods are case insensitive.
51
getXXX Methods (2) The getXXX methods retrieve column values for the current row attempting to convert the underlying data to the specified Java type. (JDBC specification defines mappings from SQL types to Java types) All of them have the follow the specification patterns: public abstract XXX getXXX (int columnIndex) throws SQLException (String columnName) throws SQLException
52
String getString boolean getBoolean byte getByte short getShort int getInt long getLong float getFloat double getDouble byte[ ] getBytes Date getDate Time getTime Timestamp getTimestamp
53
Scrolling and Updates A default ResultSet object is not updatable and has a cursor that moves forward only. It is possible to produce ResultSet objects that are scrollable and/or updatable: Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); The statement object will produce result sets that are scrollable (but not sensitive to changes by others) and updatable.
54
Scrolling methods first() last() beforeFirst() afterLast() next()
previous() absolute(n) relative(n) moveToInsertRow()
55
updateXXX Methods Similar to the getter methods, getXXX
All of them have the follow the specification patterns: public abstract void updateXXX (int columnIndex, XXX value) throws SQLException (String columnName, XXX value) throws SQLException
56
Updating an existing row
// move the cursor to the fifth row of rs rs.absolute(5); // update the NAME column of row 5 to be SMITH rs.updateString("NAME", ”SMITH"); // update the row in the data source rs.updateRow();
57
Inserting a new row // Go to the special “insert row”
// Store values in the columns with updateXXX // Use insertRow to update the data source // Return cursor to previous position rs.moveToInsertRow(); rs.updateString(1, ”SMITH"); rs.updateInt(2, 35); rs.updateBoolean(3, true); rs.insertRow(); rs.moveToCurrentRow();
58
ResultSet - other methods
public abstract ResultSetMetaData getMetaData() throws SQLException The number, types and properties of a ResultSet's columns are provided by the getMetaData method. public abstract int findColumn( String columnName ) throws SQLException Maps a Resultset column name to a ResultSet column index.
59
public interface ResultSetMetaData
Provides the information about the types and properties of the columns in a ResultSet. Some methods: int getColumnCount() int isNullable(int column) String getColumnName(int column) String getTableName(int column) int getColumnType(int column) boolean isReadOnly(int column) boolean isWritable(int column)
60
Reading blobs ResultSet rs = statement.executeQuery( "select * from agents where photo is not null"); while (rs.next()) { String ssn = rs.getString(1); Blob photo = rs.getBlob(3); int length = (int)photo.length(); InputStream is = photo.getBinaryStream(); byte[] data = new byte[length]; is.read(data, 0, length); new ImageViewer(data, ssn); // not standard }
61
Writing blobs (mysql) FileInputStream fis = new FileInputStream("office.jpg"); dbConnection.setAutoCommit(false); PreparedStatement ps = dbConnection.prepareStatement ( "update agents set photo = ? where ssn = ?"); ps.setBinaryStream(1, fis, 10047); ps.setString(2, “ ”); ps.executeUpdate(); ps.close(); dbConnection.commit(); dbConnection.setAutoCommit(true);
62
Writing blobs (oracle)
FileInputStream fis = new FileInputStream("office.jpg"); dbConnection.setAutoCommit(false); statement.executeUpdate("update agents set photo = empty_blob() where ssn = ' '"); ResultSet rs = statement.executeQuery ( "select * from agents where ssn = ' '"); rs.next(); oracle.sql.BLOB photo = ((oracle.jdbc.driver.OracleResultSet) rs).getBLOB(3); byte[] data = new byte[10047]; fis.read(data, 0, 10047); photo.putBytes(1, data); // in oracle.sql.BLOB and jdk 1.4 dbConnection.commit(); dbConnection.setAutoCommit(true);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.