I’m attempting to create a database application for a bookstore.
In the spec:
“After registration, a user can order one or more books. The total amount of the order should be reported to him/her. A user may order multiple copies of a book, one or more times.”
The total amount bit doesn’t seem that bad.
Here are the two relevant tables:
Order(orderID: INT(11), loginName: VARCHAR(45), date: DATETIME)
BooksOrdered(orderID: INT(11), ISBN: VARCHAR(45), count: INT(11))
The bolded items are the PKs.
Now, here’s the deal: Our database doesn’t allow FKs for performance issues, so I’m supposed to mimic them in the application layer. BooksOrdered.orderID is really a FK, just not stated as much. Additionally, Order.orderID is autoincrementing.
I’m trying to figure out how to make sure the orderID’s match up (mimic the foreign key constraint) when inserting new orders.
Basically, I don’t understand how I can efficiently insert an order given an ArrayList (BookOrder will simply hold an ISBN and a count).
One solution I’ve thought of is:
Every time I want to add a row into BooksOrdered, simply grab the largest orderID from Order. This seems inefficient, and I think there might be other issues with it (concurrent access/insertions?).
Therefore I ask you, how should I go about mimicking this autoincrementing FK? Or would you suggest I restructure my tables?
Or is there a way I can magically grab the last orderID I insert into Order… say in the process of inserting?
EDIT:
Alright, so I have the following incomplete function:
public int orderBooks(String loginName, ArrayList<BookOrder> bookOrders) throws Exception{
int id = -1;
//Working on the date situation
java.util.Date today = new java.util.Date();
Timestamp sqlDate = new java.sql.Timestamp(today.getTime());
Connector con=null;
String query = "INSERT INTO Order (loginName, date) " +
" VALUES ('"+loginName+"', "+sqlDate+")";
ResultSet rs;
try{
con= new Connector();
con.stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
rs = con.stmt.getGeneratedKeys();
if (rs.next()) {
id = rs.getInt(1);
} else {
throw(new Exception()); // mmm...
// throw an exception from here
}
con.stmt.close();
} catch(Exception e) {
System.err.println("Unable to execute query:"+query+"\n");
System.err.println(e.getMessage());
throw(e);
}
finally
{
if (con != null)
{
try
{
con.closeConnection();
}
catch (Exception e) { /* ignore close errors */ }
}
}
return id;
}
Now, where should I put id = stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);? Did I put it in the right spot? (continues to work on building a testable prototype)
Which language are you using? It will likely have a function to retrieve the last autoincremented value from your current database handle. In PHP, for example, you would use
mysql_insert_id()(doc). In Perl DBI you can uselast_insert_id(). (DBI Doc)For have you want to set the flag in executeUpdate:
There’s also the
stmt.getGeneratedKeys()function. See the docs. The executeUpdate entry is right below getGeneratedKeys.