MySQL - 在初始 auto_increment 值之前保留 ID
MySQL - Reserving ID's before the initial auto_increment value
我的用户table:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE users AUTO_INCREMENT = 1000;
现在我将开始添加记录(第一个用户 ID 将是 1000)。在初始 AUTO_INCREMENT 值之前手动添加用户是否有问题?
INSERT INTO `users` (`id`, `username`) VALUES (1, 'something');
# ...
INSERT INTO `users` (`id`, `username`) VALUES (900, 'something');
虽然普通插入只会使用下一个增量值
INSERT INTO `users` (`username`) VALUES ('something else'); # id: 1000
INSERT INTO `users` (`username`) VALUES ('something else'); # id: 1001
基本上我想保留 1 到 999 之间的 ID,这样我就可以手动添加它们而不会破坏 AUTO_INCREMENT
。
这会产生任何问题吗?我也有引用此用户 ID 的外键。
你可以按照你的建议去做。手动插入值,然后允许 AUTO_INCREMENT
工作。
No value was specified for the AUTO_INCREMENT column, so MySQL
assigned sequence numbers automatically. You can also explicitly
assign 0 to the column to generate sequence numbers. If the column is
declared NOT NULL, it is also possible to assign NULL to the column to
generate sequence numbers. When you insert any other value into a
AUTO_INCREMENT column, the column is set to that value and the
sequence is reset so that the next automatically generated value
follows sequentially from the inserted value.
保留范围 1-999
后,计数器从 1000
开始。您需要确保不会尝试插入重复值,因为您可能会违反 PRIMARY KEY
.
输出:
╔═══════╦═══════════╗
║ id ║ username ║
╠═══════╬═══════════╣
║ 1 ║ something ║
║ 999 ║ something ║
║ 1000 ║ new value ║
╚═══════╩═══════════╝
不,据我所知这是行不通的,AUTO_INCREMENT 仅在您省略主键时使用。更多信息可以在这里找到:
https://dev.mysql.com/doc/refman/5.6/en/replication-options-master.html
我的用户table:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE users AUTO_INCREMENT = 1000;
现在我将开始添加记录(第一个用户 ID 将是 1000)。在初始 AUTO_INCREMENT 值之前手动添加用户是否有问题?
INSERT INTO `users` (`id`, `username`) VALUES (1, 'something');
# ...
INSERT INTO `users` (`id`, `username`) VALUES (900, 'something');
虽然普通插入只会使用下一个增量值
INSERT INTO `users` (`username`) VALUES ('something else'); # id: 1000
INSERT INTO `users` (`username`) VALUES ('something else'); # id: 1001
基本上我想保留 1 到 999 之间的 ID,这样我就可以手动添加它们而不会破坏 AUTO_INCREMENT
。
这会产生任何问题吗?我也有引用此用户 ID 的外键。
你可以按照你的建议去做。手动插入值,然后允许 AUTO_INCREMENT
工作。
No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign 0 to the column to generate sequence numbers. If the column is declared NOT NULL, it is also possible to assign NULL to the column to generate sequence numbers. When you insert any other value into a AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the inserted value.
保留范围 1-999
后,计数器从 1000
开始。您需要确保不会尝试插入重复值,因为您可能会违反 PRIMARY KEY
.
输出:
╔═══════╦═══════════╗
║ id ║ username ║
╠═══════╬═══════════╣
║ 1 ║ something ║
║ 999 ║ something ║
║ 1000 ║ new value ║
╚═══════╩═══════════╝
不,据我所知这是行不通的,AUTO_INCREMENT 仅在您省略主键时使用。更多信息可以在这里找到:
https://dev.mysql.com/doc/refman/5.6/en/replication-options-master.html