创建表和列
亲手在 Supabase 中创建新表、定义列类型、设置主键并添加外键约束。
创建表和列 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。
「创建表和列」这节课中我会学到什么?
亲手在 Supabase 中创建新表、定义列类型、设置主键并添加外键约束。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Supabase Backend as a Service 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「创建表和列」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?
能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。