如何在 MySQL 中獲取標識列的種子值?


為此,可以使用 SHOW VARIABLES 命令 −

mysql> SHOW VARIABLES LIKE 'auto_inc%';

輸出

將生成以下輸出 −

+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.95 sec)

可以在外部控制 AUTO_INCREMENT。

讓我們首先建立一個表 −

mysql> create table DemoTable
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY
   -> );
Query OK, 0 rows affected (0.94 sec)

使用插入命令在表中插入一些記錄 −

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.44 sec)

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.26 sec)

使用選擇語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

將生成以下輸出 −

+-----------+
| StudentId |
+-----------+
| 1         |
| 2         |
+-----------+
2 rows in set (0.00 sec)

現在可以控制 AUTO_INCREMENT −

mysql> alter table DemoTable AUTO_INCREMENT=1000;
Query OK, 0 rows affected (0.50 sec)
Records: 0  Duplicates: 0  Warnings: 0

使用插入命令在表中插入一些記錄 −

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.51 sec)

mysql> insert into DemoTable values();
Query OK, 1 row affected (1.37 sec)

使用選擇語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

將生成以下輸出 −

+-----------+
| StudentId |
+-----------+
|         1 |
|         2 |
|      1000 |
|      1001 |
+-----------+
4 rows in set (0.00 sec)

更新於: 2020 年 6 月 30 日

525 次瀏覽

開始你的 職業

完成課程獲得認證

開始
廣告