Passing NULL to MySQL for auto increment?


Yes, we can pass NULL as in the below syntax −

insert into yourTableName values(NULL,yourValue1,yourValue2,...N);

Let us first create a table −

mysql> create table DemoTable1503
   -> (
   -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(20),
   -> ClientAge int
   -> );
Query OK, 0 rows affected (0.45 sec)

Insert some records in the table using insert command. Since we have set NOT NULL above, it won’t affect auto_increment −

mysql> insert into DemoTable1503 values(NULL,'Chris',25);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1503 values(NULL,'David',28);
Query OK, 1 row affected (0.37 sec)
mysql> insert into DemoTable1503 values(NULL,'Bob',45);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1503 values(NULL,'Mike',57);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1503;

This will produce the following output. Auto Increment got printed in the same way (not null) −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | Chris      |        25 |
|        2 | David      |        28 |
|        3 | Bob        |        45 |
|        4 | Mike       |        57 |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Updated on: 11-Dec-2019

655 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements