Download DBMS.pdf

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Oracle Database wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Functional Database Model wikipedia , lookup

Clusterpoint wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Null (SQL) wikipedia , lookup

Database model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Relational algebra wikipedia , lookup

Join (SQL) wikipedia , lookup

Relational model wikipedia , lookup

Transcript
Database Management Systems
  Introduction
to relational databases
  Structured Query Language (SQL)
  Introduction to JDBC
  Connecting to a database
 Querying a database
Based on Mathematical Relations
  Table = Relation
  Row = Tuple
  Operators take relations as operands and
produce relations as results
 
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.
*
Accidents
accident_date
damages
People
ssn
0..1
fname
*
drives lname
city
state
1
Autos
*
* vin
owns
make
year
*
handles
Agents
salary
photo
0..1
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))
 
 
 
 
 
 
 
 
 
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!
CREATE TABLE table_name
(attribute type, … )
CREATE TABLE autos(
vin varchar(12) primary key,
make varchar(10), year integer,
agent_ssn char(11))
INSERT INTO table_name (attribute, …)
VALUES (value, …)
INSERT INTO people (lname, ssn) VALUES
(‘SMITH’, ‘111-22-3333’)
INSERT INTO people VALUES
(‘123-45-6789’, ‘JANE’, ‘DOE’,
‘BOSTON’, ‘MA’)
 
Attribute names may be omitted only when
all attributes are supplied – in the same order
as in the table definition.
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
 
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.
 
UPDATE table
SET attribute = value, …
WHERE condition
 
UPDATE people
SET city = ‘Boston’, state = ‘MA’
WHERE ssn = ‘012-34-5678’
 
If there is no where clause, all rows in the table
will be updated!
DELETE FROM table_name
WHERE condition
DELETE from people
WHERE ssn = ‘111-22-3333’
DELETE from people
WHERE state <> ‘MA’
 
If there is no where clause, all rows are deleted!!!
min
  max
  sum
  avg
  count
 
SELECT avg(damages)
FROM accidents
WHERE accident_date > ‘2009-12-31’
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
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).
 
SELECT city, state, count(ssn)
FROM people
WHERE lname < ‘G’
GROUP BY city, state
HAVING count(ssn) > 5
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.
 
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.
Tuple Variables (renaming tables)
 
Sometimes useful just to save typing:
fname, lname, vin
FROM people p, owns o
WHERE p.ssn = o.ssn
SELECT
 
Sometimes necessary (e.g. find vin’s of cars that
are jointly owned by more than one person):
o1.vin
FROM owns o1, owns o2
WHERE o1.vin = o2.vin
SELECT
AND
o1.ssn <> o2.ssn
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.
 
Join Conditions
 
Natural
–  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.
Join Type
 
inner 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.
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
people NATURAL INNER JOIN owns
FROM
fname, lname, vin
FROM people INNER JOIN OWNS USING (ssn)
SELECT
fname, lname, vin
FROM people INNER JOIN OWNS ON people.ssn = owns.ssn
SELECT
 
To include all people (even if they don't own any cars):
SELECT
FROM
fname, lname, vin
people NATURAL LEFT
SELECT
fname, lname, vin
owns NATURAL RIGHT OUTER
FROM
OUTER JOIN
JOIN
owns
people
 
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
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.
NULL Values (2)
 
SQL uses a 3 valued boolean logic with values
true, false, and unknown:
AND
T
F
U
OR
T
F
U
T
T
F
U
T
T
T
T
F
F
F
F
F
T
F
U
U
U
F
U
U
T
U
U
NULL Values (3)
 
Explain:
select count(ssn) from agents where salary > 60000
==> 20
select count(ssn) from agents where salary <= 60000
==> 10
select count(ssn) from agents
where salary > 60000 OR salary <= 60000
==> 30
select count(ssn) from agents
==> 32
select count(ssn) from agents where salary = NULL
==> 0
Can be used by applications and applets(with
restrictions)
  Provides an interface to SQL
 
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
 
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) {
// …
}
try {
stmt = con.createStatement();
} catch( SQLException ex ) {
// …
}
try {
ResultSet rs;
rs = stmt.executeQuery(
”select * from student” );
while( rs.next() ){
System.out.println(
“ID: “ + rs.getString(1));
System.out.println(
“Name: “ + rs.getString(2));
System.out.println(
“Major: “ + rs.getString(3) +
“\n”);
}
rs.close();
} catch (SQLException e) { … }
TextField id;
TextField name;
// ...
stmt.executeUpdate(
”insert into student (sid, name) ”
+ “values (‘“ + id.getText()
+ “’, ‘“ + name.getText() + “’)”
);
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.
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.
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.
public abstract void setAutoCommit( boolean autoCommit )
throws SQLException
Sets autocommit.
public abstract boolean getAutoCommit( )
throws SQLException
Returns true if autocommit set, returns false otherwise.
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.
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.
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.
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.
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.
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.
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
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.
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
 
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.
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
public abstract XXX getXXX
(String columnName) throws SQLException
String getString
boolean getBoolean
byte getByte
short getShort
int getInt
long getLong
float getFloat
double getDouble
byte[ ] getBytes
Date getDate
Time getTime
Timestamp getTimestamp
 
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.
first()
last()
beforeFirst()
afterLast()
next()
previous()
 
 
 
absolute(n)
relative(n)
moveToInsertRow()
 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
public abstract void updateXXX
(String columnName, XXX value) throws SQLException
// 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();
// 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();
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.
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)
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
}
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, “238-96-0401”);
ps.executeUpdate();
ps.close();
dbConnection.commit();
dbConnection.setAutoCommit(true);
FileInputStream fis = new FileInputStream("office.jpg");
dbConnection.setAutoCommit(false);
statement.executeUpdate("update agents set photo = empty_blob
() where ssn = '282-16-3333'");
ResultSet rs = statement.executeQuery (
"select * from agents where ssn = '282-16-3333'");
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);