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.

100日でSQLの達人になる@LeetCode! Day56 <DELETE文でデータを削除する>

Last updated at Posted at 2022-03-30

DELETE句について理解する

前回に引き続きLeetCodeの問題はお休み。
LeetCodeではあまり出てこないテーマを一気に学習します。
今回のテーマはDELETE句です。
`DELETE句は、テーブルに対して条件に合う行データを削除する時に使用します。

前回同様にdb<>fiddleを使います。

基本の構文

SQL
DELETE テーブル名 WHERE句
ーー下記でも全く同じ
DELETE FROM テーブル名 WHERE句

下記が実行例です。

CREATE TABLE tbl1 (id INT, name varchar(1))

INSERT INTO tbl1 (id, name) VALUES(1, 'a'),(2, 'b'),(3, 'c'),(4, 'd'),(5, 'e')

SELECT id, name
  FROM tbl1
  
DELETE tbl1 WHERE id > 3
     
SELECT id, name
  FROM tbl1
GO

id | name
-: | :---
 1 | a   
 2 | b   
 3 | c   
 4 | d   
 5 | e   

id | name
-: | :---
 1 | a   
 2 | b   
 3 | c   

db<>fiddle here

DELETE句では、SELECT文を使用したサブクエリも使用可能。
WHERE句を指定しないと全件削除になってしまうので要注意!

今日のポイントはUPDATE句で表を更新するとした。

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?