MySQL 如何在Mysql工作台中的两个模型之间复制表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17119500/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:56:22  来源:igfitidea点击:

How to copy table between two models in Mysql workbench?

mysqlmysql-workbenchmysqldumpworkbencheer-model

提问by DraculaW

I am doing some databese thing, I need copy one table from one model to another, but i try many ways there no effect. Is there any way for doing this?

我正在做一些数据库的事情,我需要将一张表从一个模型复制到另一个模型,但我尝试了很多方法都没有效果。有没有办法做到这一点?

采纳答案by Mike Lischke

Your best option is probably to create a stripped down version of the model that contains the objects you want to carry over. Then open the target model and run File -> Include Model.... Select the stripped down source model and there you go.

您最好的选择可能是创建模型的精简版本,其中包含您要继承的对象。然后打开目标模型并运行File -> Include Model...。选择精简的源模型,然后就可以了。

回答by Ricky Hewitt

If you just want to do a single table through the MySQL Workbench.

如果你只是想通过 MySQL Workbench 做单表。

In MySQL Workbench:

在 MySQL 工作台中:

  1. Connect to a MySQL Server
  2. Expand a Database
  3. Right Click on a table
  4. Select Copy To Clipboard
  5. Select Create Statement
  1. 连接到 MySQL 服务器
  2. 扩展数据库
  3. 右键单击一个表
  4. 选择复制到剪贴板
  5. 选择创建语句

A create statement for the table will be copied to your clipboard similar to the below:

