Thuta Learning
ရှာဖွေရန်
IntermediateData & Databasesbeginner

COPY ဖြင့် Import/Export

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

  • COPY ဖြင့် Import/Export concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ SQL/command ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် အသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

`COPY` က database server filesystem ကိုဖတ်/ရေးပြီး privilege လိုအပ်ပါတယ်။ `psql` ရဲ့ `\copy` က client filesystem ကိုသုံးတာကြောင့် local import/export အတွက်အဆင်ပြေပါတယ်။ Bulk load မတိုင်မီ staging table၊ encoding၊ delimiter၊ header နဲ့ error strategy ကိုသတ်မှတ်ပြီး transaction ဖြင့် atomic ဖြစ်စေသင့်ပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Categories CSV ကို staging table ထဲအရင်ထည့်ပြီး trim/validation လုပ်ကာ production table သို့ merge မယ်။ Direct import ထက် staging pattern က bad rows ရှာခြင်းနှင့် repeatable cleanup ကိုလွယ်စေပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

sql
CREATE TABLE IF NOT EXISTS app.categories (
  category_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name text NOT NULL UNIQUE
);
CREATE TEMP TABLE category_stage (name text);

-- Run inside psql; the path is on the client machine:
\copy category_stage(name) FROM 'categories.csv' WITH (FORMAT csv, HEADER true)

INSERT INTO app.categories (name)
SELECT DISTINCT trim(name)
FROM category_stage
WHERE length(trim(name)) > 0
ON CONFLICT (name) DO NOTHING;

\copy (SELECT * FROM app.categories ORDER BY name) TO 'categories-export.csv' WITH (FORMAT csv, HEADER true)
You should see
Clean unique categories များ import ဖြစ်ပြီး verified CSV export တစ်ခုရမည်။

၅ မိနစ် စမ်းကြည့်

Malformed row ပါသော CSV တစ်ခုဖန်တီးပြီး staging table ဖြင့် ဘယ် row မှားသလဲရှာပါ။

သတိလေးတစ်ချက်

Production table သို့ untrusted CSV ကို direct COPY လုပ်ခြင်းက partial cleanup နှင့် data-quality incident ဖြစ်စေနိုင်ပါတယ်။

PostgreSQL — COPYPostgreSQL Global Development Group

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Production table သို့ untrusted CSV ကို direct COPY လုပ်ခြင်းက partial cleanup နှင့် data-quality incident ဖြစ်စေနိုင်ပါတယ်။
  • နမူနာ code ကို production data ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test database နှင့် recoverable backup ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Malformed row ပါသော CSV တစ်ခုဖန်တီးပြီး staging table ဖြင့် ဘယ် row မှားသလဲရှာပါ။

You'll know it worked when: Clean unique categories များ import ဖြစ်ပြီး verified CSV export တစ်ခုရမည်။

COPY ဖြင့် Import/Export | Thuta Learning