不灭的焱

革命尚未成功,同志仍须努力 下载Java21

作者:AlbertWen  添加时间:2026-06-15 21:04:06  修改时间:2026-06-22 23:13:10  分类:18.系统设计/UML建模  编辑

数据表结构设计规范

版本:V1.0
数据库:默认以 MySQL 8.x 为主
命名风格:小写字母 + 下划线
通用字段风格:create_by / create_time / update_by / update_time / delete_flag / delete_by / delete_time

1. 总体设计原则

1.1 命名统一优先

同一个系统中,字段命名必须保持统一,不要混用多套风格。

本规范统一采用:

create_by
create_time
update_by
update_time
delete_flag
delete_by
delete_time

不建议同一个系统中同时出现:

create_time
created_at

create_by
created_by

delete_flag
is_deleted
deleted
del_flag

规范重点是 统一、清晰、可维护

1.2 数据库命名风格

表名、字段名、索引名统一使用:

小写字母 + 下划线

推荐:

sys_user
sys_role
biz_order
biz_customer
user_name
phone_number
create_time
delete_flag

不推荐:

SysUser
sysUser
SYS_USER
userName
createTime
isDeleted

2. 表名设计规范

2.1 表名格式

表名建议使用:

模块前缀 + 业务名称

示例:

sys_user
sys_role
sys_menu
biz_order
biz_customer
biz_product
rel_user_role
log_login

2.2 常见表名前缀

前缀 含义 示例
sys_ 系统管理类表 sys_user、sys_role
biz_ 业务数据表 biz_order、biz_customer
rel_ 关系表 / 中间表 rel_user_role
log_ 日志表 log_login、log_operation
cfg_ 配置表 cfg_system
dict_ 字典表 dict_type、dict_item

2.3 避免使用数据库关键字

不建议直接使用以下表名或字段名:

user
order
group
key
index
desc
select
delete
status

其中 status 虽然常用,但不是所有数据库中的强关键字;作为字段名通常可以使用。
但 order、user、group 这类不建议直接作为表名。

推荐替代:

sys_user
biz_order
sys_group

3. 字段命名规范

3.1 字段命名格式

字段统一使用:

小写字母 + 下划线

推荐:

user_name
phone_number
order_no
create_time
update_time
delete_flag
sort_no

不推荐:

userName
phoneNumber
orderNo
createTime
updateTime
isDeleted

3.2 字段命名应表达业务含义

推荐:

customer_name varchar(100) comment '客户名称'
order_no varchar(64) comment '订单编号'
pay_amount decimal(18,2) comment '支付金额'

不推荐:

name varchar(100) comment '名称'
code varchar(64) comment '编码'
amount decimal(18,2) comment '金额'

除非业务上下文非常明确,否则不要使用过于泛化的字段名。

4. 主键字段规范

4.1 主键字段统一命名

所有业务表主键字段统一命名为:

id

推荐类型:

id bigint not null auto_increment comment '主键ID'

完整写法:

id bigint not null auto_increment comment '主键ID',
primary key (id)

4.2 主键类型规范

场景 推荐类型 示例
单体系统 / 普通业务系统 bigint auto_increment 自增主键
分布式系统 bigint 雪花算法 ID
特殊编码主键 varchar(64) 不推荐作为默认规范

推荐:

id bigint not null auto_increment comment '主键ID'

如果使用雪花算法:

id bigint not null comment '主键ID'

4.3 不建议使用业务字段作为主键

不推荐:

order_no varchar(64) primary key
phone varchar(20) primary key
user_code varchar(64) primary key

推荐:

id bigint not null auto_increment comment '主键ID',
order_no varchar(64) not null comment '订单编号',
primary key (id),
unique key uk_order_no (order_no)

业务唯一字段用唯一索引控制,不建议直接作为主键。

5. 通用审计字段规范

每张业务表建议默认包含以下字段:

create_by
create_time
update_by
update_time
delete_flag
delete_by
delete_time

5.1 通用字段定义

字段名 类型 是否为空 默认值 说明
create_by bigint null 创建人ID
create_time datetime current_timestamp 创建时间
update_by bigint null 修改人ID
update_time datetime null 修改时间
delete_flag tinyint 0 是否删除:0-否,1-是
delete_by bigint null 删除人ID
delete_time datetime null 删除时间

推荐 SQL:

create_by bigint null comment '创建人ID',
create_time datetime not null default current_timestamp comment '创建时间',
update_by bigint null comment '修改人ID',
update_time datetime null comment '修改时间',
delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
delete_by bigint null comment '删除人ID',
delete_time datetime null comment '删除时间'

