如何在只有一个自动增量列的表中插入一行?

mysqlmysqli database

您可以轻松地在只有一个自动增量列的表中插入一行。语法如下 −

insert into yourTableName set yourColumnName =NULL;

You can use the below syntax −

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)

使用 insert 命令在表中插入一些记录。 查询语句如下 −

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)

相关文章