AUTO_INCREMENTãå·»ãæ»ã
ä»ã®ä¼ç¤¾ã«å ¥ãã¾ã§ç¥ããªãã£ããã§ãããçµæ§æåãªãã°ã£ã½ãã§ããã
AUTO_INCREMENTã§æ¡çªãããçªå·ããåèµ·åããã¨MySQL5.7以åã¯å·»ãæ»ãç¾è±¡ãèµ·ãã話ã§ãã
åç¾ãã¦ã¿ã
MySQL5.7ã¨8.0ã«ããããåããã¼ãã«ãä½ã£ã¦ãã¼ã¿ãå ¥ããææ°ã®è¡ãåé¤ãã¾ãã
å ±é
mysql> use test; Database changed mysql> CREATE TABLE `t1` ( -> `id` int(11) NOT NULL AUTO_INCREMENT, -> PRIMARY KEY (`id`) -> ) ENGINE=InnoDB; Query OK, 0 rows affected, 1 warning (0.04 sec) mysql> insert into t1 values(NULL); Query OK, 1 row affected (0.01 sec) mysql> insert into t1 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> select * from t1; +----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec) mysql> delete from t1 where id = 3; Query OK, 1 row affected (0.00 sec)
MySQL5.7.29ã®å ´å
mysql> show create table t1; +-------+------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 | +-------+------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
AUTO_INCREMENTã¯4ã§ããã
MySQL8.0.19ã®å ´å
mysql> show create table t1; +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
åããAUTO_INCREMENTã¯4ã§ããã
MySQLãåèµ·åããã¨ã»ã»ã»
MySQL5.7.29ã®å ´å
mysql> show create table t1; +-------+------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 | +-------+------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
AUTO_INCREMENT=3 ã«ãªã£ã¦ãï¼ï¼4ãã3ã«å·»ãæ»ã£ã¦ãï¼
ã§ããinformation_schema.tablesã§ç¢ºèªããã¨4ã®ã¾ã¾â¦ã
MySQL8.0.19ã®å ´å
mysql> show create table t1; +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
AUTO_INCREMENT=4 ã®ã¾ã¾ã§ããã
ããã¥ã¡ã³ããã½ã¼ã¹
ãã®å¤æ´ã«ã¤ãã¦ã¯ãã®8.0ã®ããã¥ã¡ã³ãã«è¨è¼ããã¦ãã¾ãã
Googleå ççã使ã£ã¦ããã£ã½ã訳ãããã®ãããã¦ããã¾ãã
MySQL 5.7 以åã®ãã¼ã¸ã§ã³ã§ã¯ããªã¼ãã¤ã³ã¯ãªã¡ã³ãã«ã¦ã³ã¿ã¯ã¡ã¤ã³ã¡ã¢ãªã«ã®ã¿ä¿åããããã£ã¹ã¯ã«ã¯ä¿åããã¾ããã ãµã¼ãã®åèµ·åå¾ã«ãªã¼ãã¤ã³ã¯ãªã¡ã³ãã«ã¦ã³ã¿ãåæåããããã«ãInnoDB 㯠AUTO_INCREMENT ã«ã©ã ãå«ããã¼ãã«ã¸ã®æåã®æ¿å ¥æã«ä»¥ä¸ã®ã¹ãã¼ãã¡ã³ãã¨åçã®ãã®ãå®è¡ãã¾ãã
SELECT MAX(ai_col) FROM table_name FOR UPDATE;
MySQL 8.0ã§ã¯ããã®åä½ãå¤æ´ããã¦ãã¾ãã ç¾å¨ã®ãªã¼ãã¤ã³ã¯ãªã¡ã³ãã«ã¦ã³ã¿ã®æ大å¤ã¯ãå¤æ´ããããã³ã« REDO ãã°ã«æ¸ãè¾¼ã¾ãããã§ãã¯ãã¤ã³ããã¨ã«ã¨ã³ã¸ã³ã»ãã©ã¤ãã¼ãã»ã·ã¹ãã ã»ãã¼ãã«ã«ä¿åããã¾ãã ãããã®å¤æ´ã«ãããç¾å¨ã®ãªã¼ãã¤ã³ã¯ãªã¡ã³ãã«ã¦ã³ã¿ã®æ大å¤ã¯ããµã¼ãã¼ã®åèµ·åæã«ãæç¶ãã¾ãã
é常ã®ã·ã£ãããã¦ã³å¾ã®ãµã¼ãã¼åèµ·åæã«ãInnoDB ã¯ããã¼ã¿è¾æ¸ã·ã¹ãã ãã¼ãã«ã«ä¿åããã¦ããç¾å¨ã®æ大ãªã¼ãã¤ã³ã¯ãªã¡ã³ãå¤ã使ç¨ãã¦ã ã¡ã¢ãªå ãªã¼ãã¤ã³ã¯ãªã¡ã³ãã«ã¦ã³ã¿ãåæåãã¾ãã
REDOãã°ã«æ¸ãè¾¼ãã§ããã¨è¨ã£ã¦ããã®ãã½ã¼ã¹ä¸ã ã¨ãã®è¾º
ãã®dict_table_autoinc_log()ã®ããã¥ã¡ã³ãã¯ãã
ãã°ã ã£ãã®ã8ã§ç´ã£ãã¨ããæ±ã
2003å¹´ã«è¦ã¤ãã£ã¦ãç´20å¹´çµã£ã¦ç´ã£ããã§ãããâ¦ã
5.7以åã§ãå·»ãæ»ããªãããã«ãããã¨ãã人ã¯
(5.6以éãªããªã³ã©ã¤ã³DDLæ±ããªã®ã§)ã¹ãã¢ãããã·ã¼ã¸ã£ä½ã£ã¦ãããããå¤ãåãã°ã·ã¼ã±ã³ã¹ã®ãããªæåã¯ã§ãã¾ããã
- ä½ãã¨ãã¯information_schema.tablesã®auto_incrementã«ã©ã ã®å¤ãåå¾ãã¦ã
- alter table æãã§auto_incrementã®å¤ãå¤æ´
5.5以åã ã¨auto increment使ããã«ããã·ã¼ã¸ã£ã¨å°ç¨ãã¼ãã«ããªï¼ï¼ãããã«ãããããï¼
ã¨ããã§ã¿ã¤ãã«ã®æå³ã¯ï¼
ããããâ¦ãç·ã®ä¸çãã¸â¦
ã¸ã§ã¸ã§ã®å¥å¦ãªåéº Part7 ã¹ãã£ã¼ã«ã»ãã¼ã«ã»ã©ã³ã®ãã£ã©ã¯ã¿ã¼ã®ä¸äººãªã³ã´ã©ã»ãã¼ãã¢ã²ã¤ã³ã®ã»ãªãããã
ãã®ãã£ã©ããã£ããæã6ç§ãå·»ãæ»ããè½åãæã£ã¦ãããã¨ããå¼ç¨ã
ãã ããã ãï½
ã¶ã£ã¡ãã
ã½ã¼ã¹ã®å ´æã¨ãyoku0825ããã«æãã¦ããã£ããã§ãããã
æ¬å½ã«ãã¤ããããã¨ããããã¾ãã