如何使用 SQL 查询以具有相同名称和相同表和行/内容的不同名称克隆 MySQL 数据库

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

How to clone MySQL database under a different name with the same name and the same tables and rows/content using SQL query

mysqlsql

提问by Derfder

I know how to clone tables e.g.:

我知道如何克隆表,例如:

CREATE TABLE recipes_new LIKE production.recipes;?
INSERT recipes_new 
SELECT * FROM production.recipes;

But I don't know how to clone e.g. a database_oldto database_newdatabase with all the tables and rows from database_old.

但我不知道如何将database_old克隆到database_new数据库,其中包含来自 database_old 的所有表和行。

So, only the name of the database will change. Everything else stays the same.

所以,只有数据库的名称会改变。其他一切都保持不变。

Right now I am cloning it by exporting the database in phpmyadmin ad then creating a new database and importing it to the new database.

现在我通过在 phpmyadmin ad 中导出数据库然后创建一个新数据库并将其导入新数据库来克隆它。

But I guess there must be a more efficient way of doing this task via SQL query like that one for cloning tables.

但我想必须有一种更有效的方法来通过 SQL 查询来完成这项任务,比如克隆表。

IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.

重要的!它需要从 phpmyadmin 中的 SQL 查询窗口完成,而不是从 shell 命令行完成。

Thanks in advance for you suggestion how to do that.

预先感谢您建议如何做到这一点。

回答by Vineet1982

There is a alternate way for doing things such as:

有一种替代方法可以做这样的事情:

First create a blank database:

首先创建一个空白数据库:

 CREATE DATABASE `destination` DEFAULT CHARACTER SET 
    latin1 COLLATE latin1_swedish_ci;

Then use the command show tables;

然后使用命令show tables;

 show source.tables;

and then run the command for each DB table (Optimized Create table and inserting rows) as:

然后为每个数据库表运行命令(优化创建表和插入行):

 create table destination.table select * from source.table;

and other way is using like command:

另一种方式是使用 like 命令:

  create table destination.table like source.table

and then inserting rows;

然后插入行;

  insert into destination.table select * from source.table

回答by Freddie Fabregas

have you tried using MySQL Dump?

你试过使用MySQL Dump吗?

$ mysqldump yourFirstDatabase -u user -ppassword > yourDatabase.sql
$ echo "create database yourSecondDatabase" | mysql -u user -ppassword
$ mysql yourSecondDatabase -u user -ppassword < yourDatabase.sql

回答by Syed Abdul Qadeer

If phpMyAdmin is available for the database, you can clone it by following these steps:

如果 phpMyAdmin 可用于数据库,您可以按照以下步骤克隆它:

  1. Select required database
  2. Click on the operation tab
  3. In the operation tab, go to the "copy database"-option and type your desired clone-db-name into the input field
  4. Select "Structure and data"(Depends on your requirement)
  5. Check the boxes, "CREATE DATABASE before copying"and "Add AUTO_INCREMENT value"
  6. Click "GO"
  1. 选择需要的数据库
  2. 单击操作选项卡
  3. 在操作选项卡中,转到“复制数据库”-选项并在输入字段中键入所需的克隆数据库名称
  4. 选择“结构和数据”(取决于您的要求)
  5. 选中“复制前创建数据库”“添加 AUTO_INCREMENT 值”
  6. 点击“前往”

Tested with phpMyAdmin 4.2.13.3

使用 phpMyAdmin 4.2.13.3 测试

回答by Arif Hidayat

  1. export your chosen database using phpmyadmin (export.sql)
  2. import it using terminal/cmd: mysql -u username -p databasename < export.sql
  1. 使用 phpmyadmin (export.sql) 导出您选择的数据库
  2. 使用终端/cmd 导入它: mysql -u username -p databasename < export.sql

and get a cup of coffee...

然后喝杯咖啡……