การลบแถว
DELETE removes the rows that match a WHERE condition:
DELETE FROM product WHERE code = 'P03';
Only the matching rows are removed; the table and its other rows stay.
Like UPDATE, a DELETE with no WHERE hits every row:
DELETE FROM product; -- empties the whole table!
Always check your WHERE before running a DELETE. The checker runs your DELETE, then reads the table back to confirm the right row is gone.
Question. Cara (customer 3) closes the account. Remove Cara and Cara's orders.
Answer. Each of Cara's orders refers to customer 3 through the foreign key customer_id. Deleting the customer first would leave orders that point at nobody, which breaks referential integrity. So remove the orders first:
DELETE FROM orders WHERE customer_id = 3;
and then the customer:
DELETE FROM customer WHERE id = 3;
Orders 12 and 13 are gone; orders 10 and 11 (Ada's) are still there.
- There is no
*inDELETE.DELETE * FROM productis a syntax error; writeDELETE FROM product WHERE …. DELETEremoves rows,DROP TABLEremoves the table. AfterDELETE FROM productthe empty table is still there.- Test a risky delete first as a
SELECTwith the sameWHERE, and check which rows it shows.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ลบสินค้าที่มีรหัส 'P03' ออกจากตาราง product (หลังจากนั้นจะมีเพียง P01 และ P02 เหลืออยู่)
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Delete สามารถใช้เงื่อนไขใดๆ ได้ ลบสินค้าทุกชิ้นที่มีราคา ต่ำกว่า 2.00 ด้วย WHERE price < 2.00. มีเพียงปากกา (0.80) ที่หายไป; ตรวจสอบว่าเหลือแถวอะไรบ้าง
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ลบทุกการสั่งซื้อที่ทำ ก่อน '2024-03-01' วันที่เขียนในรูปแบบ 'YYYY-MM-DD' เปรียบเทียบได้ถูกต้องกับ <
คลิก Run เพื่อดูผลลัพธ์ที่นี่