5.2 字段含义说明

create_by

表示创建该记录的用户 ID。

create_by bigint null comment '创建人ID'

建议存用户表主键 ID,不建议直接存用户名。

不推荐:

create_by varchar(50) comment '创建人'

除非系统中用户 ID 本身就是字符串类型。

create_time

表示记录创建时间。

create_time datetime not null default current_timestamp comment '创建时间'

推荐创建时自动写入。

update_by

表示最后一次修改该记录的用户 ID。

update_by bigint null comment '修改人ID'

如果记录创建后从未修改,可以为空。

update_time

表示最后一次修改时间。

update_time datetime null comment '修改时间'

是否使用数据库自动更新,可以根据团队习惯二选一。

方式一:应用程序维护,推荐:

update_time datetime null comment '修改时间'

方式二:数据库自动维护:

update_time datetime null default null on update current_timestamp comment '修改时间'

如果使用 ORM 自动填充,例如 MyBatis-Plus、JPA、后端公共拦截器,建议使用方式一。

delete_flag

表示逻辑删除标识。

delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是'

取值:

含义
0 未删除
1 已删除

delete_by

表示删除该记录的用户 ID。

delete_by bigint null comment '删除人ID'

delete_time

表示逻辑删除时间。

delete_time datetime null comment '删除时间'

未删除时为空。

6. 逻辑删除规范

6.1 统一使用 delete_flag

本规范中,逻辑删除字段统一使用:

delete_flag

不使用:

is_deleted
deleted
del_flag
delete_status
is_delete

推荐:

delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是'

6.2 查询时必须过滤已删除数据

业务查询默认增加:

where delete_flag = 0

示例:

select *
from biz_customer
where delete_flag = 0;

6.3 删除时不做物理删除

不推荐:

delete from biz_customer where id = 1;

推荐:

update biz_customer
set delete_flag = 1,
    delete_by = 10001,
    delete_time = now()
where id = 1
  and delete_flag = 0;

6.4 逻辑删除字段组合

建议逻辑删除相关字段固定为:

delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
delete_by bigint null comment '删除人ID',
delete_time datetime null comment '删除时间'

7. 是否类字段规范

7.1 是否类字段统一使用 _flag 后缀

本规范建议:是否类字段统一使用 xxx_flag,不使用 is_xxx。

推荐:

enable_flag
default_flag
visible_flag
required_flag
system_flag
delete_flag

不推荐:

is_enabled
is_default
is_visible
is_required
is_system
is_deleted

7.2 字段类型统一

是否类字段统一使用:

tinyint not null default 0

标准格式:

xxx_flag tinyint not null default 0 comment '是否xxx:0-否,1-是'

如果默认值为“是”,使用:

xxx_flag tinyint not null default 1 comment '是否xxx:0-否,1-是'

7.3 取值统一

含义
0
1

不要使用:

Y / N
true / false
yes / no

数据库统一存储 0 / 1。

7.4 优先使用正向语义

推荐:

enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是'
visible_flag tinyint not null default 1 comment '是否可见:0-否,1-是'

不推荐同时出现正反两个字段:

enable_flag
disable_flag

二选一即可。

一般推荐正向语义:

enable_flag
visible_flag
required_flag
default_flag

8. 常见 is_ 字段与新规范字段对照表

8.1 基础通用类

旧字段名 新规范字段名 类型 默认值 注释
is_deleted delete_flag tinyint 0 是否删除:0-否,1-是
is_enabled enable_flag tinyint 1 是否启用:0-否,1-是
is_disabled disable_flag tinyint 0 是否禁用:0-否,1-是
is_active active_flag tinyint 1 是否激活:0-否,1-是
is_valid valid_flag tinyint 1 是否有效:0-否,1-是
is_invalid invalid_flag tinyint 0 是否无效:0-否,1-是
is_available available_flag tinyint 1 是否可用:0-否,1-是
is_locked lock_flag tinyint 0 是否锁定:0-否,1-是
is_frozen frozen_flag tinyint 0 是否冻结:0-否,1-是

8.2 默认 / 系统 / 管理类

