{"id":5921,"date":"2015-07-28T18:44:15","date_gmt":"2015-07-28T10:44:15","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=5921"},"modified":"2015-07-28T18:44:40","modified_gmt":"2015-07-28T10:44:40","slug":"mysql%e5%88%86%e5%ba%93%e5%88%86%e8%a1%a8%e7%9a%84%e5%85%a8%e5%b1%80%e5%94%af%e4%b8%80id%e7%94%9f%e6%88%90%e5%99%a8%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/5921.html","title":{"rendered":"MySQL\u5206\u5e93\u5206\u8868\u7684\u5168\u5c40\u552f\u4e00ID\u751f\u6210\u5668\u65b9\u6848"},"content":{"rendered":"<p>\u501f\u7528MySQL \u7684 auto_increment \u7279\u6027\u53ef\u4ee5\u4ea7\u751f\u552f\u4e00\u7684\u53ef\u9760ID\u3002<\/p>\n<p>\u8868\u5b9a\u4e49\uff0c\u5173\u952e\u5728\u4e8eauto_increment\uff0c\u548cUNIQUE KEY\u7684\u8bbe\u7f6e\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE `Tickets64` (\r\n  `id` bigint(20) unsigned NOT NULL auto_increment,\r\n  `stub` char(1) NOT NULL default '',\r\n  PRIMARY KEY  (`id`),\r\n  UNIQUE KEY `stub` (`stub`)\r\n) ENGINE=MyISAM\r\n<\/pre>\n<p>\u9700\u8981\u4f7f\u7528\u65f6\uff0c\u5de7\u7528replace into\u8bed\u6cd5\u6765\u83b7\u53d6\u503c\uff0c\u7ed3\u5408\u8868\u5b9a\u4e49\u7684UNIQUE KEY\uff0c\u786e\u4fdd\u4e86\u4e00\u6761\u8bb0\u5f55\u5c31\u53ef\u4ee5\u6ee1\u8db3ID\u751f\u6210\u5668\u7684\u9700\u6c42\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nREPLACE INTO Tickets64 (stub) VALUES ('a');\r\nSELECT LAST_INSERT_ID();\r\n<\/pre>\n<p>\u4ee5\u4e0a\u65b9\u5f0f\u4e2d\uff0c\u901a\u8fc7MySQL\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u786e\u4fdd\u6b64ID\u7684\u552f\u4e00\u548c\u81ea\u589e\uff0c\u4e14\u9002\u7528\u4e8e\u591a\u5e76\u53d1\u7684\u573a\u666f\u3002\u5b98\u65b9\u5bf9\u6b64\u7684\u63cf\u8ff0\uff1ahttps:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/information-functions.html<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nIt is multi-user safe because multiple clients can issue the UPDATE statement and \r\nget their own sequence value with the SELECT statement (or mysql_insert_id()), \r\nwithout affecting or being affected by other clients that generate their own sequence values.\r\n<\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5client\u91c7\u7528PHP\uff0c\u5219\u4e0d\u80fd\u4f7f\u7528mysql_insert_id()\u83b7\u53d6ID\uff0c\u539f\u56e0\u89c1\u300amysql_insert_id() \u5728bigint\u578bAI\u5b57\u6bb5\u9047\u5230\u7684\u95ee\u9898\u300b\uff1ahttp:\/\/kaifage.com\/notes\/99\/mysql-insert-id-issue-with-bigint-ai-field.html\u3002<\/p>\n<p>Flickr \u91c7\u53d6\u4e86\u6b64\u65b9\u6848\uff1a http:\/\/code.flickr.net\/2010\/02\/08\/ticket-servers-distributed-unique-primary-keys-on-the-cheap\/<\/p>\n<p>\u76f8\u5173\uff1a<\/p>\n<p>http:\/\/www.zhihu.com\/question\/30674667<\/p>\n<p>http:\/\/my.oschina.net\/u\/142836\/blog\/174465<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u501f\u7528MySQL \u7684 auto_increment \u7279\u6027\u53ef\u4ee5\u4ea7\u751f\u552f\u4e00\u7684\u53ef\u9760ID\u3002 \u8868\u5b9a\u4e49\uff0c\u5173\u952e\u5728\u4e8eauto_in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-5921","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=5921"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5921\/revisions"}],"predecessor-version":[{"id":5923,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5921\/revisions\/5923"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=5921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=5921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=5921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}