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

DROP TABLE

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

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

  • DROP TABLE, TRUNCATE, DELETE သုံးမျိုးက ဘာကို ဖျက်ပြီး ဘာကို ချန်ထားလဲ ခွဲခြားနိုင်ဖို့
  • IF EXISTS ကို သုံးပြီး ထပ်ခါ run လို့ရတဲ့ cleanup script ရေးနိုင်ဖို့
  • Foreign key ဒါမှမဟုတ် view က မှီခိုနေတဲ့ table ကို PostgreSQL က ဘာကြောင့် ငြင်းလဲ၊ CASCADE က တကယ် ဘာဖျက်လဲ ရှင်းပြနိုင်ဖို့
  • BEGIN ... ROLLBACK ထဲမှာ DROP TABLE ကို အရင်စမ်းပြီး ရလဒ်ကို စစ်နိုင်ဖို့

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

DROP TABLE ကို ရိုးရိုးလေး ပြောရရင်

DROP TABLE က table တစ်ခုကို database ထဲကနေ လုံးဝ ဖယ်ရှားပါတယ်။ Row တွေတင် မဟုတ်ပါဘူး — column တွေ၊ index တွေ၊ constraint တွေ၊ permission တွေ အကုန် ပါသွားပါတယ်။ Statement ပြီးသွားတာနဲ့ အဲဒီ table ကို ဘယ် query ကမှ ရှာမတွေ့တော့ပါဘူး။

ဗီရိုထဲက ဖိုင်တွဲ တစ်ခုကို စဉ်းစားကြည့်ပါ။ DELETE က ဖိုင်တွဲထဲက စာရွက်တချို့ ဒါမှမဟုတ် အားလုံးကို ထုတ်ပစ်တာပါ။ TRUNCATE က စာရွက်အားလုံးကို တစ်ခါတည်း သွန်ပြီး ဖိုင်တွဲအလွတ်ကို ချန်ထားတာပါ။ DROP TABLE ကတော့ ဖိုင်တွဲကိုယ်တိုင်ကို ဗီရိုထဲက ထုတ်ပြီး အမှိုက်ပုံးထဲ ပစ်လိုက်တာပါ။

Problem → Why → Solution

ပြဿနာ: ဖျက်ချင်တဲ့ table ကို တခြား object တွေ မှီခိုနေတယ်

Test data တွေ ရှင်းဖို့ departments table ကို ဖျက်ချင်ပါတယ်။ DROP TABLE departments လို့ ရိုက်လိုက်တော့ PostgreSQL က ငြင်းပါတယ်: employees table ရဲ့ foreign key နဲ့ employee_directory view က အဲဒီ table ကို မှီခိုနေလို့ပါ။ Error message ရဲ့ HINT က CASCADE သုံးဖို့ အကြံပေးပါတယ်။

အဲဒီ HINT ကို မစဉ်းစားဘဲ လိုက်လုပ်တာက ပြဿနာ အစစ်ပါ။ CASCADE က departments နဲ့အတူ view ကိုလည်း ဖျက်ပြီး employees ပေါ်က foreign key rule ကိုပါ ဖြုတ်ပစ်ပါတယ်။ ဘာတွေ ပါသွားလဲဆိုတာ NOTICE line တွေထဲမှာပဲ ပြောပြီး ဘယ်သူမှ မဖတ်မိတတ်ပါဘူး။

drop-departments.sql
DROP TABLE departments;

-- ERROR:  cannot drop table departments because other objects depend on it
-- DETAIL:  constraint employees_department_id_fkey on table employees
--          depends on table departments
--          view employee_directory depends on table departments
-- HINT:  Use DROP ... CASCADE to drop the dependent objects too.

PostgreSQL ဘာကြောင့် ငြင်းတာလဲ

DROP TABLE ရဲ့ default က RESTRICT ဖြစ်ပါတယ်: table ကို တခြား object တစ်ခုခု မှီခိုနေရင် ဘာကိုမှ မဖျက်ဘဲ ရပ်ပါတယ်။ ဒါက error မဟုတ်ဘဲ အကာအကွယ်ပါ — သင် မသိသေးတဲ့ dependency တွေကို ပြပေးနေတာပါ။ CASCADE ကို ရေးမယ်ဆိုရင် DETAIL ထဲက object တိုင်း ပျောက်သွားမှာကို သိပြီးမှ ရွေးရပါမယ်။

