Sql 简明教程

SQL - Rename Database

重命名数据库名称可能有多种原因。其中一个原因可能是为了避免命名冲突,或者是为了将不同类型的数据分隔到不同的数据库中。另一个原因可能是为了将它们按有组织的方式排列,从而使它们更具描述性并且更容易管理。

There can be several reasons to rename a database name. One of the reasons could be to avoid naming conflicts or to separate different types of data into different databases. Another reason can be to arrange them in an organized way which makes them more descriptive and easier to manage.

The ALTER DATABASE…​MODIFY Statement

SQL 中的 ALTER DATABASE…​MODIFY 语句用于在 SQL 数据库服务器中重命名现有数据库名称。请注意,此命令在 MySQL 数据库中不起作用。

The ALTER DATABASE…​MODIFY statement in SQL is used to rename the name of an existing database name in SQL Database Server. Please note that this command does not work in MySQL database.

Syntax

以下是 ALTER DATABASE…​MODIFY 命令的语法 −

Following is the syntax of the ALTER DATABASE…​MODIFY command −

ALTER DATABASE OldDatabaseName MODIFY NAME = NewDatabaseName;

Example

以下是 SQL Server 中将数据库 testDB 重命名为 tutorialsDB 的 SQL 命令:

Following is the SQL command in SQL Server to rename the database testDB to tutorialsDB:

ALTER DATABASE testDB MODIFY NAME = tutorialsDB ;

Rename Database using Dump and Reimport

如果你希望在 MySQL 中重命名数据库名称,那么简单的方法是将完整的数据库转储到一个 SQL 文件中,然后重新将其导入到一个新数据库中。这是我们本教程中将要遵循的三步流程:

If you are willing to rename a database name in MySQL, then simple way is to dump the complete database in an SQL file and then re-import it into a new database. This is three step process which we will follow in this tutorial:

Step 1 - Dump Old Database

考虑你要将 testDB 数据库重命名为 tutorialsDB 。因此,我们首先会使用 MySQL mysqldump 命令将其转储到一个简单的 SQL 文件中。此操作将从命令行执行,并且需要数据库用户名和密码(最好具有管理员权限)。

Consider you want to rename testDB database to tutorialsDB. So first we will dump it in a simple SQL file using MySQL mysqldump command. This operation will be performed from command line and will require a database user name and password, preferably admin privilege.

$ mysqldump -u username -p"password" -R testDB > testDB.sql

我们紧跟在我们的密码后面使用 -p 标志,以连接到数据库,其间没有空格。需要 -R 通知 mysqldump 复制存储过程和函数以及数据库中的正常数据。

We are using the -p flag immediately followed by our password to connect to the database with no space between. The -R is required to tell mysqldump to copy stored procedures and functions along with the normal data from the database.

Step 2 - Create New Database

下一步是使用 mysqladmin 提示命令按如下方式 create new database

Next step is to create new database using mysqladmin prompt command as follows:

$ mysqladmin -u username -p"password" create tutorialsDB;

Step 3 - Import Old Database

最后一步是按如下方式将旧数据库导入到新数据库:

The final step is to import old database into new database as follwing:

$ mysql -u username -p"password" tutorialsDB < testDB.sql;

Step 4 - Verification (Optional)

现在,你可以通过列出所有可用数据库来验证更改:

Now you can verify the changes by listing down all the available databases:

SHOW DATABASES;

输出将显示为:

The output will be displayed as −

Database

performance_schema

information_schema

mysql

testDB

tutorialsDB

Step 5 - Verification (Optional)

一旦你对自己所做的更改满意,你可以按如下方式删除旧数据库:

Once you are satisfied with your changes, you can delete your old database as follows:

DROP DATABASE testDB;

Rename Database in SQL using RENAME DATABASE…​TO (obsoleted)

SQL 提供了一个 RENAME DATABASE…​TO 语句来重命名现有数据库。如果你要重命名数据库,请确保没有正在进行的活动事务,否则在你重命名数据库后,整个操作可能会停止。

SQL provides a simple RENAME DATABASE…​TO statement to rename an existing database. If you want to rename a database, make sure there is no active transaction in progress otherwise the complete operation might halt once you rename the database.

Syntax

以下是 RENAME DATABASE…​TO 语句的语法 −

Following is the syntax of the RENAME DATABASE…​TO statement −

RENAME DATABASE OldDatabaseName TO NewDatabaseName;

Example

在重命名数据库之前,让我们列出所有可用数据库 −

Before renaming a database, let us list down all the available databases −

SHOW DATABASES;

输出将显示为:

The output will be displayed as −

Database

performance_schema

information_schema

mysql

testDB

现在,发布以下命令将数据库 testDB 重命名为 tutorialsDB

Now, issue the following command to rename the database testDB to tutorialsDB:

RENAME DATABASE testDB TO tutorialsDB;