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

Database Intro

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

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

  • Database, table, row, column ဆိုတဲ့ စကားလုံးတွေကို ဥပမာနဲ့ ရှင်းပြနိုင်ဖို့
  • Column တိုင်းမှာ data type ရှိရတဲ့ အကြောင်းနဲ့ integer, text, date, numeric ကို ရွေးတတ်ဖို့
  • Primary key က row တစ်ကြောင်းချင်းကို ဘာကြောင့် ခွဲခြားပေးနိုင်လဲ ရှင်းပြနိုင်ဖို့
  • Orders.CustomerID လို foreign key က table နှစ်ခုကို ဘယ်လို ချိတ်ပေးလဲ ဖတ်တတ်ဖို့
  • Course ရဲ့ sample table တွေကို ကိုယ့် PostgreSQL ထဲမှာ ဆောက်နိုင်ဖို့

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

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

Database ဆိုတာ data တွေကို စနစ်တကျ သိမ်းထားတဲ့ နေရာပါ။ အထဲမှာ table တွေ ရှိပြီး table တစ်ခုက spreadsheet sheet တစ်ခုနဲ့ ဆင်ပါတယ် — column တွေက ခေါင်းစဉ် (နာမည်၊ မြို့၊ နိုင်ငံ) ဖြစ်ပြီး row တစ်ကြောင်းက record တစ်ခု (customer တစ်ယောက်) ပါ။

Spreadsheet နဲ့ ကွာတာက database က စည်းကမ်းတွေ စောင့်ကြည့်ပေးတာပါ။ နံပါတ်ထည့်ရမယ့် column ထဲ စာသား ထည့်လို့ မရပါဘူး၊ customer ID တူတဲ့ row နှစ်ကြောင်း ရှိလို့ မရပါဘူး၊ မရှိတဲ့ customer အတွက် order ရေးလို့ မရပါဘူး။ ဒီ စည်းကမ်းတွေကြောင့် data ကို ယုံကြည်လို့ ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: spreadsheet တစ်ခုတည်းထဲ အကုန်ထည့်ရင်

ဆိုင်တစ်ဆိုင်က order တိုင်းကို sheet တစ်ခုတည်းမှာ ရေးတယ် ဆိုပါစို့။ Order တစ်ခုချင်းစီမှာ customer နာမည်၊ မြို့၊ နိုင်ငံကို ထပ်ခါထပ်ခါ ရိုက်ရပါတယ်။ Ana Trujillo က order ဆယ်ခု မှာရင် သူ့လိပ်စာက ဆယ်ခါ ရှိနေပြီး တစ်နေရာမှာ စာလုံးပေါင်း မှားရင် ဘယ်ဟာ မှန်လဲ မသိတော့ပါဘူး။

orders-sheet.csv
order_id,customer,city,country,date
10308,Ana Trujillo,Mexico City,Mexico,2024-09-18
10312,Ana Trujilo,Mexico Cty,Mexico,2024-09-22
10315,ana trujillo,Mexico City,mexico,yesterday

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

Sheet ထဲမှာ ဘယ် column မှာ ဘာ data type ဝင်ရမလဲ၊ customer တစ်ယောက်ကို ဘယ်လို ခွဲခြားမလဲဆိုတဲ့ စည်းကမ်း မရှိပါဘူး။ Relational database မှာ customer တွေကို Customers table မှာ တစ်ကြိမ်ပဲ သိမ်းပြီး order တွေက CustomerID နံပါတ်နဲ့ ညွှန်းပါတယ်။ date column က 'yesterday' လို စာသားကို လက်မခံပါဘူး။

Core concept: table, key နဲ့ relationship

PostgreSQL server တစ်ခုထဲမှာ database အများကြီး ရှိနိုင်ပါတယ်။ Database တစ်ခုထဲမှာ schema တွေ ရှိပြီး (default က public) schema ထဲမှာ table တွေ ရှိပါတယ်။ အစပိုင်းမှာ "database တစ်ခု၊ အထဲမှာ table တွေ" လို့ မှတ်ထားရင် လုံလောက်ပါတယ်။ Database ကို စီမံတဲ့ software (PostgreSQL, MySQL) ကိုတော့ DBMS (Database Management System) လို့ ခေါ်ပါတယ်။