旧字段名 新规范字段名 类型 默认值 注释
is_default default_flag tinyint 0 是否默认:0-否,1-是
is_system system_flag tinyint 0 是否系统内置:0-否,1-是
is_builtin builtin_flag tinyint 0 是否内置:0-否,1-是
is_admin admin_flag tinyint 0 是否管理员:0-否,1-是
is_super_admin super_admin_flag tinyint 0 是否超级管理员:0-否,1-是
is_master master_flag tinyint 0 是否主记录:0-否,1-是
is_primary primary_flag tinyint 0 是否主要:0-否,1-是
is_global global_flag tinyint 0 是否全局:0-否,1-是

8.3 显示 / 权限 / 访问控制类

旧字段名 新规范字段名 类型 默认值 注释
is_visible visible_flag tinyint 1 是否可见:0-否,1-是
is_hidden hidden_flag tinyint 0 是否隐藏:0-否,1-是
is_public public_flag tinyint 0 是否公开:0-否,1-是
is_private private_flag tinyint 0 是否私有:0-否,1-是
is_open open_flag tinyint 0 是否开放:0-否,1-是
is_anonymous anonymous_flag tinyint 0 是否允许匿名:0-否,1-是
is_readonly readonly_flag tinyint 0 是否只读:0-否,1-是
is_editable editable_flag tinyint 1 是否可编辑:0-否,1-是
is_removable removable_flag tinyint 1 是否可移除:0-否,1-是

8.4 表单 / 字段 / 页面配置类

旧字段名 新规范字段名 类型 默认值 注释
is_required required_flag tinyint 0 是否必填:0-否,1-是
is_nullable nullable_flag tinyint 1 是否允许为空:0-否,1-是
is_unique unique_flag tinyint 0 是否唯一:0-否,1-是
is_searchable searchable_flag tinyint 0 是否可搜索:0-否,1-是
is_queryable queryable_flag tinyint 0 是否可查询:0-否,1-是
is_exportable exportable_flag tinyint 0 是否可导出:0-否,1-是
is_importable importable_flag tinyint 0 是否可导入:0-否,1-是
is_printable printable_flag tinyint 0 是否可打印:0-否,1-是
is_multiple multiple_flag tinyint 0 是否多选:0-否,1-是
is_single single_flag tinyint 1 是否单选:0-否,1-是

8.5 内容 / 文章 / 商品类

旧字段名 新规范字段名 类型 默认值 注释
is_top top_flag tinyint 0 是否置顶:0-否,1-是
is_recommend recommend_flag tinyint 0 是否推荐:0-否,1-是
is_hot hot_flag tinyint 0 是否热门:0-否,1-是
is_new new_flag tinyint 0 是否新品/新内容:0-否,1-是
is_featured featured_flag tinyint 0 是否精选:0-否,1-是
is_published publish_flag tinyint 0 是否发布:0-否,1-是
is_draft draft_flag tinyint 0 是否草稿:0-否,1-是
is_original original_flag tinyint 1 是否原创:0-否,1-是
is_external external_flag tinyint 0 是否外部链接:0-否,1-是

8.6 流程 / 审批 / 任务类

旧字段名 新规范字段名 类型 默认值 注释
is_submitted submit_flag tinyint 0 是否已提交:0-否,1-是
is_approved approve_flag tinyint 0 是否已审批:0-否,1-是
is_rejected reject_flag tinyint 0 是否已驳回:0-否,1-是
is_finished finish_flag tinyint 0 是否已完成:0-否,1-是
is_completed complete_flag tinyint 0 是否已完成:0-否,1-是
is_canceled cancel_flag tinyint 0 是否已取消:0-否,1-是
is_closed close_flag tinyint 0 是否已关闭:0-否,1-是
is_timeout timeout_flag tinyint 0 是否超时:0-否,1-是
is_urgent urgent_flag tinyint 0 是否紧急:0-否,1-是
is_overdue overdue_flag tinyint 0 是否逾期:0-否,1-是

注意:流程类业务如果存在多个互斥状态,优先使用 status,不要设计成多个 xxx_flag。

不推荐:

submit_flag tinyint not null default 0 comment '是否已提交:0-否,1-是',
approve_flag tinyint not null default 0 comment '是否已审批:0-否,1-是',
reject_flag tinyint not null default 0 comment '是否已驳回:0-否,1-是'

更推荐:

status tinyint not null default 0 comment '状态:0-待提交,1-审批中,2-已通过,3-已驳回,4-已取消'

8.7 用户 / 登录 / 安全类

