DataGrip-2018.3.4-数据导出配置案例

官方文档:https://www.jetbrains.com/help/datagrip ,该文档默认是最新版本,可手动选择旧版本。

1 数据导出

DataGrip 导出整个数据库或单个表均支持两种方式,Dump Data to File(s) 以及 Dump with ‘mysqldump’。以下例子均采用 MySQL

1.1 Dump Data to File(s)

导出整个数据库的菜单选项如下所示。导出单个表的菜单选项减少了 Overwrite Existing FilesSingle File,其余相同。

注:如果数据库只有一个表,菜单与导出单个表的相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
----------------------------------|【导出格式】
SQL Inserts
SQL Updates
HTML Table
----------------------------------|【导出格式】
Tab-separated (TSV)
Comma-separated (CSV)
----------------------------------|【导出格式】
HTML-Groovy.html.groovy
HTML-JavaScript.html.js
SQL-Insert-Statements.sql.groovy
XML-Groovy.xml,groovy
CSV-Groovy.csv.groovy
JSON-Groovy.json.groovy
----------------------------------|【行为】
Skip Computed Columns (SQL)【不添加计算列】
Skip Generated Columns (SQL)【不添加自动生成的列,如自动增长列】
Add Table Definition (SQL)【添加表定义】
Overwrite Existing Files【导出整个数据库时可用。若出现重名,不勾选时生成文件名后有序号,勾选则覆盖】
Single File【导出整个数据库时可用。将各个表的 sql 合并到一个文件,否则每个表分别生成一个文件。勾选 Single File 后,Overwrite Existing Files 失效,无论是否勾选。此时若出现重名,将弹出对话框提示是否覆盖或改名】
----------------------------------|
Configure CSV Formats...
Go to Scripts Directory
----------------------------------|

创建数据库 onlytest,包含两个结构类似的表 employee、manager,其中 employee 包含计算列。创建 sql 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create table onlytest.employee
(
ID bigint auto_increment
primary key,
name tinytext not null,
age int not null,
tenyears int as ((`age` + 10))
);

create table onlytest.manager
(
ID bigint auto_increment
primary key,
name tinytext not null,
age int not null
);

1.1.1 Add Table Definition (SQL)

若勾选 Add Table Definition (SQL)Single File,导出格式选择 SQL Inserts,则在添加数据前创建表时的定义(SQL Updates 结果以此类推):

注意:该选项仅对 SQL InsertsSQL Updates 有效。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
create table employee
(
ID bigint auto_increment
primary key,
name tinytext not null,
age int not null,
tenyears int as ((`age` + 10))
);

INSERT INTO onlytest.employee (ID, name, age, tenyears) VALUES (1, 'one', 22, 32);
INSERT INTO onlytest.employee (ID, name, age, tenyears) VALUES (2, 'two', 17, 27);
INSERT INTO onlytest.employee (ID, name, age, tenyears) VALUES (3, 'three', 20, 30);
create table manager
(
ID bigint auto_increment
primary key,
name tinytext not null,
age int not null
);

INSERT INTO onlytest.manager (ID, name, age) VALUES (1, 'one', 35);
INSERT INTO onlytest.manager (ID, name, age) VALUES (2, 'two', 36);

不勾选则仅生成 insert(SQL Updates 结果以此类推):

1
2
3
4
5
INSERT INTO onlytest.employee (ID, name, age, tenyears) VALUES (1, 'one', 22, 32);
INSERT INTO onlytest.employee (ID, name, age, tenyears) VALUES (2, 'two', 17, 27);
INSERT INTO onlytest.employee (ID, name, age, tenyears) VALUES (3, 'three', 20, 30);
INSERT INTO onlytest.manager (ID, name, age) VALUES (1, 'one', 35);
INSERT INTO onlytest.manager (ID, name, age) VALUES (2, 'two', 36);

1.1.2 Skip Generated Columns (SQL)

若勾选 Skip Generated Columns (SQL)Single File,不勾选 Add Table Definition (SQL),导出格式选择 SQL Inserts,则没有自动增长的 ID 列,(SQL Updates 结果以此类推):

注意:该选项仅对 SQL InsertsSQL Updates 有效。

1
2
3
4
5
INSERT INTO onlytest.employee (name, age, tenyears) VALUES ('one', 22, 32);
INSERT INTO onlytest.employee (name, age, tenyears) VALUES ('two', 17, 27);
INSERT INTO onlytest.employee (name, age, tenyears) VALUES ('three', 20, 30);
INSERT INTO onlytest.manager (name, age) VALUES ('one', 35);
INSERT INTO onlytest.manager (name, age) VALUES ('two', 36);

