Thuta Learning
SQL
IntermediateData & Databasesအခြေခံ

DELETE

SQLသင်ခန်းစာ 1425 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • DELETE FROM ... WHERE နဲ့ လိုချင်တဲ့ row တွေကိုပဲ ဖျက်နိုင်ဖို့
  • SELECT preview, BEGIN, RETURNING, DELETE n စစ်ခြင်း, COMMIT/ROLLBACK ဆိုတဲ့ လုံခြုံတဲ့ အစီအစဉ်ကို လိုက်နာနိုင်ဖို့
  • Foreign key က ဆက်စပ် row ရှိနေတဲ့ parent row ကို ဖျက်ခွင့် မပေးပုံ ရှင်းပြနိုင်ဖို့
  • DELETE, TRUNCATE, DROP TABLE နဲ့ soft delete (deleted_at) ကြား ရွေးချယ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

DELETE က စာရင်းစာအုပ်ထဲက line တွေကို ဖြုတ်ထုတ်ပစ်တာပါ။ Table ကိုယ်တိုင်နဲ့ column တွေက ကျန်ပြီး WHERE နဲ့ ကိုက်တဲ့ row တွေပဲ ပျောက်ပါတယ်။ Cancel ဖြစ်သွားတဲ့ order၊ spam comment၊ သက်တမ်းကုန်တဲ့ login session တွေ ရှင်းတာမျိုးမှာ သုံးပါတယ်။

UPDATE လိုပဲ WHERE က သက်ကယ်ပါ: DELETE FROM customers; လို့ WHERE မပါဘဲ run ရင် customer အားလုံး ပျောက်ပါတယ်။ Recycle bin လည်း မရှိပါဘူး — COMMIT ပြီးသွားရင် နာရီပိုင်းလောက် ဟောင်းနေနိုင်တဲ့ backup ကနေ ပြန်ယူမှပဲ ရပါတော့တယ်။

Problem → Why → Solution

ပြဿနာ: spam ဖျက်ရင်း comment အားလုံး ပျောက်သွားတယ်

Moderator တစ်ယောက်က spam comment တွေ ရှင်းဖို့ query ရေးပါတယ်။ ပထမ version မှာ condition နှစ်ခုကြား AND အစား OR ရေးမိလို့ မနေ့က ရေးထားတဲ့ ပုံမှန် comment တွေပါ ပျောက်ပါတယ်။ ဒုတိယ version မှာတော့ ကော်ပီကူးရင်း semicolon က WHERE ရှေ့ ရောက်သွားလို့ comment အားလုံး ပျောက်ပြီး WHERE line က သီးခြား syntax error အဖြစ်ပဲ ကျန်ပါတယ်။

spam-cleanup-bugs.sql
-- 1. OR instead of AND: every old comment AND every spam comment
DELETE FROM comments WHERE is_spam OR created_at < '2026-06-01';

-- 2. Stray semicolon: DELETE runs with no WHERE at all
DELETE FROM comments;
WHERE is_spam AND created_at < '2026-06-01';   -- syntax error, too late

ဘာကြောင့် ပြန်ကယ်လို့ မရတာလဲ

Autocommit mode မှာ statement တစ်ခု ပြီးတာနဲ့ COMMIT ဖြစ်သွားပြီး SQL မှာ "undo" command မရှိပါဘူး။ DELETE က WHERE ကို ဘယ်လို ရေးထားလဲ စစ်မပေးပါဘူး၊ ကိုက်တဲ့ row အားလုံးကို ဖျက်ရုံပါပဲ။ ဒါကြောင့် WHERE ကို SELECT နဲ့ အရင် စမ်းကြည့်ပြီး row အရေအတွက်ကို မှတ်၊ BEGIN ထဲမှာ DELETE ... RETURNING run၊ DELETE n က ကိုက်မှ COMMIT လုပ်တဲ့ အစီအစဉ်က ဒီလို အမှားနှစ်ခုလုံးကို ဖမ်းပါတယ်။

Core concept: WHERE နဲ့ ကိုက်တဲ့ row တွေ ပျောက်တယ်

DELETE FROM table WHERE condition က UPDATE လိုပဲ အရင် WHERE နဲ့ row တွေကို ရှာပြီး ရှာတွေ့တဲ့ row အားလုံးကို ဖျက်ပါတယ်။ ဒါကြောင့် SELECT * FROM table WHERE condition က ဘာတွေ ပျောက်မလဲ ဆိုတဲ့ တိကျတဲ့ preview ပါ။ WHERE ကို primary key နဲ့ ရေးရင် row တစ်ကြောင်းထက် ပိုမပျောက်ပါဘူး။ Row ဘယ်နှကြောင်းပဲ ကိုက်ကိုက် statement တစ်ခုက atomic ဖြစ်လို့ constraint error တစ်ခု တက်ရင် ဘာမှ မပျက်ပါဘူး။ ကိုက်တဲ့ row မရှိရင် error မတက်ဘဲ DELETE 0 ပဲ ပြပါတယ်။

