现在有这样的场景,我们的数据库类型是MySQL,表是从其他库拿过来的,约束和索引都没迁移。现在希望增加一个自增序列。
且自增序列是从当前最大自增ID开始的,下面就是这样一个过程的演示。
mysql> create table t_test1(id int,name varchar(1000));
Query OK, 0 rows affected (0.12 sec)
# 修改id字段为主键字段,且自增
mysql> alter table t_test1 modify id int primary key auto_increment ;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 插入数据,自动填充id
mysql> insert into t_test1(name) values('xiaoming');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_test1;
+----+----------+
| id | name |
+----+----------+
| 1 | xiaoming |
+----+----------+
1 row in set (0.00 sec)
# 修改t_test1 表的自增起始值
mysql> alter table t_test1 auto_increment = 1000;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into t_test1(name) values('xiaoming');
Query OK, 1 row affected (0.01 sec)
# 自增序列从起始值开始
mysql> select * from t_test1;
+------+----------+
| id | name |
+------+----------+
| 1 | xiaoming |
| 1000 | xiaoming |
+------+----------+
2 rows in set (0.00 sec)