成品ppt网站国外,供水开发建设公司网站,钦州网站推广,西安的互联网企业MySQL 字段默认值该如何设置
前言#xff1a;
在 MySQL 中#xff0c;我们可以为表字段设置默认值#xff0c;在表中插入一条新记录时#xff0c;如果没有为某个字段赋值#xff0c;系统就会自动为这个字段插入默认值。关于默认值#xff0c;有些知识还是需要了解的
在 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)- ) ENGINEInnoDB DEFAULT CHARSETutf8;
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)
) ENGINEInnoDB DEFAULT CHARSETutf8;这里也要提醒下默认值一定要和字段类型匹配比如说某个字段表示状态值可能取值 1、2、3… 那这个字段推荐使用 tinyint 类型而不应该使用 char 或 varchar 类型。
笔者结合个人经验总结下关于默认值使用的几点建议
非空字段设置默认值可以预防插入报错。
默认值同样可设置在可为 null 字段。
一些状态值字段最好给出备注标明某个数值代表什么状态。
默认值要和字段类型匹配。
总结
本篇文章主要讲述 MySQL 字段默认值相关知识比较简单易懂希望各位有所收获。