0Pricing
Supabase Backend as a Service · 课时

数据库函数与触发器

将逻辑封装在 Postgres 函数中,并使用触发器自动响应数据变更;这些功能都可以从 Supabase 调用。

数据库函数与触发器 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

Logic Inside the Database

Postgres lets you store reusable logic as functions and run code automatically on data changes with triggers. This keeps critical rules close to the data.

What Is a Database Function?

A function is named, reusable SQL or PL/pgSQL that returns a value. You can call it from queries or from the Supabase client via RPC.

Writing a Simple Function

This function returns the count of active users.

create or replace function active_user_count()
returns integer
language sql
as 'select count(*)::int from users where active';

Calling It from Supabase

Use rpc to invoke a database function from the client.

const { data, error } = await supabase
  .rpc('active_user_count');
console.log(data);

Functions With Parameters

Functions can accept arguments, enabling parameterized server-side logic.

create or replace function posts_by(author_id bigint)
returns setof posts
language sql
as 'select * from posts where user_id = author_id';

What Is a Trigger?

A trigger runs a function automatically BEFORE or AFTER an INSERT, UPDATE, or DELETE on a table.

A Trigger Function

Trigger functions return trigger and access the new row via NEW.

create or replace function set_updated_at()
returns trigger
language plpgsql
as 'begin new.updated_at = now(); return new; end;';

Attaching the Trigger

Bind the function to a table and event.

create trigger touch_updated
before update on posts
for each row
execute function set_updated_at();

Common Use Cases

Triggers shine for:

  • Auto-setting timestamps
  • Maintaining denormalized counts
  • Writing audit log rows
  • Creating a profile row when a user signs up

Keep Triggers Lightweight

Triggers run inside the transaction, so heavy work slows every write. Offload long tasks to Edge Functions or queues instead.

function isLightweight(ms) { return ms < 10; }
console.log(isLightweight(4));

Putting It Together

Use functions for reusable, callable logic and triggers for automatic reactions to changes, while keeping trigger work small and fast.

Quick Check

Test your understanding of functions and triggers.

Recap

You learned to write callable database functions, invoke them with rpc, and automate logic with triggers using NEW, while keeping trigger work lightweight.

常见问题解答

「数据库函数与触发器」课时是免费的吗?

是的 — 「数据库函数与触发器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 3 节课。

「数据库函数与触发器」这节课中我会学到什么?

将逻辑封装在 Postgres 函数中,并使用触发器自动响应数据变更;这些功能都可以从 Supabase 调用。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「数据库函数与触发器」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 高级 SQL 查询与连接
  2. 用于性能优化的数据库索引
  3. 数据库函数与触发器
← 返回 Supabase Backend as a Service