0
1

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.

[SQL]DELETE文でデータを削除

Posted at

DELETE文でデータを削除

テーブルのデータを削除するときはDELETE文を使います。

DELETE FROM テーブル名 WHERE 条件文;

SELECT文でuserテーブルのデータを確認。

mysql> select * from user;
+------+---------------+----------------------------------------------------+
| id   | name          | prefecture | address                              |                             
+------+---------------+----------------------------------------------------+
|    1 | テスト        | 東京都      | 東京都千代田区丸の内1丁目                 |                             
|    2 | テスト1         | 滋賀県      | 滋賀県彦根市金剛寺町2-16ザ金剛寺町313      |                            
|    3 | テスト2         | 大阪府      | 東京都練馬区豊玉南4-11                   |
|    4 | テスト3         | 北海道      | 北海道千歳市千代田町2-3千代田町シティ102   |                          
|    5 | テスト4         | 東京都      | 東京都小平市回田町2-12-17フォレスト回田町202 |                           
+------+---------------+---------------------------+------------------------+

データを削除していきます。
「id > 3」とすると、idが3より大きいデータが対象になります。

mysql> DELETE FROM meibo WHERE id > 3;
Query OK, 2 rows affected (0.19 sec)

データが削除されているか確認します。

mysql> select * from user;
+------+---------------+----------------------------------------------------+
| id   | name          | prefecture | address                              |                             
+------+---------------+----------------------------------------------------+
|    1 | テスト        | 東京都      | 東京都千代田区丸の内1丁目                 |                             
|    2 | テスト1         | 滋賀県      | 滋賀県彦根市金剛寺町2-16ザ金剛寺町313      |                            
|    3 | テスト2         | 大阪府      | 東京都練馬区豊玉南4-11                   |                          
+------+---------------+---------------------------+------------------------+
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?