Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the default value of the column in mysql

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Let's talk about what are the default values for columns in mysql. The secret of the text is that it is relevant to the topic. So, instead of gossiping, let's go straight to the following, and I'm sure you'll benefit from reading this article on what are the default values listed in mysql.

In MySQL, each field definition contains additional constraints or modifiers that can be used to increase constraints on the data entered.

NULL and NOT NULL modifiers, DEFAULT modifiers, AUTO_INCREMENT modifiers.

NULL and NOT NULL modifiers

You can add the NULL or NOT NULL modifier after each field to specify whether the field can be empty (NULL)

Or must fill in the data (NOT NULL). MySQL specifies the field as the NULL modifier by default, if a field is specified as NOT NULL

MySQL does not allow null values to be inserted into the field (the null values are all NULL), because this is a "rule".

However, this rule does not apply in the self-increment column and the TIMESTAMP field.

Inserting NULL values into these fields will cause the next automatically incremented value or the current timestamp to be inserted.

Mysql > create table T10 (- > id int not null default 0,-> name char (10) not null default'- >); Query OK, 0 rows affected (0.01 sec) # insert two values are ok. MySQL > insert into T10 values (1, "lisi"); Query OK, 1 row affected (0.08 sec) # insert only one value, which is also ok's .MySQL > insert into T10 (id) values (2) Query OK, 1 row affected (0.00 sec) # it is found that the uninserted name column inherits the "" value of default. MySQL > select * from t10 inherit the "" value of sec. MySQL > select * from t10 where name ='| id | name | +-+-+ | 1 | lisi | 2 | +-+-+ 2 rows in set (0.00 sec) mysql > select * from t10 where name ='' +-+-+ | id | name | +-+-+ | 2 | | +-+-+ 1 row in set (0.00 sec) # create T11 table. The field id,name is not null.mysql > create table T11 (id int not null, name char (10) not null). Query OK, 0 rows affected (0.01Null) # if only one of the columns is inserted, an error is found, and the other column cannot be empty. MySQL > insert into T11 (id) values (2); ERROR 1364 (HY000): Field 'name' doesn't have a default value# will also report an error if the second column inserts "null". Set the field to not null.mysql > insert into T11 (id,name) values (2) ERROR 1048 (23000): Column 'name' cannot be null# inserts "0", the "field is ok. MySQL > insert into T11 (id,name) values (2Power0); Query OK, 1 row affected (0.00 sec) mysql > insert into T11 (id,name) values (2,"); Query OK, 1 row affected (0.01 sec)

Conclusion:

If no DEFAULT modifier is specified in a field, MySQL automatically sets the default value based on whether the field is NULL or NOT NULL.

If the specified field can be NULL, MySQL sets the default value to NULL for it.

If it is a NOT NULL field, MySQL inserts 0 for numeric types and empty strings for string types.

Is there anything you don't understand about what the default values listed in mysql are? Or if you want to know more about it, you can continue to follow our industry information section.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report