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

CREATE TABLE

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

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

  • Column တစ်ခုချင်းစီအတွက် integer, numeric, text/varchar, boolean, date, timestamptz ထဲက သင့်တော်တာကို ရွေးနိုင်ဖို့
  • NOT NULL, DEFAULT နဲ့ PRIMARY KEY က ဘယ် data ကို ငြင်းပယ်လဲ ခန့်မှန်းနိုင်ဖို့
  • GENERATED ALWAYS AS IDENTITY နဲ့ ID ကို database က အလိုအလျောက် ထုတ်ပေးအောင် လုပ်နိုင်ဖို့
  • Quoted identifier နဲ့ ငွေကို float type နဲ့ သိမ်းတာ စတဲ့ ထောင်ချောက်တွေကို ရှောင်နိုင်ဖို့

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

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

CREATE TABLE က ပုံစံ (form) အလွတ်တစ်ခုကို ဒီဇိုင်းဆွဲတာနဲ့ တူပါတယ်။ ကွက်လပ် တစ်ခုချင်းစီ (column) မှာ နာမည်၊ ဘာမျိုး ဖြည့်ရမလဲ (data type)၊ မဖြစ်မနေ ဖြည့်ရမလား (NOT NULL)၊ မဖြည့်ရင် ဘာဖြည့်ပေးမလဲ (DEFAULT) ဆိုတာတွေကို ကြိုသတ်မှတ်ပါတယ်။ အဲဒီနောက်မှ INSERT နဲ့ form တွေကို ဖြည့်ပါတယ်။

Form ကောင်းကောင်း ဒီဇိုင်းဆွဲထားရင် မှားတဲ့ data ကို database ကိုယ်တိုင် ငြင်းပယ်ပါတယ် — ရက်စွဲကွက်ထဲ စာသား ထည့်လို့ မရ၊ နာမည် မပါရင် သိမ်းလို့ မရ။ Application code ရဲ့ စစ်ဆေးမှုတွေ လွတ်သွားရင်တောင် table က နောက်ဆုံး အကာအကွယ်အဖြစ် ရပ်တည်ပေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: type မှားရွေးထားလို့ data ကြည့်ရ ရှုပ်ထွေး

အောက်က table ကို ခပ်မြန်မြန် ဆောက်ထားပါတယ်: column တိုင်းက text ဖြစ်ပြီး NOT NULL လည်း မပါပါဘူး။ ခဏနေတော့ ပြဿနာတွေ ပေါ်လာပါတယ်: MAX(hired_on) က 2024-10-05 အစား '2024-9-30' ကို အနောက်ဆုံး ရက်လို့ ပြောပါတယ်၊ salary ကို ပေါင်းလို့ မရပါဘူး၊ နာမည်မပါတဲ့ ဝန်ထမ်း row တွေလည်း ဝင်လာပါတယ်။

employees_bad.sql
CREATE TABLE employees_bad (
    id       text,
    name     text,
    salary   text,
    hired_on text
);
INSERT INTO employees_bad VALUES
  ('1', 'Aye', '1200', '2024-10-05'),
  ('2', NULL,  'n/a',  '2024-9-30');     -- no name, salary is not a number

SELECT MAX(hired_on) FROM employees_bad;  -- '2024-9-30' (text comparison)
SELECT SUM(salary)   FROM employees_bad;  -- ERROR: function sum(text) does not exist

ဘာကြောင့် type တွေ အရေးကြီးတာလဲ

