首頁 >資料庫 >mysql教程 >在MySQL中產生一個整數序列?

在MySQL中產生一個整數序列?

王林
王林轉載
2023-08-23 17:13:02970瀏覽

在MySQL中產生一個整數序列?

要在MySQL中建立一個整數序列,可以使用AUTO_INCREMENT。請記住,沒有特殊的命令來創建整數序列。 AUTO_INCREMENT將協助在MySQL中建立一個整數序列。

AUTO_INCREMENT預設從1開始。您可以使用alter命令將其變更為其他數字。讓我們來看一個例子。假設您的初始值為1000。那麼下一個數字將會是1000 1 = 1001。

這是一個AUTO_INCREMENT的例子。建立表格的查詢如下:

mysql> create table AutoIncrementDemo
   −> (
      −> EmployeeId int AUTO_INCREMENT,
      −> primary key(EmployeeId)
   −> );
Query OK, 0 rows affected (0.54 sec)

插入產生整數序列的記錄。查詢如下 −

mysql> insert into AutoIncrementDemo values();
Query OK, 1 row affected (0.13 sec)

mysql> insert into AutoIncrementDemo values();
Query OK, 1 row affected (0.27 sec)

mysql> insert into AutoIncrementDemo values();
Query OK, 1 row affected (0.09 sec)

mysql> insert into AutoIncrementDemo values();
Query OK, 1 row affected (0.09 sec)

mysql> insert into AutoIncrementDemo values();
Query OK, 1 row affected (0.09 sec)

mysql> insert into AutoIncrementDemo values();
Query OK, 1 row affected (0.07 sec)

您可以使用select語句來檢查整數序列。查詢如下 −

mysql> select *from AutoIncrementDemo;

以下是輸出 −

+------------+
| EmployeeId |
+------------+
|          1 |
|          2 |
|          3 |
|          4 |
|          5 |
|          6 |
+------------+
6 rows in set (0.00 sec)

以上是在MySQL中產生一個整數序列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除