When inserting a new row, should I include the columns that are null in the MySQL query?

MySQLMySQLi Database

If you do not specify the column list in insert statement then you can use below syntax −

insert into yourTableName values(NULL,yourValue,NULL,NULL,.....N);

Let us first create a table −

mysql> create table DemoTable1513
   -> (
   -> StudentId int,
   -> StudentName varchar(20) ,
   -> StudentAge int,
   -> StudentCountryName varchar(20)
   -> );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1513 values(NULL,'Chris Brown',NULL,NULL);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1513 values(101,NULL,NULL,NULL);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1513 values(NULL,NULL,23,NULL);
Query OK, 1 row affected (0.42 sec)
mysql> insert into DemoTable1513 values(NULL,NULL,NULL,'US');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1513;

This will produce the following output −

+-----------+-------------+------------+--------------------+
| StudentId | StudentName | StudentAge | StudentCountryName |
+-----------+-------------+------------+--------------------+
|      NULL | Chris Brown |       NULL | NULL               |
|       101 | NULL        |       NULL | NULL               |
|      NULL | NULL        |         23 | NULL               |
|      NULL | NULL        |       NULL | US                 |
+-----------+-------------+------------+--------------------+
4 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 10:43:58
Advertisements