Table တစ်ခုရဲ့ column တိုင်းမှာ နာမည်နဲ့ data type ရှိပါတယ်။ integer က ကိန်းပြည့်၊ text က စာသား၊ date က ရက်စွဲ၊ numeric(10, 2) က ငွေပမာဏလို ဒသမ တိကျရမယ့် ကိန်းပါ။ Data type က database ကို value တွေ စစ်ခိုင်းတာ ဖြစ်လို့ CustomerID column ထဲ 'abc' ထည့်ရင် error တက်ပါတယ်။ Row တစ်ကြောင်းက column တိုင်းအတွက် value တစ်ခုစီ ပါတဲ့ record တစ်ခုပါ။ Value မသိသေးရင် NULL ဖြစ်နိုင်ပြီး NOT NULL လို့ ရေးထားတဲ့ column ကတော့ အမြဲ value ရှိရပါမယ်။

Primary key ဆိုတာ row တစ်ကြောင်းချင်းကို ခွဲခြားပေးတဲ့ column ပါ။ CustomerID ကို PRIMARY KEY လို့ ကြေညာလိုက်ရင် PostgreSQL က ID တူတဲ့ row နှစ်ကြောင်းနဲ့ NULL ID ကို ငြင်းပါတယ်။ နာမည် ဒါမှမဟုတ် မြို့က primary key ဖြစ်ဖို့ မသင့်ပါဘူး — Mexico City မှာ customer နှစ်ယောက် ရှိသလို နာမည်တူ လူနှစ်ယောက်လည်း ရှိနိုင်ပါတယ်။

Foreign key ကတော့ တခြား table ရဲ့ primary key ကို ညွှန်းတဲ့ column ပါ။ Orders.CustomerID က Customers.CustomerID ကို REFERENCES နဲ့ ညွှန်းထားလို့ order 10308 ရဲ့ CustomerID = 2 ဆိုတာ "ဒီ order က Ana Trujillo ရဲ့ order" လို့ ဆိုလိုပါတယ်။ Customer လိပ်စာကို တစ်နေရာတည်းမှာ သိမ်းထားလို့ ပြင်ရင်လည်း တစ်ကြိမ်ပဲ ပြင်ရပါတယ်။ ဒီလို table တွေကို key တွေနဲ့ ချိတ်ထားတာကြောင့် "relational" database လို့ ခေါ်တာပါ။

နောက်ထပ် မှတ်စရာ တစ်ခုက table ထဲက row တွေမှာ သတ်မှတ်ထားတဲ့ အစဉ် မရှိတာပါ။ Spreadsheet မှာ row 1, row 2 ဆိုပြီး နေရာ အသေ ရှိပေမယ့် database table က row အစု (set) တစ်ခုပါ။ Row တွေကို ID နဲ့ ရှာပြီး အစဉ်လိုက် လိုချင်ရင် query မှာ ORDER BY နဲ့ တောင်းရပါတယ် (Lesson 9)။

မှတ်ထားရမယ့် ပုံစံ: ID နဲ့ ချိတ်ထားတဲ့ table နှစ်ခု
  1. Customer တစ်ယောက် = row တစ်ကြောင်း
  2. CustomerID က row ကို ခွဲခြား
  3. Order က CustomerID ကို သိမ်း
  4. ID နဲ့ customer ကို ပြန်ရှာ

Data ကို တစ်နေရာတည်းမှာ သိမ်းပြီး ကျန်တဲ့နေရာတွေက ID နဲ့ ညွှန်းပါတယ်။

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

sql
CREATE TABLE parent_table (
  id   integer PRIMARY KEY,
  name text NOT NULL
);

CREATE TABLE child_table (
  id        integer PRIMARY KEY,
  parent_id integer NOT NULL REFERENCES parent_table (id)
);

Column တစ်ခုစီကို နာမည်၊ data type၊ လိုအပ်ရင် rule (PRIMARY KEY, NOT NULL, REFERENCES) အစဉ်နဲ့ ရေးပါတယ်။ Child table ရဲ့ parent_id က parent table ရဲ့ id ကို ညွှန်းတဲ့ foreign key ပါ။ CREATE TABLE အသေးစိတ်ကို Lesson 21 မှာ ပြန်လေ့လာပါမယ်။

Example: course ရဲ့ sample table တွေကို ဆောက်ခြင်း

ဒီ script ကို ကိုယ့် PostgreSQL database ထဲမှာ တစ်ကြိမ် run ထားပါ။ နောက် lesson တွေက query တွေ အားလုံး ဒီ Customers နဲ့ Orders table တွေကို သုံးပါတယ်။ ပြီးရင် table နှစ်ခုလုံးကို ဖတ်ကြည့်ပြီး Orders ထဲက CustomerID တွေကို Customers ထဲမှာ လိုက်ရှာကြည့်ပါ။ အစဉ်က အရေးကြီးပါတယ် — Orders ထဲက foreign key က Customers ကို ညွှန်းလို့ Customers ကို အရင် ဆောက်ရပါတယ်။ Script ကို နှစ်ခါ run မိရင် relation "customers" already exists error တက်ပေမယ့် ဘာမှ မပျက်စီးပါဘူး။

