当前位置: 首页 > news >正文

将mysql数据库表结构导出成DBML格式

前言

DBML(数据库标记语言)是一种简单易读的 DSL 语言,用于定义数据库结构。

因为需要分析商品模块的表设计是否合理,所以需要图形化表,并显示表之前的关系。
想来想去,找到了DBML。所以就需要将数据库结构,导出成DBML格式。

方法

用的laravel框架。

有两种方法

  • 使用 desc tableName
输入如下:
array:13 [0 => array:6 ["Field" => "id""Type" => "bigint(20)""Null" => "NO""Key" => "PRI""Default" => null"Extra" => "auto_increment"]1 => array:6 ["Field" => "parent_id""Type" => "bigint(20)""Null" => "YES""Key" => "MUL""Default" => "0""Extra" => ""]
  • 使用 show create table tableName
输出如下:
CREATE TABLE `category` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分类ID',`parent_id` bigint(20) DEFAULT '0' COMMENT '父分类ID(0表示顶级分类)',`category_name` varchar(255) NOT NULL COMMENT '分类名称',`category_alias` varchar(255) NOT NULL DEFAULT '' COMMENT '分类别名',`category_code` varchar(50) DEFAULT NULL COMMENT '分类编码',`category_sort` int(11) NOT NULL DEFAULT '1' COMMENT '分类排序值',`level` int(11) NOT NULL DEFAULT '1' COMMENT '分类层级(1=一级分类)',`status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态(1=启用,0=停用)',`user_created` varchar(255) NOT NULL DEFAULT '' COMMENT '创建人',`user_cid` int(11) NOT NULL DEFAULT '0' COMMENT '创建人id',`user_updated` varchar(255) NOT NULL DEFAULT '' COMMENT '更新人',`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE,KEY `idx_parent_id` (`parent_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=777 DEFAULT CHARSET=utf8mb4 COMMENT='分类表'

因此就需要结合这两种命令得到DBML格式所需要的数据。,脚本如下。

<?phpnamespace App\Http\Controllers;use Illuminate\Support\Facades\DB;class TestController extends Controller
{public function dbtableToDbml(){$tables = ['category','category_attribute','attribute','attribute_option','product','product_attribute','product_attribute_option','process','process_resource','combo','combo_item','combo_item_attribute','customer_subtotal','customer_product','customer_combo','customer_combo_item','customer_combo_attribute','customer_combo_material','customer_combo_material_attribute','customer_product_process','customer_product_process_resource',];$res = $this->dbtableStruToDbml($tables);return $res;}//数据库表结构转为DBML格式public function dbtableStruToDbml($tables){$tableArr = is_string($tables) ? explode(',', $tables) : $tables;$res = '';//获取字段名及备注$extractFieldAndComment = function ($str) {$result = ['field' => null,'comment' => null];// 1. 提取字段名(匹配反引号包裹的内容)if (preg_match('/`([^`]+)`/', $str, $fieldMatches)) {$result['field'] = trim($fieldMatches[1]);}// 2. 提取COMMENT后的内容(如果存在)if (preg_match('/COMMENT\s+\'([^\']*)\'/i', $str, $commentMatches)) {$result['comment'] = trim($commentMatches[1]);}return $result;};$getTableComment = function ($str) {// 正则表达式解析:// COMMENT\s*=\s* 匹配"COMMENT"及可能的空格、等号、空格(不区分大小写)// '([^']*)' 匹配单引号内的内容(捕获组1),[^']*表示非单引号的任意字符(包括空)$pattern = '/COMMENT\s*=\s*\'([^\']*)\'/i';// 执行匹配if (preg_match($pattern, $str, $matches)) {// 匹配成功,返回捕获到的内容(trim处理避免意外空格,保留空内容)return trim($matches[1]);}// 无COMMENT时返回空字符串return '';};foreach ($tableArr as $table) {$tableComment = '';$tableDesc = DB::select("desc {$table}");$filedArr = [];foreach ($tableDesc as $item) {$filedArr[$item->Field] = ['field' => $item->Field,'type' => $item->Type,'comment' => ''];}$createTable = DB::select("show create table {$table}");$createTable = json_decode(json_encode($createTable), true);$createTable = $createTable[0]['Create Table'];$fieldInfos = explode("\n", $createTable);foreach ($fieldInfos as $fieldInfo) {$fieldInfo = trim($fieldInfo, ' '); //去掉前面的空格$pattern = '/^`/'; //是否以 ` 号开头,是的话,才是字段,否则就是其他的,其他的不考虑if (preg_match($pattern, $fieldInfo)) {//是字段才处理$fieldAndComment = $extractFieldAndComment($fieldInfo);$filedArr[$fieldAndComment['field']]['comment'] = $fieldAndComment['comment'];} elseif (preg_match('/^\)/', $fieldInfo)) {$tableComment = $getTableComment($fieldInfo);}}$str = '';$str = "Table {$table} {\n";foreach ($filedArr as $fieldItem) {$str .= "   {$fieldItem['field']} {$fieldItem['type']} [note: '{$fieldItem['comment']}'] \n";}$str .= "   Note: '{$tableComment}'\n";$str .= "}\n";strlen($res) ? $res =  $res . "\n\n\n" : '';$res .= $str;}return $res;}}

导出的内容如下:

Table category {id bigint(20) [note: '分类ID'] parent_id bigint(20) [note: '父分类ID(0表示顶级分类)'] category_name varchar(255) [note: '分类名称'] category_alias varchar(255) [note: '分类别名'] category_code varchar(50) [note: '分类编码'] category_sort int(11) [note: '分类排序值'] level int(11) [note: '分类层级(1=一级分类)'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_cid int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '分类表'
}Table category_attribute {id bigint(20) [note: '记录ID'] category_id bigint(20) [note: '分类ID'] attribute_id bigint(20) [note: '属性ID'] limit_ids text [note: '限定属性值id'] Note: '分类属性关联表'
}Table attribute {id bigint(20) [note: '属性ID'] attribute_name varchar(255) [note: '属性名称(如颜色,尺寸)'] attribute_code varchar(50) [note: '属性编码(程序识别唯一值)'] sort mediumint(9) [note: '排序'] remark varchar(255) [note: '属性说明'] input_type varchar(50) [note: '输入类型(single=单选,double=多选)'] unit varchar(50) [note: '属性单位'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '属性定义表'
}Table attribute_option {id bigint(20) [note: '选项ID'] attribute_id bigint(20) [note: '所属属性ID'] option_value varchar(255) [note: '属性选项值'] option_code varchar(50) [note: '属性选项编码'] status tinyint(4) [note: '状态(1=启用,0=停用)'] sort_order int(11) [note: '排序值(用于前端展示顺序)'] remark varchar(255) [note: '备注'] Note: '属性选项表(全局)'
}Table product {id bigint(20) [note: '商品ID(主键,自增)'] category_id bigint(20) [note: '分类ID(外键)'] category_top_ids varchar(255) [note: '上级分类id'] category_top_name varchar(255) [note: '上级分类名称'] product_code varchar(50) [note: '商品编码(内部唯一)'] product_name varchar(255) [note: ''] product_image varchar(255) [note: '商品图片'] product_type int(11) [note: '物料类型:100-原料 200-辅料 201-纸箱 202-套袋'] short_name varchar(255) [note: '商品简称'] scientific_name varchar(255) [note: '商品学名'] label_alias varchar(255) [note: '标签别名'] description text [note: '商品描述'] unit_name varchar(50) [note: '基础单位名称'] inspection_standard text [note: '质检标准'] shelf_life int(11) [note: '保质期,单位:天'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] safe_stock int(11) [note: '安全库存值'] Note: '商品主数据表'
}Table product_attribute {id bigint(20) [note: '记录ID'] product_id bigint(20) [note: '商品ID'] attribute_id bigint(20) [note: '属性ID'] is_must tinyint(4) [note: '是否必填:0-否 1-是'] Note: '商品属性值表'
}Table product_attribute_option {id bigint(20) [note: '记录ID'] product_id bigint(20) [note: '商品ID'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '商品属性可选值关联表'
}Table process {id bigint(20) [note: '工艺ID'] process_code varchar(50) [note: '工艺编码'] process_name varchar(255) [note: '工艺名称'] process_type varchar(255) [note: '工艺类型'] process_duration int(11) [note: '工艺工期'] duration varchar(255) [note: '时间单位'] description text [note: '工艺描述'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '组合商品工艺表'
}Table process_resource {process_id bigint(20) [note: '工艺ID'] product_id bigint(20) [note: '物料ID'] Note: '工艺物料关联表'
}Table combo {id bigint(20) [note: '组合ID'] category_id bigint(20) [note: '分类ID(外键)'] category_top_ids varchar(255) [note: '上级分类id'] category_top_name varchar(255) [note: '上级分类名称'] combo_name varchar(255) [note: '组合名称'] combo_code varchar(255) [note: '组合编码'] combo_image text [note: '组合图片'] combo_price decimal(10,2) [note: '组合价格'] combo_label varchar(255) [note: '组合成品标签'] subtotal_name varchar(255) [note: '小计名称'] unit_name varchar(50) [note: '基础单位名称'] unit_spec decimal(10,2) [note: '基础单位规格'] unit_pack_name varchar(50) [note: '包装单位名称'] unit_pack_spec decimal(10,2) [note: '包装单位规格'] description text [note: '组合描述'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '组合产成品主表'
}Table combo_item {id bigint(20) [note: '组合项ID'] combo_id bigint(20) [note: '组合ID'] product_id bigint(20) [note: '所属组合的商品ID'] category_id bigint(20) [note: '所属组合的分类ID'] category_name varchar(255) [note: '分类名称'] item_name varchar(255) [note: '组合名称'] item_type varchar(50) [note: '组合项类型(LABEL/PRODUCT)'] quantity int(11) [note: '数量'] label_name varchar(50) [note: '标签名称'] remark text [note: '备注'] Note: '组合项表'
}Table combo_item_attribute {id bigint(20) [note: '记录ID'] item_id bigint(20) [note: '组合项ID'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '组合项属性值表'
}Table customer_subtotal {id bigint(20) [note: '小计ID'] name varchar(128) [note: '小计名称'] remark varchar(255) [note: '备注'] plan_name varchar(128) [note: '小计名称-采购'] production_name varchar(128) [note: '小计名称-生产'] sort_num int(11) [note: '排序值'] Note: '客户商品小计关联表'
}Table customer_product {id bigint(20) [note: '记录ID'] client_id bigint(20) [note: '客户ID'] customer_product_name varchar(255) [note: '客户成品名称'] customer_product_code varchar(255) [note: '客户成品编码'] customer_product_alias varchar(255) [note: '客户成品代号'] customer_product_process text [note: '加工工艺描述'] customer_product_label text [note: '关联组合成品名称(combo名称)'] customer_product_tag text [note: '关联组合成品代码(combo编码)'] customer_product_image text [note: '客户成品图片'] subtotal_id bigint(20) [note: '小计关联'] subtotal_name varchar(255) [note: '小计分类名称'] unit_name varchar(50) [note: '单位名称'] unit_spec int(11) [note: '单位规格'] custom_price decimal(10,2) [note: '客户成品定价'] piece_count decimal(10,2) [note: '计件价格'] gross_weight int(11) [note: '毛重'] pure_weight int(11) [note: '净重'] currency varchar(10) [note: '币种'] custom_spec1 varchar(50) [note: '规格1'] custom_spec2 varchar(50) [note: '规格2'] custom_length varchar(50) [note: '客户商品长度'] status tinyint(4) [note: '状态(1=启用,0=停用)'] user_created varchar(255) [note: '创建人'] user_id int(11) [note: '创建人id'] user_updated varchar(255) [note: '更新人'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] remark varchar(255) [note: '备注'] Note: '客户商品表'
}Table customer_combo {id bigint(20) [note: '记录ID'] customer_product_id bigint(20) [note: '客户商品ID'] customer_combo_name varchar(255) [note: '组合名称'] customer_combo_code varchar(255) [note: '组合代码'] quantity int(11) [note: '组合数量'] Note: '客户组合商品表'
}Table customer_combo_item {id bigint(20) [note: '组合项ID'] customer_combo_id bigint(20) [note: '组合ID'] product_id bigint(20) [note: '所属组合的商品ID'] category_id bigint(20) [note: '所属组合的分类ID'] category_name varchar(255) [note: '分类名称'] item_name varchar(255) [note: '组合名称'] item_type varchar(50) [note: '组合项类型(LABEL/PRODUCT)'] quantity int(11) [note: '数量'] label_name varchar(50) [note: '标签名称'] replace_product_id varchar(255) [note: '可替换商品ID'] replace_label varchar(255) [note: '可替换标签'] remark text [note: '备注'] Note: '客户组合商品明细表'
}Table customer_combo_attribute {id bigint(20) [note: '记录ID'] item_id bigint(20) [note: '组合项ID'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '客户组合商品属性表'
}Table customer_combo_material {id bigint(20) [note: '辅料ID'] customer_product_id bigint(20) [note: '客户商品ID'] customer_combo_id bigint(20) [note: '客户商品组合ID'] product_id bigint(20) [note: '产品ID'] process_type varchar(255) [note: '标记所属工艺'] product_type varchar(255) [note: '标记物料类型'] quantity int(11) [note: '数量'] remark text [note: '备注'] is_new tinyint(4) [note: '是否新数据:1-新'] Note: '客户组合辅料表'
}Table customer_combo_material_attribute {id bigint(20) [note: '记录ID'] customer_material_id bigint(20) [note: '辅料ID,关联customer_combo_material表id'] attribute_id bigint(20) [note: '属性ID'] option_id bigint(20) [note: '属性值ID'] Note: '客户组合辅料属性值表'
}Table customer_product_process {id bigint(20) [note: '记录ID'] customer_product_id bigint(20) [note: '客户商品ID'] process_id bigint(20) [note: '工艺id'] description text [note: '描述'] source varchar(255) [note: '来源'] created_at datetime [note: '创建时间'] updated_at datetime [note: '更新时间'] Note: '客户组合商品工艺表'
}Table customer_product_process_resource {id bigint(20) [note: 'ID'] customer_process_id bigint(20) [note: '客户商品工艺ID,关联customer_product_process表id'] product_id bigint(20) [note: '工艺产品id'] Note: '客户组合商品工艺物料关联表'
}
总结

表与表之间的关系,需要在导出的DBML中用ref定义。

http://www.lryc.cn/news/620364.html

相关文章:

  • 古中医学习笔记专题文章导航
  • GUI Grounding: ScreenSpot
  • 在云蝠智能VoiceAgent中融入通话背景音:解析如何打造拟人化语音交互体验
  • NY219NY220美光固态闪存NY224NY229
  • 双机热备实验
  • 数据库访问模式详解
  • week1-[分支嵌套]公因数
  • 身份全景图
  • 【20-模型诊断调优】
  • 云原生俱乐部-k8s知识点归纳(2)
  • 云原生俱乐部-杂谈1
  • B站 韩顺平 笔记 (Day 18)
  • 从合规到卓越:全星QMS如何成为制造企业的质量战略引擎
  • 十一,算法-快速排序
  • Python/Node.js 调用taobao API:构建实时商品详情数据采集服务
  • Neural Bandit Based Optimal LLM Selection for a Pipeline of Tasks
  • 监控插件SkyWalking(二)集成方法
  • Node.js/Python 实战:封装淘宝商品详情 API 客户端库(SDK)
  • vLLM(Vectorized Large Language Model Serving) 的深度解析
  • npm介绍,指令合集,换源指令
  • 问题总结三
  • VSC遇到的问题:无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。
  • P12348 [蓝桥杯 2025 省 A 第二场] 交互
  • Java零基础笔记16(Java编程核心:存储读写数据方案—File文件操作、IO流、IO框架)
  • 17. 如何判断一个对象是不是数组
  • 【LeetCode】4. 寻找两个正序数组的中位数
  • hadoop 前端yarn 8088端口查看任务执行情况
  • 【深入浅出STM32(1)】 GPIO 深度解析:引脚特性、工作模式、速度选型及上下拉电阻详解
  • 数据结构:队列(Queue)与循环队列(Circular Queue)
  • linux_网络层-ip协议