0Pricing
Supabase Backend as a Service · บทเรียน

การออกแบบโครงสร้างฐานข้อมูล

เรียนรู้แนวทางปฏิบัติที่ดีที่สุดในการจัดโครงสร้างข้อมูล กำหนดความสัมพันธ์ และวางแผนตารางฐานข้อมูลให้ขยายระบบได้และมีประสิทธิภาพ

การออกแบบโครงสร้างฐานข้อมูล เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Supabase Backend as a Service และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What's a Database Schema?

Imagine building a house. Before you lay a single brick, you need a blueprint! In databases, that blueprint is called a schema.

A database schema defines the structure of your data. It tells you:

  • What tables exist
  • What columns each table has
  • How different tables are connected

A good schema is key for a well-organized, efficient, and scalable Supabase project.

Identify Entities & Attributes

First, think about the 'things' you want to store. These are your entities. For a social app, entities might be Users, Posts, or Comments.

Next, what details does each entity have? These are its attributes.

  • User: name, email, profile picture URL
  • Post: title, content, creation date

Listing these helps you imagine your tables.

Primary Keys: Your Unique ID

Every entity needs a way to be uniquely identified. That's where a Primary Key comes in!

A primary key is a column (or set of columns) that contains a unique value for each row in a table. Think of it like a student ID number – no two students have the same one.

The most common primary key is an id column, often an auto-incrementing integer or a UUID (Universally Unique Identifier).

How Data Connects: Relationships

Your entities don't live in isolation; they interact! These interactions are called relationships.

The most common type is One-to-Many. For example:

  • One User can write Many Posts.
  • One Album can have Many Songs.

Understanding these connections is crucial for linking your tables.

Foreign Keys: Building Connections

To establish a One-to-Many relationship, we use a Foreign Key.

A foreign key is a column in one table that refers to the primary key in another table. It's how we say, 'This post belongs to this specific user.'

If a posts table needs to know which user created it, the posts table would have a user_id column that points to the id of a row in the users table.

Data Types: Store Smartly

When defining your columns, you need to pick a data type. This tells the database what kind of information that column will hold (text, numbers, dates, etc.).

Common PostgreSQL (Supabase's database) data types include:

  • text: For names, descriptions
  • integer: For whole numbers (e.g., age, count)
  • boolean: For true/false values (e.g., is_active)
  • timestamp: For dates and times (e.g., created_at)

Choosing the right type saves space and improves performance.

Normalization: Tidy Your Data

Normalization is a process to organize your database to reduce data redundancy and improve data integrity.

Imagine if every post stored the user's full name and email. If the user changes their email, you'd have to update it in many places! Normalization helps avoid this.

By splitting data into separate, related tables (e.g., users and posts), you store common information only once.

Denormalization: For Speed

While normalization is great for data integrity, sometimes it can slow down data retrieval because you need to 'join' many tables.

Denormalization is the intentional introduction of redundancy to improve read performance. For example, storing a user's name directly in a posts table, even though it's also in the users table.

This is a trade-off: faster reads, but more complex updates and potential for inconsistencies. Use it wisely!

Design in Action: Users & Tasks

Let's design a simple schema for users and their to-do tasks. Each user can have many tasks.

Here's how we might define the tables based on our design principles:

CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name TEXT NOT NULL,
  email TEXT UNIQUE NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE TABLE tasks (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES users(id) ON DELETE CASCADE,
  title TEXT NOT NULL,
  description TEXT,
  is_complete BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

Quick Check: Design Concepts

You're planning a database for a blog. You need to store Authors and their Articles. Each author can write many articles.

Which of these statements correctly apply good schema design principles for this scenario?

Recap: Your Design Journey

You've just taken your first steps into database schema design!

We covered:

  • Identifying entities and attributes
  • The role of primary and foreign keys
  • Understanding relationships (especially One-to-Many)
  • The importance of selecting proper data types
  • Briefly touching on normalization and denormalization

A well-designed schema is the foundation of any robust application. In the next lesson, we'll put this knowledge into practice by creating tables!

คำถามที่พบบ่อย

บทเรียน “การออกแบบโครงสร้างฐานข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การออกแบบโครงสร้างฐานข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบโครงสร้างฐานข้อมูล”

เรียนรู้แนวทางปฏิบัติที่ดีที่สุดในการจัดโครงสร้างข้อมูล กำหนดความสัมพันธ์ และวางแผนตารางฐานข้อมูลให้ขยายระบบได้และมีประสิทธิภาพ คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การออกแบบโครงสร้างฐานข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Supabase Backend as a Service นี้ได้ไหม

ได้ บทเรียน Supabase Backend as a Service ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การออกแบบโครงสร้างฐานข้อมูล
  2. การสร้างตารางและคอลัมน์
  3. การเพิ่มและสืบค้นข้อมูลเบื้องต้น
  4. ความสัมพันธ์และคีย์ภายนอก
← กลับไปที่ Supabase Backend as a Service