sample-tables.sql
CREATE TABLE Customers (
  CustomerID   integer PRIMARY KEY,
  CustomerName text NOT NULL,
  ContactName  text,
  City         text,
  Country      text
);

INSERT INTO Customers (CustomerID, CustomerName, ContactName, City, Country) VALUES
  (1, 'Alfreds Futterkiste', 'Maria Anders',   'Berlin',      'Germany'),
  (2, 'Ana Trujillo',        'Ana Trujillo',   'Mexico City', 'Mexico'),
  (3, 'Antonio Moreno',      'Antonio Moreno', 'Mexico City', 'Mexico'),
  (4, 'Around the Horn',     'Thomas Hardy',   'London',      'UK');

CREATE TABLE Orders (
  OrderID    integer PRIMARY KEY,
  CustomerID integer NOT NULL REFERENCES Customers (CustomerID),
  OrderDate  date NOT NULL
);

INSERT INTO Orders (OrderID, CustomerID, OrderDate) VALUES
  (10308, 2, '2024-09-18'),
  (10309, 4, '2024-09-19'),
  (10310, 3, '2024-09-20');

SELECT * FROM Customers;
SELECT * FROM Orders;

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

  1. CustomerIDCustomerID integer PRIMARY KEY,

    Customer တစ်ယောက်စီရဲ့ ID ပါ။ PRIMARY KEY ကြောင့် တန်ဖိုးတူ နှစ်ခု မရှိနိုင်သလို NULL လည်း မဖြစ်နိုင်ပါဘူး။

  2. CustomerNameCustomerName text NOT NULL,

    နာမည်က မဖြစ်မနေ လိုလို့ NOT NULL ပါ။ ContactName, City, Country မှာတော့ rule မပါလို့ မသိသေးရင် ချန်ထားခဲ့လို့ ရပါတယ်။

  3. INSERT(2, 'Ana Trujillo', 'Ana Trujillo', 'Mexico City', 'Mexico'),

    Row တစ်ကြောင်းက column အစဉ်အတိုင်း value ငါးခုပါ။ စာသားတွေကို single quote ထဲ ထည့်ပြီး နံပါတ်ကို quote မလိုပါဘူး။ psql က INSERT 0 4 လို့ ပြတာက row လေးကြောင်း ဝင်သွားကြောင်းပါ။

  4. Foreign keyCustomerID integer NOT NULL REFERENCES Customers (CustomerID),

    Order တိုင်းက Customers ထဲမှာ တကယ်ရှိတဲ့ CustomerID ကိုပဲ ညွှန်းနိုင်ပါတယ်။ Customer 99 အတွက် order ထည့်ရင် foreign key constraint error တက်ပါတယ်။

  5. OrderDateOrderDate date NOT NULL

    date type က '2024-09-18' လို ISO ပုံစံ ရက်စွဲကို လက်ခံပြီး 'yesterday' ဒါမှမဟုတ် '2024-13-45' လို မှားတဲ့ ရက်စွဲကို ငြင်းပါတယ်။

You should see
CREATE TABLE
INSERT 0 4
CREATE TABLE
INSERT 0 3
 customerid |    customername     |  contactname   |    city     | country
------------+---------------------+----------------+-------------+---------
          1 | Alfreds Futterkiste | Maria Anders   | Berlin      | Germany
          2 | Ana Trujillo        | Ana Trujillo   | Mexico City | Mexico
          3 | Antonio Moreno      | Antonio Moreno | Mexico City | Mexico
          4 | Around the Horn     | Thomas Hardy   | London      | UK
(4 rows)

 orderid | customerid | orderdate
---------+------------+------------
   10308 |          2 | 2024-09-18
   10309 |          4 | 2024-09-19
   10310 |          3 | 2024-09-20
(3 rows)

Table နှစ်ခုကို ID နဲ့ ဖတ်ကြည့်ခြင်း

