"Class=" Wiz-editor-body wiz-readonly "contenteditable=" false ">
Postgresql provides a system of rules. A rule system is a system that queries rewrite rules. From the usage, some functions of the rule system can be implemented by functions and triggers, but the rule system is completely different from the trigger, it is the SQL that is sent to the user before execution is changed into another SQL by the internal rule definition . a way to do it later.
The basic syntax for a rule is as follows:
CREATE RULE Rule name as
on {SELECT| INSERT | UPDATE | DELETE }
& nbsp; to table name [ where rule condition ]
do [INSTEAD] {Nothing | Command | ( command , command ...)}
in the PostgreSQL , the view is actually implemented by rules, as illustrated below:
-- Create a View V_test1
mydb=# CREATE VIEW v_test1 asselect * from T1;
CREATE VIEW
-- Create the table used by the rule before creating the rule
mydb=# CREATE TABLE V_test2 (Id1int);
CREATE TABLE
mydb=# Create Rule "_return" as on select to V_test2 does instead select * from T1;
CREATE RULE
the name of the rule must be written "_return" , or report the following error
ERROR: View rule for "V_test2" must benamed "_return"
The result of the creation is as follows, and you can see V_test1 , V_test2 types are views.
PostgreSQL's Rules system