首页 > 编程语言 > MySQL 字段默认值该如何设置
2021
02-26

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 字段默认值的资料请关注自学编程网其它相关文章!

编程技巧