How can we specify default values in MySQL INSERT statement?

MySQLMySQLi Database

At the time of creation of a table, if any column is defined with default values then by using the keyword ‘DEFAULT’ in the INSERT statement, we can take default value for that column. For example, we have created a table ‘employee’ with a default value of column ‘DOJ’ as follows −

mysql> Create table employee(id int, name varchar(20), doj date DEFAULT '2005-01-01');
Query OK, 0 rows affected (0.09 sec)

mysql> Insert into employee(id, name, doj) values(1, ’Aarav’, DEFAULT);
Query OK, 1 row affected (0.03 sec)

mysql> select * from employee;
| id   | name       | doj           |
| 1    |Aarav       | 2005-01-01    |
1 row in set (0.00 sec)

From the query above, it can be observed that while inserting the values we use DEFAULT keyword, MySQL insert the default value specified at the time of defining the column.

Published on 19-Feb-2018 16:20:51