{"id":37,"date":"2006-08-22T20:43:15","date_gmt":"2006-08-22T20:43:15","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=37"},"modified":"2006-09-01T14:14:17","modified_gmt":"2006-09-01T14:14:17","slug":"last_insert_id-and-multi-row-inserts","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=37","title":{"rendered":"LAST_INSERT_ID and multi-row inserts"},"content":{"rendered":"<p>A bit of a quiz to see if you&#8217;re paying attention:<\/p>\n<p>First, we&#8217;ll set up a couple tables with some auto increment fields:<\/p>\n<pre>CREATE TABLE t1 (id INT AUTO_INCREMENT, INDEX(id));\r\nCREATE TABLE t2 (id INT AUTO_INCREMENT, refT1 INT, INDEX(id));<\/pre>\n<p>Next, we&#8217;ll insert a row to one table:<\/p>\n<pre>INSERT INTO t1 VALUES (NULL);<\/pre>\n<p>Then, we&#8217;ll see what happens when we insert two rows using LAST_INSERT_ID():<\/p>\n<pre>INSERT INTO t2 VALUES (LAST_INSERT_ID(), 1), (LAST_INSERT_ID(), 2);<\/pre>\n<p>What happens here? Did the insert go ok, or did we get an error? What happens if we now issue<\/p>\n<pre>SELECT * FROM t2;<\/pre>\n<p><spoiler 'Show Answer' 'Hide Answer'><\/p>\n<p>No errors, because we defined the auto_increment field for t2 not as a primary or unique key, but just with index. Remember that LAST_INSERT_ID() only picks up the last value you inserted using NULL, and not explicitly defined values. So we get two copies of the value &#8216;1&#8217; in our id field.<\/p>\n<pre>+----+-------+\r\n| id | refT1 |\r\n+----+-------+\r\n|  1 |     1 | \r\n|  1 |     2 | \r\n+----+-------+<\/pre>\n<p><\/spoiler><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A bit of a quiz to see if you&#8217;re paying attention: First, we&#8217;ll set up a couple tables with some auto increment fields: CREATE TABLE t1 (id INT AUTO_INCREMENT, INDEX(id)); CREATE TABLE t2 (id INT AUTO_INCREMENT, refT1 INT, INDEX(id)); Next, we&#8217;ll insert a row to one table: INSERT INTO t1 VALUES (NULL); Then, we&#8217;ll see [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[6],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-gotchas"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-B","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":0,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}