Data type က column ထဲ ဘာဝင်ခွင့်ရှိလဲ ကန့်သတ်ရုံသာမက value တွေကို ဘယ်လို နှိုင်းယှဉ်၊ တွက်ချက်မလဲကိုပါ ဆုံးဖြတ်ပါတယ်။ text အဖြစ် သိမ်းထားတဲ့ ရက်စွဲကို စာလုံးအစဉ်နဲ့ နှိုင်းယှဉ်လို့ '9' က '1' ထက် ကြီးတယ်လို့ ယူပါတယ်။ hired_on date NOT NULL လို့ ကြေညာထားရင် '2024-9-30' က ရက်စွဲမှန်အဖြစ် ပြောင်းသိမ်းပြီး ရက်စွဲအစဉ်နဲ့ နှိုင်းယှဉ်ပါတယ်၊ 'n/a' လို ရက်စွဲ မဟုတ်တာနဲ့ NULL ကိုတော့ ငြင်းပါတယ်။ Constraint တွေကလည်း မပြည့်စုံတဲ့ row တွေကို ဝင်ပေါက်မှာတင် ပိတ်ပေးပါတယ်။

Core concept: column တစ်ခုစီက ကတိတစ်ခု

CREATE TABLE table_name (...) ထဲမှာ column တစ်ခုစီကို ကော်မာနဲ့ ခွဲပြီး "နာမည် type constraint" အစဉ်နဲ့ ရေးပါတယ်။ Column definition တစ်ခုချင်းစီက database က ထိန်းသိမ်းပေးမယ့် ကတိတစ်ခုပါ: hired_on date NOT NULL ဆိုတာ "ဒီ column မှာ ရက်စွဲမှန် တစ်ခု အမြဲ ရှိမယ်" လို့ ကတိပေးတာပါ။ ဒီကတိကို ချိုးဖောက်မယ့် INSERT ဒါမှမဟုတ် UPDATE တိုင်းကို error နဲ့ ငြင်းပယ်ပြီး row တစ်ကြောင်းမှ မသိမ်းပါဘူး။

PostgreSQL မှာ အသုံးများတဲ့ type တွေက: ID နဲ့ အရေအတွက်အတွက် integer (ဒါမှမဟုတ် row သန်းပေါင်းများစွာ ရှိနိုင်ရင် bigint)၊ ငွေကြေးလို တိကျရမယ့် ဒသမ ဂဏန်းအတွက် numeric(10, 2)၊ စာသားအတွက် text ဒါမှမဟုတ် varchar(n)၊ true/false အတွက် boolean၊ ရက်စွဲအတွက် date နဲ့ အချိန်အတိအကျအတွက် timestamptz ပါ။ PostgreSQL မှာ text နဲ့ varchar(n) ရဲ့ performance က အတူတူပါ၊ varchar(n) က အရှည် ကန့်သတ်ချက်ကိုပဲ ထပ်ထည့်ပါတယ်။ အတွေ့များတဲ့ varchar(255) က MySQL ကနေ လာတဲ့ အလေ့အထ ဖြစ်ပြီး PostgreSQL မှာ 255 က ထူးခြားတဲ့ ဂဏန်း မဟုတ်ပါဘူး — business rule ရှိမှ အရှည်ကန့်သတ်ပါ။

NOT NULL က value မပါတဲ့ row ကို ငြင်းပါတယ်။ DEFAULT က INSERT မှာ column ကို ချန်ခဲ့ရင် ဖြည့်ပေးမယ့် value ပါ (ဥပမာ is_active boolean NOT NULL DEFAULT true)။ PRIMARY KEY က row တစ်ကြောင်းချင်းစီကို ခွဲခြားသိနိုင်တဲ့ column ဖြစ်ပြီး NOT NULL နဲ့ UNIQUE ကို တစ်ပြိုင်နက် အာမခံပြီး index တစ်ခုကိုလည်း အလိုအလျောက် ဆောက်ပေးပါတယ်။ integer GENERATED ALWAYS AS IDENTITY ကတော့ 1, 2, 3 လို နံပါတ်တွေကို database က ထုတ်ပေးတာပါ — ဒါက SQL standard ဖြစ်ပြီး PostgreSQL 10 ကစ ရပါတယ်။ ဗားရှင်းဟောင်း tutorial တွေမှာ တွေ့ရတဲ့ serial က အလားတူ အလုပ်လုပ်တဲ့ PostgreSQL ရဲ့ အတိုကောက် ပုံစံဟောင်းပါ။ MySQL က AUTO_INCREMENT၊ SQL Server က IDENTITY(1,1) နဲ့ ရေးပါတယ်။