Core concept: ဘာကို ဖျက်ပြီး ဘာကို ချန်ထားလဲ

Data ဖယ်ရှားတဲ့ command သုံးခုက ရည်ရွယ်ချက် မတူပါဘူး။ မှန်တာကို ရွေးဖို့ 'နောက်ဆုံးမှာ table ကိုယ်တိုင် ကျန်နေသေးဖို့ လိုသလား' နဲ့ 'row တချို့ပဲ ဖယ်ချင်တာလား' ဆိုတဲ့ မေးခွန်းနှစ်ခုကို မေးပါ။

Commandဖျက်တာချန်ထားတာ
DELETE FROM t WHERE ...WHERE နဲ့ ကိုက်တဲ့ row တွေTable, row ကျန်တွေ, structure
TRUNCATE tRow အားလုံး (မြန်မြန်)Table ပုံစံ, index, permission
DROP TABLE tRow အားလုံးနဲ့ table ကိုယ်တိုင်ဘာမှ မကျန်

DROP TABLE မှာ option သုံးခု အရေးကြီးပါတယ်။ IF EXISTS က table မရှိရင် error အစား NOTICE ပဲ ပေးလို့ script ကို နှစ်ခါ run လည်း ပြဿနာ မရှိပါဘူး။ RESTRICT (default) က မှီခိုနေတဲ့ object ရှိရင် ငြင်းပါတယ်။ CASCADE က မှီခိုနေတဲ့ view တွေကို ဖျက်ပြီး တခြား table ပေါ်က foreign key constraint တွေကို ဖြုတ်ပါတယ်၊ ဒါပေမဲ့ အဲဒီ table တွေကိုယ်တိုင်နဲ့ သူတို့ row တွေကိုတော့ မဖျက်ပါဘူး။ DROP TABLE a, b, c လို့ table အများကြီးကို တစ်ခါတည်း ဖျက်လို့လည်း ရပါတယ်။

PostgreSQL မှာ table ကို owner, schema owner ဒါမှမဟုတ် superuser ပဲ ဖျက်ခွင့်ရှိပါတယ်၊ ဒါကြောင့် app ရဲ့ login role ကို table owner မဖြစ်စေတာက ကာကွယ်မှု တစ်ခုပါ။ DDL က transactional ဖြစ်လို့ BEGIN ထဲမှာ DROP TABLE ကို run ပြီး COMMIT မလုပ်ခင် ROLLBACK နဲ့ ပြန်ယူလို့ ရပါတယ်။ COMMIT ပြီးသွားရင်တော့ recycle bin မရှိပါဘူး — backup (pg_dump ဒါမှမဟုတ် point-in-time recovery) ကနေပဲ ပြန်ယူနိုင်ပါတယ်။ Oracle မှာတော့ PURGE မပါရင် recycle bin ထဲ ရောက်သွားတာမို့ database အလိုက် ကွဲပါတယ်။

Drop မလုပ်ခင် dependency တွေကို ရှာပါ

IF EXISTS မပါ၊ CASCADE မပါတဲ့ DROP TABLE ကို transaction ထဲမှာ run ကြည့်တာက dependency စာရင်းကို ရဖို့ အလွယ်ဆုံးနည်းပါ — fail ရင် DETAIL က object တိုင်းကို ပြောပြပါတယ်။ psql ထဲမှာ \d+ table_name ကလည်း ဒီ table ကို ညွှန်နေတဲ့ foreign key တွေကို 'Referenced by' အောက်မှာ ပြပေးပါတယ်။

DROP TABLE မတိုင်ခင် စစ်ဆေးချက်
  1. Table ကိုယ်တိုင် မလိုတော့တာ သေချာလား
  2. ဘယ် object တွေ မှီခိုနေလဲ ကြည့်
  3. Backup ရှိကြောင်း အတည်ပြု
  4. BEGIN ထဲ drop, စစ်, ပြီးမှ COMMIT

Row တွေပဲ ဖယ်ချင်ရင် DROP မလိုပါဘူး၊ TRUNCATE ဒါမှမဟုတ် DELETE က table ပုံစံနဲ့ permission တွေကို ချန်ထားပေးပါတယ်။

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