Foreign key တွေက DELETE ကို ကာကွယ်ပါတယ်။ orders.customer_id က customers ကို REFERENCES လုပ်ထားရင် order ရှိနေတဲ့ customer ကို ဖျက်ဖို့ ကြိုးစားတာက "violates foreign key constraint" error နဲ့ ကျရှုံးပါတယ် — order တွေ ပိုင်ရှင်မဲ့ မဖြစ်အောင်ပါ။ Table ဖန်တီးတုန်းက ON DELETE CASCADE ရေးထားရင်တော့ parent ကို ဖျက်တာနဲ့ child row တွေပါ လိုက်ပျောက်ပါတယ်၊ ဒါက အဆင်ပြေသလောက် အန္တရာယ်လည်း များပါတယ်။

RETURNING (PostgreSQL) က ဖျက်လိုက်တဲ့ row တွေကို ပြန်ပေးလို့ log ရေးဖို့၊ archive table ထဲ ရွှေ့ဖို့ ဒါမှမဟုတ် ကိုယ်ဖျက်လိုက်တာ မှန်မမှန် မျက်စိနဲ့ စစ်ဖို့ သုံးပါတယ်။ BEGIN နဲ့ တွဲရင် COMMIT မလုပ်ခင် ROLLBACK နဲ့ ပြန်ယူလို့ ရပါတယ်။ SQL Server မှာ OUTPUT clause၊ SQLite မှာ RETURNING ရှိပေမဲ့ MySQL မှာ DELETE ... RETURNING မရှိလို့ အဲဒီမှာ preview SELECT ကို run ပြီး သူ့ output ကို သိမ်းထားရပါတယ်။

PostgreSQL မှာ DELETE က row ကို disk ပေါ်ကနေ ချက်ချင်း မဖယ်ပါဘူး၊ "dead" လို့ အမှတ်အသား လုပ်ပြီး နောက်မှ VACUUM က အဲဒီ နေရာကို ပြန်သုံးလို့ ရအောင် လုပ်ပေးပါတယ်။ ဒါကြောင့် row သန်းချီ ဖျက်ပြီးတာနဲ့ file size ချက်ချင်း မကျပါဘူး။ ပုံမှန်အားဖြင့် autovacuum က နောက်ကွယ်မှာ ဒီအလုပ်ကို လုပ်ပေးပြီး table ကို exclusive lock နဲ့ ပြန်ရေးတဲ့ VACUUM FULL ကမှသာ နေရာကို operating system ဆီ တကယ် ပြန်ပေးပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: ဖျက်ခင် ရေတွက်
  1. SELECT နဲ့ ပျောက်မယ့် row ကြည့်
  2. Row အရေအတွက် မှတ်
  3. BEGIN; DELETE ... RETURNING
  4. DELETE n နဲ့ မှတ်ထားတာ တိုက်
  5. COMMIT ဒါမှမဟုတ် ROLLBACK

COMMIT ပြီးရင် backup ကလွဲလို့ ပြန်ယူနည်း မရှိပါဘူး။

အထွေထွေ ပုံစံ

sql
SELECT count(*) FROM table_name WHERE condition;   -- preview

BEGIN;
DELETE FROM table_name
WHERE condition
RETURNING key_column, other_column;
COMMIT;   -- or ROLLBACK; if DELETE n is not what you expected

DELETE မှာ column list မရှိပါဘူး၊ row တစ်ကြောင်းလုံး ပျောက်လို့ပါ။ Preview SELECT နဲ့ DELETE က WHERE တူတူ သုံးရပါမယ်။ RETURNING နဲ့ BEGIN/COMMIT က optional ဖြစ်ပေမဲ့ production data အတွက် အမြဲ သုံးသင့်ပါတယ်။

Example: customer တစ်ယောက်ကို လုံခြုံစွာ ဖျက်

customers table မှာ လေးယောက် ရှိပြီး orders table က customer 1 နဲ့ 2 ကို ညွှန်ထားပါတယ်။ Around the Horn (id 4) မှာ order မရှိလို့ ဖျက်လို့ ရပါတယ်။ Example က preview SELECT၊ transaction ထဲက DELETE ... RETURNING နဲ့ COMMIT ဆိုတဲ့ အစီအစဉ် တစ်ခုလုံးကို ပြပါတယ်။

