---
title: "CREATE RULE"
url: "https://docs.yugabyte.com/stable/api/ysql/the-sql-language/statements/ddl_create_rule/"
---

# CREATE RULE

Use the CREATE RULE statement to create a rule.

## Synopsis

Use the CREATE RULE statement to create a rule.

## Syntax

- [![Grammar Icon](/icons/file-lines.svg)Grammar](#grammar-1)
- [![Diagram Icon](/icons/diagram.svg)Diagram](#diagram-1)

```output.ebnf
create_rule ::= CREATE [ OR REPLACE ] RULE rule_name AS ON rule_event 
                TO table_name  [ WHERE boolean_expression ] DO 
                [ ALSO | INSTEAD ] { NOTHING
                                     | command
                                     | ( command [ ; ... ] ) }

rule_event ::= SELECT | INSERT | UPDATE | DELETE

command ::= SELECT | INSERT | UPDATE | DELETE | NOTIFY
```

#### create\_rule

CREATEORREPLACERULE[rule\_name](/stable/api/ysql/syntax_resources/grammar_diagrams#rule-name)ASON[rule\_event](#rule-event)TO[table\_name](/stable/api/ysql/syntax_resources/grammar_diagrams#table-name)WHERE[boolean\_expression](/stable/api/ysql/syntax_resources/grammar_diagrams#boolean-expression)DOALSOINSTEADNOTHING[command](#command)(;[command](#command))

#### rule\_event

SELECTINSERTUPDATEDELETE

#### command

SELECTINSERTUPDATEDELETENOTIFY

## Semantics

See the semantics of each option in the [PostgreSQL docs](https://www.postgresql.org/docs/15/sql-createrule.html "PostgreSQL docs").

## Examples

Basic example.

```plpgsql
yugabyte=# CREATE TABLE t1(a int4, b int4);
yugabyte=# CREATE TABLE t2(a int4, b int4);
yugabyte=# CREATE RULE t1_to_t2 AS ON INSERT TO t1 DO INSTEAD
             INSERT INTO t2 VALUES (new.a, new.b);
yugabyte=# INSERT INTO t1 VALUES (3, 4);
yugabyte=# SELECT * FROM t1;
```

```output
 a | b
---+---
(0 rows)
```

```plpgsql
yugabyte=# SELECT * FROM t2;
```

```output
 a | b
---+---
 3 | 4
(1 row)
```

## See also

- [DROP RULE](/stable/api/ysql/the-sql-language/statements/ddl_drop_rule "DROP RULE")
