PostgreSQL: เก็บสินค้าและออเดอร์
PostgreSQL คือฐานข้อมูลแบบตาราง (relational) ข้อมูลอยู่ใน table ที่เชื่อมกันด้วย key และสั่งงานด้วยภาษา SQL ในคอร์สเราเปิด PostgreSQL บน Supabase เพื่อไม่ต้องตั้งเซิร์ฟเวอร์เอง
At a glance
| คำศัพท์ | ความหมาย |
|---|---|
| Table | ตาราง เช่น products, orders |
| Row / Column | 1 รายการ / 1 ช่องข้อมูล |
| Primary key | คอลัมน์ที่ไม่ซ้ำ ใช้ระบุแต่ละแถว (มักชื่อ id) |
| Foreign key | คอลัมน์ที่ชี้ไปยัง id ของอีกตาราง |
| SQL | ภาษาที่ใช้สั่งฐานข้อมูล |
| Connection string | URL ที่บอก Express ว่าฐานข้อมูลอยู่ไหน และใช้รหัสอะไร |
Installation
ใช้ PostgreSQL บน Supabase (ฟรี ไม่ต้องติดตั้งบนเครื่อง)
สร้างโปรเจกต์
ไปที่ supabase.com/dashboard ล็อกอินด้วย GitHub แล้วกด New project ตั้งรหัสผ่านฐานข้อมูลและจดไว้
เลือก Region
เลือก Southeast Asia (Singapore) เพื่อให้เร็วที่สุดจากไทย
เปิด SQL Editor
เมนูซ้าย → SQL Editor แล้ววางคำสั่งสร้างตารางด้านล่าง กด Run
เอา 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 | คอลัมน์ | เชื่อมกับ |
|---|---|---|
products | id, name, price, stock, created_at | — |
orders | id, customer_name, total, status, created_at | — |
order_items | id, order_id, product_id, quantity, price | orders.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 ที่อ่านตารางเหล่านี้