CREATE TABLE customers (
  customer_id   integer PRIMARY KEY,
  customer_name text NOT NULL,
  country       text
);
INSERT INTO customers VALUES
  (1, 'Alfreds Futterkiste', 'Germany'),
  (2, 'Ana Trujillo',        'Mexico'),
  (3, 'Antonio Moreno',      'Mexico'),
  (4, 'Around the Horn',     'UK');

CREATE TABLE orders (
  order_id    integer PRIMARY KEY,
  customer_id integer NOT NULL REFERENCES customers (customer_id),
  total       numeric(8, 2) NOT NULL
);
INSERT INTO orders VALUES (101, 1, 45.00), (102, 2, 19.90), (103, 1, 249.00);
delete.sql
-- 1. Preview: exactly the rows the DELETE will remove
SELECT customer_id, customer_name
FROM customers
WHERE customer_id = 4;

-- 2. Delete inside a transaction and show what was removed
BEGIN;
DELETE FROM customers
WHERE customer_id = 4
RETURNING customer_id, customer_name;
COMMIT;

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. PreviewSELECT customer_id, customer_name

    DELETE မှာ သုံးမယ့် WHERE customer_id = 4 နဲ့ SELECT ကို အရင် run လို့ Around the Horn တစ်ယောက်တည်း ပျောက်မယ်ဆိုတာ ကြိုသိပါတယ်။

  2. BEGINBEGIN;

    Transaction ဖွင့်လိုက်လို့ COMMIT မလုပ်မချင်း ROLLBACK နဲ့ ပြန်ယူခွင့် ရှိပါတယ်။ psql က BEGIN လို့ ပြန်ဖြေပါတယ်။

  3. DELETE ... WHEREDELETE FROM customers WHERE customer_id = 4

    Primary key နဲ့ ဖျက်လို့ အများဆုံး row တစ်ကြောင်းပဲ ပျောက်နိုင်ပါတယ်။ customer 4 မှာ order မရှိလို့ foreign key က တားမှာ မဟုတ်ပါဘူး။

  4. RETURNING and COMMITRETURNING customer_id, customer_name;

    ဖျက်လိုက်တဲ့ row ကို ပြန်ပြပြီး psql က DELETE 1 လို့ ဆိုပါတယ်။ Preview နဲ့ ကိုက်လို့ COMMIT လုပ်ပြီး ပြောင်းလဲမှုကို အတည်ပြုပါတယ်။

You should see
 customer_id |  customer_name
-------------+-----------------
           4 | Around the Horn
(1 row)

BEGIN
 customer_id |  customer_name
-------------+-----------------
           4 | Around the Horn
(1 row)

DELETE 1
COMMIT

Foreign key က ဖျက်ခွင့် မပေးတဲ့အခါ

Alfreds Futterkiste (id 1) မှာ order နှစ်ခု ရှိပါတယ်။ သူ့ကို ဖျက်ဖို့ ကြိုးစားရင် PostgreSQL က error ပေးပြီး row တစ်ကြောင်းမှ မပျောက်ပါဘူး။ ရွေးစရာ သုံးခု ရှိပါတယ်: child row တွေကို အရင် ဖျက်ပါ၊ foreign key ကို ON DELETE CASCADE နဲ့ သတ်မှတ်ပါ (child တွေ အလိုလို ပျောက်)၊ ဒါမှမဟုတ် customer ကို လုံးဝ မဖျက်ဘဲ soft delete လုပ်ပါ။

fk-blocks.sql
-- ERROR: update or delete on table "customers" violates
--        foreign key constraint "orders_customer_id_fkey" on table "orders"
-- DELETE FROM customers WHERE customer_id = 1;

-- No matching row is not an error: 0 rows come back (DELETE 0)
DELETE FROM customers WHERE customer_id = 99 RETURNING customer_id;

DELETE, TRUNCATE, DROP နဲ့ soft delete

Commandဘာဖြစ်လဲဘယ်အချိန် သုံးလဲ
DELETE ... WHEREကိုက်တဲ့ row တွေကိုပဲ ဖျက်၊ RETURNING နဲ့ trigger တွေ အလုပ်လုပ်Row တချို့ကို ရွေးဖျက်တဲ့အခါ
TRUNCATERow အားလုံးကို အမြန်ဆုံး ရှင်း၊ WHERE မရှိStaging / test table ကို အလုံးစုံ ရှင်းတဲ့အခါ
DROP TABLETable ကိုယ်တိုင်နဲ့ structure ပါ ပျောက်Table ကို လုံးဝ မလိုတော့တဲ့အခါ
Soft deleteUPDATE ... SET deleted_at = now(); row ကျန်History, audit, undo လိုတဲ့ data

