Experts Round Table Network
Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:
Password:
Remember me
Forgot your password?
Register
getting new record id on autoincrementing field
Welcome,
Guest
. Please
login
or
register
.
December 03, 2008, 11:20:30 PM
11306
Posts in
1249
Topics by
499
Members
Latest Member:
haulaslemycle
Home
Help
Search
Login
Register
Experts Round Table Network
|
Databases
|
MySQL
|
getting new record id on autoincrementing field
« previous
next »
Pages:
[
1
]
Print
Author
Topic: getting new record id on autoincrementing field (Read 415 times)
klitscher
Offline
Posts: 24
getting new record id on autoincrementing field
«
on:
October 26, 2006, 11:14:05 PM »
Hey there,
I am inserting a new row into a table where the record_id is an autoincrementing field. I have other data that has to be inserted into other tables with the record_id for that row, but don't know how to grab the new id to use to insert the rows in the other tables...would I just use
"SELECT record_id FROM table ORDER BY record_id DESC LIMIT 0,1;"
Thanks in advance...
Ken
Logged
COBOLdinosaur
ERT.com Admin
Offline
Posts: 481
Re: getting new record id on autoincrementing field
«
Reply #1 on:
October 27, 2006, 04:33:20 AM »
SELECT MAX(record_id) FROM table;
Logged
VGR
Mentor
Offline
Posts: 682
Re: getting new record id on autoincrementing field
«
Reply #2 on:
October 27, 2006, 06:31:23 AM »
yo. classical problem.
the ***theoretical*** solution is to use mysql_last_insert_id() function from PHP, ***but*** you could just as well rely - as I do - on the select you provided : select id from yourtable order by id desc limit 1;". Moreover, it's faster than the max() computation. You can just say "LIMIT 1" by the way.
This said, to be honest you run the risk of a second replace or insert having taken place between YOUR insert and YOUR select, as your two operations are not atomical. You can either use a TRANSACTION, or just LOCK the table if you really want to ensure 0 risk (a too high price to pay IMHO for classical webservers doing 99% selects and 1% updates/inserts/replaces/deletes and having medium traffic & activity)
best regards
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
klitscher
Offline
Posts: 24
Re: getting new record id on autoincrementing field
«
Reply #3 on:
October 28, 2006, 09:47:15 PM »
Thanks for the help gents.
I am not too worried about an insert taking place between the insert and the select...this isn't a high usage script that will be running and we have a total userbase of under 200. I appreciate the help...kinda feel like you all are writing half my script...I'll spread the word.
Ken
Logged
rdivilbiss
Governing Council Member
Offline
Posts: 414
Re: getting new record id on autoincrementing field
«
Reply #4 on:
October 29, 2006, 03:48:36 PM »
Just to add to the already good advice, MySQL 5+ now has @@IDENTITY like other SQL databases.
You should be able to execute a multiple query like this:
"INSERT INTO foo (auto,text) VALUES(NULL,'text'); SELECT @@IDENTITY;"
Which is worthless from PHP, by the way.
«
Last Edit: October 29, 2006, 04:04:43 PM by rdivilbiss
»
Logged
Rod
VGR
Mentor
Offline
Posts: 682
Re: getting new record id on autoincrementing field
«
Reply #5 on:
October 30, 2006, 06:49:53 AM »
to complete what my colleague wrote, you have since MySql 3.23.x the MySql function LAST_INSERT_ID() that could be used in a multi-statement query like above.
the LAST_INSERT_ID() type-of function is not a SQL-99 requirement, even optional, and is not supported by any other major RDBMS vendor, at least under a function form (there may be "macros" like the @@identity above, a bit like PL/SQL extensions to Oracle, available only in the console/TOAD and not at the API level).
I only checked postgresql, oracle, mysql, sql-server for SQL-99 features IDs B031 to P003 , so I may have missed something.
Aaaah sorry last minute modification : SQL-Server has a ident_current(`table_name') function as a vendor extension to SQL-99
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
Pages:
[
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
ERT 1.5
-----------------------------
=> Round Table Learning Center
=> Bug reports
-----------------------------
Legacy
-----------------------------
=> The next level
=> History of ERT
-----------------------------
Community Affairs
-----------------------------
=> Introductions
=> Ballot Box
===> Closed Polls
=> Soapbox
=> Propose and Consult
===> Propose and Consult...CLOSED
-----------------------------
Bits and Bytes
-----------------------------
=> Tips, Tricks, Snippets, Tidbits And General Pearls Of Wisdom
-----------------------------
Serverside Technology
-----------------------------
=> PHP
=> ASP
-----------------------------
Webservers
-----------------------------
=> Apache
=> IIS
-----------------------------
Databases
-----------------------------
=> MySQL
=> Access
=> MS SQL Server
-----------------------------
Clientside Technology
-----------------------------
=> HTML
=> CSS
=> Javascript
=> Flash
=> WAP/WML
-----------------------------
Web Technologies
-----------------------------
=> General Web Dev
=> Web Standards
=> XML
=> Online Marketing
-----------------------------
Graphics
-----------------------------
=> Graphics Design and Animation
-----------------------------
Programming
-----------------------------
=> .NET
=> JAVA
=> MS DOS Batch Scripting
=> Mathematics
=> C & C++
=> VB
=> Delphi
=> Algorithm design
-----------------------------
Operating Systems
-----------------------------
=> Windows (General)
=> NT Based (2K, 2K-03, NT, XP, Vista)
=> Open Source (All)
-----------------------------
Hardware
-----------------------------
=> Hardware General
=> Gamers Hardware (Advanced)
-----------------------------
Networking
-----------------------------
=> Home (small)
=> Office (large)
=> Internet
-----------------------------
Security
-----------------------------
=> General Security Issues
-----------------------------
Rants/Opinions/Proposals
-----------------------------
=> Site operation
Powered by SMF 1.1 RC2
|
SMF © 2001-2005, Lewis Media
Joomla Bridge by
JoomlaHacks.com