旧字段名 新规范字段名 类型 默认值 注释
is_online online_flag tinyint 0 是否在线:0-否,1-是
is_login login_flag tinyint 0 是否已登录:0-否,1-是
is_verified verify_flag tinyint 0 是否已验证:0-否,1-是
is_certified certify_flag tinyint 0 是否已认证:0-否,1-是
is_real_name real_name_flag tinyint 0 是否实名:0-否,1-是
is_password_expired password_expire_flag tinyint 0 密码是否过期:0-否,1-是
is_force_change_password force_change_password_flag tinyint 0 是否强制修改密码:0-否,1-是
is_mfa_enabled mfa_enable_flag tinyint 0 是否启用多因素认证:0-否,1-是
is_account_locked account_lock_flag tinyint 0 账号是否锁定:0-否,1-是

8.8 订单 / 支付 / 交易类

旧字段名 新规范字段名 类型 默认值 注释
is_paid pay_flag tinyint 0 是否已支付:0-否,1-是
is_refunded refund_flag tinyint 0 是否已退款:0-否,1-是
is_shipped ship_flag tinyint 0 是否已发货:0-否,1-是
is_received receive_flag tinyint 0 是否已收货:0-否,1-是
is_invoiced invoice_flag tinyint 0 是否已开票:0-否,1-是
is_settled settle_flag tinyint 0 是否已结算:0-否,1-是
is_confirmed confirm_flag tinyint 0 是否已确认:0-否,1-是
is_split split_flag tinyint 0 是否拆分:0-否,1-是
is_merge merge_flag tinyint 0 是否合并:0-否,1-是

订单类复杂状态更推荐拆成状态字段:

order_status tinyint not null default 0 comment '订单状态:0-待提交,1-待支付,2-待发货,3-待收货,4-已完成,5-已取消',
pay_status tinyint not null default 0 comment '支付状态:0-未支付,1-已支付,2-已退款',
delivery_status tinyint not null default 0 comment '发货状态:0-未发货,1-已发货,2-已收货'

8.9 文件 / 附件 / 图片类

旧字段名 新规范字段名 类型 默认值 注释
is_image image_flag tinyint 0 是否图片:0-否,1-是
is_video video_flag tinyint 0 是否视频:0-否,1-是
is_audio audio_flag tinyint 0 是否音频:0-否,1-是
is_folder folder_flag tinyint 0 是否文件夹:0-否,1-是
is_encrypted encrypt_flag tinyint 0 是否加密:0-否,1-是
is_compressed compress_flag tinyint 0 是否压缩:0-否,1-是
is_uploaded upload_flag tinyint 0 是否已上传:0-否,1-是
is_downloaded download_flag tinyint 0 是否已下载:0-否,1-是

8.10 消息 / 通知 / 阅读类

旧字段名 新规范字段名 类型 默认值 注释
is_read read_flag tinyint 0 是否已读:0-否,1-是
is_unread unread_flag tinyint 1 是否未读:0-否,1-是
is_sent send_flag tinyint 0 是否已发送:0-否,1-是
is_pushed push_flag tinyint 0 是否已推送:0-否,1-是
is_notified notify_flag tinyint 0 是否已通知:0-否,1-是
is_reminded remind_flag tinyint 0 是否已提醒:0-否,1-是
is_confirmed confirm_flag tinyint 0 是否已确认:0-否,1-是

9. 状态字段规范

9.1 多状态字段统一使用 status

当业务不是简单的“是/否”,而是多个互斥状态时,使用 status。

推荐:

status tinyint not null default 1 comment '状态:0-禁用,1-启用'

或:

status tinyint not null default 0 comment '状态:0-待提交,1-审批中,2-已通过,3-已驳回,4-已取消'

9.2 状态字段与标识字段区别

类型 字段示例 使用场景
标识字段 enable_flag 是/否
状态字段 status 多个互斥状态
类型字段 type 分类
状态细分字段 pay_status 某一业务维度的状态

9.3 不要用多个 flag 表达一个流程

不推荐:

submit_flag tinyint not null default 0 comment '是否提交:0-否,1-是',
approve_flag tinyint not null default 0 comment '是否审批:0-否,1-是',
reject_flag tinyint not null default 0 comment '是否驳回:0-否,1-是'

推荐:

status tinyint not null default 0 comment '状态:0-待提交,1-审批中,2-已通过,3-已驳回'

10. 排序字段规范

10.1 排序号字段统一使用 sort_no

排序号字段统一命名为:

sort_no

推荐定义:

sort_no int not null default 0 comment '排序号'

10.2 排序规则

默认规则建议:

sort_no 越小越靠前

查询示例:

order by sort_no asc, id desc

10.3 不推荐字段名

不推荐:

sort
sort_num
order
order_no
order_num
seq
sequence

原因:

字段名 问题
sort 语义略泛
order 接近 SQL 关键字
order_no 容易和订单编号混淆
seq 缩写不够直观
sequence 偏长,且可能和数据库序列概念混淆

推荐统一使用:

sort_no int not null default 0 comment '排序号'

11. 常用字段类型规范

11.1 字符串类型

场景 推荐类型 示例
姓名 varchar(50) user_name varchar(50)
手机号 varchar(20) phone varchar(20)
邮箱 varchar(100) email varchar(100)
编号 varchar(64) order_no varchar(64)
标题 varchar(200) title varchar(200)
地址 varchar(255) address varchar(255)
备注 varchar(500) remark varchar(500)
简介 varchar(1000) summary varchar(1000)
长文本 text content text
富文本 text / mediumtext content text

示例:

user_name varchar(50) not null comment '用户名称',
phone varchar(20) null comment '手机号',
email varchar(100) null comment '邮箱',
remark varchar(500) null comment '备注'

注意:

  • 手机号、身份证号、银行卡号、订单号、编号类字段不要使用数字类型。

  • 编号类字段推荐使用 varchar。

  • 大段文本使用 text。

  • 超大内容使用 mediumtext,但不建议滥用。

11.2 数字类型

场景 推荐类型 示例
主键ID bigint id bigint
用户ID bigint user_id bigint
数量 int quantity int
排序号 int sort_no int
状态 tinyint status tinyint
是否标识 tinyint enable_flag tinyint
金额 decimal(18,2) amount decimal(18,2)
汇率 decimal(18,6) exchange_rate decimal(18,6)
经纬度 decimal(10,6) longitude decimal(10,6)

示例:

quantity int not null default 0 comment '数量',
sort_no int not null default 0 comment '排序号',
status tinyint not null default 1 comment '状态:0-禁用,1-启用',
amount decimal(18,2) not null default 0.00 comment '金额'

金额字段必须使用 decimal,不使用:

float
double

11.3 时间类型

场景 推荐类型 示例
创建时间 datetime create_time datetime
修改时间 datetime update_time datetime
删除时间 datetime delete_time datetime
开始时间 datetime start_time datetime
结束时间 datetime end_time datetime
生日 date birthday date
日期 date stat_date date
时间点 time start_clock time

推荐:

create_time datetime not null default current_timestamp comment '创建时间',
update_time datetime null comment '修改时间',
delete_time datetime null comment '删除时间',
start_time datetime null comment '开始时间',
end_time datetime null comment '结束时间'

不推荐把时间存成字符串:

create_time varchar(20)

11.4 JSON 类型

如果字段结构灵活,可以使用:

extra_json json null comment '扩展信息JSON'

或:

config_json json null comment '配置JSON'

命名建议:

xxx_json

示例:

extra_json json null comment '扩展信息JSON',
config_json json null comment '配置JSON'

不要用含义不清的字段名:

json
data
extra

12. 常用业务字段规范

12.1 编号字段

编号字段统一使用:

xxx_no

示例:

order_no varchar(64) not null comment '订单编号',
customer_no varchar(64) not null comment '客户编号',
product_no varchar(64) not null comment '商品编号'

唯一业务编号建议加唯一索引:

unique key uk_order_no (order_no)

12.2 名称字段

名称字段统一使用:

xxx_name

示例:

user_name varchar(50) not null comment '用户名称',
role_name varchar(50) not null comment '角色名称',
customer_name varchar(100) not null comment '客户名称'

12.3 类型字段

类型字段使用:

xxx_type

示例:

user_type tinyint not null default 1 comment '用户类型:1-普通用户,2-管理员',
order_type tinyint not null default 1 comment '订单类型:1-普通订单,2-售后订单'

如果只有一个类型维度,也可以使用:

type tinyint not null comment '类型'

但更推荐带业务前缀,避免含义不清。

12.4 状态字段

状态字段使用:

status

或业务维度状态:

order_status
pay_status
audit_status
delivery_status

示例:

status tinyint not null default 1 comment '状态:0-禁用,1-启用',
audit_status tinyint not null default 0 comment '审核状态:0-待审核,1-已通过,2-已驳回'

12.5 备注字段

备注字段统一使用:

remark

推荐:

remark varchar(500) null comment '备注'

如果内容较长:

remark text null comment '备注'

13. 字段顺序规范

建议字段顺序统一如下:

1. 主键字段
2. 业务核心字段
3. 业务状态字段
4. 是否标识字段
5. 排序字段
6. 备注字段
7. 创建人、创建时间
8. 修改人、修改时间
9. 删除标识、删除人、删除时间