sql
DROP TABLE [IF EXISTS] table_name [, other_table ...] [RESTRICT | CASCADE];

-- keep the table, remove every row
TRUNCATE TABLE table_name [RESTART IDENTITY] [CASCADE];

-- keep the table, remove some rows
DELETE FROM table_name WHERE condition;

Square bracket ထဲက အပိုင်းတွေက optional ပါ။ RESTRICT က default ဖြစ်လို့ မရေးလည်း ရပါတယ်၊ CASCADE ကတော့ တမင်ရည်ရွယ်ပြီး ရေးမှသာ အလုပ်လုပ်ပါတယ်။

Worked example: staging table နဲ့ employees ကို ဖျက်ခြင်း

Database ထဲမှာ departments, employees နဲ့ import တစ်ခုက ကျန်ခဲ့တဲ့ import_staging table တွေ ရှိပါတယ်။ employees က departments ကို foreign key နဲ့ ညွှန်ပေမဲ့ employees ကို ဘယ်သူမှ မညွှန်ပါဘူး၊ ဒါကြောင့် RESTRICT နဲ့တောင် ဖျက်လို့ ရပါတယ်။ နောက်ဆုံး query က ဘယ် table တွေ ကျန်လဲ ပြပါတယ်။

drop-employees.sql
-- Remove a leftover staging table; no error if it is already gone
DROP TABLE IF EXISTS import_staging;

-- Remove the employees table: its structure and all of its rows
DROP TABLE employees;

-- Which tables are left?
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;

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

  1. Idempotent cleanupDROP TABLE IF EXISTS import_staging;

    Table ရှိရင် ဖျက်ပြီး မရှိရင် NOTICE ပဲ ပေးပါတယ်။ Deploy script ကို နှစ်ခါ run မိလည်း ဒုတိယအကြိမ်က fail မဖြစ်ပါဘူး။

  2. Drop with the default RESTRICTDROP TABLE employees;

    Row သုံးကြောင်း၊ primary key index နဲ့ departments ကို ညွှန်တဲ့ foreign key constraint အားလုံး ပါသွားပါတယ်။ departments table ကိုတော့ မထိပါဘူး၊ constraint က employees ပေါ်မှာ ရှိတာမို့ပါ။

  3. Confirm what is leftFROM information_schema.tables

    public schema ထဲက table နာမည်တွေကို ပြန်ဖတ်ပါတယ်။ departments တစ်ခုပဲ ကျန်ပါတယ်။ psql မှာဆိုရင် \dt နဲ့လည်း ကြည့်လို့ ရပါတယ်။

You should see
table_name
-----------
departments

Transaction ထဲမှာ DROP ကို အရင်စမ်းခြင်း

CASCADE က ဘာတွေ ယူသွားမလဲ မသေချာရင် PostgreSQL ရဲ့ transactional DDL ကို သုံးပြီး စမ်းကြည့်ပါ။ BEGIN ပြီးနောက် DROP ကို run၊ ကျန်နေတဲ့ object တွေကို query လုပ်၊ ပြီးရင် ROLLBACK နဲ့ အကုန် ပြန်ထားပါ။ အောက်က script မှာ transaction ထဲက ပထမ SELECT က employees, import_staging ကိုပဲ ပြပြီး ROLLBACK ပြီးနောက် SELECT က table သုံးခုနဲ့ view ကို ပြန်ပြပါတယ်။

rehearse-cascade.sql
BEGIN;
DROP TABLE departments CASCADE;  -- also drops the view and the FK

SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;

ROLLBACK;  -- nothing was really dropped

SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;

Lock ကို သတိထားပါ

Transaction ဖွင့်ထားသရွေ့ DROP TABLE က ACCESS EXCLUSIVE lock ကို ကိုင်ထားလို့ တခြား session တွေက အဲဒီ table ကို ဖတ်ဖို့တောင် စောင့်နေရပါတယ်။ Production မှာ rehearse လုပ်မယ်ဆိုရင် မြန်မြန် ROLLBACK ဒါမှမဟုတ် COMMIT လုပ်ပါ၊ ပိုကောင်းတာက staging copy ပေါ်မှာ စမ်းပါ။

