πŸ‘‰ DML : Delete

Min-Ho LeeΒ·2020λ…„ 8μ›” 23일
1

Learn SQL

λͺ©λ‘ 보기
3/3

Sunday_August_23
.
.
.
μ•žμ„œ 데이터λ₯Ό μƒμ„±ν–ˆλ‹€λ©΄ 데이터λ₯Ό μ‚­μ œν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | μƒν’ˆ1    | 23200.00 |            20 |
|  2 | image_url2      | μƒν’ˆ2    | 17200.00 |            10 |
|  3 | image_url3      | μƒν’ˆ3    | 13500.00 |            30 |
|  4 | image_url4      | μƒν’ˆ4    | 19000.00 |            15 |
|  5 | image_url5      | μƒν’ˆ5    | 19000.00 |            40 |
+----+-----------------+---------+----------+---------------+
5 rows in set (0.00 sec)

자! 일단 ν˜„μž¬ productsλΌλŠ” ν…Œμ΄λΈ”μ˜ λ°μ΄ν„°λŠ” 이와 κ°™μŠ΅λ‹ˆλ‹€.

βž– 데이터 μ‚­μ œν•˜κΈ°

λ§Œμ•½ idκ°€ 3인 κ·Έ ν–‰μ˜ 데이터λ₯Ό μ‚­μ œν•˜κ³  μ‹Άλ‹€λ©΄,
문법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

DELETE FROM products WHERE id=3;

μ΄λ ‡κ²Œ ν•˜λ©΄ products λΌλŠ” ν…Œμ΄λΈ”μ—μ„œ idκ°€ 3인 행을 μ‚­μ œν•œλ‹€λŠ” λœ»μž…λ‹ˆλ‹€.

자 그럼 κ²°κ³ΌλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | μƒν’ˆ1    | 23200.00 |            20 |
|  2 | image_url2      | μƒν’ˆ2    | 17200.00 |            10 |
|  4 | image_url4      | μƒν’ˆ4    | 19000.00 |            15 |
|  5 | image_url5      | μƒν’ˆ5    | 19000.00 |            40 |
+----+-----------------+---------+----------+---------------+
4 rows in set (0.00 sec)

idκ°€ 3인 ν–‰μ˜ 데이터가 잘 μ‚­μ œλœ 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

ν•˜μ§€λ§Œ μ—¬κΈ°μ„œ μ€‘μš”ν•œ ν•œ 가지가 μžˆμŠ΅λ‹ˆλ‹€.

λ°”λ‘œ whereμ—μ„œ 쑰건을 쀄 λ•Œ, ν•΄λ‹Ή where에 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  ν–‰μ˜ 데이터가 μ‚­μ œλ©λ‹ˆλ‹€.

κ·Έλž˜μ„œ μ œκ°€ μΌλΆ€λŸ¬ priceκ°€ 19000원인 데이터λ₯Ό 2개λ₯Ό λ§Œλ“€μ–΄ λ΄€μŠ΅λ‹ˆλ‹€.

자 그럼 idκ°€ μ•„λ‹Œ, priceκ°€ 19000원인 μƒν’ˆμ„ μ‚­μ œν•˜λ©΄ μ–΄λ–»κ²Œ λ κΉŒμš”?

mysql> delete from products where price=19000;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | μƒν’ˆ1    | 23200.00 |            20 |
|  2 | image_url2      | μƒν’ˆ2    | 17200.00 |            10 |
+----+-----------------+---------+----------+---------------+
2 rows in set (0.00 sec)

μ œκ°€ λ§ν–ˆλ˜ κ²ƒμ²˜λŸΌ where price=19000에 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  데이터가(μ—¬κΈ°μ„œλŠ” 2개) μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

이 점에 μœ μ˜ν•΄μ„œ 데이터λ₯Ό μ‚­μ œν•΄μ£Όμ‹œλ©΄ λ©λ‹ˆλ‹€ 😁

profile
πŸ‡ Rabbit can take a rest, but 🐒 turtle can't

0개의 λŒ“κΈ€