当前位置:网站首页>Yiwen teaches you to quickly generate MySQL database diagram
Yiwen teaches you to quickly generate MySQL database diagram
2022-06-28 15:04:00 【1024 Q】
Requirements describe :
Demand analysis :
Technical solution :
Solution :
summary
Requirements describe :In the old system of the company , There are many database tables , But at the beginning of the design, the relationship diagram was not established , As a result, the new recruits have just joined the company , face N Databases , Hundreds of tables per library , Very inconvenient .
for example : There are 300 tables in a system of the company , Not familiar with the project , Open the database and see a column of tables , Very unclear , Very unfriendly to new colleagues .

We have a system , There may be many modules , For example, there are commodity modules in the mall system 、 Coupon module 、 Store module, etc , Every module has dozens of tables , Each module needs to generate the following diagram :( Make complaints about it ,Navicat The reverse diagram is not PowerDesigner good-looking )

Using tools :Navicat
Navicat Is a domestic database client , Built in model function , Can be realized in the requirements , Select all tables of a module , Reverse table to model , To generate ER chart , But if you select , No foreign key associations , The generated model , Not as in the requirements , There is an intuitive line connection , So you need to generate foreign keys for the corresponding tables SQL.
from Navicat Select all tables to export as SQL I found the following pattern

1. Split each table statement
2. Main table and associated table , The main table does not need to produce foreign keys , A small apostrophe can be used to determine whether a foreign key is required for an association table
3. It is better to read the code once than to say more words
package com.example.demo;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.nio.file.Files;import java.nio.file.Paths;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.stream.Collectors;public class MysqlERGenerate { public static void main(String[] args) throws IOException { StringBuilder builder = new StringBuilder(); // Input from Navicat Exported table structure sql file Read the file out Put it in the string InputStream is = Files.newInputStream(Paths.get("C:\\Users\\admin\\Desktop\\ddl.sql")); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); line = reader.readLine(); while (line != null) { builder.append(line); builder.append("\n"); line = reader.readLine(); } reader.close(); is.close(); String sql = builder.toString(); // According to the law Use CREATE TABLE Segmentation And delete a file comment section String[] split = sql.split("CREATE TABLE"); List<String> list = new ArrayList<>(Arrays.asList(split)); list.remove(0); // Use the first two apostrophes to intercept Get the name of the watch To Map< Table name , SQL> Map<String, String> collect = list.stream().collect(Collectors.toMap(k -> { int firstIndex = k.indexOf("`"); return k.substring(++firstIndex, k.indexOf("`", firstIndex)); }, v -> v)); // You need to create foreign key fields and corresponding main table names Map< Foreign key name , Foreign key main table name > Map<String, String> foreignKey = new HashMap<>(); foreignKey.put("ticket_no", "ticket"); foreignKey.put("ticket_define_no", "ticket_define"); foreignKey.put("pro_no", "pro_main"); // Circular judgement , Generate foreign keys SQL Set<String> foreignKeyFields = foreignKey.keySet(); for (String mainTableName : collect.keySet()) { String val = collect.get(mainTableName); for (String field : foreignKeyFields) { if (!mainTableName.equals(foreignKey.get(field)) && val.indexOf("`" + field + "`") > 0) { String createForeignKeySql = String.format("alter table %s add foreign key %s(%s) references %s(%s);", mainTableName, mainTableName + field + System.currentTimeMillis(), field, foreignKey.get(field), field); System.out.println(createForeignKeySql); } } } }}Running effect : Only part of it is copied , It's actually far more than this
alter table pro_param add foreign key pro_parampro_no1650765563395(pro_no) references pro_main(pro_no);alter table pro_shop_priority_his_20200805 add foreign key pro_shop_priority_his_20200805pro_no1650765563423(pro_no) references pro_main(pro_no);alter table ticket_define_shop add foreign key ticket_define_shopticket_define_no1650765563423(ticket_define_no) references ticket_define(ticket_define_no);alter table ticket_define_item add foreign key ticket_define_itemticket_define_no1650765563425(ticket_define_no) references ticket_define(ticket_define_no);alter table ticket_his_2019 add foreign key ticket_his_2019ticket_no1650765563432(ticket_no) references ticket(ticket_no);alter table ticket_his_2018 add foreign key ticket_his_2018ticket_no1650765563433(ticket_no) references ticket(ticket_no);
Foreign key statements should not be developed, tested, or executed in the production environment , You should build a new database locally
When creating a new library , Just turn the structure , Otherwise, there are too many data , The execution of foreign key statements is very slow
After the end , Want diagrams between those tables , You only need to select and reverse the table to the model to get the effect in the requirements
If there's plenty of money , You can buy me a cup of coffee as a reward , thank you Thanks*(・ω・)ノ
summaryThis is about rapid generation MySQL This is the end of the database diagram article , More related builds MySQL Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !
边栏推荐
- 10 key points to effectively improve performance interview
- Combined sum leetcode
- Angers medical sprint scientific innovation board: annual revenue of RMB 300million and proposed fund raising of RMB 770million
- How to solve the following problems in the Seata database?
- Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
- 第四大运营商,难成「鲶鱼」
- Unable to create process using 'd:\program file
- 技术弄潮儿
- The hidden crisis of Weilai: past, present and future
- 【算法篇】刷了两道大厂面试题,含泪 ”重学数组“
猜你喜欢
![[C language] nextday problem](/img/7b/422792e07dd321e3a37c1fff55c0ca.png)
[C language] nextday problem

Recommended practice sharing of Zhilian recruitment based on Nebula graph

High "green premium" of environmental protection products? How far is the low-carbon lifestyle from people

猫狗图像数据集上的深度学习模型性能对比

优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿

Angers medical sprint scientific innovation board: annual revenue of RMB 300million and proposed fund raising of RMB 770million

PMP认证证书的续证费用是多少?

After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time

3. Caller 服务调用 - dapr

WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
随机推荐
Jackie Chan and fast brand, who is the Savior of Kwai?
Send2vec tutorial
seata-server 1.5.0 如何支持mysql8.0?
R语言ggplot2可视化:使用patchwork包将3个ggplot2可视化结果自定义组合起来构成组合图、两个子图横向组合后和另外一个图纵向组合构成最终组合图
Calculator (force buckle)
Leetcode (665) -- non decreasing column
Leetcode (167) -- sum of two numbers II - input ordered array
WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
open3d里pointcloud和numpy数组之间的转化
Vscode writes markdown file and generates pdf
股票开户优惠链接,我如何才能得到?手机开户是安全么?
Express模板引擎
Do not use short circuit logic to write STL sorter multi condition comparison
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
324. swinging sort II: not a simple construction problem
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、将两个可视的组合结果对齐
[Zhongyi Xinsheng] 5 SPI interface test TF Card
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合起来形成单个可视化结果图
halcon 基础总结(一)裁切图片并旋转图像
使用LamdbaUpdateWrapper的setSql作用及风险