前言:
在 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: 0mysql> 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 字段默认值相干常识,比较简单易懂,心愿各位有所播种。