การสร้างตารางและคอลัมน์
ฝึกสร้างตารางใหม่ กำหนดชนิดคอลัมน์ ตั้งคีย์หลัก และเพิ่มข้อจำกัดคีย์นอกโดยตรงใน Supabase
การสร้างตารางและคอลัมน์ เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Supabase Backend as a Service และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Building Blocks of Your Data
Welcome to creating your first database! Just like organizing information in a spreadsheet, databases use tables to hold related data.
Think of a table as a single sheet in your spreadsheet. Each table has columns, which are like the headings, defining what kind of information each entry will hold.
Into the Supabase Studio
Supabase provides a powerful web interface called the Supabase Studio (or Dashboard) to manage your project. This is where we'll create our tables.
- First, log in to your Supabase project.
- On the left sidebar, click the Database icon (it looks like a cylinder).
- Then, select Table Editor from the submenu.
Your First Table
Once in the Table Editor, you'll see an option to create a new table. Let's make one for 'products'.
- Click the '+ New Table' button.
- Give your table a descriptive name, like
products. - Keep the schema as
publicfor now. This is the default container for your tables. - Click 'Save' to create the empty table.
Adding Columns to Your Table
An empty table isn't very useful! Now, let's add some columns to our products table. Columns define the specific pieces of information each product will have.
- In the table editor, find your new
productstable. - Click '+ Add column'.
- You'll specify the column's name, data type, and other properties.
Choosing the Right Data Type
Each column needs a data type, which tells the database what kind of data it can store (text, numbers, dates, etc.).
text: For names, descriptions, or any string of characters.integer: For whole numbers (e.g., quantity).numericordecimal: For numbers with decimal points (e.g., price).boolean: For true/false values (e.g.,is_available).uuid: A universally unique identifier, great for primary keys.
Unique Identifiers: Primary Keys
Every table needs a Primary Key (PK). This is a special column (or set of columns) that uniquely identifies each row in the table. No two rows can have the same primary key value.
A common practice is to use a uuid (Universally Unique Identifier) as the primary key. Supabase can automatically generate these for you.
- When adding an
idcolumn, set its type touuid. - Check the 'Is Primary Key' box.
- Set the 'Default Value' to
gen_random_uuid(). This will auto-generate a unique ID for every new row.
Connecting Your Data: Foreign Keys
Databases are powerful because they can link related information across different tables. This is done using a Foreign Key (FK).
A foreign key in one table points to the primary key of another table. It establishes a relationship, ensuring data consistency. For example, a posts table might have a user_id foreign key linking to the id in the users table.
Practical Example: Users & Posts
Let's create two tables, users and posts, and link them using a foreign key. Run this SQL directly in the Supabase SQL Editor (accessible from the left sidebar).
CREATE TABLE users (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
username text NOT NULL UNIQUE,
email text NOT NULL UNIQUE
);
CREATE TABLE posts (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
user_id uuid REFERENCES users(id) ON DELETE CASCADE,
title text NOT NULL,
content text,
created_at timestamp WITH TIME ZONE DEFAULT now()
);Reviewing Your Database Structure
After running the SQL or creating tables via the UI, you can always review your database structure in the Supabase Studio.
- Go back to the Table Editor.
- You'll see your new tables listed.
- Click on a table name to see its columns, data types, and any primary or foreign key constraints.
- The 'Relationships' tab can also show how your tables are connected.
Test Your Knowledge
Which of the following statements about database tables and keys are TRUE?
Tables & Columns: Recap
Great job! In this lesson, you've learned the fundamental concepts of database structure:
- Tables are containers for related data.
- Columns define the specific attributes within a table.
- Data types specify the kind of data a column can hold.
- Primary Keys uniquely identify each row.
- Foreign Keys link tables together, defining relationships.
You now have the skills to start structuring your Supabase database effectively. Next, we'll learn how to put data into these tables!
คำถามที่พบบ่อย
บทเรียน “การสร้างตารางและคอลัมน์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างตารางและคอลัมน์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างตารางและคอลัมน์”
ฝึกสร้างตารางใหม่ กำหนดชนิดคอลัมน์ ตั้งคีย์หลัก และเพิ่มข้อจำกัดคีย์นอกโดยตรงใน Supabase คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างตารางและคอลัมน์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Supabase Backend as a Service นี้ได้ไหม
ได้ บทเรียน Supabase Backend as a Service ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบโครงสร้างฐานข้อมูล
- การสร้างตารางและคอลัมน์
- การเพิ่มและสืบค้นข้อมูลเบื้องต้น
- ความสัมพันธ์และคีย์ภายนอก