Order 10309 ရဲ့ CustomerID က 4 ပါ။ Customers table ထဲမှာ CustomerID 4 က Around the Horn (London) ဖြစ်လို့ ဒီ order ကို London ကို ပို့ရမယ်ဆိုတာ သိပါတယ်။ အခုတော့ မျက်လုံးနဲ့ ချိတ်ကြည့်နေပေမယ့် Lesson 17-19 မှာ JOIN ကို သုံးပြီး ဒီအလုပ်ကို query တစ်ခုတည်းနဲ့ လုပ်ပါမယ်။ Customer 1 ဖြစ်တဲ့ Alfreds Futterkiste မှာ order တစ်ခုမှ မရှိသေးတာကိုလည်း သတိထားပါ။ ဒါက လုံးဝ ခွင့်ပြုပါတယ် — foreign key က order တိုင်း တကယ်ရှိတဲ့ customer ကို ညွှန်းလားပဲ စစ်တာဖြစ်ပြီး customer တိုင်းမှာ order ရှိရမယ်လို့ မစစ်ပါဘူး။

text
Customers <- Orders
-------------------
Customers                           Orders
+------------+-----------------+    +---------+------------+
| CustomerID | CustomerName    |    | OrderID | CustomerID |
+------------+-----------------+    +---------+------------+
|          2 | Ana Trujillo    |<---|   10308 |          2 |
|          3 | Antonio Moreno  |<---|   10310 |          3 |
|          4 | Around the Horn |<---|   10309 |          4 |
+------------+-----------------+    +---------+------------+
  primary key                         foreign key -> Customers

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

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

Online shop တစ်ခုရဲ့ database မှာ customers, products, orders, order_items, payments လို table တွေ ရှိပြီး order_items က orders နဲ့ products နှစ်ခုလုံးကို foreign key နဲ့ ညွှန်းပါတယ်။ ဆေးရုံ system မှာ patients, doctors, appointments၊ ကျောင်း system မှာ students, courses, enrollments ဆိုပြီး ပုံစံ အတူတူပါပဲ — အရာဝတ္ထု တစ်မျိုးကို table တစ်ခု၊ ဆက်သွယ်မှုတွေကို foreign key နဲ့ပါ။

Project အသစ် စတဲ့အခါ developer တွေက code မရေးခင် table တွေ၊ column တွေ၊ key တွေကို ပုံဆွဲပြီး ဆွေးနွေးကြပါတယ်။ ဒီ design ကို နောက်မှ ပြောင်းရတာ ခက်ပါတယ် — data သန်းချီ ရှိနေတဲ့ table တစ်ခုကို ခွဲရတာ migration ကြီးတစ်ခု ဖြစ်လို့ပါ။ ဒါကြောင့် primary key ရွေးတာ၊ data type ရွေးတာကို အစကတည်းက ဂရုစိုက်ကြပြီး table တွေနဲ့ သူတို့ကြား ချိတ်ဆက်မှုကို ပုံကြမ်းလေး ဆွဲပြီး ဆွေးနွေးလေ့ ရှိပါတယ်။

အမှားများ

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

1. နာမည်ကို primary key အဖြစ် သုံးတာ

CustomerName ကို primary key လုပ်ထားရင် နာမည်တူ customer နောက်တစ်ယောက် ရောက်လာတာနဲ့ INSERT က duplicate key error ပေးပါတယ်။ Customer က နာမည်ပြောင်းရင်လည်း order တိုင်းရဲ့ reference ကို လိုက်ပြင်ရပါတယ်။ အဓိပ္ပာယ် မရှိတဲ့ ID နံပါတ် (surrogate key) ကို သုံးတာက ပိုဘေးကင်းပါတယ်။

2. နံပါတ် ဒါမှမဟုတ် ရက်စွဲကို text column ထဲ သိမ်းတာ

OrderDate ကို text အဖြစ် သိမ်းရင် '18/09/2024', '2024-9-18', 'yesterday' တွေ ရောနှောပြီး ရက်စွဲအလိုက် စီလို့ မှန်မှန်ကန်ကန် မရတော့ပါဘူး။ Price ကို text နဲ့ သိမ်းရင် '100' က '9' ထက် ငယ်တယ်လို့ စာလုံးအစဉ်နဲ့ စီသွားပါတယ်။ date, numeric, integer လို တကယ့် type ကို သုံးပါ။

3. Foreign key မထည့်ဘဲ ID ကိုပဲ သိမ်းတာ

REFERENCES မပါဘဲ CustomerID ကို integer ရိုးရိုး သိမ်းရင် ဖျက်ပြီးသား customer ကို ညွှန်းနေတဲ့ "မိဘမဲ့" order တွေ ကျန်ခဲ့နိုင်ပါတယ်။ Foreign key ထည့်ထားရင် database က အဲဒီလို ဖြစ်ခွင့် မပေးပါဘူး။

