课程: Supabase Essential Training
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Writing trigger functions
- [Instructor] Triggers are useful for reacting to changes in a table. This way you can add functionality that does work for you automatically. For instance, this table is keeping track of gift card balances. We can write a trigger that will automatically create a log of what is being added to or deducted from the balance. We're going to do that in this gift_card_transactions table. It has an id, a timestamp, the amount in cents, and a status. Let's go to the SQL console now and add a trigger. We're creating a function called generate_gift_card_transaction, and it returns a trigger as its return type. And then all this function is doing is making an entry into that gift_card_transactions table. Once it's done with that, it is returning the new record. Notice we have both the old record and the new record here being referenced. We're taking the old records balance and subtracting the new records balance and then setting that in the transaction. Notice here at the end, we're returning…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。