နောက်ဆုံး စည်းမျဉ်းတစ်ခု: PostgreSQL က quote မပါတဲ့ identifier တွေကို lowercase ပြောင်းပါတယ်။ CREATE TABLE Employees (EmployeeID int) ဆိုရင် တကယ်တမ်း employees ဆိုတဲ့ table နဲ့ employeeid ဆိုတဲ့ column ကို ဆောက်ပါတယ်၊ Employees ဒါမှမဟုတ် EMPLOYEES လို့ ရေးရေး အလုပ်လုပ်ပါတယ်။ ဒါကြောင့် PostgreSQL မှာ employee_id လို snake_case နဲ့ နာမည်ပေးတာ စံဖြစ်ပါတယ်။

မှတ်ထားရမယ့် ပုံစံ
  1. INSERT က row တစ်ကြောင်း ပို့
  2. ချန်ထားတဲ့ column ကို DEFAULT ဖြည့်
  3. Value ကို column type အဖြစ် ပြောင်း
  4. NOT NULL / PRIMARY KEY စစ်
  5. အားလုံးအောင်မှ row သိမ်း

အဆင့်တစ်ခုခု ကျရင် statement တစ်ခုလုံး ပယ်ဖျက်ခံရပြီး table က မပြောင်းလဲပါဘူး။

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

sql
CREATE TABLE table_name (
    id_column   integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    column_one  data_type NOT NULL,
    column_two  data_type DEFAULT default_value,
    column_three data_type
);

Column တစ်ခုစီက "နာမည် type constraint" အစဉ်နဲ့ ရေးပြီး ကော်မာနဲ့ ခွဲပါတယ်၊ နောက်ဆုံး column နောက်မှာ ကော်မာ မပါရပါဘူး။ Constraint မပါတဲ့ column က NULL ကို လက်ခံပါတယ်။

Example: employees table ကို ဆောက်ပြီး စမ်းကြည့်ခြင်း

Employees table တစ်ခုကို PostgreSQL ပုံစံနဲ့ ဆောက်ထားပါတယ်: snake_case နာမည်တွေ၊ ID ကို database က ထုတ်ပေးတဲ့ identity column၊ ငွေအတွက် numeric နဲ့ default ပါတဲ့ boolean တစ်ခု။ Table ဆောက်ပြီးတာနဲ့ row နှစ်ကြောင်း ထည့်ပြီး ပြန်ဖတ်ကြည့်ပါမယ်။ Output ရဲ့ ပထမ နှစ်ကြောင်းက psql က statement တစ်ခုစီ ပြီးတိုင်း ပြတဲ့ command tag တွေပါ။

create_employees.sql
CREATE TABLE employees (
    employee_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    first_name  varchar(100) NOT NULL,
    last_name   varchar(100) NOT NULL,
    city        text,
    salary      numeric(10, 2) NOT NULL,
    is_active   boolean NOT NULL DEFAULT true,
    hired_on    date NOT NULL
);

INSERT INTO employees (first_name, last_name, city, salary, hired_on)
VALUES ('Aye', 'Chan', 'Yangon', 1200.00, '2024-03-01'),
       ('Min', 'Thu', NULL, 950.50, '2024-06-15');