psql
INSERT INTO Orders VALUES (10311, 99, '2024-09-21');
ERROR:  insert or update on table "orders" violates foreign key constraint
        "orders_customerid_fkey"

Best Practices

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

  • Table တိုင်းမှာ primary key တစ်ခု အမြဲ ထားပါ၊ အဓိပ္ပာယ် မရှိတဲ့ ID နံပါတ်က အလွယ်ဆုံးနဲ့ အဘေးကင်းဆုံးပါ။
  • Column တစ်ခုစီအတွက် data ရဲ့ တကယ့် type (integer, numeric, date) ကို ရွေးပြီး text ကို အရာအားလုံးအတွက် မသုံးပါနဲ့။
  • မဖြစ်မနေ လိုတဲ့ column တွေကို NOT NULL နဲ့ ကာကွယ်ပါ။
  • Table တစ်ခုက တခြား table ကို ID နဲ့ ညွှန်းရင် REFERENCES နဲ့ foreign key အဖြစ် ကြေညာပါ။

Mini challenge

Mini challenge: Products table ဆောက်ပါ

ဆိုင်ရဲ့ ကုန်ပစ္စည်းတွေအတွက် Products table တစ်ခု ဆောက်ပြီး data ထည့်ကာ ပြန်ဖတ်ပါ။

  • ProductID ကို integer primary key ထားပါ
  • ProductName ကို text၊ NOT NULL ထားပါ
  • Price ကို numeric(10, 2) ထားပြီး NOT NULL ထားပါ
  • ပစ္စည်း သုံးမျိုး ထည့်ပြီး column သုံးခုလုံးကို နာမည်တပ် ပြန်ဖတ်ပါ
products.sql
-- TODO: CREATE TABLE Products with three columns
--   ProductID   -> whole number, unique per product
--   ProductName -> text, required
--   Price       -> exact money value, required

-- TODO: insert three products in one statement

-- TODO: read the three columns back

ရလဒ်: SELECT က row သုံးကြောင်း ပြန်ပေးပြီး price column မှာ 3.50, 12.00, 7.25 လို ဒသမ နှစ်နေရာ ပါပါတယ်။

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

Customers table ရဲ့ ပုံစံကို လိုက်ပါ။ ငွေကို float မသုံးဘဲ numeric(10, 2) သုံးရင် 3.50 လို တန်ဖိုးကို တိတိကျကျ သိမ်းပါတယ်။

အဖြေ ကြည့်ရန်
products.sql
CREATE TABLE Products (
  ProductID   integer PRIMARY KEY,
  ProductName text NOT NULL,
  Price       numeric(10, 2) NOT NULL
);

INSERT INTO Products (ProductID, ProductName, Price) VALUES
  (1, 'Green Tea', 3.50),
  (2, 'Coffee Beans', 12.00),
  (3, 'Honey', 7.25);

SELECT ProductID, ProductName, Price FROM Products;

Quick Quiz 1 / 3

Customers table မှာ primary key အဖြစ် ဘယ် column က အသင့်တော်ဆုံးလဲ။

Quick Quiz 2 / 3

Orders ထဲမှာ (10309, 4, '2024-09-19') ဆိုတဲ့ row ရှိတယ်။ ဒီ order က ဘယ်သူ့ order လဲ။

Quick Quiz 3 / 3

INSERT INTO Orders VALUES (10311, 99, '2024-09-21'); ကို run ရင် ဘာဖြစ်မလဲ (customer 99 မရှိပါ)။

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

  • Database ထဲမှာ table တွေ ရှိပြီး table တစ်ခုမှာ column (type ရှိတဲ့ ခေါင်းစဉ်) နဲ့ row (record) တွေ ပါပါတယ်။
  • Primary key က row တစ်ကြောင်းချင်းကို ခွဲခြားပေးပြီး တန်ဖိုးထပ်တာနဲ့ NULL ကို ငြင်းပါတယ်။
  • Foreign key က တခြား table ရဲ့ primary key ကို ညွှန်းပြီး မရှိတဲ့ record ကို ညွှန်းခွင့် မပေးပါဘူး။
  • Data တစ်ခုကို တစ်နေရာတည်းမှာ သိမ်းပြီး ID နဲ့ ချိတ်တာက relational design ရဲ့ အနှစ်သာရပါ။

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

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

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

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

Database Intro | Thuta Learning