Use Rules to create a table view in PostgreSQL that can be queried, inserted, modified, and deleted

Source: Internet
Author: User

The details are as follows:
Note the relationship between new. attribute and old. attribute and source table and view attributes.
[Root @ me root] # su postgres
Bash-2.05b $ psql-E-h me.linux.edu.cn mydb1
* ******** QUERY **********
BEGIN; SELECT usesuper FROM pg_catalog.pg_user WHERE usename = 'hours'; COMMIT
**************************

Welcome to psql 7.3.2, PostgreSQL interactive terminal.

Type \ copyright to obtain the publishing information.
\ H help with SQL commands
\? Obtain the help of the Internal Reverse Support Command
\ G or end with a semicolon for query
\ Q exit

Mydb1 = # \ help
Available help:
ABORT CREATE TABLE EXECUTE
ALTER DATABASE CREATE TABLE AS EXPLAIN
ALTER GROUP CREATE TRIGGER FETCH
ALTER TABLE CREATE TYPE GRANT
ALTER TRIGGER CREATE USER INSERT
ALTER USER CREATE VIEW LISTEN
ANALYZE DEALLOCATE LOAD
BEGIN DECLARE LOCK
CHECKPOINT DELETE MOVE
Close drop aggregate between Y
CLUSTER DROP CAST PREPARE
COMMENT DROP CONVERSION REINDEX
COMMIT DROP DATABASE RESET
COPY DROP DOMAIN REVOKE
CREATE AGGREGATE DROP FUNCTION ROLLBACK
CREATE CAST DROP GROUP SELECT
CREATE CONSTRAINT TRIGGER DROP INDEX SELECT
CREATE CONVERSION DROP LANGUAGE SET
CREATE DATABASE DROP OPERATOR CLASS SET CONSTRAINTS
CREATE DOMAIN DROP OPERATOR SET SESSION AUTHORIZATION
CREATE FUNCTION DROP RULE SET TRANSACTION
CREATE GROUP DROP SCHEMA SHOW
CREATE INDEX DROP SEQUENCE START TRANSACTION
CREATE LANGUAGE DROP TABLE TRUNCATE
CREATE OPERATOR CLASS DROP TRIGGER UNLISTEN
CREATE OPERATOR DROP TYPE UPDATE
CREATE RULE DROP USER VACUUM
CREATE SCHEMA DROP VIEW
CREATE SEQUENCE END
Mydb1 = # select * from table_tamplate;
Sname | sadds | sphon | semail
-------- + -------------- + ------------------
Bu shi | New York Town, New York state | 010-84452484 | bu shi @ USA. New York
Putin | Moscow town | 020-12345678 | Putin @ Russia. Moscow
Blair | London Town | 030-12345678 | Blair @ UK. London
K | Paris Town | 040-12345678 | K @ France. Paris
(4 rows)

Mydb1 = # create table president () inherits (table_tamplate );
CREATE TABLE
Mydb1 = # select * from president;
Sname | sadds | sphon | semail
------- + --------
(0 rows)

Mydb1 = # \ help create view
Command: CREATE VIEW
Description: define a new view.
Syntax:
CREATE [or replace] VIEW [(column name list)] as select query

Mydb1 = # create view v_president (name, adds, phon, email) as select p. sname, p. sadds, p. sphon, p. semail from president p;
CREATE VIEW
Mydb1 = # select * from v_president;
Name | adds | phon | email
------ + -------
(0 rows)

Mydb1 = # insert into v_president values ('jin Dazhong ', 'seoul', '050-12345678 ', 'jin Dazhong @ Korea. Seoul ');
Error: Cannot insert into a view
You need an unconditional on insert do instead rule
Mydb1 = # Haha, it seems that data cannot be inserted in the view currently, so you have to use the rules !;

Mydb1 = # create rule r_insert_v_president as on insert TO v_president do instead
Mydb1-# insert into president (sname, sadds, sphon, semail) values (new. name, new. adds, new. phon, email );
Error: Attribute "email" not found
Mydb1 = # create rule r_insert_v_president as on insert TO v_president do instead
Mydb1-# insert into president (sname, sadds, sphon, semail) values (new. name, new. adds, new. phon, new. email );
CREATE RULE
Mydb1 = # insert into v_president values ('jin Dazhong ', 'seoul', '050-12345678 ', 'jin Dazhong @ Korea. Seoul ');
INSERT 17287 1
Mydb1 = # select * from v_president;
Name | adds | phon | email
-------- + ------ + -------------- + ------------------
Jin Dazhong | Seoul | 050-12345678 | Jin Dazhong @ Korea. Seoul
(1 line)

Mydb1 = # select * from president;
Sname | sadds | sphon | semail
-------- + ------- + -------------- + ------------------
Jin Dazhong | Seoul | 050-12345678 | Jin Dazhong @ Korea. Seoul
(1 line)

Mydb1 = # Haha, now you can insert data! Rewrite and delete again!

Mydb1 = # create rule r_delete_v_president as on delete TO v_president do instead
Mydb1-# delete from president where sname = old. name;
CREATE RULE
Mydb1 = # delete from v_president where name ~ '^ King ';
DELETE 1
Mydb1 = # select * from v_president;
Name | adds | phon | email
------ + -------
(0 rows)

Mydb1 = # select * from president;
Sname | sadds | sphon | semail
------- + --------
(0 rows)

Mydb1 = # It seems that deletion is okay! To update!
Mydb1 = # create rule r_update_v_president as on update TO v_president do instead
Mydb1-# update president set sname = new. name, sadds = new. adds, sphon = new. phon, semail = new. email
Mydb1-# where sname = new. name;
CREATE RULE
Mydb1 = # insert into v_president values ('jin Dazhong ', 'seoul', '050-12345678 ', 'jin Dazhong @ Korea. Seoul ');
INSERT 17290 1
Mydb1 = # select * from v_president;
Name | adds | phon | email
-------- + ------ + -------------- + ------------------
Jin Dazhong | Seoul | 050-12345678 | Jin Dazhong @ Korea. Han
(1 line)

Mydb1 = # select * from president;
Sname | sadds | sphon | semail
-------- + ------- + -------------- + ------------------
Jin Dazhong | Seoul | 050-12345678 | Jin Dazhong @ Korea. Han
(1 line)

Mydb1 = # update v_president set email = 'jin Dazhong @ Seoul. Korea 'where name = 'jin da ';
UPDATE 1
Mydb1 = # select * from v_president;
Name | adds | phon | email
-------- + ------ + -------------- + ------------------
Jin Dazhong | Seoul | 050-12345678 | Jin Dazhong @ Seoul. Korea
(1 line)

Mydb1 = # select * from president;
Sname | sadds | sphon | semail
-------- + ------- + -------------- + ------------------
Jin Dazhong | Seoul | 050-12345678 | Jin Dazhong @ Seoul. Korea
(1 line)

Mydb1 = # All right, now our view v

Mydb1 = # Okay. Now we can query, insert, modify, and delete data in the view! Rules can also be used as triggers !;
Mydb1 = #

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.