auto_increment 属性
AUTO_INCREMENT: 是⽤于⾃动填充默认字段值的属性
tidb> show variables like 'auto_increment_%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 | # 每次增长多少,步长
| auto_increment_offset | 1 | # 初始的数值是多少。
+--------------------------+-------+
# tidb_allow_remove_auto_inc 允许删除 列上的auto increment属性。默认是不允许。
tidb> show variables like 'tidb_allow_remove_auto_inc';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| tidb_allow_remove_auto_inc | OFF |
+----------------------------+-------+
1 row in set (0.01 sec)
DROP TABLE IF EXISTS test.t1;
CREATE TABLE test.t1 (
id int PRIMARY KEY AUTO_INCREMENT,
from_port char(4));
DROP TABLE IF EXISTS test.t2;
CREATE TABLE test.t2 (
id int PRIMARY KEY AUTO_INCREMENT,
from_port char(4))
AUTO_ID_CACHE 300; # 从300的基础上 开始增长 TiDB server 1 0-300 TiDB Server 2 301-600
/* Populate */
INSERT INTO test.t1 (from_port) VALUES ('4000'), ('4000'),
('4000');
# id : 1,2,3
/* Check value */
select id, from_port from test.t1;
/* Explictly assign value "7" to auto_increment column */
insert into test.t1 values (7, '4000');
# id: 1,2,3,7
/* Check value */
select id, from_port from test.t1;
/* Relying on auto_increment values to assign values to new rows
*/
insert into test.t1 (from_port) values ('4000');
insert into test.t1 (from_port) values ('4000');
/* Check value */
select id, from_port from test.t1;
# id: 1,2,3,7,8,9
注意:如果实在4000端口上执行,则结果是
# id: 1,2,3,4,5,7
/* 在4000端⼝的tidb上插⼊数据 */
insert into test.t2 (from_port) values ('4000');
insert into test.t2 (from_port) values ('4000');
# id: 1,2
/* 在4001端⼝的tidb上插⼊数据 */
insert into test.t2 (from_port) values ('4001');
insert into test.t2 (from_port) values ('4001');
/* 查询t2中的数据 */
select * from test.t2
# id: 1,2,301,302
/* 在4000端⼝的tidb上插⼊数据 */
insert into test.t2 (id,from_port) values (90000,'4000');
/* 在4001端⼝的tidb上插⼊数据 */
insert into test.t2 (id,from_port) values (90000,'4001');
注意:这个会报错,提示插入的是重复值