基于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 普通索引 否
相关推荐
parade岁月1 小时前
没有称手工具了怎么办?搓一个!AI 时代顺手造了个单文件 Mermaid ER 查看器
数据库·后端
hryou09226 小时前
向量库选型别踩坑:10个候选、6个暗坑,一次说清
数据库·架构
emoji1111116 小时前
python总结
数据库
zxcvb1538 小时前
运维实践里把备份mysql数据库做稳的一些思路
运维·数据库·mysql
一级新生10 小时前
Dify 迭代节点:从输入输出到九步实操
数据库·人工智能·mysql·低代码·工作流·dify
夜雪一千11 小时前
PostgreSQL 中哪些操作可以写成自定义函数
数据库·postgresql
唐青枫12 小时前
MySQL mysql.help_topic 详解及实战 Demo
mysql
科力锐品牌君12 小时前
信创进入核心系统深水区:X86 与国产架构长期并存,如何落地统一灾备?
运维·网络·数据库·数据安全·灾备
在角落发呆13 小时前
sqlserver 如何备份数据库:从手动备份到自动定时、异地容灾的完整方案
数据库·windows·备份·数据备份
xixiaoyunya13 小时前
MySQL 全库备份:用 mysqldump 打好基线,再补上自动化与异地能力
数据库·mysql·自动化