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