当前位置:网站首页>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 !
边栏推荐
- Force deduction solution summary 522- longest special sequence II
- Functools: high order functions and operations on callable objects (continuous updating ing...)
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、将两个可视的组合结果对齐
- Leetcode (665) -- non decreasing column
- functools:对callable对象的高位函数和操作(持续更新ing...)
- 浪潮网络步步为赢
- Leetcode(167)——两数之和 II - 输入有序数组
- Leetcode(88)——合并两个有序数组
- Le patron a donné trois ordres: discret, discret, discret
- 验证回文串
猜你喜欢

Leetcode 48. Rotate image (yes, resolved)

蔚来潜藏的危机:过去、现在到未来

当下不做元宇宙,就像20年前没买房!
ORACLE中dbms_output.put_line输出问题的解决过程

Four visualization tools are recommended to solve 99% of large screen visualization projects!

动力电池,是这样被“瓜分”的

halcon 基础总结(一)裁切图片并旋转图像

Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
![[C language] nextday problem](/img/7b/422792e07dd321e3a37c1fff55c0ca.png)
[C language] nextday problem

Facebook出手!自适应梯度打败人工调参
随机推荐
力扣今日题-522. 最长特殊序列
Functools: high order functions and operations on callable objects (continuous updating ing...)
使用Karmada实现Helm应用的跨集群部署
兼顾企业抗疫和发展的5个解决方案,来自IBM
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
[JS] Fibonacci sequence implementation (recursion and loop)
从五大能力到 “1+5+N”,华为让政企转型更稳健
坐拥1200亿,她又要IPO敲钟了
Leetcode (167) -- sum of two numbers II - input ordered array
Power battery is divided up like this
spacy教程(持续更新ing...)
seata-server 1.5.0 如何支持mysql8.0?
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合起来形成单个可视化结果图
Vscode writes markdown file and generates pdf
Conversion between pointcloud and numpy arrays in open3d
Which is safer, a securities company or a bank? How to open an account is the safest
请问一下,是不是insert all这种oracle的批量新增没拦截?
High "green premium" of environmental protection products? How far is the low-carbon lifestyle from people
使用LamdbaUpdateWrapper的setSql作用及风险
Ding! Techo day Tencent technology open day arrived as scheduled!