Berkeley CSUA MOTD:Entry 13368
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/24 [General] UID:1000 Activity:popular
5/24    

2004/4/25-26 [Computer/SW/Database] UID:13368 Activity:nil
4/24    In SQL, when I insert a row with sequence (auto_increment), I don't
        know which sequence it is assigned to. How do I find out what number
        I just inserted? I guess I could try (SELECT max(id) ...) then insert
        max+1, but if I have 2 people doing it, I may be screwed. By the way
        I'm using mysql so there is no transaction...
        \_ Um, the whole point of auto_increment fields is that *you* don't
           have to worry about them.  The way you phrased your question seems
           to suggest you're trying to perform an insert where you specify the
           auto_increment.  That's just silly.
        \_ Take a look at:
           http://dev.mysql.com/doc/mysql/en/Getting_unique_ID.html
           http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html
           (If you ever switch to postgres you will have to rewrite this
           to use curval/nextval instead)
           to use curval/netval instead)
                            \_ fixed.
        \_ That's funny, I'm using transactions on some InnoDB tables with
           mysql 3....
                         \_ probably a typo.  should be nextval
2025/05/24 [General] UID:1000 Activity:popular
5/24    

You may also be interested in these entries...
2013/4/9-5/18 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Apps, Computer/SW/Languages/Perl] UID:54650 Activity:nil
4/04    Is there a good way to diff 2 files that consist of columns of
        floating point numbers, such that it only tells me if there's a
        difference if the numbers on a given line differ by at least a given
        ratio?  Say, 1%?
        \_ Use Excel.
           1. Open foo.txt in Excel.  It should convert all numbers to cells in
	...
2011/12/29-2012/2/6 [Computer/SW/Database] UID:54274 Activity:nil
12/29   Is it worthwhile to use ext4 on VMs? Is Journaling necessary on VMs?
         \_ what about DBs?  I read somewhere ext3 was better for DB voumes (mysql)
	...
2010/11/21-2011/1/13 [Computer/SW/Languages/Web] UID:53988 Activity:moderate
11/21   Lifehacker's recommending Dreamhost as a personal web hosting service.
        Apart from csua, who do you guys use? --erikred
        \_ What do you want to use it for? Do you need CGI or PHP?  My
           brother worked for Dreamhost and said they are unethical. In
           fact, he sued them. This refers to their treatment of customers
           and employees both. I don't know who  or what "Lifehacker" is,
	...
2010/8/12-9/7 [Computer/SW/Languages/Perl] UID:53922 Activity:nil
8/12    Ruby coders, do you mostly DIY your stuff or use the ruby libs out
        there?   How is their quality compared to other libs you have used
        for other langs?  Thx.
        \_ I use Ruby for hobby stuff, etc.  I use libraries for system stuff
           (web access, process, etc.) but that's about it.  Perl libraries are
           much better/more complete.  I assume because of the maturity and
	...
2010/1/22-30 [Computer/HW/Laptop, Computer/SW/OS/OsX] UID:53655 Activity:high
1/22    looking to buy a new development laptop
        needs ssdrive, >6 hr possible batt life, and runs linux reasonably
        Anyone have a recommendation? Thx.
        \_ thinkpad t23 w ssdrive and battery inplace of drive bay
        \_ Ever wondered what RICHARD STALLMAN uses for a laptop?  Well,
           wonder no more!
	...
2009/10/27-11/3 [Computer/HW/Drives] UID:53474 Activity:nil
10/27   I just read an article that Facebook had moved their database
        to all SSD to speed throughput, but now I can't find it. Has
        anyone else seen this? Any experience with doing this? -ausman
        \_ I hope you're not running mission critical data:
           http://ask.slashdot.org/story/09/10/27/1559248/Reliability-of-PC-Flash-SSDs?from=rss
        \_ Do you have any idea how much storage space is used by Facebook,
	...
2009/9/23-10/5 [Computer/SW/Database] UID:53392 Activity:nil
9/23    I never took CS188, is there a good book that's an intro to formal
        database theory, normalization, etc.?  I've got experience with SQL
        (MySQL & MSSQL), and understand tables, etc.
        \_ You mean CS186?
           \_ Oops, yah.  188 is AI or something?
              \_ That's right.
	...
Cache (628 bytes)
dev.mysql.com/doc/mysql/en/Getting_unique_ID.html
When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID statement mysql_query and retrieving the value from the result set returned by the statement. For LAST_INSERT_ID , the most recently generated ID is maintained in the server on a per-connection basis. It will not even be changed if you update another AUTO_INCREMENT column with a non-magic value that is, a value that is not NULL and not 0 . If you want to use the ID that was generated for one table and insert it into a second table, you can use SQL statements like this: INSERT INTO foo auto,text VALUESNULL,text;
Cache (1471 bytes)
dev.mysql.com/doc/mysql/en/mysql_insert_id.html
INSERT statements that generate an AUTO_INCREMENT value by inserting LAST_INSERT_IDexpr into any column. INSERT statements that generate an AUTO_INCREMENT value by updating any column to LAST_INSERT_IDexpr . The value of mysql_insert_id is not affected by statements such as SELECT that return a result set. If the previous statement returned an error, the value of mysql_insert_id is undefined. Note that mysql_insert_id returns 0 if the previous statement does not use an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id immediately after the statement that generates the value. The value of mysql_insert_id is affected only by statements issued within the current client connection. Also note that the value of the SQL LAST_INSERT_ID function always contains the most recently generated AUTO_INCREMENT value, and is not reset between statements because the value of that function is maintained in the server. Another difference is that LAST_INSERT_ID is not updated if you set an AUTO_INCREMENT column to a specific non-special value. The reason for the difference between LAST_INSERT_ID and mysql_insert_id is that LAST_INSERT_ID is made easy to use in scripts while mysql_insert_id tries to provide a little more exact information of what happens to the AUTO_INCREMENT column. Previous / Next / Up / Table of Contents User Comments Add your own comment. Top / Previous / Next / Up / Table of Contents 1995-2004 MySQL AB.