我們在向 MySQL 中插入資料時是否可以跳過列?


如果你的第一列是 AUTO_INCREMENT,那麼你可以跳過該列並放置 NULL 值。讓我們先建立一個表 -

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentFirstName varchar(100),
   StudentAge int
);
Query OK, 0 rows affected (0.60 sec)

使用 insert 命令在表中插入一些記錄。這裡,我們跳過了第一列,因為它屬於 AUTO_INCREMENT -

mysql> insert into DemoTable values(NULL,'Robert',21);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(NULL,'Sam',22);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(NULL,'Bob',24);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(NULL,'Carol',20);
Query OK, 1 row affected (0.14 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+-----------+------------------+------------+
| StudentId | StudentFirstName | StudentAge |
+-----------+------------------+------------+
|         1 | Robert           |         21 |
|         2 | Sam              |         22 |
|         3 | Bob              |         24 |
|         4 | Carol            |         20 |
+-----------+------------------+------------+
4 rows in set (0.00 sec)

更新於: 01-10-2019

656 次瀏覽

開啟你的 職業

透過完成課程,獲取認證

開始
廣告
© . All rights reserved.