Soft delete မှာ query တိုင်းက WHERE deleted_at IS NULL ကို မမေ့ဘဲ ထည့်ရပါတယ်၊ မမေ့အောင် view တစ်ခု ဖန်တီးထားလေ့ ရှိပါတယ်။ PostgreSQL မှာ data modifying CTE နဲ့ row တွေကို archive table ဆီ statement တစ်ခုတည်းနဲ့ ရွှေ့လို့ ရပါတယ် (CTE ကို နောက် lesson မှာ အသေးစိတ် လေ့လာပါမယ်)။

archive-spam.sql
WITH moved AS (
  DELETE FROM comments
  WHERE is_spam
  RETURNING *
)
INSERT INTO comments_archive
SELECT * FROM moved;

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

User account တွေ၊ order တွေ၊ invoice တွေလို စီးပွားရေး data ကို team အများစုက hard delete မလုပ်ပါဘူး: deleted_at column နဲ့ soft delete လုပ်ပြီး accounting နဲ့ audit အတွက် သိမ်းထားပါတယ်။ GDPR လို privacy ဥပဒေကြောင့် user က account ဖျက်ခိုင်းရင်တော့ ကိုယ်ရေးကိုယ်တာ column တွေကို ဖျက် ဒါမှမဟုတ် anonymize လုပ်ရပါတယ်၊ ဒါကြောင့် "ဘာကို ဘယ်တော့ တကယ် ဖျက်မလဲ" ဆိုတာ design ဆုံးဖြတ်ချက်တစ်ခုပါ။

Login session, password reset token, job log လို ယာယီ data တွေကိုတော့ scheduled job တစ်ခုက ညတိုင်း DELETE FROM sessions WHERE expires_at < now() နဲ့ ရှင်းပါတယ်။ Row သန်းချီ ရှိရင် တစ်ခါတည်း ဖျက်လိုက်တာက lock ကြာပြီး WAL အများကြီး ထုတ်လို့ id range အလိုက် ထောင်ဂဏန်း batch လေးတွေ ခွဲ ဖျက်ကြပါတယ်။ Data အရမ်းများရင် table ကို ရက်စွဲအလိုက် partition လုပ်ပြီး partition အဟောင်း တစ်ခုလုံးကို drop လုပ်တာက DELETE ထက် အဆပေါင်းများစွာ မြန်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. AND နေရာမှာ OR ရေးတာ

"Spam ဖြစ်ပြီး ရက်ဟောင်း" ကို WHERE is_spam OR created_at < '2026-06-01' လို့ ရေးမိရင် spam အားလုံးနဲ့ ရက်ဟောင်း comment အားလုံး ပျောက်ပါတယ်။ DELETE အတွက် OR က row တွေကို ပိုများစေတာကို သတိရပြီး preview SELECT ရဲ့ count ကို ကြည့်ပါ: spam နှစ်ခု ဖျက်မယ် ထင်ထားပြီး count က 4 ဆိုရင် ရပ်ပါ။

2. CASCADE ကို မသိဘဲ parent row ဖျက်တာ

Foreign key က ON DELETE CASCADE ဖြစ်နေရင် customer တစ်ယောက် ဖျက်တာနဲ့ သူ့ order၊ payment၊ review တွေ အကုန် လိုက်ပျောက်ပြီး RETURNING ကလည်း customers row ကိုပဲ ပြပါတယ်။ Parent table က row ကို မဖျက်ခင် schema ကို စစ်ပါ (psql မှာ \d orders)။

3. Table ရှင်းချင်လို့ WHERE မပါတဲ့ DELETE ကို production မှာ run တာ

Staging table ရှင်းဖို့ ရည်ရွယ်တဲ့ DELETE FROM orders; ကို connection မှားပြီး production မှာ run မိတာက တကယ် ဖြစ်တတ်ပါတယ်။ psql prompt မှာ database နာမည်ကို စစ်ပါ၊ production မှာ read-only role နဲ့ ဝင်ပါ၊ table တစ်ခုလုံး ရှင်းဖို့ ရည်ရွယ်ရင် TRUNCATE ကို ထင်ထင်ရှားရှား ရေးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • DELETE မရေးခင် WHERE ကို SELECT count(*) နဲ့ အရင် စမ်းပါ။
  • Production data ကို BEGIN ထဲမှာပဲ ဖျက်ပြီး RETURNING နဲ့ DELETE n ကို စစ်ပါ။
  • History လိုနိုင်တဲ့ business data ကို soft delete လုပ်ပါ။
  • Data အများကြီးကို batch လေးတွေ ခွဲ ဖျက်ပြီး backup restore ကို ကြိုစမ်းထားပါ။

