Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2008

Tip: Looking for answers? Try searching our database.

Consistently insert into two MySQL tables.

Thread view: 
mebe - 23 Apr 2008 19:55 GMT
Hi Folks,

i am in need of some advice about how to consistently add data into
two
MySQL tables. The database looks like this:

   CREATE TABLE Items (
                itemId    INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                itemName  VARCHAR(255) DEFAULT NULL,
                FULLTEXT (itemName)

   CREATE TABLE ViewCount (
                itemId        INT UNSIGNED PRIMARY KEY,
                viewCnt       INT UNSIGNED DEFAULT 0)
                TYPE = InnoDB

Both tables are connected through the column 'itemId'.
How would a SQL statement look that:
1. Inserts a new row into table 'Items'
2. Query Items.itemId, (note that itemName is not unique)
3. Inserts a new row into table 'ViewCount' using
  ViewCount.itemId = Items.itemId
4. Does the whole operation in a thread save way.

The main problems I've encountered are how can i query the newly
inserted Items.itemId if all other columns are not unique? And how do
i make the two inserts plus the one select atomic/thread save?
I appreciate any help,
Thanks in advance!
Laurent D.A.M. MENTEN - 23 Apr 2008 21:13 GMT
mebe a écrit :
> Hi Folks,
>
[quoted text clipped - 25 lines]
> I appreciate any help,
> Thanks in advance!

AFAIK mysql does not support transactions... maybe you can switch to
postgresql?
Arne Vajhøj - 23 Apr 2008 23:15 GMT
> mebe a écrit :
>> i am in need of some advice about how to consistently add data into
[quoted text clipped - 26 lines]
> AFAIK mysql does not support transactions... maybe you can switch to
> postgresql?

MySQL support transactions on InnoDB tables but not for MyISAM
tables.

It seems from the above that it is InnoDB tables.

Arne
Arne Vajhøj - 23 Apr 2008 23:20 GMT
> i am in need of some advice about how to consistently add data into
> two
[quoted text clipped - 21 lines]
> inserted Items.itemId if all other columns are not unique? And how do
> i make the two inserts plus the one select atomic/thread save?

You do not query. You can get the last auto increment generated value
for the connection.

Something like:

con.setAutoCommit(false);
...
stmt.executeUpdate("INSERT INTO items(itenname) VALUES('bla bla')");
stmt.executeUpdate("INSERT INTO viewcount VALUES(LAST_INSERT_ID(), 1)");
...
con.commit();

Arne

PS: PreparedStatement would be better and you can get the auto increment
    value in a non MySQL-specific way, but you get the idea.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.