Contents
CREATE RULE
An SQL command for defining a rule
CREATE RULE
is a DDL command for defining a rule.
CREATE RULE
has always been present in PostgreSQL.
Change history
- PostgreSQL 7.3
CREATE [ OR REPLACE ] RULE
syntax added (commit 248c67d7)
Examples
Example usage for CREATE RULE
, here creating a rule which results in rows with odd-numbered id values being dropped from INSERT
operations:
postgres=# CREATE RULE foobar AS ON INSERT TO foo WHERE NEW.id % 2 = 1 DO INSTEAD NOTHING; CREATE RULE
Attempting to insert a sequential series of values results in the target table being populated as follows:
postgres=# INSERT INTO foo VALUES (1), (2), (3); INSERT 0 1 postgres=# SELECT * FROM foo; id ---- 2 (1 row)
References
- PostgreSQL documentation: CREATE RULE