如何向只含單個自動增加列的表中插入一行?


只要一張表裡僅含一個自增列,便可輕鬆向其中插入一行。語法如下 −

insert into yourTableName set yourColumnName =NULL;

可以使用以下語法 −

insert into yourTableName values(NULL);

為了理解上述語法,讓我們建立一個表。建立表的查詢如下 −

mysql> create table singleAutoIncrementColumnDemo
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY
   -> );
Query OK, 0 rows affected (0.62 sec)

使用插入命令向表中插入一些記錄。查詢如下 −

mysql> insert into singleAutoIncrementColumnDemo set UserId =NULL;
Query OK, 1 row affected (0.18 sec)
mysql> insert into singleAutoIncrementColumnDemo values(NULL);
Query OK, 1 row affected (0.11 sec)

使用 select 語句顯示錶中所有記錄。查詢如下 −

mysql> select *from singleAutoIncrementColumnDemo;

輸出如下 −

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

更新日期: 2019 年 7 月 30 日

瀏覽量 376 次

開啟你的 職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.