0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

MySQLは別のデータベースにテーブルを移すことができます。今回はその方法を試してみました。

別のDBにテーブルを移す方法

まずはmysqlにログインします。

mysql -u root -p

データベースを確認します。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| app_development    |
| app_test           |
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

今回は、app_developmentのusersテーブルを別のデータベースに移します。

second_databaseを作成します。

mysql> create database second_database;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| app_development    |
| app_test           |
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| second_database    |
| sys                |
+--------------------+
8 rows in set (0.00 sec)

app_developmentのusersテーブルをsecond_databaseに移します。

mysql> RENAME TABLE app_development.users TO second_database.users;

Query OK, 0 rows affected (0.03 sec)

しっかりと移動しているか確認します。

mysql> use second_database;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_second_database |
+---------------------------+
| users                     |
+---------------------------+
1 row in set (0.00 sec)

中身も確認します。
しっかり中身のレコードも新しいデータベースに移っています。

mysql> SELECT * FROM users;
+-----+----------+------------------+------+----------------------------+----------------------------+
| id  | name     | email            | age  | created_at                 | updated_at                 |
+-----+----------+------------------+------+----------------------------+----------------------------+
|   1 | ?????1   | test1@test.com   |    1 | 2024-07-08 13:20:59.774423 | 2024-07-08 13:20:59.774423 |
|   2 | ?????2   | test2@test.com   |    2 | 2024-07-08 13:20:59.786474 | 2024-07-08 13:20:59.786474 |
|   3 | ?????3   | test3@test.com   |    3 | 2024-07-08 13:20:59.792206 | 2024-07-08 13:20:59.792206 |
|   4 | ?????4   | test4@test.com   |    4 | 2024-07-08 13:20:59.797279 | 2024-07-08 13:20:59.797279 |
|   5 | ?????5   | test5@test.com   |    5 | 2024-07-08 13:20:59.800999 | 2024-07-08 13:20:59.800999 |
|   6 | ?????6   | test6@test.com   |    6 | 2024-07-08 13:20:59.805134 | 2024-07-08 13:20:59.805134 |
...省略

終わりに

データベース名を間違ってしまったので名前だけ変更したい場合や、特定のテーブルを別のデータベースに移行したい場合などに使えそうです。
便利なので、覚えておいて損はないです。

参考

以下の記事を参考にさせていただきました。
こちらの記事では、複数のテーブルを一度に変更するコマンドを生成するためのSQLが載っています。とてもに便利です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?