Createtableemployee(idint,namevarchar(20),dojdateDEFAULT'2005-01-01');QueryOK,0rowsaffected(0.09 sec)mysql>Insertintoemploye"/> Createtableemployee(idint,namevarchar(20),dojdateDEFAULT'2005-01-01');QueryOK,0rowsaffected(0.09 sec)mysql>Insertintoemploye">

Home  >  Article  >  Database  >  How to specify default value in MySQL INSERT statement?

How to specify default value in MySQL INSERT statement?

WBOY
WBOYforward
2023-09-15 20:29:061246browse

如何在 MySQL INSERT 语句中指定默认值?

While creating the table, if any column has a default value defined, then by using the keyword "DEFAULT" in the INSERT statement, we can get the default value for that column. For example, we have created a table "employee" with default value as column "DOJ" as shown below -

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)

As can be seen from the above query, when we insert the value using DEFAULT keyword, MySQL inserts the default value specified when defining the column.

The above is the detailed content of How to specify default value in MySQL INSERT statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete