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
Release: 2023-08-23 23:25:16
forward
1043 people have browsed it

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
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template