Sample Books Database - Four Tables
Sample Books Database - Four Tables
Field Description
authorID Author’s ID number in the database. In the books database, this integer
field is defined as an autoincremented field. For each new record inserted
in this table, the database automatically increments the authorID value
to ensure that each record has a unique authorID. This field represents
the table’s primary key.
firstName Author’s first name (a string).
lastName Author’s last name (a string).
Field Description
publisherID The publisher’s ID number in the database. This autoincremented
integer is the table’s primary-key field.
publisherName The name of the publisher (a string).
publisherID publisherName
1 Prentice Hall
2 Prentice Hall PTG
The authorISBN Table
Field Description
authorID The author’s ID number, which allows the database to associate
each book with a specific author. The integer ID number in this
field must also appear in the authors table.
isbn The ISBN number for a book (a string).
Example Data for the authorISBN Table
authorID isbn authorID isbn
1 0130895725 2 0139163050
1 0132261197 2 013028419x
1 0130895717 2 0130161438
1 0135289106 2 0130856118
1 0139163050 2 0130125075
1 013028419x 2 0138993947
1 0130161438 2 0130852473
1 0130856118 2 0130829277
1 0130125075 2 0134569555
1 0138993947 2 0130829293
1 0130852473 2 0130284173
1 0130829277 2 0130284181
1 0134569555 2 0130895601
1 0130829293 3 013028419x
1 0130284173 3 0130161438
1 0130284181 3 0130856118
1 0130895601 3 0134569555
2 0130895725 3 0130829293
2 0132261197 3 0130284173
2 0130895717 3 0130284181
2 0135289106 4 0130895601
The titles Table
Field Description
isbn ISBN number of the book (a string).
title Title of the book (a string).
editionNumber Edition number of the book (an integer).
copyright Copyright year of the book (a string).
publisherID Publisher’s ID number (an integer). This value must correspond to an
ID number in the publishers table.
imageFile Name of the file containing the book’s cover image (a string).
price Suggested retail price of the book (a real number). [Note: The prices
shown in this book are for example purposes only.]
Sample Data from the titles Table
isbn title edition- copy- publish- image-File price
Number right erID
0 1 3 0 8 9 5 7 2 5 C H o w to 3 2001 1 chtp3.jpg 6 9 .9 5
P ro g ra m
0 1 3 2 2 6 1 1 9 7 C H o w to 2 1994 1 chtp2.jpg 4 9 .9 5
P ro g ra m
0 1 3 0 8 9 5 7 1 7 C + + H o w to 3 2001 1 cpphtp3.jpg 6 9 .9 5
P ro g ra m
0 1 3 5 2 8 9 1 0 6 C + + H o w to 2 1998 1 cpphtp2.jpg 4 9 .9 5
P ro g ra m
0 1 3 9 1 6 3 0 5 0 T h e C o m p le te 3 2001 2 cppctc3.jpg 1 0 9 .9 5
C + + T ra in in g
C o u rs e
0 1 3 0 2 8 4 1 9 x e -B u s in e s s a n d e - 1 2001 1 e b e c h t p 1 . j p g 6 9 .9 5
C o m m e rc e H o w
to P ro g ra m
0 1 3 0 1 6 1 4 3 8 In te rn e t a n d 1 2000 1 iw3htp1.jpg 6 9 .9 5
W o rld W id e W e b
H o w to P ro g ra m
0 1 3 0 8 5 6 1 1 8 T h e C o m p le te 1 2000 2 iw3ctc1.jpg 1 0 9 .9 5
In te rn e t a n d
W o rld W id e W e b
P ro g ra m m in g
T ra in in g C o u rs e
0 1 3 0 1 2 5 0 7 5 J a v a H o w to 3 2000 1 jhtp3.jpg 6 9 .9 5
P ro g ra m (J a v a 2 )
0 1 3 8 9 9 3 9 4 7 J a v a H o w to 2 1998 1 jhtp2.jpg 4 9 .9 5
P ro g ra m (J a v a
1 .1 )
0 1 3 0 8 5 2 4 7 3 T h e C o m p le te 3 2000 2 j a v a c t c 3 . j p g 1 0 9 .9 5
J a v a 2 T ra in in g
C o u rs e
The books Database – table relationships
• SQL keywords
SQL keyword Description
SELECT Select (retrieve) fields from one or more tables.
FROM Tables from which to get fields. Required in every SELECT.
WHERE Criteria for selection that determine the rows to be retrieved.
GROUP BY Criteria for grouping records.
ORDER BY Criteria for ordering records.
INSERT INTO Insert data into a specified table.
UPDATE Update data in a specified table.
DELETE FROM Delete data from a specified table.
Basic SELECT Query
4. Create a Statement
Statement statement = connection.createStatement();
5. Execute a Query
String query = "SELECT col1, col2, col3 FROM
sometable";
ResultSet resultSet = statement.executeQuery(query);
Connection connection =
DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement();
String query =
"SELECT col1, col2, col3 FROM testDB";
// Print results
while(results.next()) {
System.out.println
(pad(results.getString(1), 4) + " | " +
pad(results.getString(2), 4) + " | " +
results.getString(3) + "\n" + divider);
}
connection.close();
} catch(ClassNotFoundException cnfe) {
System.out.println("No such class: " + driver);
} catch(SQLException se) {
System.out.println("SQLException: " + se);
}
}
...