1.1.3 Skip Computed Columns (SQL)

若勾选 Skip Computed Columns (SQL)Single File,不勾选 Add Table Definition (SQL),导出格式选择 SQL Inserts,则没有 tenyears 列,(SQL Updates 结果以此类推):

注意:该选项仅对 SQL InsertsSQL Updates 有效。

1
2
3
4
5
INSERT INTO onlytest.employee (ID, name, age) VALUES (1, 'one', 22);
INSERT INTO onlytest.employee (ID, name, age) VALUES (2, 'two', 17);
INSERT INTO onlytest.employee (ID, name, age) VALUES (3, 'three', 20);
INSERT INTO onlytest.manager (ID, name, age) VALUES (1, 'one', 35);
INSERT INTO onlytest.manager (ID, name, age) VALUES (2, 'two', 36);

1.2 Dump with ‘mysqldump’

经过实测发现,当数据量较大时,使用 Dump Data to File(s) 方式导出的 sql 文件与 Dump with ‘mysqldump’ 方式相比会大很多,可能是由于前者重复字符量大以及换行多导致。因此推荐优先使用后者导出。

导出整个数据库和单个表的菜单选项相同,故合并讨论。Dump with ‘mysqldump’ 有如下菜单选项:

  • Multiple rows inserts【将 insert 语句合并为一行】
  • Add drop table【增加判断条件,若表存在则删除】
  • Disable keys【禁用索引】
  • Delay inserts【提示错误 mysqldump: [ERROR] unknown option '--delayed-insert'.,无输出结果】
  • MySQL create table options【若不勾选,暂时未发现有明显差异】
  • Lock tables【锁表,可能是防止有新数据在导出时进入】
  • Add locks【导入时,在执行 insert 前添加 write 锁,完成后解除】
  • Add drop trigger【导入时,删除对方的触发器】

案例 1

勾选 Multiple rows inserts、Add drop table、MySQL create table options、Add drop trigger,生成结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- MySQL dump 10.13  Distrib 8.0.18, for osx10.14 (x86_64)
--
-- Host: 127.0.0.1 Database: onlytest
-- ------------------------------------------------------
-- Server version 8.0.18

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `employee`
--

DROP TABLE IF EXISTS `employee`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `employee` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`name` tinytext COLLATE utf8mb4_general_ci NOT NULL,
`age` int(11) NOT NULL,
`tenyears` int(11) GENERATED ALWAYS AS ((`age` + 10)) VIRTUAL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `employee`
--

INSERT INTO `employee` (`ID`, `name`, `age`) VALUES (1,'one',22),(2,'two',17),(3,'three',20);

--
-- Table structure for table `manager`
--

DROP TABLE IF EXISTS `manager`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `manager` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`name` tinytext COLLATE utf8mb4_general_ci NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `manager`
--

INSERT INTO `manager` VALUES (1,'one',35),(2,'two',36);
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-02-19 20:43:57

案例 2

若增加勾选 Disable keys,禁用索引,在大批量导入时先禁用索引,在完全导入后,再开启索引。

1
2
3
4
5
6
7
8
9
...
/*!40000 ALTER TABLE `employee` DISABLE KEYS */;
INSERT INTO `employee` (`ID`, `name`, `age`) VALUES (1,'one',22),(2,'two',17),(3,'three',20);
/*!40000 ALTER TABLE `employee` ENABLE KEYS */;
...
/*!40000 ALTER TABLE `manager` DISABLE KEYS */;
INSERT INTO `manager` VALUES (1,'one',35),(2,'two',36);
/*!40000 ALTER TABLE `manager` ENABLE KEYS */;
...

案例 3

勾选 Add locks,添加了 write 锁

1
2
3
4
5
6
7
8
9
...
LOCK TABLES `employee` WRITE;
INSERT INTO `employee` (`ID`, `name`, `age`) VALUES (1,'one',22),(2,'two',17),(3,'three',20);
UNLOCK TABLES;
...
LOCK TABLES `manager` WRITE;
INSERT INTO `manager` VALUES (1,'one',35),(2,'two',36);
UNLOCK TABLES;
...

其他 mysqldump 参数(待定)

参数 描述
xxx xxx

2 查看创建脚本

右键 SQL Script 选择 SQL Generator

3 执行 SQL

右键 New 选择 Console,