How do I change the database name using MySQL?

2020-06-30 11:39发布

How can I change the database name of my database?

I tried to use the rename database command, but on the documents about it it is said that it is dangerous to use. Then what should I need to do to rename my database name?

For example, if I want to rename my database to this.

database1 -> database2?

标签: mysql
12条回答
小情绪 Triste *
2楼-- · 2020-06-30 12:27

"As long as two databases are on the same file system, you can use RENAME TABLE to move a table from one database to another"

-- ensure the char set and collate match the existing database.
SHOW VARIABLES LIKE 'character_set_database';
SHOW VARIABLES LIKE 'collation_database';


CREATE DATABASE `database2` DEFAULT CHARACTER SET = `utf8` DEFAULT COLLATE = `utf8_general_ci`;
RENAME TABLE `database1`.`table1` TO `database2`.`table1`;
RENAME TABLE `database1`.`table2` TO `database2`.`table2`;
RENAME TABLE `database1`.`table3` TO `database2`.`table3`;

http://dev.mysql.com/doc/refman/5.7/en/rename-table.html

查看更多
甜甜的少女心
3楼-- · 2020-06-30 12:29

Go to data directory and try this:

mv database1 database2 

It works for me on a 900 MB database size.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-06-30 12:30

You can use below command

alter database Testing modify name=LearningSQL;

Old Database Name = Testing, New Database Name = LearningSQL

查看更多
时光不老,我们不散
5楼-- · 2020-06-30 12:31

After much aggravation this is what I have found to work"simply". First thing, I am using MYSQL Workbench and the import would not work as it should, as the import dump file would always revert to the original schema name. I spent several hours trying every thing to no avail,all for a spelling error. I solved the issue by opening one of the .sql dump files in notebook and hand editing the typo's of the schema name, take care to rename all instances schema name has three in the beginning, save the file and then import. this worked perfectly for me and hope that it will help others looking for the simple answer to changing database names/schema names. One more tip that I have found true, when programs do not do as they should go to the "source" literally find the source code. Hope this helps someone

Low rep so they wont let me comment on the prior/post answer(it keeps changing rank or position), so I added it here. reverse engineering will work fine as long as there is no data in the sever table. if data exists and you try to update the server after the name change it will either pull an error or just create a new database/schema with no data, I know I tried ten times to no avail. The above works simply and avoids headaches, as one can review the SQL code for other errors if any or change table names or creation data. the .sql file is just a compiled SQL code so in theory one could copy and add it through PHP or the script console of the database management tool.

查看更多
看我几分像从前
6楼-- · 2020-06-30 12:36

I don't think it's possible.

You can use mysqldump to dump the data and then create a schema with your new name and then dump the data into that new database.

查看更多
三岁会撩人
7楼-- · 2020-06-30 12:36

I agree with above answers and tips but there is a way to change database name with phpmyadmin

Renaming the Database From cPanel, click on phpMyAdmin. (It should open in a new tab.) Click on the database you wish to rename in the left hand column. Click on the Operations tab. Where it says "Rename database to:" enter the new database name. Click the Go button. When it asks you to want to create the new database and drop the old database, click OK to proceed. (This is a good time to make sure you spelled the new name correctly.) Once the operation is complete, click OK when asked if you want to reload the database.

here's the video tutorial:

http://support.hostgator.com/articles/specialized-help/technical/phpmyadmin/how-to-rename-a-database-in-phpmyadmin

查看更多
登录 后发表回答