mirror of
https://github.com/wahyd4/knowledge.git
synced 2026-08-09 05:06:28 +10:00
Add postgres trigger
This commit is contained in:
@@ -32,6 +32,41 @@
|
||||
- will impacted by time zone
|
||||
- index by timestamp will be faster than datetime due to 4 bytes.
|
||||
|
||||
## Postgres trigger example
|
||||
|
||||
Giving we have a table called users, and the columns are:
|
||||
```bash
|
||||
id name age
|
||||
1 tom 10
|
||||
2 Dave 17
|
||||
```
|
||||
there is a requirement which is when some user's age turn to `18` move that person to `adults` table.
|
||||
The adults table have the same fields with `users`.
|
||||
|
||||
The potential trigger implementation can be
|
||||
|
||||
```sql
|
||||
|
||||
CREATE OR REPLACE FUNCTION to_adults ()
|
||||
RETURNS TRIGGER
|
||||
AS $$
|
||||
BEGIN
|
||||
INSERT INTO adults VALUES (new.*);
|
||||
DELETE FROM users WHERE age >= 18;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$
|
||||
LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER to_adults_trigger
|
||||
AFTER UPDATE ON users
|
||||
FOR EACH ROW
|
||||
WHEN (NEW.age >= 18)
|
||||
EXECUTE PROCEDURE to_adults();
|
||||
```
|
||||
|
||||
|
||||
|
||||
## SQL join diagram
|
||||
|
||||

|
||||
|
||||
Reference in New Issue
Block a user