Home >Database >Mysql Tutorial >What does the default constraint do? How do I apply this to the columns when creating the table?

What does the default constraint do? How do I apply this to the columns when creating the table?

WBOY
WBOYforward
2023-08-23 23:25:161044browse

What does the default constraint do? How do I apply this to the columns when creating the table?

DEFAULT constraint is used to set default values ​​for columns in MySQL tables. If applied to a column, the column will take on the default value, i.e. no value will be assigned to the column. Its syntax is as follows −

Syntax

DEFAULT default_value

Here, default_value is the default value set for the column.

Example

The following query will create a table called workers where we assign a default value of 1000 to the column id.

mysql> Create table workers(Name Varchar(25), Id INT NOT NULL DEFAULT 1000);
Query OK, 0 rows affected (0.47 sec)

mysql> Insert into workers(Name, Id) values('Ram', 101);
Query OK, 1 row affected (0.04 sec)

mysql> Insert into workers(Name) values('Mohan');
Query OK, 1 row affected (0.10 sec)

mysql> Select * from Workers;

+-------+------+
| Name  | Id   |
+-------+------+
| Ram   | 101  |
| Mohan | 1000 |
+-------+------+

2 rows in set (0.00 sec)

When we do not provide any value for the id, the above result set will store the default value of 1000.

The above is the detailed content of What does the default constraint do? How do I apply this to the columns when creating the table?. 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