On 10/07/13 18:30, Peter H. Coffin wrote:
> On Wed, 10 Jul 2013 10:19:22 -0700 (PDT), SpreadTooThin wrote:
>> I want to insert a record into a table and then get back the Unique ID for the Last Inserted Row.
>>
>> CREATE TABLE IF NOT EXISTS `MyDatabase`.`JobID` (
>> `JobID` INT NOT NULL AUTO_INCREMENT ,
>> `SubmissionTime` DATETIME NOT NULL ,
>> PRIMARY KEY (`JobID`) )
>> ENGINE = InnoDB;
>>
>> USE `MyDatabase`;
>> INSERT INTO `JobID` (`JobID`, `SubmissionTime`) Values (NULL, `2013-07-10 11:11:11`);
>>
>>
>> So how do I get back the JobID that was just added safely?
>>
>> As Submission TIme may not be unique (it only has 1 second granularity.) I can't get the JobID based on the Submission Time...
>>
>> If I've understood the documentation then the following should return the JobID I just added.
>>
>> SELECT LAST_INSERT_ID();
>>
>> But should I have some table locking around the insert and select?
>> I've never used table locking so I don't know a lot about it...
>>
>
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
>
> Short answer: you don't need to lock anything because that
> last_insert_id() function is a connection-aware thing that does the
> right thing for you.
>
>
to amplify that, its stored inside the connection state that you opened.
(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.