User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 6 Mar 2003, 11:46   #1
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
MySQL badness

Okay, I've been a busy bunny for the last week putting together a webby for my galaxy (hence my absence) but I ran into one problem that I still haven't found an elegant solution to.

I want to insert data into table X with an incrementing primary key, I then want to insert data into table Y but using the primary key value from table X as an identifier. Did that make sense?

Right now I have to insert data into table X, select all entries by the current user, pick the highest id returned, and use that as the id for all fields entered into table Y. It works but it's ugly, kludgey, and generally horrible.

Any ideas on an elegant solution?
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 6 Mar 2003, 12:36   #2
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Tbh, that's the way I tend to deal with these things. I really need to look into this as it's bothering me too. I'll let you know...

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 6 Mar 2003, 12:40   #3
Pitchfork
Tourist
 
Join Date: Jun 2001
Location: moon
Posts: 90
Pitchfork is an unknown quantity at this point
Re: MySQL badness

Quote:
Originally posted by Gayle29uk
Okay, I've been a busy bunny for the last week putting together a webby for my galaxy (hence my absence) but I ran into one problem that I still haven't found an elegant solution to.

I want to insert data into table X with an incrementing primary key, I then want to insert data into table Y but using the primary key value from table X as an identifier. Did that make sense?

Right now I have to insert data into table X, select all entries by the current user, pick the highest id returned, and use that as the id for all fields entered into table Y. It works but it's ugly, kludgey, and generally horrible.

Any ideas on an elegant solution?
I'm not quite sure, what you mean? Is the key from table X a foreign key in table Y? And what relation is that?
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
Pitchfork is offline   Reply With Quote
Unread 6 Mar 2003, 12:56   #4
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Re: Re: MySQL badness

Quote:
Originally posted by Pitchfork
I'm not quite sure, what you mean? Is the key from table X a foreign key in table Y? And what relation is that?
Ok, I'll try to be clearer.

Table X is a list of combat reports, each combat report has an ID number which is assigned by MySQL when the record is created and acts as the primary key.

Table Y is a list of elements that make up the combat report in X (ships present, roids taken etc). There is no primary key, each element has a non-unique id that identifies it as being a component of a combat report in table X.

Make sense now?
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 6 Mar 2003, 13:13   #5
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
So its just a standard referential 1-many relationship then.

Just use the last_insert_id() function after you insert into the news table, to get the ID. Then use that ID to insert the entries. The call to insert the news report, and the ID would obviously need to be transaction locked in a concurrent environment (Unless each db connection is independant and not from a pool, ala the commands description below).

Btw, I looked the command up, case anyone shockingly thinks ive lowered myself to use MySQL.

Quote:
MySQL has a built in function to return the value of the auto-increment column from the last insert on the connection. last_insert_id() will return the value of an auto_increment column from the last insert when the insert specifically referred to the column and inserted a null or 0 on the current connection, so other connections will not affect the returned value.
__________________
Chimney Pots.
Raging.Retard is offline   Reply With Quote
Unread 6 Mar 2003, 13:15   #6
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
http://www.php.net/manual/en/functio...-insert-id.php

http://www.mysql.com/doc/en/mysql_insert_id.html

LAST_INSERT_ID ?
queball is offline   Reply With Quote
Unread 6 Mar 2003, 13:16   #7
Pitchfork
Tourist
 
Join Date: Jun 2001
Location: moon
Posts: 90
Pitchfork is an unknown quantity at this point
I am not sure about MySQL's foreign key behaviour, because I tend to use PostgreSQL, but from what I read here something like
Code:
CREATE TABLE X (
    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    something SOME_DATATYPE,
    PRIMARY KEY (id)
);

CREATE TABLE Y (
    something SOME_DATATYPE,
    x_id SMALLINT UNSIGNED NOT NULL REFERENCES X(id)
);

INSERT INTO X (something) VALUES ($something);

INSERT INTO Y (something, x_id) VALUES ($something, LAST_INSERT_ID());
should work with MySQL 3.23.44 and up using InnoDB storage engine.
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
Pitchfork is offline   Reply With Quote
Unread 6 Mar 2003, 13:47   #8
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
You're all wonderful \o/

Needless to say you've helped to greatly clean up sections of my code
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 6 Mar 2003, 13:49   #9
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
NO!

On a busy site, there's a small chance that the last ID will belong to another user, think about it!

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 6 Mar 2003, 13:54   #10
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by Mong
NO!

On a busy site, there's a small chance that the last ID will belong to another user, think about it!

M.
Apparently it's the last value issued on the current connection, not the last one issued globally.
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 6 Mar 2003, 13:57   #11
Pineapple
Banned
 
Join Date: Jul 2001
Posts: 98
Pineapple is an unknown quantity at this point
the only problem youd have is if you used persistant connections (i presume)
Pineapple is offline   Reply With Quote
Unread 6 Mar 2003, 14:19   #12
Pitchfork
Tourist
 
Join Date: Jun 2001
Location: moon
Posts: 90
Pitchfork is an unknown quantity at this point
Quote:
Originally posted by Mong
NO!

On a busy site, there's a small chance that the last ID will belong to another user, think about it!

M.
Code:
BEGIN;

<insert your insert-statements here>

COMMIT;
Should solve that.
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
Pitchfork is offline   Reply With Quote
Unread 6 Mar 2003, 16:03   #13
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
Quote:
Originally posted by Raging.Retard
The call to insert the news report, and the ID would obviously need to be transaction locked in a concurrent environment (Unless each db connection is independant and not from a pool
Quote:
Originally posted by Mong
NO!

On a busy site, there's a small chance that the last ID will belong to another user, think about it!

M.

MONG IN NOT PAYING ATTENTION TO PREVIOUS POSTS SHOCKAH.
__________________
Chimney Pots.
Raging.Retard is offline   Reply With Quote
Unread 6 Mar 2003, 16:36   #14
CjC
Throwing Shapes
 
CjC's Avatar
 
Join Date: Apr 2000
Posts: 797
CjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant futureCjC has a brilliant future
Not being a MySQL expert, I assume this is refering to the same kind of thing in MySQL as the @@IDENTITY function in T-SQL(MS SQL Server) - to recall the next Primary key ID from a table (in your case 'X') before its created? Just curious.
CjC is offline   Reply With Quote
Unread 6 Mar 2003, 17:52   #15
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by Raging.Retard
Unless each db connection is independant and not from a pool
Or pooled and independent (from any other process), like the most common setup.
queball is offline   Reply With Quote
Unread 7 Mar 2003, 00:49   #16
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Quote:
Originally posted by Raging.Retard
MONG IN NOT PAYING ATTENTION TO PREVIOUS POSTS SHOCKAH.
It doesn't happen much, treasure it, print it and frame it

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 01:51.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018