当前位置:网站首页>Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts

Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts

2022-06-25 09:10:00 ZWZhangYu

explain

It is used in development projects today navicat When the synchronization tool accidentally sees data synchronization and structure synchronization, it is a little curious , So I simply used it , After use, I feel it is very suitable for the current needs ;

【1】 Comparison of database tables in different environments , And automatically generate differences DDL sentence
【2】 The difference of data records in different environments , And automatic generation DML sentence
 Insert picture description here
【 The data transfer 】 I believe that developers are familiar with and often use , And for 【 Data synchronization 】【 Structure synchronization 】 There was no contact before , The data synchronization mainly compares the data differences of the corresponding tables between the two data , Structure synchronization mainly compares the difference of data tables between two databases and the generation of SQL Script .

Structure synchronization

Prepare two databases , Then create a data table with the same table name in the two databases , The upper table is one more than the lower one address Field .

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;

choice 【 Tools 】【 Structure contrast 】, choice t_user The results are as follows , You can see the difference intuitively through the following display

 Insert picture description here
Click on 【 Deployment scripts 】 You can see the differences that the tool helps us generate SQL, Very convenient

 Insert picture description here

Data synchronization

Based on the table structure above , Execute the above generated DDL Statement to keep the data tables on both sides consistent , Then add the following test data

【 Source library 】
INSERT INTO `t_user`(`id`, `name`, `age`, `address`, `create_time`) VALUES (1, ' Zhang San ', 25, ' Hefei, Anhui ', '2022-06-22 15:29:16');
INSERT INTO `t_user`(`id`, `name`, `age`, `address`, `create_time`) VALUES (2, ' Li Si ', 25, ' Hefei ', '2022-06-22 15:29:54');
INSERT INTO `t_user`(`id`, `name`, `age`, `address`, `create_time`) VALUES (3, ' Wang Wu ', 25, ' Hefei ', '2022-06-22 15:29:54');
 
 
【 Target library 】
INSERT INTO `t_user`(`id`, `name`, `age`, `address`, `create_time`) VALUES (1, ' Zhang San ', 25, ' Hefei, Anhui ', '2022-06-22 15:29:16');
INSERT INTO `t_user`(`id`, `name`, `age`, `address`, `create_time`) VALUES (3, ' Wang Wu ', 26, ' Shanghai ', '2022-06-22 15:29:54');

Click on 【 Tools 】【 Data synchronization 】, Select source library and target library , The following figure is the result of this comparison .
 Insert picture description here
【A】 The area shows the records of tables with different data after comparison , It shows different number of records and the same number, etc .

【B】 Here, you can choose to display only different data , Same data , All the data, etc , The results are shown in C Area , Will be marked with different colors .

【C】 The actual comparison results are shown , From the above figure, you can see the relationship between the two libraries ID by 1 The data records are the same , One more table on the left ID by 2 The record of , The two tables ID by 3 The recorded data of are different

【D】 Deploy , Clicking deploy will generate a different SQL, As shown in the example above , From source library to target library , There will be a new ID by 2 The record of , And update the ID by 3 The record of
 Insert picture description here
Generated DML as follows :

SET FOREIGN_KEY_CHECKS = 0;
 
INSERT INTO `test`.`t_user`(`id`, `name`, `age`, `address`, `create_time`) VALUES (2, ' Li Si ', 25, ' Hefei ', '2022-06-22 15:29:54');
 
UPDATE `test`.`t_user` SET `name` = ' Wang Wu ', `age` = 25, `address` = ' Hefei ', `create_time` = '2022-06-22 15:29:54' WHERE `id` = 3;
 
SET FOREIGN_KEY_CHECKS = 1;

among :SET FOREIGN_KEY_CHECKS effect : In execution sql Script time , To prevent foreign keys from being affected .

Summary

【1】 This time, we simply tested the functions of data synchronization and structure synchronization , These two functions are also inadvertently touched during work , After understanding, I really feel that it will be helpful for future project development .
【2】 For which the data structure is synchronized , During version testing or large version upgrade , If the upgrade script is not retained , This tool can be used to quickly generate relevant DDL sentence . If you have prepared an upgrade script , You can also use this tool to compare the source library with the target library , This ensures that there will be no missing items when upgrading , Make a guarantee .

【3】 The data synchronization function can be applied to some basic tables 、 Configuration table 、 Dictionaries, tables, etc , If someone changes some data during development but fails to submit it in time , It will lead to different environments . Besides , Changes to the data structure can generally be submitted in a timely manner , But for temporary modification of some configuration data , Basic data may be omitted , Then you can compare the differences through this tool , And automatically generate differences SQL.

【4】 After touching this function , I also found some similar products , such as Liquibase、dbForge Data Compare、 And some open source platforms . The overall function is similar ,Navicat The functions provided here are very convenient and powerful , Most importantly, there are no other costs , As the database connection tool currently used by the project team , You can directly connect to various environment databases without additional configuration , And it is stable and easy to use .

原网站

版权声明
本文为[ZWZhangYu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250825247474.html