CREATE RULE

Synopsis

Use the CREATE RULE statement to create a new rule.

Syntax

  1. create_rule ::= CREATE [ OR REPLACE ] RULE rule_name AS ON rule_event
  2. TO table_name [ WHERE condition ] DO
  3. [ ALSO | INSTEAD ] { NOTHING
  4. | command
  5. | ( command [ ; ... ] ) }
  6. rule_event ::= SELECT | INSERT | UPDATE | DELETE
  7. command ::= SELECT | INSERT | UPDATE | DELETE | NOTIFY

create_rule

CREATE RULE - 图1

rule_event

CREATE RULE - 图2

command

CREATE RULE - 图3

Semantics

See the semantics of each option in the [PostgreSQL docs][postgresql-docs-create-rule].

Examples

Basic example.

  1. yugabyte=# CREATE TABLE t1(a int4, b int4);
  2. yugabyte=# CREATE TABLE t2(a int4, b int4);
  3. yugabyte=# CREATE RULE t1_to_t2 AS ON INSERT TO t1 DO INSTEAD
  4. INSERT INTO t2 VALUES (new.a, new.b);
  5. yugabyte=# INSERT INTO t1 VALUES (3, 4);
  6. yugabyte=# SELECT * FROM t1;
  1. a | b
  2. ---+---
  3. (0 rows)
  1. yugabyte=# SELECT * FROM t2;
  1. a | b
  2. ---+---
  3. 3 | 4
  4. (1 row)

See also