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?

More than 3 years have passed since last update.

メモ⑦-テーブルへのDDL処理(DROPとALTER)-

Posted at

【DROP】
テーブルを削除するコマンド。
⑥で作成した「tbl_employee4」を、以下のように入力して削除する↓
MariaDB [company]> DROP TABLE tbl_employee4;
Query OK, 0 rows affected (0.010 sec)

確認すると。
MariaDB [company]> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| tbl_customer |
| tbl_customerm |
| tbl_department |
| tbl_employee |
| tbl_employee2 |
| tbl_employee3 |
| tbl_post |←tbl_employee4が消えている
| tbl_supplier |
+-------------------+
8 rows in set (0.001 sec)

【ALTER】
フィールドの追加や削除など、テーブルの内容を編集するときは、
ALTER TABLEコマンドを使用する。

tbl_employee3テーブルに血液型のフィールドを追加する↓
MariaDB [company]> ALTER TABLE tbl_employee3
-> ADD blood VARCHAR(2);
Query OK, 0 rows affected (0.006 sec)
Records: 0 Duplicates: 0 Warnings: 0

確認すると…
MariaDB [company]> desc tbl_employee3;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| code | int(11) | NO | | NULL | |
| name | varchar(40) | NO | | NULL | |
| birthday | datetime | YES | | NULL | |
| dpt_code | int(11) | YES | | NULL | |
| post_code | int(11) | YES | | NULL | |
| manager | int(11) | YES | | NULL | |
| blood | varchar(2) | YES | | NULL | |←初期値では、フィールドの最後に追加される。
+-----------+-------------+------+-----+---------+-------+
7 rows in set (0.011 sec)

※先頭に追加したい場合は
MariaDB [company]> ALTER TABLE tbl_employee3
-> ADD blood VARCHAR(2) FIRST;

※任意の場所に追加したい場合は、「〇〇の後」と追記する
MariaDB [company]> ALTER TABLE tbl_employee3
-> ADD blood VARCHAR(2) AFTER name;

また、追加したフィールドを削除するには、
「DROP COLUMN」を使用する。
MariaDB [company]> ALTER TABLE tbl_employee3
-> DROP COLUMN blood;
Query OK, 0 rows affected (0.009 sec)
Records: 0 Duplicates: 0 Warnings: 0

確認すると…
MariaDB [company]> desc tbl_employee3;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| code | int(11) | NO | | NULL | |
| name | varchar(40) | NO | | NULL | |
| gender | varchar(1) | YES | | NULL | |
| birthday | datetime | YES | | NULL | |
| dpt_code | int(11) | YES | | NULL | |
| post_code | int(11) | YES | | NULL | |
| manager | int(11) | YES | | NULL | |←「blood」フィールドが消えている
+-----------+-------------+------+-----+---------+-------+
7 rows in set (0.011 sec)

・・・⑧へ続く

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?