2
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文はテーブルの行を削除するためのものです。SELECTやUPDATEはピンポイントで列を指定して更新や取得できましたが、DELETEは行(レコード)を消してしまうという点が注意です。

基本は下記のとおりです。

DELETE
  FROM テーブル名

しかしながら上記だけで実行してしまうと、そのテーブルの中身が全て削除されてしまうので、普通はWHERE句と一緒に使います。UPDATEもそうでしたね。

下記のような家計簿テーブルがあるとします。

日付 費目 メモ 入金額 出金額
2021-08-02 食費 パンを購入 0 300
2021-08-03 食費 晩ご飯材料 0 1000
2021-08-08 交通費 電車代 0 520
2021-08-13 給料 7月分給料 250,000 0
2021-08-13 食費 外食 0 7000

これをDELETEで8/3の晩ご飯材料を消してみます。

DELETE
  FROM 家計簿
 WHERE 日付 = '2021-08-03'

こうするとWHEREで指定した行(レコード)がまるまる消えます。

日付 費目 メモ 入金額 出金額
2021-08-02 食費 パンを購入 0 300
2021-08-08 交通費 電車代 0 520
2021-08-13 給料 7月分給料 250,000 0
2021-08-13 食費 外食 0 7000

参考: スッキリわかるSQL入門

2
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
2
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?