我们如何在 MySQL 5.0 中重命名数据库名称

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

How can we rename the database name in MySQL 5.0

mysql

提问by sivakumar

I am using MySQL 5.0.

我在用 MySQL 5.0.

I have created a database named accounts, but now I want to change the database name to FinanceAccounts.

我创建了一个名为 的数据库accounts,但现在我想将数据库名称更改为FinanceAccounts.

How can I change the database name in MySQL 5.0?

如何更改数据库名称MySQL 5.0

回答by Stefan Gehrig

I think there is only one way (besides renaming directory in the MySQL datadir which will fail for InnoDB tables):

我认为只有一种方法(除了重命名 MySQL 数据目录中的目录之外,这对于 InnoDB 表会失败):

  • create new database (with new name)
  • make dump of old database
  • import dumped data into new database
  • delete old database
  • 创建新数据库(使用新名称)
  • 转储旧数据库
  • 将转储的数据导入新数据库
  • 删除旧数据库

To create the new DB:

要创建新数据库:

mysql> CREATE DATABASE new_database;

To create the dump of the old DB:

创建旧数据库的转储:

mysqldump -u "your_username" -p --lock-tables old_database > old_database_dump.sql

To import dumped data into the new DB:

将转储数据导入新数据库:

mysql -u "your username" -p new_database  < old_database_dump.sql

To delete the old DB:

删除旧数据库:

mysql> DROP DATABASE old_database;

Bear in mind that your permissions on the old DB will need to be deleted as well. See here for more info: Revoke all privileges for all users on a MySQL DB

请记住,您对旧数据库的权限也需要删除。有关更多信息,请参见此处: 撤销 MySQL 数据库上所有用户的所有权限

MySQL 5.1.7 to MySQL 5.1.22 had a RENAME {DATABASE | SCHEMA} db_name TO new_db_name;command but this one has been removed in MySQL 5.1.23 for being too dangerous.

MySQL 5.1.7 到 MySQL 5.1.22 有一个RENAME {DATABASE | SCHEMA} db_name TO new_db_name;命令,但是这个命令在 MySQL 5.1.23 中被删除了,因为它太危险了。

回答by brian-brazil

The best way is probably to rename each of the tables inside the database to the new name. For example:

最好的方法可能是将数据库中的每个表重命名为新名称。例如:

Update: There are two steps here

更新:这里有两个步骤

  1. Create a new blank database as you want say "new accounts"

    CREATE DATABASE newaccounts;

  2. Migrate each table one-by-one

    RENAME TABLE accounts.tablename TO newaccounts.tablename;

  1. 创建一个新的空白数据库,因为你想说“新账户”

    创建数据库新帐户;

  2. 逐个迁移每个表

    RENAME TABLE accounts.tablename TO newaccounts.tablename;

See http://dev.mysql.com/doc/refman/5.0/en/rename-table.htmlfor more information.

有关 更多信息,请参阅 http://dev.mysql.com/doc/refman/5.0/en/rename-table.html

回答by spencerthayer

MySQL kinda sucks for this. The only solid reliable solution is to use phpMyAdmin.

MySQL 有点糟透了。唯一可靠的解决方案是使用 phpMyAdmin。

Login > click Scheme > click "Operations" > find "Rename database to:" > write NewName > click "Go."

登录> 单击方案> 单击“操作”> 找到“将数据库重命名为:”> 写入新名称> 单击“执行”。

As simple as that. All permissions are carried over.

就如此容易。所有权限都被继承。

回答by majid Chabab

here , I rename mydb database to ecommerce, you follow this steps, but usin phpmyadmin is to easy

在这里,我将 mydb 数据库重命名为 ecommerce,您按照以下步骤操作,但是使用 phpmyadmin 很容易

CREATE DATABASE `ecommerce` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

RENAME TABLE `mydb`.`Articles` TO `ecommerce`.`Articles` ;

RENAME TABLE `mydb`.`Categories` TO `ecommerce`.`Categories` ;

RENAME TABLE `mydb`.`Utilisateurs` TO `ecommerce`.`Utilisateurs` ;

ALTER TABLE `Articles` ADD CONSTRAINT fk_Articles_Categories FOREIGN KEY ( Categorie_id ) REFERENCES Categories( id ) ON DELETE NO ACTION ON UPDATE NO ACTION ;

DROP DATABASE `mydb` ;

回答by nazmulhaqued

To Rename MySQL Database name follow the following steps:

要重命名 MySQL 数据库名称,请按照以下步骤操作:

1) Click the database name 

2) Click at Operations from the top menu

3) Type new database name Under Rename database to:

Please check the screen shot

请检查屏幕截图