⚠️ 注意:
current_timestamp不要加引号,写成default current_timestamp,写成'current_timestamp'会当成普通字符串,不是时间函数。
mysql5.6.5+ 才支持 datetime 使用 current_timestamp;mysql8.0 支持表达式默认值(需要包括号default (xxx))
一、时间类系统默认值(你重点关心的)
| 默认值写法 | 适用字段类型 | 说明 | 示例 |
|---|---|---|---|
current_timestamp | timestamp / datetime | 当前时间戳(日期+时分秒),插入时自动赋值 | created_at datetime default current_timestamp |
current_timestamp(n) | timestamp(n) / datetime(n) | 带微秒,n=0~6 | dt datetime(3) default current_timestamp(3) |
current_date | date | 当前日期(只有年月日),mysql8.0+支持 | ct_date date default (current_date) |
current_time | time | 当前时间(时分秒),mysql8.0+支持 | t time default (current_time) |
on update current_timestamp | timestamp / datetime | 不是default,是附加属性:该行其他字段更新时,自动刷新为本字段时间 | updated_at datetime default current_timestamp on update current_timestamp |
❗ 坑:
now()/sysdate()不能直接放在 default 里(mysql5.7不支持,8.0可以写default (now()),但推荐统一用 current_timestamp)
二、常量默认值(所有版本通用)
| 默认值 | 适用类型 | 说明 |
|---|---|---|
null | 任意允许null字段 | 默认空,col varchar(20) default null |
''(空字符串) | char/varchar/text | 字符串空值 |
0 | tinyint/int/bigint/float | 数字默认0 |
1 / -1 / 99 | 数值型 | 自定义固定数字 |
'固定字符串' | varchar/char | 固定文本,如 'unknown' |
'0000-00-00' | date | date零值(sql_mode严格模式下不推荐) |
'0000-00-00 00:00:00' | datetime/timestamp | 时间零值(严格模式报错) |
三、mysql8.0+ 支持【表达式默认值】(必须带括号)
| 默认表达式 | 字段类型 | 说明 |
|---|---|---|
default (uuid()) | char/varchar | 默认生成uuid字符串 |
default (uuid_to_bin(uuid())) | binary(16) | uuid转二进制存储 |
default (rand()) | float | 默认随机数0~1 |
default (json_array()) | json | 默认空json数组 |
default (current_date + interval 1 day) | date | 默认明天日期 |
四、各类型隐式默认值(不写default时自动自带)
| 字段类型 | 不写default的隐式默认值 |
|---|---|
| int/tinyint/bigint | 0(not null);null(允许null) |
| varchar/char | ''(not null);null(允许null) |
| date | 0000-00-00(not null);null |
| datetime | 0000-00-00 00:00:00(not null);null |
| timestamp | 受 explicit_defaults_for_timestamp 参数影响 |
create table test( id int primary key auto_increment, name varchar(32) default '', status tinyint default 1, created_at datetime default current_timestamp, updated_at datetime default current_timestamp on update current_timestamp, create_date date default (current_date), uuid_col char(36) default (uuid()) );
补充:
常用默认值速查
数值类型:int default 0、tinyint default 1、decimal(10,2) default 0.00 这类写法最常用,适合状态位、计数、金额等字段。
字符串类型:varchar(50) default '中国'、char(1) default 'm',适合国家、性别这类固定取值场景。
日期时间类型:datetime default current_timestamp 取当前时间,timestamp default current_timestamp on update current_timestamp 则能在更新记录时自动刷新时间,非常适合 create_time、update_time 字段。
布尔/枚举类型:boolean default false(存储为 0/1)、enum('active','inactive') default 'pending',适合开关状态、业务状态流转场景。
下面这个建表语句基本覆盖了最常见的用法,可以直接参考:
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:停用', `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;
⚠️ 容易踩的坑
- 版本差异:mysql 8.0 才支持
text/blob类型设置默认值,写法是text default ('');5.7 及以下版本不支持。 - 函数限制:只有
current_timestamp可以直接作为默认值;uuid()、rand()、now()这些函数在 8.0.13 之前都不能直接写,之后需要加括号写成表达式形式,如default (uuid())。 - null 与空字符串:
default null表示“未知”,default ''表示“有值但为空文本”,两者语义完全不同,别混用。 - 修改默认值:用
alter table... alter column... set default修改,drop default删除。
🛠️ 操作小贴士
- 新增字段时直接带默认值:
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 alter column col3 drop default;
日常开发里,时间戳、逻辑删除标记、状态位这三个场景是默认值用得最多的,建议把上面那个建表语句存成模板,建新表时直接改字段名就行。
到此这篇关于mysql 常用 default 默认值一览表的文章就介绍到这了,更多相关mysql default默认值内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论