基于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. 所有表应至少包含idgmt_creategmt_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 普通索引
相关推荐
KellenKellenHao19 分钟前
MySQL数据库主从复制
数据库·mysql
@ chen43 分钟前
Redis事务机制
数据库·redis
KaiwuDB1 小时前
使用Docker实现KWDB数据库的快速部署与配置
数据库·docker
一只fish1 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(16)
数据库·mysql
泊浮目2 小时前
未来数据库硬件-网络篇
数据库·架构·云计算
静若繁花_jingjing2 小时前
Redis线程模型
java·数据库·redis
叁沐3 小时前
MySQL 07 行锁功过:怎么减少行锁对性能的影响?
mysql
Java烘焙师3 小时前
架构师必备:业务扩展模式选型
mysql·elasticsearch·架构·hbase·多维度查询
飞翔的佩奇4 小时前
Java项目:基于SSM框架实现的忘忧小区物业管理系统【ssm+B/S架构+源码+数据库+毕业论文+开题报告】
java·数据库·mysql·vue·毕业设计·ssm框架·小区物业管理系统
@Ryan Ding4 小时前
MySQL主从复制与读写分离概述
android·mysql·adb