TRUNCATE ကလည်း foreign key ကို လေးစားပါတယ်: employees က departments ကို ညွှန်နေရင် TRUNCATE departments က cannot truncate a table referenced in a foreign key constraint ဆိုပြီး fail ပါတယ်။ TRUNCATE employees, departments လို့ နှစ်ခုလုံးကို တစ်ပြိုင်တည်း ရှင်းရပါမယ်။ RESTART IDENTITY ထည့်ရင် identity column counter တွေကိုလည်း 1 ကနေ ပြန်စပါတယ်။

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

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

Production မှာ DROP TABLE ကို ရှားရှားပါးပါးပဲ သုံးပြီး သုံးတဲ့အခါလည်း review လုပ်ထားတဲ့ migration ထဲမှာပဲ သုံးပါတယ်။ Feature တစ်ခု ပိတ်လိုက်လို့ table မလိုတော့ရင် team အများစုက ချက်ချင်း မဖျက်ပါဘူး — feature_x_archived လို နာမည်ပြောင်း ဒါမှမဟုတ် archive schema ဆီ ရွှေ့ပြီး release တစ်ခု နှစ်ခုကြာမှ ဘယ်သူမှ မသုံးကြောင်း သေချာတော့ ဖျက်ပါတယ်။

  • ETL နဲ့ data import job တွေက staging table တွေကို run တိုင်း DROP TABLE IF EXISTS နဲ့ ရှင်းပြီး ပြန်ဆောက်ပါတယ်၊ ဒါမှမဟုတ် session ပြီးရင် အလိုလို ပျောက်တဲ့ CREATE TEMP TABLE ကို သုံးပါတယ်။
  • Automated test suite တွေက test တစ်ခုချင်းစီ ပြီးတိုင်း TRUNCATE ... RESTART IDENTITY CASCADE နဲ့ table တွေကို ရှင်းလေ့ ရှိပါတယ်၊ table ပုံစံကို ပြန်ဆောက်တာထက် အများကြီး မြန်လို့ပါ။
  • Log ဒါမှမဟုတ် event data ကို လအလိုက် partition ခွဲထားရင် data အဟောင်းကို DELETE နဲ့ မဖျက်ဘဲ partition အဟောင်းကို DROP TABLE လုပ်ပါတယ်၊ row သန်းချီကို ချက်ချင်း ဖယ်ရှားပေးပါတယ်။

အမှားများ

DROP TABLE နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ

Row တွေပဲ ဖယ်ချင်တာကို DROP သုံးခြင်း

DROP ပြီး CREATE TABLE ပြန်ရေးရင် GRANT တွေ၊ index တွေ၊ trigger တွေ၊ comment တွေကို မေ့ကျန်ခဲ့တတ်ပါတယ်။ Table က ဆက်လိုအပ်နေရင် TRUNCATE ဒါမှမဟုတ် DELETE ကို သုံးပါ၊ table ပုံစံအကုန် ကျန်ပါတယ်။

HINT ကို ဖတ်ပြီး CASCADE ကို မျက်စိမှိတ် ထည့်ခြင်း

CASCADE က report တွေ မှီခိုနေတဲ့ view တွေကို ဖျက်ပြီး တခြား table ပေါ်က foreign key ကို ဖြုတ်ပါတယ်။ နောက်ပိုင်း orphan row တွေ ဝင်လာလည်း ဘယ်သူမှ မတားတော့ပါဘူး။ DETAIL ထဲက object စာရင်းကို ဖတ်ပြီး dependency တွေကို တစ်ခုချင်း ကိုယ်တိုင် ဖြုတ်တာက ပိုလုံခြုံပါတယ်။

Database/session မှားပြီး run ခြင်း

Terminal နှစ်ခု ဖွင့်ထားရင် staging လို့ ထင်ပြီး production မှာ DROP TABLE run မိတာ အဖြစ်များပါတယ်။ SELECT current_database(); နဲ့ အရင်စစ်ပါ၊ production role ကို table owner မဖြစ်စေပါနဲ့။

Mini challenge

Mini challenge: departments table ကို အနားပေးပါ

