Skip to content

RULE

CREATE TABLE NEW_ORDERS (
ORDER_ID serial PRIMARY KEY,
ITEM_NAME varchar(255),
QUANTITY int
);
CREATE RULE new_orders AS ON INSERT TO ORDERS
WHERE NEW.QUANTITY > 100
DO INSTEAD
INSERT INTO NEW_ORDERS VALUES (NEW.ORDER_ID, NEW.ITEM_NAME, NEW.QUANTITY);
CREATE TABLE
CREATE RULE

This example creates a RULE in PostgreSQL. The RULE ‘new_orders’ is set on an INSERT action on the table ‘ORDERS’. The rule is designed to check if the quantity of the new order is more than 100. If this condition is true, the new row data is instead inserted into the ‘NEW_ORDERS’ table. The OUTPUT represents the successful creation of the table and rule.