MySQL 字段默认值该如何设置

前言: 

在 mysql 中,我们可以为表字段设置默认值,在表中插入一条新记录时,如果没有为某个字段赋值,系统就会自动为这个字段插入默认值。关于默认值,有些知识还是需要了解的,本篇文章我们一起来学习下字段默认值相关知识。

  1.默认值相关操作

我们可以用 default 关键字来定义默认值,默认值通常用在非空列,这样能够防止数据表在录入数据时出现错误。

创建表时,我们可以给某个列设置默认值,具体语法格式如下:

# 格式模板
<字段名> <数据类型> default <默认值>

# 示例
mysql> create table `test_tb` (
    ->   `id` int not null auto_increment,
    ->   `col1` varchar(50) not null default 'a',
    ->   `col2` int not null default 1,
    ->   primary key (`id`)
    -> ) engine=innodb  default charset=utf8;
query ok, 0 rows affected (0.06 sec)

mysql> desc test_tb;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| col1  | varchar(50) | no   |     | a       |                |
| col2  | int(11)     | no   |     | 1       |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into test_tb (col1) values ('fdg');
query ok, 1 row affected (0.01 sec)

mysql> insert into test_tb (col2) values (2);
query ok, 1 row affected (0.03 sec)

mysql> select * from test_tb;
+----+------+------+
| id | col1 | col2 |
+----+------+------+
|  1 | fdg  |    1 |
|  2 | a    |    2 |
+----+------+------+
2 rows in set (0.00 sec)

通过以上实验可以看出,当该字段设置默认值后,插入数据时,若不指定该字段的值,则以默认值处理。

关于默认值,还有其他操作,例如修改默认值,增加默认值,删除默认值等。一起来看下这些应该如何操作。

# 添加新字段 并设置默认值
alter table `test_tb` add column `col3` varchar(20) not null default 'abc';

# 修改原有默认值
alter table `test_tb` alter column `col3` set default '3a';
alter table `test_tb` change column `col3` `col3` varchar(20) not null default '3b';
alter table `test_tb` modify column `col3` varchar(20) not null default '3c';

# 删除原有默认值
alter table `test_tb` alter column `col3` drop default;

# 增加默认值(和修改类似)
alter table `test_tb` alter column `col3` set default '3aa';

  2.几点使用建议

其实不止非空字段可以设置默认值,普通字段也可以设置默认值,不过一般推荐字段设为非空。

mysql> alter table `test_tb` add column `col4` varchar(20) default '4a';
query ok, 0 rows affected (0.12 sec)
records: 0  duplicates: 0  warnings: 0

mysql>  desc test_tb;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| col1  | varchar(50) | no   |     | a       |                |
| col2  | int(11)     | no   |     | 1       |                |
| col3  | varchar(20) | no   |     | 3aa     |                |
| col4  | varchar(20) | yes  |     | 4a      |                |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

在项目开发中,有些默认值字段还是经常使用的,比如默认为当前时间、默认未删除、某状态值默认为 1 等等。简单通过下表展示下常用的一些默认值字段。

create table `default_tb` (
  `id` int unsigned not null auto_increment comment '自增主键',
  ...
  `country` varchar(50) not null default '中国',
  `col_status` tinyint not null default 1 comment '1:代表啥 2:代表啥...',
  `col_time` datetime not null default '2020-10-01 00:00:00' comment '什么时间',
  `is_deleted` tinyint not null default 0 comment '0:未删除 1:删除',
  `create_time` timestamp not null default current_timestamp comment '创建时间',
  `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
  primary key (`id`)
) engine=innodb  default charset=utf8;

这里也要提醒下,默认值一定要和字段类型匹配,比如说某个字段表示状态值,可能取值 1、2、3… 那这个字段推荐使用 tinyint 类型,而不应该使用 char 或 varchar 类型。

笔者结合个人经验,总结下关于默认值使用的几点建议:

非空字段设置默认值可以预防插入报错。

默认值同样可设置在可为 null 字段。

一些状态值字段最好给出备注,标明某个数值代表什么状态。

默认值要和字段类型匹配。

总结: 

本篇文章主要讲述 mysql 字段默认值相关知识,比较简单易懂,希望各位有所收获。

以上就是mysql 字段默认值该如何设置的详细内容,更多关于mysql 字段默认值的资料请关注www.887551.com其它相关文章!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