插入MySQL时可以跳过列吗?

如果第一列是AUTO_INCREMENT,则可以跳过该列并将值NULL放置。让我们首先创建一个表-

create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentFirstName varchar(100),
   StudentAge int
);

使用insert命令在表中插入一些记录。在这里,我们跳过了第一列,因为它是AUTO_INCREMENT-

insert into DemoTable values(NULL,'Robert',21);
insert into DemoTable values(NULL,'Sam',22);
insert into DemoTable values(NULL,'Bob',24);
insert into DemoTable values(NULL,'Carol',20);

使用select语句显示表中的所有记录-

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)
猜你喜欢