如何在 MySQL Workbench 中获取表创建脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2433133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a table creation script in MySQL Workbench?
提问by Jader Dias
I am rolling back to MySQL GUI Tools' MySQL Query Browser since I can't find the shortcut to get a table's creation script in MySQL Workbench.
我正在回滚到 MySQL GUI 工具的 MySQL 查询浏览器,因为我找不到在 MySQL Workbench 中获取表创建脚本的快捷方式。
回答by Pascal MARTIN
I cannot find such an option either, at least in the Community edition.
我也找不到这样的选项,至少在社区版中是这样。
I suppose this corresponds to the Reverse Engineeringfeature, which, unfortunately, is only available in the commercial edition (quoting):
我想这对应于逆向工程功能,不幸的是,它仅在商业版中可用(引用):
reverse engineering a database directly from a MySQL server applies to commercial versions of MySQL Workbench only.
直接从 MySQL 服务器对数据库进行逆向工程仅适用于 MySQL Workbench 的商业版本。
Still, you can use plain-SQL to get the create table
instruction that will allow you to create a table.
不过,您可以使用普通 SQL 来获取create table
允许您创建表的指令。
For instance, the following query :
例如,以下查询:
show create table url_alias;
when executed on a drupal database, would give, when using right click > copy field content
on the result :
在 drupal 数据库上执行时,会给出,在click > copy field content
结果上使用 right时:
'CREATE TABLE `url_alias` (
`pid` int(10) unsigned NOT NULL auto_increment,
`src` varchar(128) NOT NULL default '''',
`dst` varchar(128) NOT NULL default '''',
`language` varchar(12) NOT NULL default '''',
PRIMARY KEY (`pid`),
UNIQUE KEY `dst_language` (`dst`,`language`),
KEY `src_language` (`src`,`language`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8'
Unfortunately (again), MySQL Workbench adds some quotes everywhere when copying this way :-(
不幸的是(再次),MySQL Workbench 在以这种方式复制时在各处添加了一些引号:-(
EDIT: Using MySQL 8.0, there is an option to right click > copy field (unquoted)
on the result to get the desired result without quotes.
编辑:使用 MySQL 8.0,有一个选项可以click > copy field (unquoted)
在结果上正确获取所需的结果,而无需引号。
In the end, the simplestsolution, except from staying with MySQL Query Browser, will most likely be to connect to the database, using the command-line client, and execute the show create table
query from there :
最后,除了使用 MySQL 查询浏览器之外,最简单的解决方案很可能是连接到数据库,使用命令行客户端,并show create table
从那里执行查询:
mysql> show create table url_alias\G
*************************** 1. row ***************************
Table: url_alias
Create Table: CREATE TABLE `url_alias` (
`pid` int(10) unsigned NOT NULL auto_increment,
`src` varchar(128) NOT NULL default '',
`dst` varchar(128) NOT NULL default '',
`language` varchar(12) NOT NULL default '',
PRIMARY KEY (`pid`),
UNIQUE KEY `dst_language` (`dst`,`language`),
KEY `src_language` (`src`,`language`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
Getting "the right portion" of the output is easier, there : no quote to remove.
获得输出的“正确部分”更容易,没有要删除的引用。
And, just for the sake of completness, you could also use mysqldump
to get your table's structure :
而且,为了完整起见,您还可以使用mysqldump
来获取表的结构:
mysqldump --no-data --user=USERNAME --password=PASSWORD --host=HOST DATABASE_NAME TABLE_NAME
Using the --no-data
switch, you'll only get the structure -- in the middle of some mode settings and all that.
使用--no-data
开关,您只能获得结构——在某些模式设置和所有这些的中间。
回答by Phil McCullick
To get an individual table's creation script just right click on the table name and click Copy to Clipboard > Create Statement.
要获取单个表的创建脚本,只需右键单击表名称,然后单击复制到剪贴板 > 创建语句。
To enable the File > Forward Engineering SQL_CREATE Script.. option and to get the creation script for your entire database :
要启用 File > Forward Engineering SQL_CREATE Script.. 选项并获取整个数据库的创建脚本:
- Database > Reverse Engineer (Ctrl+R)
- Go through the steps to create the EER Diagram
- When viewing the EER Diagram click File > Forward Engineering SQL_CREATE Script... (Ctrl+Shift+G)
- 数据库 > 逆向工程师 (Ctrl+R)
- 完成创建 EER 图的步骤
- 查看 EER 图表时,单击文件 > 正向工程 SQL_CREATE 脚本... (Ctrl+Shift+G)
回答by Cez
Right-click on the relevant table and choose either of:
右键单击相关表并选择以下任一选项:
- Copy to Clipboard > Create Statement
- Send to SQL Editor > Create Statement
- 复制到剪贴板 > 创建语句
- 发送到 SQL 编辑器 > 创建语句
That seems to work for me.
这似乎对我有用。
回答by aronkerr
It is located in server administration rather than in SQL development.
它位于服务器管理而不是 SQL 开发中。
- From the home screen select the database server instance your database is located on from the server administration section on the far right.
- From the menu on the right select Data Export.
- Select the database you want to export and choose a location.
- Click start export.
- 从主屏幕最右侧的服务器管理部分选择您的数据库所在的数据库服务器实例。
- 从右侧的菜单中选择Data Export。
- 选择要导出的数据库并选择一个位置。
- 单击开始导出。
回答by user21884
show create table
显示创建表
回答by exception
I came here looking for the answer to the same question. But I found a much better answer myself.
我来到这里寻找同一问题的答案。但我自己找到了更好的答案。
In the tables list, if you right-click on the table name there is a suite of CRUD script generation options in "Send to SQL Editor". You can select multiple tables and take the same approach too.
In the tables list, if you right-click on the table name there is a suite of CRUD script generation options in "Send to SQL Editor". You can select multiple tables and take the same approach too.
My version of MySQL Workbench: 5.2.37
我的 MySQL Workbench 版本:5.2.37
回答by bbb
Not sure if I fully understood your problem, but if it's just about creating export scripts, you should forward engineer to SQL script - Ctrl + Shift + G or File -> Export -> first option.
不确定我是否完全理解您的问题,但如果只是创建导出脚本,您应该将工程师转发到 SQL 脚本 - Ctrl + Shift + G 或文件 -> 导出 -> 第一个选项。
回答by MrBii
回答by Caat c
Solution for MySQL Workbench 6.3E
MySQL Workbench 6.3E 解决方案
- On left panel, right click your table and selecct "Table Inspector"
- On center panel, click DDL label
- 在左侧面板上,右键单击您的表格并选择“表格检查器”
- 在中心面板上,单击 DDL 标签
回答by Urias BT
In "model overview" or "diagram" just right-click on the table and you have the folowing options: "Copy Insert to clipboard" OR "Copy SQL to clipboard"
在“模型概述”或“图表”中,只需右键单击表格,您就有以下选项:“将插入复制到剪贴板”或“将 SQL 复制到剪贴板”