基于AI生成的MySQL建表SOP

简介

希望通过一套SOP,提高MySQL建表效率,降低出错的可能性。使用填表+AI的形式,可以省去编写MySQL建表脚本的过程。

SOP

MySQL建表

表属性 值
表名 user
引擎 InnoDB
字符集 utf8
表注释 用户表
字段名 数据类型 是否允许为空 默认值 注释
id unsigned bigint NOT NULL AUTO_INCREMENT 主键
user_name varchar(50) NOT NULL DEFAULT '' 用户名
user_rank unsigned int NOT NULL DEFAULT '0' 用户等级
gmt_create date_time NOT NULL CURRENT_TIMESTAMP 创建时间
gmt_modified date_time NOT NULL CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 更新时间
字段名 索引名 索引类型 是否为联合索引
user_rank idx_user_user_rank 普通索引 否

将以上三张表丢给AI,即可生成如下建表脚本

mysql 复制代码
CREATE TABLE `user` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `user_name` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
  `user_rank` int unsigned NOT NULL DEFAULT '0' COMMENT '用户等级',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_user_rank` (`user_rank`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

建表规约

  1. 引擎统一使用InnoDB或与旧表相同
  2. 字符集统一使用utf8或与旧表相同
  3. 所有字段都应为NOT NULL
  4. 所有字段都应有默认值
  5. 字符类型字段使用varchar
  6. 数字类型字段参考下表
对象 年龄区间 类型 字节
人 150岁之内 unsigned tinyint 1
龟 数百岁 unsigned smallint 2
恐龙化石 数千万岁 unsigned int 4
太阳 约50亿年 unsigned bigint 8
  1. 所有表应至少包含id、gmt_create、gmt_modified三个字段

notes/MySQL/MySQL数据库开发的三十六条军规.md at master · guanguans/notes (github.com)

p3c/p3c-gitbook/MySQL数据库/建表规约.md at master · alibaba/p3c (github.com)

MySQL 时区全解,datetime 和 timestamp 区别,GMT、UTC、CST、东八区分别指什么? | 老郭种树 (guozh.net)

应用

资源表

资源检索系统中,图片、视频等资源以url的形式存储在数据库中,并拥有类型、格式等属性。

表属性 值
表名 resource
引擎 InnoDB
字符集 utf8
表注释 资源表
字段名 数据类型 是否允许为空 默认值 注释
id unsigned bigint NOT NULL AUTO_INCREMENT 主键
name VARCHAR(100) NOT NULL DEFAULT '' 名称
url VARCHAR(6000) NOT NULL DEFAULT '' URL
type unsigned tinyint NOT NULL DEFAULT '0' 类型。0表示文字、1表示图片、2表示视频
format unsigned smallint NOT NULL DEFAULT '0' 格式。
length unsigned int NOT NULL DEFAULT '0' 长度单位:像素
width unsigned int NOT NULL DEFAULT '0' 宽度。单位:像素
size unsigned bigint NOT NULL DEFAULT '0' 大小。单位:kb
duration unsigned bigint NOT NULL DEFAULT '0' 持续时间。单位:ms
user_id unsigned bigint NOT NULL DEFAULT '0' 用户id
status unsigned tinyint NOT NULL DEFAULT '0' 状态
gmt_create date_time NOT NULL CURRENT_TIMESTAMP 创建时间
gmt_modified date_time NOT NULL CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 更新时间
字段名 索引名 索引类型 是否为联合索引
user_id idx_resource_user_id 普通索引 否

单元表

一个单元下拥有多个资源

表属性 值
表名 unit
引擎 InnoDB
字符集 utf8
表注释 单元表
字段名 数据类型 是否允许为空 默认值 注释
id unsigned bigint NOT NULL AUTO_INCREMENT 主键
name varchar(100) NOT NULL DEFAULT '' 名称
status unsigned tinyint NOT NULL DEFAULT '0' 状态
gmt_create date_time NOT NULL CURRENT_TIMESTAMP 创建时间
gmt_modified date_time NOT NULL CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 更新时间

单元资源表

单元表与资源表间的关系表

表属性 值
表名 unit_resouce
引擎 InnoDB
字符集 utf8
表注释 单元资源表
字段名 数据类型 是否允许为空 默认值 注释
id unsigned bigint NOT NULL AUTO_INCREMENT 主键
unit_id unsigned bigint NOT NULL DEFAULT '0' 单元id
resource_id unsigned bigint NOT NULL DEFAULT '0' 资源id
status unsigned tinyint NOT NULL DEFAULT '0' 状态
gmt_create date_time NOT NULL CURRENT_TIMESTAMP 创建时间
gmt_modified date_time NOT NULL CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 更新时间
字段名 索引名 索引类型 是否为联合索引
unit_id idx_unit_resouce_unit_id 普通索引 否

地区表

一个单元有地区特性

表属性 值
表名 unit_district
引擎 InnoDB
字符集 utf8
表注释 单元地区表
字段名 数据类型 是否允许为空 默认值 注释
id unsigned bigint NOT NULL AUTO_INCREMENT 主键
unit_id unsigned bigint NOT NULL DEFAULT '0' 单元id
province varchar(100) NOT NULL DEFAULT '' 省份
city varchar(100) NOT NULL DEFAULT '' 城市
status unsigned tinyint NOT NULL DEFAULT '0' 状态
gmt_create date_time NOT NULL CURRENT_TIMESTAMP 创建时间
gmt_modified date_time NOT NULL CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 更新时间
字段名 索引名 索引类型 是否为联合索引
unit_id idx_unit_district_unit_id 普通索引 否

关键词

一个单元有关键词特性

表属性 值
表名 unit_keyword
引擎 InnoDB
字符集 utf8
表注释 单元关键词表
字段名 数据类型 是否允许为空 默认值 注释
id unsigned bigint NOT NULL AUTO_INCREMENT 主键
unit_id unsigned bigint NOT NULL DEFAULT '0' 单元id
keyword varchar(100) NOT NULL DEFAULT '' 关键词
status unsigned tinyint NOT NULL DEFAULT '0' 状态
gmt_create date_time NOT NULL CURRENT_TIMESTAMP 创建时间
gmt_modified date_time NOT NULL CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 更新时间
字段名 索引名 索引类型 是否为联合索引
unit_id idx_unit_keyword_unit_id 普通索引 否
相关推荐
ss2733 小时前
为什么PostgreSQL适合AI场景?
数据库·人工智能·postgresql
瑞克-桑切斯3 小时前
7、MySQL 慢查询排查与优化实战指南
java·服务器·数据库
z06.4 小时前
MySQL索引
数据库·mysql
考虑考虑4 小时前
Posrgresql恢复.backup文件
数据库·后端·postgresql
云祺vinchin5 小时前
中小企业备份软件选型从恢复需求到总体成本评估
数据库·数据备份·中小企业备份·容灾备份软件
Zenova EdgeOS5 小时前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
KKKlucifer5 小时前
国内数据安全服务的产品智能化转型:大模型赋能下的技术能力重构分析
大数据·数据库·重构
字节跳动开源6 小时前
OpenViking 上线原生 3D 内容理解模型
数据库·开源
CCIE-Yasuo8 小时前
网络安全-学习数据库-Mysql-2
数据库·学习·mysql
这个DBA有点耶8 小时前
内核级AI与多模融合:AI数据库在海外场景的落地要点
数据库·人工智能·架构