Mini challenge

Mini challenge: spam comment ဟောင်းတွေ ရှင်း

comments table ကနေ 2026-06-01 မတိုင်ခင် ရေးခဲ့တဲ့ spam comment တွေကို ဖျက်ပါ။ ပုံမှန် comment တွေနဲ့ ဇွန်လ spam (cheap4u) ကတော့ ကျန်ရပါမယ်။

  • DELETE မလုပ်ခင် ပျောက်မယ့် row အရေအတွက်ကို SELECT count(*) နဲ့ ပြရမယ် (2 ဖြစ်ရမယ်)
  • DELETE ကို BEGIN / COMMIT ထဲမှာ run ရမယ်
  • RETURNING နဲ့ comment_id နဲ့ author ကို ပြရမယ်
  • နောက်ဆုံး SELECT က ကျန်တဲ့ comment သုံးခုကို ပြရမယ်
spam-cleanup.sql
CREATE TABLE comments (
  comment_id integer PRIMARY KEY,
  author     text NOT NULL,
  is_spam    boolean NOT NULL,
  created_at timestamp NOT NULL
);
INSERT INTO comments VALUES
  (1, 'mya',     false, '2026-05-02 09:00'),
  (2, 'promo99', true,  '2026-05-03 10:00'),
  (3, 'kyaw',    false, '2026-05-20 18:30'),
  (4, 'promo99', true,  '2026-05-28 07:15'),
  (5, 'cheap4u', true,  '2026-06-03 12:00');

-- TODO 1: preview: how many spam comments are older than 2026-06-01?

-- TODO 2: in a transaction, delete exactly those rows
--         and return comment_id, author

-- TODO 3: list what is left, ordered by comment_id

ရလဒ်: Preview: 2။ DELETE က (2, promo99) နဲ့ (4, promo99) ကို ပြန်ပေးပြီး DELETE 2။ ကျန်တာ: 1 mya, 3 kyaw, 5 cheap4u။

အရိပ်အမြွက် ကြည့်ရန်

Condition နှစ်ခုလုံး မှန်ရမှာမို့ AND ကို သုံးပါ: is_spam AND created_at < '2026-06-01'။ Preview, DELETE နဲ့ စစ်ဆေးချက် အားလုံးမှာ WHERE တူတူ ကော်ပီကူးသုံးပါ။

အဖြေ ကြည့်ရန်
spam-cleanup.sql
SELECT count(*)
FROM comments
WHERE is_spam AND created_at < '2026-06-01';

BEGIN;
DELETE FROM comments
WHERE is_spam AND created_at < '2026-06-01'
RETURNING comment_id, author;
COMMIT;

SELECT comment_id, author, is_spam
FROM comments
ORDER BY comment_id;

Quick Quiz 1 / 3

DELETE FROM customers WHERE customer_id = 99; မှာ id 99 မရှိရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

orders.customer_id က ON DELETE ဘာမှ မရေးထားဘဲ customers ကို REFERENCES လုပ်ထားတယ်။ Order ရှိတဲ့ customer ကို ဖျက်ရင် ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

BEGIN; DELETE FROM comments WHERE is_spam OR created_at < '2026-06-01'; ပြီး DELETE 4 ပြတယ်။ Spam နှစ်ခုပဲ ဖျက်ဖို့ ရည်ရွယ်ခဲ့ရင် ဘာလုပ်ရမလဲ။

မှတ်ထားရမယ့် အချက်များ

  • DELETE FROM ... WHERE က ကိုက်တဲ့ row တွေကို ဖျက်တယ်၊ WHERE မပါရင် အားလုံး၊ undo မရှိ။
  • WHERE တူတူနဲ့ SELECT preview → BEGIN → DELETE ... RETURNING → DELETE n စစ် → COMMIT/ROLLBACK။
  • Foreign key က child ရှိတဲ့ parent ကို ဖျက်ခွင့် မပေးဘူး၊ CASCADE က child တွေပါ ဖျက်တယ်။
  • Table တစ်ခုလုံး ရှင်းရင် TRUNCATE၊ history လိုရင် soft delete (deleted_at)။

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

DELETE | Thuta Learning