現在有這樣的場景,我們的數據庫類型是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)