SELECT employee_id, first_name, city, salary, is_active, hired_on
FROM employees;

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

  1. Line 2employee_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,

    Database က 1, 2, ... နံပါတ်တွေကို ထုတ်ပေးပြီး PRIMARY KEY က ထပ်နေတာနဲ့ NULL ကို တားပါတယ်။ ALWAYS ကြောင့် INSERT မှာ ID ကို ကိုယ်တိုင် ထည့်ရင် error တက်ပါတယ်။

  2. Lines 3-4first_name varchar(100) NOT NULL,

    နာမည်ကို မဖြစ်မနေ ထည့်ရပြီး စာလုံး 100 ထက် ရှည်ရင် "value too long" error နဲ့ ငြင်းပါတယ်။

  3. Line 5city text,

    Constraint မပါလို့ optional ပါ — Min ရဲ့ city က NULL အဖြစ် သိမ်းပြီး psql မှာ ကွက်လပ် ပေါ်ပါတယ်။

  4. Line 6salary numeric(10, 2) NOT NULL,

    စုစုပေါင်း ဂဏန်း 10 လုံး၊ ဒသမနောက် 2 လုံး။ 1200.00 နဲ့ 950.50 ကို တိတိကျကျ သိမ်းပြီး float လို rounding error မရှိပါဘူး။

  5. Line 7is_active boolean NOT NULL DEFAULT true,

    INSERT က is_active ကို မထည့်လို့ DEFAULT true ဝင်ပါတယ်။ psql က boolean true ကို t လို့ ပြပါတယ်။

  6. Lines 11-13INSERT INTO employees (first_name, last_name, city, salary, hired_on)

    Column list ထဲမှာ employee_id နဲ့ is_active မပါပါဘူး — database က တစ်ခုကို ထုတ်ပေးပြီး နောက်တစ်ခုကို default နဲ့ ဖြည့်ပါတယ်။

You should see
CREATE TABLE
INSERT 0 2
 employee_id | first_name |  city  | salary  | is_active |  hired_on
-------------+------------+--------+---------+-----------+------------
           1 | Aye        | Yangon | 1200.00 | t         | 2024-03-01
           2 | Min        |        |  950.50 | t         | 2024-06-15
(2 rows)

Constraint တွေ ငြင်းပယ်ပုံ

Example table ပေါ်မှာ အောက်က statement တွေကို run ကြည့်ရင် တစ်ခုချင်းစီ error နဲ့ ငြင်းခံရပါတယ်။ Error message တွေကို ဖတ်တတ်ရင် ဘယ် column ရဲ့ ဘယ်ကတိ ချိုးဖောက်မိလဲ ချက်ချင်း သိနိုင်ပါတယ်။

StatementPostgreSQL ရဲ့ အဖြေ
INSERT ... (first_name, salary, hired_on) VALUES ('X', 1, '2024-01-01')null value in column "last_name" ... violates not-null constraint
INSERT ... (employee_id, ...) VALUES (10, ...)cannot insert a non-DEFAULT value into column "employee_id"
first_name ထဲ စာလုံး 101 လုံး ထည့်value too long for type character varying(100)
hired_on ထဲ 'n/a' ထည့်invalid input syntax for type date: "n/a"

IF NOT EXISTS ထည့်ရင် table ရှိပြီးသားဆိုရင် error အစား notice တစ်ခုပဲ ပြပြီး ကျော်သွားပါတယ် — setup script တွေကို အကြိမ်ကြိမ် run လို့ ရအောင် အသုံးဝင်ပါတယ်။ ဒါပေမယ့် ရှိပြီးသား table ရဲ့ column တွေ ကိုယ်ရေးထားတာနဲ့ ကိုက်လား မစစ်ပေးပါဘူး၊ table ပြင်ဖို့က ALTER TABLE ရဲ့ အလုပ်ပါ။ UNIQUE, CHECK, REFERENCES (foreign key) constraint တွေကိုတော့ Constraints lesson မှာ အသေးစိတ် လေ့လာပါမယ်။

if_not_exists.sql
CREATE TABLE IF NOT EXISTS tags (
    tag_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    label  text NOT NULL
);
-- running it again only raises a notice: relation "tags" already exists
CREATE TABLE IF NOT EXISTS tags (
    tag_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    label  text NOT NULL
);

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

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

