views:

377

answers:

4

How can I change database name in mysql ?

My Database name is SPM and I want to change it to spm(small letters)

I tried using

RENAME DATABASE SPM TO spm;

and am getting following error message: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATABASE SPM to spm' at line 1

MY Server version: 5.0.45

+3  A: 

This is done with a RENAME DATABASE statement:

RENAME DATABASE old_db_name TO new_db_name;

This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23.

I hope you don't mind that I altered your answer. Feel free to revert if you think it was inappropriate.
soulmerge
I don't mind :) I did add a little note however.
It does not apply. And it should not be used.
Peter Lindqvist
Useless comment, since the answer already it shouldn't be used.
What good is an answer if it doesn't answer the question.
Peter Lindqvist
To inform that there is no database command to do it. Unless you use MySQL 5.1.7 .
+3  A: 

Use rename database command.

You can also try to stop your mysql server and rename a folder that contains your db data to the name you prefer. Then start your server and check the grants - they might still contain references to your old database name.

silent
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SPM SPM to spm' at line 1
Rachel
Please give me a command that you try to execute.
silent
mysql> RENAME DATABASE SPM TO spm;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATABASE SPM TO spm' at line 1
Rachel
You should upgrade your server to 5.1.7
silent
I cannot upgrate it to 5.1.7, is there any way to work around it with 5.0 VERSION
Rachel
Look at my answer
Peter Lindqvist
The rename database does not exist, and if it existed should not be used. `However, use of this statement could result in loss of database contents, which is why it was removed.`
Peter Lindqvist
You can also try to stop your mysql server and rename a folder that contains your db data to the name you prefer. Then start your server and check the grants - they might still contain references to your old database name.
silent
I cannot stop the server as it is centralized server. I agree with Peter's answer but do I need to have DBA permission for this purpose.
Rachel
You can try. But test it before dropping old database.
silent
You do not need to stop the server if you use the method i describe with mysqladmin/mysqldump which is actually answered on another question .
Peter Lindqvist
I will try your approach Peter and see if it works out. Actually currently our server is down for upgrade and will check as soon as server is up and running...it should not take more than 20 mins...I will update soon with the output.
Rachel
Peter: Do I have use this command at mysql prompt or $ prompt ?
Rachel
+6  A: 

There is no database command to do it. You basically have to do it outside the database. Below are some references outlining possible solutions. It has been answered pretty good in this question

This is probably what it should look like in your case

mysqladmin create spm
mysqldump SPM | mysql spm

After you have verified that everything is in order you can drop the original database.

drop database SPM

References Rename database 1 / Rename database 2

Peter Lindqvist
A: 

Use mysql_dump to dump out the database contents of the old schema (it produces SQL output, and can include all the object CREATE statements), switch to the new schema, and execute that script mysql> . dump.sql

If it's a large database, this may take a while, but it's the safest way to do it (make sure you suspend any applications using the database while the conversion process is going on).

Delete the old schema when you're satisfied that everything worked.

MightyE
Can you explain more on how can this be accomplished as I have never used mysql dump and so not sure of how will it work
Rachel
Do you really have to put it into a file? Is this windows specific?
Peter Lindqvist