{"id":537,"date":"2012-12-25T21:59:51","date_gmt":"2012-12-25T19:59:51","guid":{"rendered":"https:\/\/www.npcglib.org\/~stathis\/blog\/?p=537"},"modified":"2013-10-01T05:48:04","modified_gmt":"2013-10-01T03:48:04","slug":"sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table","status":"publish","type":"post","link":"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/","title":{"rendered":"sql tip: Reset the count of an auto_increment key of a table in mysql"},"content":{"rendered":"<p>Sometimes it is useful to &#8220;reset&#8221; the numbers of an auto-incrementing field in a MySQL table. <u>This obviously is a VERY bad idea if you have relations to these indices, so make sure you know what you&#8217;re doing before executing this on your tables!<\/u><\/p>\n<p>So, having warned you, here is the SQL code to do this:<\/p>\n<pre>\r\nSET @count = 0;\r\nUPDATE IGNORE `table` SET `table`.`key_id` = @count:= @count + 1;\r\n<\/pre>\n<p>This may not work as you expect if you have a PRIMARY KEY in your table. One way to get around this is to turn the PRIMARY KEY into an INDEX, perform the counter reset and convert the INDEX back to a PRIMARY KEY again:<\/p>\n<pre>\r\nALTER TABLE `table` DROP PRIMARY KEY , ADD INDEX ( `key_id` );\r\nSET @count = 0;\r\nUPDATE IGNORE `table` SET `table`.`key_id` = @count:= @count + 1;\r\nALTER TABLE `table` AUTO_INCREMENT = 1;\r\nALTER TABLE `table` DROP INDEX `key_id` , ADD PRIMARY KEY ( `key_id` );\r\n<\/pre>\n<p>If you know of an SQL statement that preserves relations and can resets the counters, then let me know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is useful to &#8220;reset&#8221; the numbers of an auto-incrementing field in a MySQL table. This obviously is a VERY bad idea if you have relations to these indices, so make sure you know what you&#8217;re doing before executing this on your tables! So, having warned you, here is the SQL code to do [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,7],"tags":[39,38,37,36],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"description\" content=\"How to reset the numbers of an auto-incrementing field in a MySQL table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"stathis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/#website\",\"url\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/\",\"name\":\"sigmoid\",\"description\":\"..oo..oo..oo..oo..oo..oo..\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/#webpage\",\"url\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/\",\"name\":\"sql tip: Reset the count of an auto_increment key of a table in mysql - sigmoid\",\"isPartOf\":{\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/#website\"},\"datePublished\":\"2012-12-25T19:59:51+00:00\",\"dateModified\":\"2013-10-01T03:48:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/#\/schema\/person\/508363c4ebd1fd6066edf00c94e37e41\"},\"description\":\"How to reset the numbers of an auto-incrementing field in a MySQL table.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"sql tip: Reset the count of an auto_increment key of a table in mysql\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/#\/schema\/person\/508363c4ebd1fd6066edf00c94e37e41\",\"name\":\"stathis\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/214f29f604ec7d8d2f1345c5fa617c09?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/214f29f604ec7d8d2f1345c5fa617c09?s=96&d=mm&r=g\",\"caption\":\"stathis\"},\"url\":\"https:\/\/www.npcglib.org\/~stathis\/blog\/author\/stathis\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"description":"How to reset the numbers of an auto-incrementing field in a MySQL table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/","twitter_misc":{"Written by":"stathis","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/#website","url":"https:\/\/www.npcglib.org\/~stathis\/blog\/","name":"sigmoid","description":"..oo..oo..oo..oo..oo..oo..","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.npcglib.org\/~stathis\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/#webpage","url":"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/","name":"sql tip: Reset the count of an auto_increment key of a table in mysql - sigmoid","isPartOf":{"@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/#website"},"datePublished":"2012-12-25T19:59:51+00:00","dateModified":"2013-10-01T03:48:04+00:00","author":{"@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/#\/schema\/person\/508363c4ebd1fd6066edf00c94e37e41"},"description":"How to reset the numbers of an auto-incrementing field in a MySQL table.","breadcrumb":{"@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/2012\/12\/25\/sql-tip-reset-the-count-of-an-auto_increment-key-of-a-mysql-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.npcglib.org\/~stathis\/blog\/"},{"@type":"ListItem","position":2,"name":"sql tip: Reset the count of an auto_increment key of a table in mysql"}]},{"@type":"Person","@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/#\/schema\/person\/508363c4ebd1fd6066edf00c94e37e41","name":"stathis","image":{"@type":"ImageObject","@id":"https:\/\/www.npcglib.org\/~stathis\/blog\/#personlogo","inLanguage":"en-US","url":"http:\/\/2.gravatar.com\/avatar\/214f29f604ec7d8d2f1345c5fa617c09?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/214f29f604ec7d8d2f1345c5fa617c09?s=96&d=mm&r=g","caption":"stathis"},"url":"https:\/\/www.npcglib.org\/~stathis\/blog\/author\/stathis\/"}]}},"_links":{"self":[{"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/posts\/537"}],"collection":[{"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/comments?post=537"}],"version-history":[{"count":6,"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/posts\/537\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/posts\/537\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/media?parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/categories?post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npcglib.org\/~stathis\/blog\/wp-json\/wp\/v2\/tags?post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}