Real project တွေမှာ CREATE TABLE ကို production database ပေါ်မှာ လက်နဲ့ ရိုက်ပြီး run လေ့ မရှိပါဘူး။ Migration file တွေထဲမှာ ရေးပြီး Git ထဲ သိမ်းကာ Flyway, Liquibase, Prisma Migrate, Django migrations လို tool တွေက environment တိုင်း (developer laptop, staging, production) မှာ အစဉ်လိုက် run ပေးပါတယ်။ ဒါကြောင့် schema ပြောင်းလဲမှု တိုင်းကို code review လုပ်နိုင်ပြီး ဘယ်အချိန် ဘာပြောင်းလဲ ဆိုတဲ့ မှတ်တမ်း ရှိပါတယ်။

Type ရွေးချယ်မှုက နောင်တစ်ချိန် ပြင်ရခက်တဲ့ ဆုံးဖြတ်ချက် ဖြစ်လို့ စောစောကတည်းက သေချာ စဉ်းစားပါ။ Row သန်းပေါင်းများစွာ ရှိလာမယ့် table ရဲ့ ID ကို integer ထက် bigint ရွေးတာ ဘေးကင်းပါတယ်။ အချိန်တွေကို timezone ပါတဲ့ timestamptz နဲ့ သိမ်းရင် ကမ္ဘာ့နေရာ အမျိုးမျိုးက user တွေအတွက် မှန်ကန်ပါတယ်။ Table ကြီးတစ်ခုရဲ့ column type ကို နောက်မှ ပြောင်းရင် table တစ်ခုလုံးကို ပြန်ရေးရပြီး lock ကြာကြာ ယူနိုင်လို့ production downtime ဖြစ်စေနိုင်ပါတယ်။

အမှားများ

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

1. Table နာမည်ကို double quote နဲ့ ရေးတာ

CREATE TABLE "Employees" (...) လို့ quote နဲ့ ရေးရင် နာမည်က E အကြီးနဲ့ အတိအကျ သိမ်းပါတယ်။ နောက်ပိုင်း SELECT * FROM Employees; က employees (lowercase) ကို ရှာလို့ "relation employees does not exist" error တက်ပြီး အမြဲ "Employees" လို့ quote နဲ့ ရေးရပါတော့မယ်။ GUI tool အချို့က quote ကို အလိုအလျောက် ထည့်တတ်လို့ သတိထားပါ၊ lowercase snake_case ကိုပဲ သုံးပါ။

2. ငွေကို real ဒါမှမဟုတ် double precision နဲ့ သိမ်းတာ

Floating-point type တွေက ဒသမ ဂဏန်းတွေကို ခန့်မှန်းခြေနဲ့ပဲ သိမ်းပါတယ်: SELECT 0.1::float8 + 0.2::float8; က 0.30000000000000004 ပြန်ပေးပါတယ်။ Invoice ထောင်ချီ ပေါင်းတဲ့အခါ ပြားတွေ လွဲလာနိုင်ပါတယ်။ ငွေနဲ့ ပမာဏ တိကျရမယ့် data အတွက် numeric(p, s) ကို သုံးပါ၊ 0.1 + 0.2 က 0.3 အတိ ရပါတယ်။

3. ရက်စွဲနဲ့ ဂဏန်းကို text နဲ့ သိမ်းတာ

Problem section မှာ မြင်ခဲ့သလို text ရက်စွဲတွေက စာလုံးအစဉ်နဲ့ sort ဖြစ်ပြီး 'n/a' လို အမှိုက် value တွေကိုလည်း လက်ခံပါတယ်။ date, timestamptz, integer, numeric လို type မှန်ကို သုံးရင် database က input ကို စစ်ပေးပြီး ရက်စွဲ တွက်ချက်မှု (hired_on + 30) လို function တွေကိုလည်း သုံးလို့ ရပါတယ်။

Mini challenge

Mini challenge: courses table ဒီဇိုင်း

