sql重命名数据库

In the real world, there is very minimal chance when we need to rename database but still renaming database is a very valid scenario in real time.

在现实世界中,当我们需要重命名数据库但仍然重命名数据库是实时的非常有效方案时,机会很小。

Today we will try to rename databases in some of the most used Databases of the world.

今天,我们将尝试在世界上一些最常用的数据库中重命名数据库。

介绍 (Introduction)

In this section, we will see how different databases provides the feature of renaming a database. So let’s start with the following set of databases.

在本节中,我们将看到不同的数据库如何提供重命名数据库的功能。 因此,让我们从以下数据库开始。

  1. PostgreSQL

    PostgreSQL
  2. MySQL

    MySQL
  3. SQLServer

    SQL服务器

We will try to understand database renaming in each of the above mentioned DBs one by one.

我们将尝试一一理解上述每个DB中的数据库重命名。

PostgreSQL (PostgreSQL)

Let us try to rename a database in PostgreSQL.

让我们尝试在PostgreSQL中重命名数据库。

Suppose the database that you want to rename is TestDB.

假设您要重命名的数据库是TestDB。

First, disconnect from the database that you want to rename by connecting to another database, we will connect to Postgres database.
By connecting to another database, you are automatically disconnected from the database to which you connected.

首先,通过连接到另一个数据库与要重命名的数据库断开连接,我们将连接到Postgres数据库。
通过连接到另一个数据库,您将自动与连接的数据库断开连接。

Before renaming any database it is always good to check if the database has an active connection.

重命名任何数据库之前,最好检查数据库是否具有活动连接。

We will also check in PostgreSQL if the database that we want to rename has any active connections.

我们还将在PostgreSQL中检查要重命名的数据库是否具有任何活动连接。

SELECT
    *
FROM
    pg_stat_activity
WHERE
    datname = 'TestDB';

Output: –

输出:–

PostgreSQL Check Connection

PostgreSQL检查连接

As we see that the database has only one connection. If the database has many connections it is better to inform the respective user about the rename.

正如我们看到的,数据库只有一个连接。 如果数据库有许多连接,最好将重命名通知相应的用户。

After that, rename the TestDB database to NewTestDB using the ALTER DATABASE RENAME TO statement as follows:

之后,使用ALTER DATABASE RENAME TO语句将TestDB数据库重命名为NewTestDB ,如下所示:

ALTER DATABASE "TestDB" RENAME TO "NewTestDB";

PostgreSQL rename database

PostgreSQL重命名数据库

MySQL (MySQL)

In the earlier version of MySQL rename database was done through a simple SQL command. But, due to security issues, the feature is revoked from the latest versions.

在早期版本MySQL中,重命名数据库是通过简单SQL命令完成的。 但是,由于安全问题,该功能已从最新版本中撤消。

We can create a dumped copy, then create a new DB and then re-import from the dumbed copy.

我们可以创建一个转储副本,然后创建一个新的数据库,然后从该哑副本中重新导入。

SQL Command for Dump copy

转储副本SQL命令

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

SQL Command for creating new DB

用于创建新数据库SQL命令

$ mysqladmin -u username -p"password" create testDB1

SQL Command for Import

用于导入SQL命令

$ mysql -u username -p"password" testDb1 < testDb.sql

The above is one such solution for renaming a DB in MySQL

上面是在MySQL中重命名数据库的一种解决方案

Also for Unix, database names are case-sensitive so please make sure that appropriate case is used.

同样对于Unix,数据库名称区分大小写,因此请确保使用适当的大小写。

SQL服务器 (SQLServer)

We will now use SQLServer to rename a database. Let us rename TestDB using SQL Server Management Studio.

现在,我们将使用SQLServer重命名数据库。 让我们使用SQL Server Management Studio重命名TestDB。

Please find below the steps to be followed to rename the database.

请在下面找到重命名数据库的步骤。

  1. Connect to the Database in the Object Explorer.

    在对象资源管理器中连接到数据库。
  2. We will try to make sure that there is no more connection to the DB before renaming.

    我们将尝试确保重命名之前没有与数据库的更多连接。
  3. Set the connection to single user mode.

    将连接设置为单用户模式。
  4. Right Click on the database and click on properties.

    右键单击数据库,然后单击属性。
  5. In the Database Properties box, click the Options page.

    在“ 数据库属性”框中,单击“ 选项”页面。
  6. From the Restrict Access option, select Single.

    从“ 限制访问”选项中,选择“ 单个”
  7. If other users are connected to the database, an Open Connections message will appear. To change the property and close all other connections, click Yes.

    如果其他用户已连接到数据库,则会显示“ 打开连接”消息。 要更改属性并关闭所有其他连接,请单击
  8. Now right click the DB and click on Rename.

    现在,右键单击数据库,然后单击重命名
  9. Enter the new database name and click on OK button.

    输入新的数据库名称,然后单击确定按钮。

SQLServer Properties

SQLServer属性

SQLServer Restrict Access

SQLServer限制访问

SQLServer Properties Rename

SQLServer属性重命名

SQLServer Rename

SQLServer重命名

Once the rename process is complete please revert the SINGLE USER mode.

重命名过程完成后,请恢复“单用户”模式。

翻译自: https://www.journaldev/24390/sql-rename-database

sql重命名数据库

更多推荐

sql重命名数据库_SQL重命名数据库