Database ထဲမှာ departments, employees (departments ကို foreign key နဲ့ ညွှန်), employee_directory view နဲ့ import_staging table ရှိပါတယ်။ CASCADE မသုံးဘဲ dependency တွေကို ကိုယ်တိုင် ဖြုတ်ပြီး departments ကို ဖျက်ပါ။ Employees row တွေကတော့ ကျန်ရပါမယ်။

  • အားလုံးကို BEGIN ... COMMIT တစ်ခုထဲမှာ လုပ်ပါ
  • import_staging နဲ့ မရှိတဲ့ import_staging_old ကို statement တစ်ကြောင်းတည်းနဲ့ error မတက်ဘဲ ဖျက်ပါ
  • View ကို DROP VIEW နဲ့ ဖျက်ပြီး foreign key ကို ALTER TABLE ... DROP CONSTRAINT နဲ့ ဖြုတ်ပါ
  • departments ကို CASCADE မပါဘဲ ဖျက်ပြီး ကျန်တဲ့ table တွေနဲ့ employees row အရေအတွက်ကို ပြပါ
retire-departments.sql
BEGIN;

-- TODO: drop import_staging and import_staging_old in one statement;
--       one of them does not exist, and that must not be an error

-- TODO: drop the employee_directory view

-- TODO: remove the foreign key employees_department_id_fkey

-- TODO: drop departments (no CASCADE)

COMMIT;

-- TODO: list the tables in the public schema
-- TODO: count the rows in employees

ရလဒ်: ကျန်တဲ့ table က employees တစ်ခုတည်း ဖြစ်ပြီး employees ထဲမှာ row သုံးကြောင်း ကျန်ပါတယ်။

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

IF EXISTS ကို table နာမည် အများကြီးနဲ့ တွဲသုံးလို့ ရပါတယ်။ Constraint နာမည်ကို problem section ရဲ့ error DETAIL ထဲမှာ တွေ့နိုင်ပါတယ်။ Dependency နှစ်ခုလုံး ဖြုတ်ပြီးရင် RESTRICT နဲ့တင် departments ကို ဖျက်လို့ ရပါပြီ။

အဖြေ ကြည့်ရန်
retire-departments.sql
BEGIN;

DROP TABLE IF EXISTS import_staging, import_staging_old;

DROP VIEW employee_directory;

ALTER TABLE employees
    DROP CONSTRAINT employees_department_id_fkey;

DROP TABLE departments;  -- RESTRICT: nothing depends on it any more

COMMIT;

SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;

SELECT count(*) AS employees_left FROM employees;

Quick Quiz 1 / 3

orders_backup ဆိုတဲ့ table မရှိပါဘူး။ DROP TABLE IF EXISTS orders_backup; ကို run ရင် ဘာဖြစ်မလဲ?

Quick Quiz 2 / 3

nightly_import table ကို မနက်ဖြန် import အတွက် ဆက်သုံးမှာပါ၊ ဒါပေမဲ့ ဒီနေ့ row အားလုံးကို အမြန်ဆုံး ဖယ်ချင်ပါတယ်။ ဘာသုံးမလဲ?

Quick Quiz 3 / 3

employees.department_id က departments ကို foreign key နဲ့ ညွှန်ပါတယ်။ DROP TABLE departments CASCADE; ပြီးနောက် employees table က ဘယ်လို ဖြစ်မလဲ?

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

  • DROP TABLE က table ပုံစံနဲ့ data အားလုံးကို ဖျက်ပါတယ်၊ TRUNCATE နဲ့ DELETE ကတော့ table ကို ချန်ထားပါတယ်။
  • IF EXISTS က မရှိတဲ့ table အတွက် error ကို NOTICE အဖြစ် ပြောင်းပေးလို့ script ကို ထပ်ခါ run လို့ ရပါတယ်။
  • Default RESTRICT က မှီခိုနေတဲ့ object ရှိရင် ငြင်းပြီး CASCADE က view တွေနဲ့ foreign key constraint တွေကိုပါ ဖျက်ပါတယ်။
  • PostgreSQL မှာ COMMIT မတိုင်ခင် ROLLBACK နဲ့ ပြန်ယူလို့ ရပေမဲ့ COMMIT ပြီးရင် backup ကပဲ ကယ်နိုင်ပါတယ်။

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

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

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

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

DROP TABLE | Thuta Learning