/Projects
PostgreSQL
ProjectsCS ChallengeFull-StackPostgreSQL

PostgreSQL: เก็บสินค้าและออเดอร์

ใช้ใน Challenge Day 3 · ส่วน P ของ PERN Stack

PostgreSQL คือฐานข้อมูลแบบตาราง (relational) ข้อมูลอยู่ใน table ที่เชื่อมกันด้วย key และสั่งงานด้วยภาษา SQL ในคอร์สเราเปิด PostgreSQL บน Supabase เพื่อไม่ต้องตั้งเซิร์ฟเวอร์เอง

At a glance

คำศัพท์ความหมาย
Tableตาราง เช่น products, orders
Row / Column1 รายการ / 1 ช่องข้อมูล
Primary keyคอลัมน์ที่ไม่ซ้ำ ใช้ระบุแต่ละแถว (มักชื่อ id)
Foreign keyคอลัมน์ที่ชี้ไปยัง id ของอีกตาราง
SQLภาษาที่ใช้สั่งฐานข้อมูล
Connection stringURL ที่บอก Express ว่าฐานข้อมูลอยู่ไหน และใช้รหัสอะไร

Installation

ใช้ PostgreSQL บน Supabase (ฟรี ไม่ต้องติดตั้งบนเครื่อง)

  1. สร้างโปรเจกต์

    ไปที่ supabase.com/dashboard ล็อกอินด้วย GitHub แล้วกด New project ตั้งรหัสผ่านฐานข้อมูลและจดไว้

  2. เลือก Region

    เลือก Southeast Asia (Singapore) เพื่อให้เร็วที่สุดจากไทย

  3. เปิด SQL Editor

    เมนูซ้าย → SQL Editor แล้ววางคำสั่งสร้างตารางด้านล่าง กด Run

  4. เอา connection string

    กดปุ่ม Connect ด้านบน เลือก Session pooler แล้วคัดลอก URI ไปใส่ใน .env ของ Express

ติดตั้งบนเครื่องอยากลง PostgreSQL บนเครื่องเอง: ดาวน์โหลดที่ postgresql.org/download แล้วใช้ connection string postgresql://postgres:รหัส@localhost:5432/postgres

เก็บรหัสให้ดีอย่าเอา connection string ใส่ในโค้ดฝั่ง React หรือ push ขึ้น GitHub เก็บไว้ใน .env เสมอ

Structure: ตารางร้านค้า

Tableคอลัมน์เชื่อมกับ
productsid, name, price, stock, created_at—
ordersid, customer_name, total, status, created_at—
order_itemsid, order_id, product_id, quantity, priceorders.id, products.id
create table products (
  id          bigint generated always as identity primary key,
  name        text not null,
  price       numeric(10, 2) not null check (price >= 0),
  stock       int not null default 0,
  created_at  timestamptz not null default now()
);

create table orders (
  id             bigint generated always as identity primary key,
  customer_name  text not null,
  total          numeric(10, 2) not null default 0,
  status         text not null default 'pending',
  created_at     timestamptz not null default now()
);

create table order_items (
  id          bigint generated always as identity primary key,
  order_id    bigint not null references orders(id) on delete cascade,
  product_id  bigint not null references products(id),
  quantity    int not null check (quantity > 0),
  price       numeric(10, 2) not null
);

SQL ที่ใช้บ่อย

-- เพิ่มข้อมูล (Create)
insert into products (name, price, stock)
values ('Latte', 65, 20), ('Croissant', 55, 10);

-- อ่านข้อมูล (Read)
select * from products;
select name, price from products where price < 60 order by price;

-- แก้ข้อมูล (Update)
update products set stock = stock - 1 where id = 1;

-- ลบข้อมูล (Delete)
delete from products where id = 2;

-- รวมตาราง: ออเดอร์นี้มีสินค้าอะไรบ้าง
select o.id, o.customer_name, p.name, oi.quantity
from orders o
join order_items oi on oi.order_id = o.id
join products p on p.id = oi.product_id;
คำสั่งทำอะไร
create tableสร้างตาราง
insert into … valuesเพิ่มแถว
select … from … whereค้นหาแถว
update … set … whereแก้แถว (ลืม where = แก้ทุกแถว!)
delete from … whereลบแถว
join … onรวมข้อมูลจากหลายตาราง

ฝึกต่อ

เว็บสำหรับลงมือทำ

วิดีโอเรียนเอง

Next steps

  • Express: เขียน API ที่อ่านตารางเหล่านี้
CS ChallengeLast updated Oct 2026