1、背景指定数据列表的排序顺序、上移下移、指定位置插入是日常开发中一些比较常见的需求,比如:支付宝中用户扣款银行卡的先后顺序,QQ 好友分组的按序展示、上移下移调整。本文以一个用户表为例,假定用户的展示顺序有严格要求,并且支持上移下移自定义调整。探讨在 MySQL 中设计和实现该功能,建表语句如下:create table sys_user ( id bigint not null auto_increment comment '主键', username varchar(100) not null comment '用户名', sort bigint not null comment '排序字段', deleted bigint not null default 0 comment '删除标识(0存在,非0删除)', create_time datetime default current_timestamp
1、背景数据唯一性校验是日常开发中非常常见的一个需求,比如:注册的用户名不能重复。在中小型项目(未分库分表)中,唯一索引是解决这类问题非常有效的一个方案。以 MySQL 为例,建表语句如下:create table sys_user ( id bigint not null auto_increment comment '主键', username varchar(100) comment '用户名', create_time datetime default current_timestamp comment '创建时间', update_time datetime default current_timestamp on update current_timestamp comment '更新时间', primary key (id), unique key uk_username (username) ) comment '用户表';第一
这是我在实际项目中遇到的一个数据库表升级变更的案例,以此为原型抽取出 用户表,报告表,报告详情子项表 三张表用于本文演示。变更前一个用户有一份报告,一份报告有多份报告子项。历史版本的数据表设计图如下:以上的关联设计可能不合理,但历史数据表就是如此完成了早期的需求。建表 SQL 及初始化测试数据:/* 用户表 */ create table t_user ( id bigint primary key comment '用户ID', username varchar(50) comment '用户名' ) comment '用户表';/* 报告表 */ create table t_report ( id bigint primary key comment '报告ID', user_id bigint comment '用户ID', content varchar(255) comment '报告内容' ) c