Online သင်တန်း site တစ်ခုအတွက် courses table ကို ဆောက်ပါ။ ပြီးရင် သင်တန်း နှစ်ခု ထည့်ပါ — တစ်ခုက default value တွေကို အားကိုးပြီး နောက်တစ်ခုက value အားလုံး ကိုယ်တိုင် ပေးပါ — နောက်ဆုံး row တွေကို ပြန်ဖတ်ပါ။

  • course_id: database က ထုတ်ပေးတဲ့ identity primary key
  • title: မဖြစ်မနေ လိုတဲ့ text၊ level: varchar(20) NOT NULL DEFAULT 'beginner'
  • price: numeric(8, 2) NOT NULL DEFAULT 0၊ published_on: optional date
  • is_active: boolean NOT NULL DEFAULT true
courses.sql
CREATE TABLE courses (
    -- TODO: course_id (identity primary key)
    -- TODO: title (required)
    -- TODO: level (default 'beginner')
    -- TODO: price (exact money, default 0)
    -- TODO: published_on (optional date)
    -- TODO: is_active (default true)
);

-- TODO: insert 'SQL Basics' (published 2024-01-10) using the defaults
-- TODO: insert 'Advanced Joins', level 'advanced', price 49.99, not yet published

-- TODO: select every column ordered by course_id

ရလဒ်: Row နှစ်ကြောင်း ရပါမယ်: course_id | title | level | price | published_on | is_active -----------+----------------+----------+-------+--------------+----------- 1 | SQL Basics | beginner | 0.00 | 2024-01-10 | t 2 | Advanced Joins | advanced | 49.99 | | t (2 rows)

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

Default ကို အသုံးချချင်တဲ့ column ကို INSERT ရဲ့ column list ထဲက ချန်ထားပါ။ published_on ကို မထည့်ရင် NULL ဝင်ပါတယ်။ price ရဲ့ default 0 က numeric(8, 2) ဖြစ်လို့ 0.00 အဖြစ် ပြပါတယ်။

အဖြေ ကြည့်ရန်
courses.sql
CREATE TABLE courses (
    course_id    integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    title        text NOT NULL,
    level        varchar(20) NOT NULL DEFAULT 'beginner',
    price        numeric(8, 2) NOT NULL DEFAULT 0,
    published_on date,
    is_active    boolean NOT NULL DEFAULT true
);

INSERT INTO courses (title, published_on)
VALUES ('SQL Basics', '2024-01-10');

INSERT INTO courses (title, level, price)
VALUES ('Advanced Joins', 'advanced', 49.99);

SELECT * FROM courses ORDER BY course_id;

Quick Quiz 1 / 3

CREATE TABLE Products (ProductID int); ပြီးနောက် ဘယ် query က error တက်မလဲ။

Quick Quiz 2 / 3

price column အတွက် ငွေကို တိတိကျကျ သိမ်းချင်ရင် ဘယ် type ကို ရွေးမလဲ။

Quick Quiz 3 / 3

id integer GENERATED ALWAYS AS IDENTITY ရှိတဲ့ table မှာ INSERT INTO t (id, name) VALUES (5, 'x'); ကို run ရင် ဘာဖြစ်မလဲ။

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

  • Column တစ်ခုစီက "နာမည် type constraint" ဖြစ်ပြီး database က ထိန်းသိမ်းပေးမယ့် ကတိတစ်ခုပါ။
  • ငွေအတွက် numeric၊ ရက်စွဲအတွက် date/timestamptz၊ စာသားအတွက် text ဒါမှမဟုတ် varchar(n) ကို ရွေးပါ — text ထဲ အရာအားလုံး မထည့်ပါနဲ့။
  • NOT NULL က value မပါတာကို ငြင်းပြီး DEFAULT က ချန်ထားတဲ့ column ကို ဖြည့်ကာ PRIMARY KEY က row တစ်ကြောင်းချင်းစီကို ခွဲခြားပေးပါတယ်။
  • ID အတွက် GENERATED ALWAYS AS IDENTITY သုံးပြီး quote မပါတဲ့ snake_case နာမည်တွေကို အမြဲ သုံးပါ။

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

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

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

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

CREATE TABLE | Thuta Learning