标准顺序示例:

id
业务字段...
status
xxx_flag
sort_no
remark
create_by
create_time
update_by
update_time
delete_flag
delete_by
delete_time

14. 字段默认值规范

14.1 必填字段建议设置默认值

示例:

status tinyint not null default 1 comment '状态:0-禁用,1-启用',
sort_no int not null default 0 comment '排序号',
delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
create_time datetime not null default current_timestamp comment '创建时间'

14.2 可选字段允许为空

示例:

phone varchar(20) null comment '手机号',
email varchar(100) null comment '邮箱',
remark varchar(500) null comment '备注',
update_time datetime null comment '修改时间',
delete_time datetime null comment '删除时间'

14.3 字符串字段不要滥用空字符串默认值

不推荐:

remark varchar(500) not null default '' comment '备注'

除非团队明确要求所有字符串非空。

一般推荐:

remark varchar(500) null comment '备注'

15. 索引设计规范

15.1 主键索引

每张业务表必须有主键:

primary key (id)

15.2 唯一索引

业务上不允许重复的字段使用唯一索引。

示例:

unique key uk_order_no (order_no)

命名规范:

uk_字段名

示例:

unique key uk_user_name (user_name),
unique key uk_phone (phone),
unique key uk_order_no (order_no)

15.3 普通索引

经常作为查询条件的字段可以建立普通索引。

命名规范:

idx_字段名

示例:

key idx_customer_name (customer_name),
key idx_create_time (create_time),
key idx_status (status)

15.4 联合索引

多个字段经常组合查询时,建立联合索引。

命名规范:

idx_字段1_字段2_字段3

示例:

key idx_delete_flag_status_create_time (delete_flag, status, create_time)

15.5 逻辑删除字段索引建议

delete_flag 是低基数字段,单独建索引价值通常有限。
更推荐和业务查询条件组合建联合索引。

不太推荐:

key idx_delete_flag (delete_flag)

更推荐:

key idx_delete_flag_status (delete_flag, status),
key idx_delete_flag_create_time (delete_flag, create_time),
key idx_delete_flag_customer_name (delete_flag, customer_name)

具体是否建索引,要看实际查询条件。

15.6 索引不宜过多

不要给每个字段都建索引。

索引会提升查询性能,但会降低写入性能,并增加存储空间。

16. 字段注释规范

16.1 所有字段必须写注释

推荐:

customer_name varchar(100) not null comment '客户名称'

不推荐:

customer_name varchar(100) not null

16.2 状态字段必须说明枚举值

推荐:

status tinyint not null default 1 comment '状态:0-禁用,1-启用'

不推荐:

status tinyint not null default 1 comment '状态'

16.3 是否字段必须说明 0 和 1

推荐:

enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是'

不推荐:

enable_flag tinyint not null default 1 comment '是否启用'

17. Java 实体字段映射规范

数据库字段使用下划线命名,Java 实体字段使用驼峰命名。

17.1 通用字段映射

数据库字段 Java 字段
id id
create_by createBy
create_time createTime
update_by updateBy
update_time updateTime
delete_flag deleteFlag
delete_by deleteBy
delete_time deleteTime
sort_no sortNo
enable_flag enableFlag
default_flag defaultFlag

17.2 数据类型映射

MySQL 类型 Java 类型
bigint Long
int Integer
tinyint Integer / Boolean
decimal BigDecimal
varchar String
text String
datetime LocalDateTime
date LocalDate
time LocalTime
json String / 对象类型

17.3 是否字段 Java 类型建议

数据库统一使用:

tinyint

Java 可使用:

private Integer deleteFlag;
private Integer enableFlag;

或:

private Boolean deleteFlag;
private Boolean enableFlag;

如果使用 Boolean,需要确保 ORM、序列化、代码生成规则一致。

为了减少歧义,后台管理系统中也可以统一使用 Integer 表示 0 / 1。

18. 标准基础字段模板

新建业务表时,可以直接使用以下模板:

id bigint not null auto_increment comment '主键ID',

create_by bigint null comment '创建人ID',
create_time datetime not null default current_timestamp comment '创建时间',
update_by bigint null comment '修改人ID',
update_time datetime null comment '修改时间',
delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
delete_by bigint null comment '删除人ID',
delete_time datetime null comment '删除时间',

primary key (id)

如果需要排序、状态、备注,增加:

status tinyint not null default 1 comment '状态:0-禁用,1-启用',
sort_no int not null default 0 comment '排序号',
remark varchar(500) null comment '备注'

19. 标准业务表模板

create table biz_xxx (
    id bigint not null auto_increment comment '主键ID',

    -- 业务字段
    xxx_name varchar(100) not null comment '名称',
    xxx_no varchar(64) null comment '编号',

    -- 状态与标识
    status tinyint not null default 1 comment '状态:0-禁用,1-启用',
    enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是',
    sort_no int not null default 0 comment '排序号',
    remark varchar(500) null comment '备注',

    -- 创建信息
    create_by bigint null comment '创建人ID',
    create_time datetime not null default current_timestamp comment '创建时间',

    -- 修改信息
    update_by bigint null comment '修改人ID',
    update_time datetime null comment '修改时间',

    -- 删除信息
    delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
    delete_by bigint null comment '删除人ID',
    delete_time datetime null comment '删除时间',

    primary key (id),
    key idx_delete_flag_status (delete_flag, status),
    key idx_create_time (create_time)
) engine=InnoDB default charset=utf8mb4 comment='业务表';

20. 完整建表示例:客户表

create table biz_customer (
    id bigint not null auto_increment comment '主键ID',

    customer_no varchar(64) not null comment '客户编号',
    customer_name varchar(100) not null comment '客户名称',
    phone varchar(20) null comment '手机号',
    email varchar(100) null comment '邮箱',
    address varchar(255) null comment '地址',

    customer_type tinyint not null default 1 comment '客户类型:1-个人客户,2-企业客户',
    status tinyint not null default 1 comment '状态:0-禁用,1-启用',
    enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是',
    default_flag tinyint not null default 0 comment '是否默认:0-否,1-是',
    sort_no int not null default 0 comment '排序号',
    remark varchar(500) null comment '备注',

    create_by bigint null comment '创建人ID',
    create_time datetime not null default current_timestamp comment '创建时间',
    update_by bigint null comment '修改人ID',
    update_time datetime null comment '修改时间',
    delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
    delete_by bigint null comment '删除人ID',
    delete_time datetime null comment '删除时间',

    primary key (id),
    unique key uk_customer_no (customer_no),
    key idx_customer_name (customer_name),
    key idx_delete_flag_status (delete_flag, status),
    key idx_create_time (create_time)
) engine=InnoDB default charset=utf8mb4 comment='客户信息表';

21. 完整建表示例:角色表

create table sys_role (
    id bigint not null auto_increment comment '主键ID',

    role_code varchar(64) not null comment '角色编码',
    role_name varchar(100) not null comment '角色名称',

    enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是',
    system_flag tinyint not null default 0 comment '是否系统内置:0-否,1-是',
    default_flag tinyint not null default 0 comment '是否默认:0-否,1-是',
    sort_no int not null default 0 comment '排序号',
    remark varchar(500) null comment '备注',

    create_by bigint null comment '创建人ID',
    create_time datetime not null default current_timestamp comment '创建时间',
    update_by bigint null comment '修改人ID',
    update_time datetime null comment '修改时间',
    delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
    delete_by bigint null comment '删除人ID',
    delete_time datetime null comment '删除时间',

    primary key (id),
    unique key uk_role_code (role_code),
    key idx_delete_flag_enable_flag (delete_flag, enable_flag),
    key idx_sort_no (sort_no)
) engine=InnoDB default charset=utf8mb4 comment='角色表';

22. 关系表设计规范

关系表一般用于多对多关系。

例如用户角色关系表:

create table rel_user_role (
    id bigint not null auto_increment comment '主键ID',

    user_id bigint not null comment '用户ID',
    role_id bigint not null comment '角色ID',

    create_by bigint null comment '创建人ID',
    create_time datetime not null default current_timestamp comment '创建时间',

    primary key (id),
    unique key uk_user_id_role_id (user_id, role_id),
    key idx_role_id (role_id)
) engine=InnoDB default charset=utf8mb4 comment='用户角色关系表';

关系表是否需要 update_by / update_time / delete_flag,按业务决定。

如果关系需要逻辑删除和审计,则使用完整通用字段:

create_by
create_time
update_by
update_time
delete_flag
delete_by
delete_time

23. 日志表设计规范

日志表一般可以不做逻辑删除。

示例:

create table log_operation (
    id bigint not null auto_increment comment '主键ID',

    user_id bigint null comment '用户ID',
    user_name varchar(100) null comment '用户名称',
    operation_type varchar(50) not null comment '操作类型',
    operation_content varchar(500) null comment '操作内容',
    request_uri varchar(255) null comment '请求地址',
    request_method varchar(20) null comment '请求方法',
    request_ip varchar(50) null comment '请求IP',
    success_flag tinyint not null default 1 comment '是否成功:0-否,1-是',
    error_msg text null comment '错误信息',

    create_time datetime not null default current_timestamp comment '创建时间',

    primary key (id),
    key idx_user_id (user_id),
    key idx_create_time (create_time),
    key idx_success_flag (success_flag)
) engine=InnoDB default charset=utf8mb4 comment='操作日志表';

日志表通常不需要:

update_by
update_time
delete_flag
delete_by
delete_time

24. 不推荐写法汇总

24.1 不推荐的创建修改字段

不推荐:

creator
create_user
created_by
created_at
modifier
modify_user
modified_time

本规范推荐:

create_by
create_time
update_by
update_time

24.2 不推荐的删除字段

不推荐:

is_deleted
is_delete
deleted
del_flag
delete_status
delete

本规范推荐:

delete_flag
delete_by
delete_time

24.3 不推荐的是否字段

不推荐:

is_enabled
is_default
is_visible
is_required
is_locked

本规范推荐:

enable_flag
default_flag
visible_flag
required_flag
lock_flag

24.4 不推荐的排序字段

不推荐:

sort
sort_num
order
order_no
order_num
seq

本规范推荐:

sort_no

25. 最终统一规范摘要

25.1 每张业务表默认字段

id bigint not null auto_increment comment '主键ID',
create_by bigint null comment '创建人ID',
create_time datetime not null default current_timestamp comment '创建时间',
update_by bigint null comment '修改人ID',
update_time datetime null comment '修改时间',
delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
delete_by bigint null comment '删除人ID',
delete_time datetime null comment '删除时间'

25.2 常用扩展字段

status tinyint not null default 1 comment '状态:0-禁用,1-启用',
enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是',
sort_no int not null default 0 comment '排序号',
remark varchar(500) null comment '备注'

25.3 是否类字段规范

统一使用:xxx_flag
字段类型:tinyint
取值规则:0-否,1-是
命名要求:不用 is_ 前缀
注释要求:必须写清楚 0 和 1 的含义

示例:

delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是',
default_flag tinyint not null default 0 comment '是否默认:0-否,1-是',
visible_flag tinyint not null default 1 comment '是否可见:0-否,1-是',
required_flag tinyint not null default 0 comment '是否必填:0-否,1-是',
system_flag tinyint not null default 0 comment '是否系统内置:0-否,1-是'

25.4 推荐字段顺序

id

业务字段...

status
enable_flag
default_flag
sort_no
remark

create_by
create_time
update_by
update_time
delete_flag
delete_by
delete_time

26. 推荐最终建表骨架

create table 表名 (
    id bigint not null auto_increment comment '主键ID',

    -- 业务字段
    name varchar(100) not null comment '名称',
    code varchar(64) null comment '编码',

    -- 状态、标识、排序、备注
    status tinyint not null default 1 comment '状态:0-禁用,1-启用',
    enable_flag tinyint not null default 1 comment '是否启用:0-否,1-是',
    sort_no int not null default 0 comment '排序号',
    remark varchar(500) null comment '备注',

    -- 创建信息
    create_by bigint null comment '创建人ID',
    create_time datetime not null default current_timestamp comment '创建时间',

    -- 修改信息
    update_by bigint null comment '修改人ID',
    update_time datetime null comment '修改时间',

    -- 删除信息
    delete_flag tinyint not null default 0 comment '是否删除:0-否,1-是',
    delete_by bigint null comment '删除人ID',
    delete_time datetime null comment '删除时间',

    primary key (id),
    key idx_delete_flag_status (delete_flag, status),
    key idx_create_time (create_time)
) engine=InnoDB default charset=utf8mb4 comment='表注释';

最终建议你们团队直接定死这几个核心规则:

1. 审计字段统一使用 create_by / create_time / update_by / update_time / delete_by / delete_time
2. 逻辑删除统一使用 delete_flag
3. 是否类字段统一使用 xxx_flag
4. 是否类字段统一 tinyint,0-否,1-是
5. 排序字段统一使用 sort_no
6. 状态字段统一使用 status 或 xxx_status
7. 主键字段统一使用 id,类型 bigint
8. 所有表和字段必须写 comment
9. 同一系统内禁止混用 created_at、is_deleted、deleted、del_flag 等其他风格