You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: psql/advance.md
+29-6Lines changed: 29 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ CREATE TABLE example_table (
57
57
);
58
58
```
59
59
60
-
Constraints can be defined when a table is created or added later using the ALTER TABLE statement. Constraints can also be dropped using tthe ALTER TABLE statement.
60
+
Constraints can be defined when a table is created or added later using the ALTER TABLE statement. Constraints can also be dropped using the ALTER TABLE statement.
61
61
62
62
## Joins
63
63
@@ -198,25 +198,48 @@ In the above syntax, the trigger function trigger_function_name is created using
198
198
199
199
Example:
200
200
```sql
201
-
-- Example of creating a trigger
201
+
-- 1. Create target table
202
+
CREATETABLEtarget_table (
203
+
id SERIALPRIMARY KEY,
204
+
name TEXT,
205
+
age INT
206
+
);
207
+
208
+
-- 2. Create audit table
209
+
CREATETABLEaudit_table (
210
+
audit_id SERIALPRIMARY KEY,
211
+
event_type TEXTNOT NULL,
212
+
event_time TIMESTAMPNOT NULL DEFAULT NOW(),
213
+
table_name TEXTNOT NULL,
214
+
old_data JSONB,
215
+
new_data JSONB
216
+
);
217
+
218
+
-- 3. Create trigger function
202
219
CREATE OR REPLACEFUNCTIONlog_changes()
203
220
RETURNS TRIGGER AS $$
204
221
BEGIN
205
222
IF TG_OP ='INSERT' THEN
206
223
INSERT INTO audit_table (event_type, event_time, table_name, new_data)
0 commit comments