Database Concept
Database Concept
Mimi Opkins
Introduction UML design
Basic UML & SQL Many-to-Many
Models Many-to-Many 2
Classes & Schemes Subkeys
Rows & Tables Repeated Attributes
Associations Multi-Valued Attributes
Keys Domains
SQL technique Enumerated Domains
SQL and RA Subclasses
DDL & DML Aggregation
Join Recursive Associations
Multiple Joins Normalization
Join Types BCNF
Functions Transactions
Subqueries JDBC
Union & Minus
Views & Indexes
Check Constraints
Introduction
What is a database?
Why do we need one?
Avoid redundancy
duplication of information in multiple tables within a database
Data integrity
Refers to the validity of data
Referential Integrity
ensures that relationships between tables remain consistent
Deletion Anomalies
Deletion of one row of a table results in the deletion of
unintended information
Different Types of Databases
Paper
Flatfile
Hierarchical
Network
Relational
Object
Introduction – Redundancy
The duplication of information in multiple tables
within a database
Ex. School Records may all have info about you
Admissions
Enrollment Services
Department
Student Union
Introduction – Data Integrity
Refers to the validity of data.
Data integrity can be compromised in a number
of ways:
Human errors when data is entered
Errors that occur when data is transmitted from one
computer to another
Software bugs or viruses
Hardware malfunctions, such as disk crashes
Natural disasters, such as fires and floods
Introduction – Referential Integrity
Referential integrity ensures that relationships
between tables remain consistent.
When one table has a foreign key to another
table, the concept of referential integrity states
that you may not add a record to the table that
contains the foreign key unless there is a
corresponding record in the linked table.
It also includes the techniques known as
cascading update and cascading delete, which
ensure that changes made to the linked table
are reflected in the primary table.
Introduction – Deletion Anomalies
A situation, usually caused by redundancy in the
database design, in which the deletion of one
row of a table results in the deletion of
unintended information
Ex. You’re the only student enrolled in CECS
323 and you drop the course. As a result all the
info about CECS 323 is removed from the
catalog
Introduction – Relational Database
What is a relational database?
Based on Relational Algebra
Tables and their relationships to each other
Easily navigated
Popular
Standard query language
Familiar Concepts
Models and Languages
Database design is a process of modeling an
enterprise in the real world.
A database itself is a model of the real world that
contains selected information needed by the
enterprise.
There are many models and languages used for
this modeling. Some of these are mathematically
based. Others are less formal and more intuitive.
Unified Modeling Language (UML)
The Unified Modeling Language (UML) was designed
for software engineering of large systems using object-
oriented (OO) programming languages.
UML is a very large language; we will use only a small
portion of it here, to model those portions of an
enterprise that will be represented in the database.
It is our tool for communicating with the client in terms
that are used in the enterprise.
Used to describe the conceptual view of a database.
Entity-Relationship (ER)
The Entity-Relationship (ER) model is used in
many database development systems.
There are many different graphic standards that
can represent the ER model. Some of the most
modern of these look very similar to the UML
class diagram, but may also include elements of
the relational model.
Relational Model (RM)
The Relational Model (RM) is the formal model
of a database that was developed for IBM in the
early 1970s by Dr. E.F. Codd.
It is largely based on set theory, which makes it
both powerful and easy to implement in
computers.
All modern relational databases are based on
this model.
We will use it to represent information that does
not (and should not) appear in the UML model
but is needed for us to build functioning
databases.
Relational Algebra (RA)
Relational Algebra (RA) is a formal language
used to symbolically manipulate objects of the
relational model.
Terms used to refer to the logical view of the
database.
Table Model
The table model is an informal set of terms for
relational model objects. These are the terms
used most often by database developers.
Terms used to refer to the physical view of the
database.
Structured Query Language (SQL)
The Structured Query Language (SQL,
pronounced sequel or ess-que-ell) is used to
build and manipulate relational databases.
It is based on relational algebra, but provides
additional capabilities that are needed in
commercial systems.
It is a declarative, rather than a procedural,
programming language.
There is a standard for this language, but
products vary in how closely they implement it.
Basic Structures: Classes and
Schemes – UML Class
UML class (ER term: entity) is anything in the
enterprise that is to be represented in our database
The first step in modeling a class is to describe it in
natural language.
Example: build a sales database. Let’s start by defining
customer using natural language.
Attribute (properties) is a piece of information that
characterizes each member of a class
Descriptive attributes (natural attribute) are those
which actually provide real-world information about the
class.
UML only uses descriptive attributes
ID number are not descriptive attributes
Basic Structures: Classes and
Schemes – Class Diagram
A class diagram shows the class name and list of
attributes that identify data elements we need to know
about each member (instance, occurrence, of a class)
The Customer class represents any person who has
done business with us or who we think might do
business with us in the future. Its attributes are:
Customer first name.
Customer last name.
Customer phone.
Customer street.
Customer zip code.
Basic Structures: Classes and
Schemes – Relation Scheme
In an OO programming language, each class is
instantiated with objects of that class. In building a
relational database, each class is first translated into a
relation model scheme. The scheme starts with all of
the attributes from the class diagram.
Additional rows are built on the Customers scheme as before. The table or
relation consists of all rows.
Three of the attributes in the Customers scheme are now identified as the
primary key, which is explained later on.
Basic Structures: Rows and Tables
- Tuples
Knowing that the relation (table) is a set of tuples (rows)
tells us more about this structure, as we saw with
schemes and domains.
Each tuple/row is unique; there are no duplicates
Tuples/rows are unordered; we can display them in any way we
like and the meaning doesn’t change. (SQL gives us the
capability to control the display order.)
Tuples/rows may be included in a relation/table set if they are
constructed on the scheme of that relation; they are excluded
otherwise. (It would make no sense to have an Order row in the
Customers table.)
We can define subsets of the rows by specifying criteria for
inclusion in the subset. (Again, this is part of a SQL query.)
We can find the union, intersection, or difference of the rows in
two or more tables, as long as they are constructed over the
same scheme.
Basic Structures: Rows and Tables
– Insuring Unique Rows
Each row in a table must be distinct. So
there must be a set of attributes in each
relation that guarantee uniqueness. Any
set of attributes that can do this is called a
super key of the relation.
The database designer picks of the
possible super key sets to serve as the
primary key or unique identifier of each
row.
Basic Structures: Rows and Tables
– Super Keys
cfirstname,clastname,cphone,cstreet,czipcode
cfirstname,clastname,cphone,cstreet
cfirstname,clastname,cphone,czipcode
cfirstname,clastname,cstreet,czipcode
cfirstname,clastname,cphone
cfirstname,clastname,cstreet
Basic Structures: Rows and Tables
– Insuring Unique Rows - SQL
Let R be
NewStudent(stuId, lastName, major, credits, status, socSecNo)
FDs in R include
{stuId}→{lastName}, but not the reverse
{stuId} →{lastName, major, credits, status, socSecNo,
stuId}
{socSecNo} →{stuId, lastName, major, credits, status,
socSecNo}
{credits}→{status}, but not {status}→{credits}
Trivial Functional Dependency
The FD X→Y is trivial if set {Y} is a subset of
set {X}
FDs:
{courseNo,stuId} → {lastName}
{courseNo,stuId} →{facId}
{courseNo,stuId} →{schedule}
{courseNo,stuId} →{room}
{courseNo,stuId} →{grade}
courseNo → facId **partial FD
courseNo → schedule **partial FD
courseNo →room ** partial FD
stuId → lastName ** partial FD
…plus trivial FDs that are partial…
Second Normal Form-2NF
A relation is in second normal form (2NF) if it is
in first normal form and all the non-key attributes
are fully functionally dependent on the key.
No non-key attribute is FD on just part of the key
If key has only one attribute, and R is 1NF, R is
automatically 2NF
Converting to 2NF
Identify each partial FD
Remove the attributes that depend on each of
the determinants so identified
Place these determinants in separate relations
along with their dependent attributes
In original relation keep the composite key and
any attributes that are fully functionally
dependent on all of it
Even if the composite key has no dependent
attributes, keep that relation to connect logically
the others
2NF Example
NewClass(courseNo, stuId, stuLastName, facId, schedule, room, grade )
CSC201A S1006 C
HST205A S1001
Example:
NewStudent (stuId, lastName, major, credits, status)
FD:
credits→status (and several others)
By transitivity:
stuId→credits credits→status implies stuId→status
FDs:
office → dept
facName,dept → office, rank, dateHired
facName,office → dept, rank, dateHired
Fac1 Fac2
office dept facName office rank dateHired
A101 Art Adams A101 Professor 1975
C101 CSC Byrne M201 Assistant 2000
C105 CSC Davis A101 Associate 1992
H102 History Gordon M201 Professor 1982
M201 Math Hughes M203 Associate 1990
M203 Math Smith C101 Professor 1980
Smith H102 Associate 1990
Tanaka C101 Instructor 2001
Vaughn C101 Associate 1995
Converting to BCNF
Identify all determinants and verify that they are
superkeys in the relation
If not, break up the relation by decomposition
for each non-superkey determinant, create a separate
relation with all the attributes it determines, also
keeping it in original relation
Preserve the ability to recreate the original relation by
joins.
Repeat on each relation until you have a set of
relations all in BCNF
Normalization Example
Relation that stores information about projects in
large business
Work (projName, projMgr, empId, hours, empName,
budget, startDate, salary, empMgr, empDept, rating)
prijName projMgr empId hours Emp budget startDate salary Emp Emp rating
Name Mgr Dept
Jupiter Smith E101 25 Jones 100000 01/15/04 60000 Levine 10 9
Emp
Proj Work1
prijName empId hours rating
prijName projMgr budget startDate Jupiter E101 25 9
Jupiter Smith 100000 01/15/04
Jupiter E105 40
Maxima Lee 200000 03/01/04
Jupiter E110 10 8
Maxima E101 15
Maxima E110 30
Maxima E120 15
FDs:
office → dept
facName,dept → office, rank, dateHired
facName,office → dept, rank, dateHired
If, after all possible changes have been made to S, a row is made
up entirely of a symbols, a(1, a(2, …,a(n), then the join is lossless.
If there is no such row, the join is lossy.
Normalization Methods
Analysis
Decomposition method shown previously
Synthesis
Begin with attributes, combine them into groups
having the same determinant
Use functional dependencies to develop a set of
normalized relations
Mapping from ER diagram provides almost-
normalized schema
De-normalization
When to stop the normalization process
When applications require too many joins
When you cannot get a non-loss decomposition that
preserves dependencies
Inference Rules for FDs
Armstrong’s Axioms
Reflexivity If B is a subset of A, then A → B..
Augmentation If A → B, then AC → BC.
Transitivity If A → B and B → C, then A → C
Additional rules that follow:
Additivity If A → B and A → C, then A → BC
Projectivity If A → BC, then A → B and A → C
Pseudotransitivity If A → B and CB → D, then AC
→D
Closure of Set of FDs
If F is a set of functional dependencies for a
relation R, then the set of all functional
dependencies that can be derived from F, F+, is
called the closure of F
Could compute closure by applying Armstrong’s
Axioms repeatedly
Closure of an Attribute
If A is an attribute or set of attributes of relation
R, all the attributes in R that are functionally
dependent on A in R form the closure of A, A+
Computed by Closure Algorithm for A
result ← A;
while (result changes) do
for each functional dependency B → C in F
if B is contained in result then result ← result C;
end;
A+ ← result;
Uses of Attribute Closure
Can determine if A is a superkey-if every
attribute in R functionally dependent on A
Can determine whether a given FD X→Y is in
the closure of the set of FDs. (Find X+, see if it
includes Y)
Redundant FDs and Covers
Given a set of FDs, can determine if any of them
is redundant, i.e. can be derived from the
remaining FDs, by a simple
If a relation R has two sets of FDs, F and G
then F is a cover for G if every FD in G is also in F+
F and G are equivalent if F is a cover for G and G is a
cover for F (i.e. F+ = G+)
Minimal Set of FDs
Set of FDs, F is minimal if
The right side of every FD in F has a single attribute
(called standard or canonical form)
No attribute in the left side of any FD is extraneous
F has no redundant FDs
Minimal Cover for Set of FDs
A minimal cover for a set of FDs is a cover such
that no proper subset of itself is also a cover
A set of FDs may have several minimal covers
Synthesis Algorithm for 3NF
Can always find 3NF decomposition that is
lossless and that preserves all FDs
3NF Algorithm uses synthesis
Begin with universal relation and set of FDs,G
Find a minimal cover for G
Combine FDs that have the same determinant
Include a relation with a key of R
Basic Queries: SQL and RA
To look at the data in tables, we use the
SELECT statement. The result of this statement
is always a new table that we can view with our
database client software or use with
programming languages to build dynamic web
pages or desktop applications.
Although the result table is not stored in the
database we can also use it as part of other
SELECT statements.
Basic Queries: SQL and RA –
Required Clauses
Only the SELECT and FROM clauses are
required.
The scheme of the result of πXr is X. The tuples resulting from this
operation are tuples of the original relation, r, cut down to the attributes
contained in X.
For X to be a subscheme of R, it must be a subset of the attributes in R, and
preserve the assignment rule from R (that is, each attribute of X must have the
same domain as its corresponding attribute in R).
If X is a super key of r, then there will be the same number of tuples in the result
as there were to begin with in r. If X is not a super key of r, then any duplicate
(non-distinct) tuples are eliminated from the result.
Just as in the SQL statement, we can apply the project operator to the
output of the select operation to produce the results that we want: πXσθr or
πcLastName, cFirstName, cPhone σcZipCode='90840'customers.
Basic queries: SQL and RA – Select
and Project Step 4
4. Since RA considers relations strictly as sets of
tuples, there is no way to specify the order of
tuples in a result relation.
Basic SQL Statements: DDL and DML
SQL statements are divided into two major
categories:
data definition language (DDL) - used to build and
modify the structure of your tables and other objects
in the database
data manipulation language (DML) - used to work
with the data in tables
All of the information about objects in your
schema is contained in a set of tables called the
data dictionary.
Basic SQL Statements: DDL and DML
– DDL- CREATE Statement
The CREATE TABLE statement does exactly that:
CREATE TABLE <table name> (
<attribute name 1> <data type 1>,
...
<attribute name n> <data type n>);
The data types that you will use most frequently are
character strings, which might be called VARCHAR or
CHAR for variable or fixed length strings; numeric types
such as NUMBER or INTEGER, which will usually
specify a precision; and DATE or related types.
Data type syntax is variable from system to system; the
only way to be sure is to consult the documentation for
your own software.
Basic SQL Statements: DDL and DML
– DDL- ALTER TABLE Statement
The ALTER TABLE statement may be used as you have seen to
specify primary and foreign key constraints, as well as to make other
modifications to the table structure. Key constraints may also be
specified in the CREATE TABLE statement.
ALTER TABLE <table name>
ADD CONSTRAINT <constraint name>
PRIMARY KEY (<attribute list>);
You get to specify the constraint name. Get used to following a
convention of tablename_pk (for example, Customers_pk), so you
can remember what you did later.
The attribute list contains the one or more attributes that form this
PK; if more than one, the names are separated by commas.
Basic SQL Statements: DDL and DML
– DDL- Foreign Key Constraint
The FOREIGN KEY CONSTRAINT is a bit more complicated, since
we have to specify both the FK attributes in this (child) table, and the
PK attributes that they link to in the parent table.
ALTER TABLE <table name>
ADD CONSTRAINT <constraint name>
FOREIGN KEY (<attribute list>)
REFERENCES <parent table name> (<attribute list>);
Name the constraint in the form childtable_parenttable_fk (for
example, Orders_Customers_fk). If there is more than one attribute
in the FK, all of them must be included (with commas between) in
both the FK attribute list and the REFERENCES (parent table)
attribute list.
You need a separate foreign key definition for each relationship in
which this table is the child.
Basic SQL Statements: DDL and DML
– DDL- DROP statement
If you totally mess things up and want to start over, you can always
get rid of any object you’ve created with a drop statement. The
syntax is different for tables and constraints.
DROP TABLE <table name>;
262
Transactions - Concurrency
The goal in a ‘concurrent’ DBMS is to allow multiple
users to access the database simultaneously without
interfering with each other.
A problem with multiple users using the DBMS is that it
may be possible for two users to try and change data in
the database simultaneously. If this type of action is not
carefully controlled, inconsistencies are possible.
To control data access, we first need a concept to allow
us to encapsulate database accesses. Such
encapsulation is called a ‘Transaction’.
Transactions - What is a Transaction?
267
Transactions - Reasons for Abort
System crash
Transaction aborted by system
Execution cannot be made atomic (a site is down)
Execution did not maintain database consistency
(integrity constraint is violated)
Execution was not isolated
Resources not available (deadlock)
268
Transactions – Problem 1
Ex. Reserving a seat for a flight --
If concurrent access is allowed to data in a DBMS, two users
may try to book the same seat simultaneously
Agent 1 finds
time seat 35G empty
Agent 2 finds
seat 35G empty
Agent 1 sets
seat 35G occupied
Agent 2 sets
seat 35G occupied
Transactions - Schedules
A transaction schedule is a tabular representation of
how a set of transactions were executed over time. This
is useful when examining problem scenarios. Within the
diagrams various nomenclatures are used:
READ(a) - This is a read action on an attribute or data item
called ‘a’.
WRITE(a) - This is a write action on an attribute or data item
called ‘a’.
WRITE(a[x]) - This is a write action on an attribute or data item
called ‘a’, where the value ‘x’ is written into ‘a’.
tn (e.g. t1,t2,t10) - This indicates the time at which something
occurred. The units are not important, but tn always occurs
before tn+1.
Transactions – Problem 2
Problems can occur when concurrent transactions execute in an
uncontrolled manner.
Examples of one problem.
A original equals 100, after executing T1 and T2, A is supposed to
be 100+10-8=102 but A is 92
A=A+10 100
Read(A) 100
A=A-8 100
Write(A) 110
Write(A) 92
Transactions – Problem 3
Consider transaction A, which loads in a bank
account balance X (initially $20) and adds $10 to it.
Such a schedule would look like this:
Time Transaction
t1 TOTAL:=READ(X)
t2 TOTAL:=TOTAL+10
t3 WRITE(X[30])
Transactions – Problem 3
Now consider that, at the same time as trans A runs, trans B runs.
Transaction B gives all accounts a 10% increase. Will X be 32 or
33?
Time Transaction A Transaction B
t1 BALANCE:=READ(X)
t2 TOTAL:=READ(X)
t3 TOTAL:=TOTAL+10
t4 WRITE(X[30])
t5 BONUS:=BALANCE*110%
t6 WRITE(X[22])
t1 READ(R)
t2 READ(R)
t3 WRITE(R)
t4 WRITE(R)
t1 WRITE(R)
t2 READ(R)
t3 ABORT
t3 40 50 30 READ(Z)
t4 40 50 20 WRITE(Z[20])
t5 40 50 20 READ(X)
t6 50 50 20 WRITE(X[50])
t7 50 50 20 COMMIT
t8 50 50 20 SUM+=READ(Z) 110
279
Transactions – Atomicity (cont.)
Partial effects of a transaction must be undone
when
User explicitly aborts the transaction using
ROLLBACK
Application asks for user confirmation in the last step and
issues COMMIT or ROLLBACK depending on the response
An error, exception, or constraint violation occurs
during a transaction
The DBMS crashes before a transaction commits
How is atomicity achieved?
Logging
Transactions - Isolation
Serial Execution: transactions execute in sequence
Each one starts after the previous one completes.
Execution of one transaction is not affected by the
operations of another since they do not overlap in time
The execution of each transaction is isolated from all
others.
If the initial database state and all transactions are
consistent, then the final database state will be
consistent and will accurately reflect the real-world
state, but
Serial execution is inadequate from a performance
perspective
281
Transactions – Isolation (cont.)
282
Transactions – Isolation (cont.)
Transactions must appear to be executed in a serial
schedule (with no interleaving operations)
For performance, DBMS executes transactions using a
serializable schedule
In this schedule, operations from different transactions can
interleave and execute concurrently
But the schedule is guaranteed to produce the same effects as a
serial schedule
How is isolation achieved?
Locking, multi-version concurrency control (method commonly
used to provide concurrent access to the database)
Transactions – Database Consistency
Enterprise (Business) Rules limit the occurrence of
certain real-world events
Student cannot register for a course if the current
number of registrants equals the maximum allowed
Correspondingly, allowable database states are
restricted
cur_reg <= max_reg
These limitations are called (static) integrity
constraints: assertions that must be satisfied by all
database states
284
Transactions – Transaction Consistency
A consistent database state does not necessarily model
the actual state of the enterprise
A deposit transaction that increments the balance by the
wrong amount maintains the integrity constraint balance 0,
but does not maintain the relation between the enterprise
and database states
A consistent transaction maintains database consistency
and the correspondence between the database state and
the enterprise state (implements its specification)
Specification of deposit transaction includes
balance = balance + amt_deposit ,
(balance is the next value of balance)
285
Transactions - Consistency
Consistency of the database is guaranteed by
constraints and triggers declared in the
database and/or transactions themselves
When inconsistency arises, abort the transaction or
fix the inconsistency within the transaction
Transactions - Durability
287
Transactions – Durability (cont.)
Effects of committed transactions must survive
DBMS crashes
How is durability achieved?
DBMS manipulates data in memory; forcing all
changes to disk at the end of every transaction is very
expensive
Logging
Transactions – Implementing Durability
Database stored redundantly on mass storage
devices to protect against media failure
Architecture of mass storage devices affects type
of media failures that can be tolerated
Related to Availability: extent to which a (possibly
distributed) system can provide service despite
failure
Non-stop DBMS (mirrored disks)
Recovery based DBMS (log)
289
Transactions – Isolation Levels
Strongest isolation level: SERIALIZABLE
Complete isolation
SQL default
Weaker isolation levels: REPEATABLE READ,
READ COMMITTED, READ UNCOMMITTED
Increase performance by eliminating overhead and
allowing higher degrees of concurrency
Trade-off: sometimes you get the wrong answer
Transactions – Example Schema
CREATE TABLE Account
(accno INTEGER NOT NULL PRIMARY KEY,
name CHAR(30) NOT NULL,
balance FLOAT NOT NULL
CHECK(balance >= 0));
Transactions – Read Uncommitted
Can read dirty data
A data item is dirty if it is written by an uncommitted
transaction
Problem: What if the transaction that wrote the dirty data
eventually aborts?
Example: wrong average
-- T1: -- T2:
UPDATE Account
SET balance = balance - 200
WHERE accno = 142857; SELECT AVG(balance)
FROM Account;
ROLLBACK;
COMMIT;
Transactions – Read Committed
No dirty reads, but non-repeatable reads possible
Reading the same data item twice can produce different results
Example: different averages
-- T1: -- T2:
SELECT AVG(balance)
FROM Account;
UPDATE Account
SET balance = balance - 200
WHERE accno = 142857;
COMMIT;
SELECT AVG(balance)
FROM Account;
COMMIT;
Transactions – Repeatable Read
Reads are repeatable, but may see phantoms
A phantom read occurs when, in the course of a transaction, two
identical queries are executed, and the collection of rows returned by
the second query is different from the first.
Example: different average (still!)
-- T1: -- T2:
SELECT AVG(balance)
FROM Account;
INSERT INTO Account
VALUES(428571, 1000);
COMMIT;
SELECT AVG(balance)
FROM Account;
COMMIT;
Transactions - Serializable
None of these problems can happen
Transactions – Summary of SQL
Isolation Levels
Isolation Level/Anomaly Dirty Reads Non-Repeatable Reads Phantoms
JDBC Driver
JDBC Driver Installation
Must download the driver, then add the .jar file to
your $CLASSPATH or NetBeans project
To set up your classpath on Windows
Control panel
Search for Environment Variables
Add the jar file to your CLASSPATH variable
JDBC Driver Management
All drivers are managed by the DriverManager
class
Example - loading an Oracle JDBC driver:
In the Java code:
Class.forName(“oracle.jdbc.driver.OracleDriver”)
Driver class names:
Oracle: oracle.jdbc.driver.OracleDriver
MySQL: com.mysql.jdbc.Driver
MS SQL Server:
com.microsoft.jdbc.sqlserver.SQLServerDriver
Establishing a Connection
• Create a Connection object
• Use the DriverManager to grab a connection
with the getConnection method
• Necessary to follow exact connection syntax
• Problem 1: the parameter syntax for
getConnection varies between JDBC drivers
• Problem 2: one driver can have several
different legal syntaxes
Establishing a Connection (cont.)
Oracle Example
• Connection con =
DriverManager.getConnection(string,
“username", “password");
• what to supply for string ?
• “jdbc:oracle:thin:@augur.seas.gwu.edu:1521:orcl10g2”
ResultSet rs=pstmt.executeQuery();
// rs is now a cursor
While (rs.next()) {
// process the data
}
ResultSets (cont.)
A ResultSet is a very powerful cursor:
previous(): moves one row back
absolute(int num): moves to the row with the
specified number
relative (int num): moves forward or backward
first() and last()
Retrieving the results
The ResultSet interface sports getDate, getInt,
getString, and so on to convert from the
database representation of the data to a Java
representation of the data.
Matching Java-SQL Data Types
SQL Type Java class ResultSet get method
BIT Boolean getBoolean()
CHAR String getString()
VARCHAR String getString()
DOUBLE Double getDouble()
FLOAT Double getDouble()
INTEGER Integer getInt()
REAL Double getFloat()
DATE java.sql.Date getDate()
TIME java.sql.Time getTime()
TIMESTAMP java.sql.TimeStamp getTimestamp()
Data About the Data
ResultSetMetaData rsmd = rs.getMetaData();
Use the import: java.sql.* for the ResultSetMetaData
interface.
For more information, see the documentation at:
https://docs.oracle.com/javase/8/docs/api/java/sql/Res
ultSetMetaData.html.
What I’m going to cover is just a fraction of what this
class can do.
Bear in mind that these methods are all operating on
the entire result set, not any given row in the result
set.
Useful methods
int – getColumnCount() returns the # of columns in the
result set. Good first call to make when looping through
the meta data for a given result set.
These utilities all start at 1 for the first column. Do not let
that confuse you with starting at 0 for arrays.
int – getColumnDisplaySize (int) – returns the
designated column’s maximum width.
String – getColumnName (int)
int getColumnType (int) – returns an integer
corresponding to the column type. See java.sql.Types
for the list of valid types that can be returned.
String getColumnTypeName (int) – returns name of the
column type.
You Also Might be Interested In:
int isNullable (int) – you receive an integer
{columnNoNulls, columnNullable,
columnNullableUnknown} in return
String getTableName (int) – returns the table
name from the specified column index
boolean isReadOnly (int)
boolean isWriteable (int)
JDBC: Exceptions and Warnings
Most of java.sql can throw and SQLException if
an error occurs (use try/catch blocks to find
connection problems)
SQLWarning is a subclass of SQLException; not
as severe (they are not thrown and their
existence has to be explicitly tested)
JDBC MySQL example:
Excellent instructions can be found at:
http://www.tutorialspoint.com/jdbc/
Sample Code can be found at:
http://www.csulb.edu/~mopkins/cecs323/FirstEx
ample.java