表的创建语句将复制到您的剪贴板,类似于以下内容:

   CREATE TABLE `cache` (
  `cid` varchar(255) NOT NULL DEFAULT '',
  `data` longblob,
  `expire` int(11) NOT NULL DEFAULT '0',
  `created` int(11) NOT NULL DEFAULT '0',
  `headers` text,
  `serialized` smallint(6) NOT NULL DEFAULT '0',
  PRIMARY KEY (`cid`),
  KEY `expire` (`expire`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Create the table in the new database

在新数据库中创建表

  1. Open a new SQL tab for executing queries (File->New Query Tab)
  2. Alter the create table code to include the database to create the table on.

     CREATE TABLE `databaseName`.`cache` (
      `cid` varchar(255) NOT NULL DEFAULT '',
      `data` longblob,
      `expire` int(11) NOT NULL DEFAULT '0',
      `created` int(11) NOT NULL DEFAULT '0',
      `headers` text,
      `serialized` smallint(6) NOT NULL DEFAULT '0',
      PRIMARY KEY (`cid`),
      KEY `expire` (`expire`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

  3. Then click the Execute button (looks like a lightening Bolt)

  1. 打开一个新的 SQL 选项卡以执行查询(文件-> 新查询选项卡)
  2. 更改创建表代码以包含要在其上创建表的数据库。

     CREATE TABLE `databaseName`.`cache` (
      `cid` varchar(255) NOT NULL DEFAULT '',
      `data` longblob,
      `expire` int(11) NOT NULL DEFAULT '0',
      `created` int(11) NOT NULL DEFAULT '0',
      `headers` text,
      `serialized` smallint(6) NOT NULL DEFAULT '0',
      PRIMARY KEY (`cid`),
      KEY `expire` (`expire`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

  3. 然后单击执行按钮(看起来像闪电螺栓)

That will copy the table schema from one db to another using the MySQL workbench. Just refresh the tables in the database and you should see your newly added table

这将使用 MySQL 工作台将表模式从一个数据库复制到另一个数据库。只需刷新数据库中的表,您就会看到新添加的表

回答by x1z1x

  1. Select tab with source database
  2. In menu: Server->Data Export
  3. Select Schema and the Table as Schema Object
  4. Select option Export to Self-Contained File and check Create Dump in a Single Transaction (self-contained only)
  5. Copy full file path to clipboard
  6. Start Export
  7. Select tab with target database
  8. In menu: Server->Data Import. Make sure your target database name is at the top left corner of the Data Import view
  9. Select Import from self contained file and paste full file path from clipboard
  10. Select Default Target Schema
  11. Select Dump Content (Dump Structure and Data etc…)
  12. Start Import
  1. 选择带有源数据库的选项卡
  2. 在菜单中:服务器->数据导出
  3. 选择架构和表作为架构对象
  4. 选择选项导出到自包含文件并选中在单个事务中创建转储(仅限自包含)
  5. 将完整文件路径复制到剪贴板
  6. 开始导出
  7. 选择带有目标数据库的选项卡
  8. 在菜单中:服务器->数据导入。确保您的目标数据库名称位于数据导入视图的左上角
  9. 选择从自包含文件导入并从剪贴板粘贴完整文件路径
  10. 选择默认目标架构
  11. 选择转储内容(转储结构和数据等...)
  12. 开始导入

回答by Mnl

You can just use a select statement. Here I am creating a duplicate of "original_table" table from the "original_schema" schema/database to the "new_schema" schema :

您可以只使用 select 语句。在这里,我正在创建从“original_schema”模式/数据库到“new_schema”模式的“original_table”表的副本:

CREATE TABLE new_schema.duplicate_table AS
Select * from original_schema.original_table;

You can just put any select statement you need ,add a condition and select the columns :

您可以放置​​您需要的任何选择语句,添加条件并选择列:

CREATE TABLE new_schema.duplicate_table AS
SELECT column1, column2       
FROM original_schema.original_table
WHERE column2 < 11000000;

回答by constant283

I think it is worth mentioning that

我认为值得一提的是

  1. a copied table may reference fields in tables of the original schema, that do not exist, in the schema where it's to be copied. It might be a good idea, to inspect the table for these discrepancies, before adding it to the other schema.
  2. it's probably a good idea, to check engine compatibility (e.g. InnoDB vs MyISAM) and character set.
  1. 复制的表可能会引用原始模式表中的字段,这些字段在要复制的模式中不存在。在将表添加到其他模式之前,检查表是否存在这些差异可能是个好主意。
  2. 检查引擎兼容性(例如 InnoDB 与 MyISAM)和字符集可能是个好主意。

回答by RobertLonnberg

If you already have your table created and just want to copy the data, I'd recommend using the "Export Data Wizard" and "Import Data Wizard". It is basically choosing stuff in the program for exporting and then importing the data and is easy to use.

如果您已经创建了表并且只想复制数据,我建议您使用“导出数据向导”和“导入数据向导”。它基本上是在程序中选择内容进行导出然后导入数据,并且易于使用。

MySQL has an article on the wizards here: Table Data Export and Import Wizard

MySQL 有一篇关于向导的文章:表数据导出和导入向导

To copy data using the wizards, do the following:

要使用向导复制数据,请执行以下操作:

  1. Find the table in the list from which you want to copy data from.
  2. Right click and choose "Table Data Export Wizard."
  3. Choose the columns you wish to copy.
  4. Choose a location to save a *.csv or *.json file with the copied data.

  5. Find the table to insert the copied data to.

  6. Right click and choose "Table data import wizard".
  7. Choose the file you just exported.
  8. Map the columns from the table you copied from to the table you insert to.
  9. Press "Finish". The data is inserted as you chose.
  1. 在列表中找到要从中复制数据的表。
  2. 右键单击并选择“表数据导出向导”。
  3. 选择要复制的列。
  4. 选择一个位置来保存带有复制数据的 *.csv 或 *.json 文件。

  5. 找到要插入复制数据的表。

  6. 右键单击并选择“表数据导入向导”。
  7. 选择您刚刚导出的文件。
  8. 将您复制的表中的列映射到您插入的表。
  9. 按“完成”。数据按您的选择插入。

回答by Raushan

step 1 : Righit click on table > copy to clipboard > create statement

第 1 步:右键单击表格 > 复制到剪贴板 > 创建语句

step 2: paste clipboard in the query field of workbench.

第二步:在工作台的查询字段中粘贴剪贴板。

step 3: remove (``) from the name of the table and name of the model(schema)followed by a dot.

第 3 步:从表名和模型(模式)名称中删除 (``) 后跟一个点。

eg : `cusine_menus` -> schema_name.cusine_menus

例如:`cusine_menus` -> schema_name.cusine_menus

execute

执行

回答by Vinod Joshi

create table .m_property_nature like .m_property_nature;

创建表 .m_property_nature 就像 .m_property_nature;

INSERT INTO .m_property_nature SELECT * from .m_property_nature;

INSERT INTO .m_property_nature SELECT * from .m_property_nature;

回答by Tarun Voora

You can get the crate table query from table info and use the same query on different database instance.

您可以从 table info 获取 crate table 查询,并在不同的数据库实例上使用相同的查询。

  1. show create table TABLENAME.content and copy the query;
  2. Run the generated query on another Db instance connected.
  1. 显示创建表 TABLENAME.content 并复制查询;
  2. 在另一个连接的数据库实例上运行生成的查询。