How to insert a row into a table that has only a single autoincrement column?


You can easily insert a row into a table that has only a single auto increment column. The syntax is as follows −

insert into yourTableName set yourColumnName =NULL;

You can use the below syntax −

insert into yourTableName values(NULL);

To understand the above syntax, let us create a table. The query to create a table is as follows −

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

Insert some records in the table using insert command. The query is as follows −

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)

Display all records from the table using select statement. The query is as follows −

mysql> select *from singleAutoIncrementColumnDemo;

Here is the output −

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements