PostgreSQL's Rules system

Source: Internet
Author: User
Tags postgresql

"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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.