Skip to content

Commit d9cd22e

Browse files
committed
adding notes
Former-commit-id: a71e39a
1 parent 9fe2466 commit d9cd22e

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

notes.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
==== Constraints
2+
3+
SELECT c.conname AS constraint_name,
4+
CASE c.contype
5+
WHEN 'c' THEN 'CHECK'
6+
WHEN 'f' THEN 'FOREIGN KEY'
7+
WHEN 'p' THEN 'PRIMARY KEY'
8+
WHEN 'u' THEN 'UNIQUE'
9+
END AS "constraint_type",
10+
CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS is_deferrable,
11+
CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS is_deferred,
12+
t.relname AS table_name,
13+
array_to_string(c.conkey, ' ') AS constraint_key,
14+
CASE confupdtype
15+
WHEN 'a' THEN 'NO ACTION'
16+
WHEN 'r' THEN 'RESTRICT'
17+
WHEN 'c' THEN 'CASCADE'
18+
WHEN 'n' THEN 'SET NULL'
19+
WHEN 'd' THEN 'SET DEFAULT'
20+
END AS on_update,
21+
CASE confdeltype
22+
WHEN 'a' THEN 'NO ACTION'
23+
WHEN 'r' THEN 'RESTRICT'
24+
WHEN 'c' THEN 'CASCADE'
25+
WHEN 'n' THEN 'SET NULL'
26+
WHEN 'd' THEN 'SET DEFAULT'
27+
END AS on_delete,
28+
CASE confmatchtype
29+
WHEN 'u' THEN 'UNSPECIFIED'
30+
WHEN 'f' THEN 'FULL'
31+
WHEN 'p' THEN 'PARTIAL'
32+
END AS match_type,
33+
t2.relname AS references_table,
34+
array_to_string(c.confkey, ' ') AS fk_constraint_key
35+
FROM pg_constraint c
36+
LEFT JOIN pg_class t ON c.conrelid = t.oid
37+
LEFT JOIN pg_class t2 ON c.confrelid = t2.oid;
38+
39+
40+
SELECT tc.constraint_name
41+
, tc.constraint_type
42+
, tc.table_name
43+
, kcu.column_name
44+
, tc.is_deferrable
45+
, tc.initially_deferred
46+
, rc.match_option AS match_type
47+
, rc.update_rule AS on_update
48+
, rc.delete_rule AS on_delete
49+
, ccu.table_name AS references_table
50+
, ccu.column_name AS references_field
51+
FROM information_schema.table_constraints tc
52+
LEFT JOIN information_schema.key_column_usage kcu
53+
ON tc.constraint_catalog = kcu.constraint_catalog
54+
AND tc.constraint_schema = kcu.constraint_schema
55+
AND tc.constraint_name = kcu.constraint_name
56+
LEFT JOIN information_schema.referential_constraints rc
57+
ON tc.constraint_catalog = rc.constraint_catalog
58+
AND tc.constraint_schema = rc.constraint_schema
59+
AND tc.constraint_name = rc.constraint_name
60+
LEFT JOIN information_schema.constraint_column_usage ccu
61+
ON rc.unique_constraint_catalog = ccu.constraint_catalog
62+
AND rc.unique_constraint_schema = ccu.constraint_schema
63+
AND rc.unique_constraint_name = ccu.constraint_name
64+
WHERE tc.constraint_schema = 'public'
65+
AND tc.constraint_type = 'UNIQUE';
66+
67+
68+
==== Trigger and Function
69+
70+
SELECT *
71+
FROM information_schema.triggers
72+
WHERE trigger_schema NOT IN
73+
('pg_catalog', 'information_schema');
74+
75+
76+
CREATE TABLE emp (
77+
empname text,
78+
salary integer,
79+
last_date timestamp,
80+
last_user text
81+
);
82+
83+
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
84+
BEGIN
85+
-- Check that empname and salary are given
86+
IF NEW.empname IS NULL THEN
87+
RAISE EXCEPTION 'empname cannot be null';
88+
END IF;
89+
IF NEW.salary IS NULL THEN
90+
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
91+
END IF;
92+
93+
-- Who works for us when she must pay for it?
94+
IF NEW.salary < 0 THEN
95+
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
96+
END IF;
97+
98+
-- Remember who changed the payroll when
99+
NEW.last_date := current_timestamp;
100+
NEW.last_user := current_user;
101+
RETURN NEW;
102+
END;
103+
$emp_stamp$ LANGUAGE plpgsql;
104+
105+
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
106+
FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
107+
108+
CREATE TRIGGER check_update
109+
BEFORE UPDATE ON emp
110+
FOR EACH ROW
111+
WHEN (OLD.empname IS DISTINCT FROM NEW.empname)
112+
EXECUTE PROCEDURE emp_stamp();

0 commit comments

Comments
 (0)