データベース・テーブル・他色々

記事の内容

概要

DROP分は、データベース・テーブル、インデックス・ビュー・などのオブジェクトを削除できます。

DROP
記述

DROP DATABASE データベース名;

【DB一覧】
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| laravel            |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set (0.01 sec)

mysql> DROP DATABASE test;
Query OK, 1 row affected (0.01 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| laravel            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

指定テーブル削除

DROP(テーブル削除)
記述

DROP TABLE テーブル名;

【テーブル一覧】
mysql> SHOW TABLES;
+-----------------------+
| Tables_in_testDB      |
+-----------------------+
| cache                 |
| cache_locks           |
| failed_jobs           |
| job_batches           |
| jobs                  |
| migrations            |
| password_reset_tokens |
| sessions              |
| test                  |
| users                 |
+-----------------------+
10 rows in set (0.01 sec)

【testテーブル削除】
mysql> DROP TABLE test;
Query OK, 1 row affected (0.01 sec)

【テーブル一覧】
mysql> SHOW TABLES;
+-----------------------+
| Tables_in_testDB      |
+-----------------------+
| cache                 |
| cache_locks           |
| failed_jobs           |
| job_batches           |
| jobs                  |
| migrations            |
| password_reset_tokens |
| sessions              |
| users                 |
+-----------------------+
9 rows in set (0.01 sec)

インデックス削除

DROP(テーブル削除)
記述

DROP INDEX インデックス名 ON テーブル名;

【テーブル一覧】
mysql> SHOW INDEX FROM users;
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| users |          0 | PRIMARY   |            1 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| users |          1 | user_name |            1 | name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)

【testテーブル削除】
mysql> DROP INDEX user_name ON users;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

【テーブル一覧】
mysql> SHOW INDEX FROM users;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| users |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.01 sec)

ビュー削除

DROP(ビュー削除)
記述

DROP VIEW ビュー名;

【テーブル一覧】
mysql> SHOW TABLES;
+--------------------------+
| Tables_in_test_db        |
+--------------------------+
| departments              |
| employees                |
| employee_department_view |
+--------------------------+
3 rows in set (0.01 sec)

【ビュー削除】
mysql> DROP VIEW employee_department_view;
Query OK, 0 rows affected (0.01 sec)

【テーブル一覧】
mysql> SHOW TABLES;
+--------------------------+
| Tables_in_test_db        |
+--------------------------+
| departments              |
| employees                |
+--------------------------+
2 rows in set (0.01 sec)
記事の内容
閉じる