简体   繁体   English

在PostgreSQL表中创建插入,更新规则

[英]creating insert, update rules in postgresql tables

I am trying to create a postgresql table with insert and update rules that can make it possible for the users to automatically insert information or update the the existing information on the database table. 我试图用插入和更新规则创建一个postgresql表,该规则可以使用户自动插入信息或更新数据库表上的现有信息。 When i try to run the SQL query within the database inorder to create the table, i keep getting this error. 当我尝试在数据库中运行SQL查询以创建表时,我不断收到此错误。 I tried to follow the documentaion steps in PostgreSQL 9.1. 我尝试遵循PostgreSQL 9.1中的文档步骤。 It keeps giving me error on the expression for one of my primary keys stating that i need to be re-write it . 它一直使我的一个主键表达式出现错误,表明我需要重新编写它。 Please can somebody look at this script and help me out. 请有人看看这个脚本并帮帮我。 Thanks for your most valued contributions! 感谢您最宝贵的贡献!

Here is my SQL script 这是我的SQL脚本

 CREATE TABLE fieldtally1
  (fieldtally1_id serial NOT NULL,
  pipeno character varying,
  wthick real,
  heatno1 character varying(32),
  pipeno2 character varying(32),
  heatno2 character varying(32),
  Djointno character varying(32),
  ContractorNo character varying(32),
  measuredlength double precision,
  serialno character varying(32),
  CoatingType character varying(32),
  coatingno character varying(32),
  mnfcno character varying(32),
  FactoryLength double precision,
  pipeod_in numeric,
  pipeod_mm numeric,
  pipeweight double precision,
  pipegrade numeric,
  loadtally numeric,
  dateweilded date,
  datereceived date,
  dataenteredby character varying(50),
  deliveryno character varying(50),
  manufacturer character varying(50),
  Remarks character varying(100),
  ManualUser character varying(100),
  log_when timestamp,
  CONSTRAINT fieldtally1_pkey PRIMARY KEY (fieldtally1_id, pipeno)
  );
  Create rule fieldtally1_ins as on INSERT to fieldtally1
  Do Instead
  Insert into fieldtally1 values (
                New.pipeno,
                New.wthick,
                New.heatno1,
                New.pipeno2,
                New.heatno2,
                New.Djointno,
                New.ContractorNo,
                New.measuredlength,
                New.serialno,
                New.coatingtype,
                New.coatingno,
                New.mnfcno,
                New.factorylength,
                New.pipeod_in,
                New.pipeod_mm,
                New.pipeweight,
                New.pipegrade,
                New.ManualUser, 
                current_timestamp
                );

CREATE RULE fieldtally1_upd AS ON UPDATE TO fieldtally1
    DO INSTEAD
    UPDATE fieldtally1
    SET PIPENO = New.pipeno,
    wthick = New.wthick,
    heatno1 = New.heatno1,
    pipeno2 = New.pipeno2,
    heatno2 = New.heatno2,
    Djointno = New.Djointno,
    ContractorNo = New.ContractorNo,
    measuredlength = New.measuredlength,
    New.serialno = New.serialno,
    coatingtype = New.coatingtype,
    coatingno = New.coatingno,
    mnfcno = New.mnfcno,
    factorylength = New.factorylength,
    pipeod_in = New.pipeod_in,
    pipeod_mm = New.pipeod_mm,
    pipeweight = New.pipeweight,
    pipegrade =  New.pipegrade,
    ManualUser = New.ManualUser 
    WHERE pipeno = OLD.pipeno;      

CREATE RULE fieldtally1_del AS ON DELETE TO fieldtally1
    DO INSTEAD
    DELETE FROM fieldtally1
     WHERE pipeno = OLD.pipeno;

Here is the error i get when i run the script 这是我运行脚本时遇到的错误

NOTICE: CREATE TABLE will create implicit sequence "fieldtally1_fieldtally1_id_seq" for serial column "fieldtally1.fieldtally1_id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "fieldtally1_pkey" for table "fieldtally1" ERROR: column "fieldtally1_id" is of type integer but expression is of type character varying LINE 34: New.pipeno, ^ HINT: You will need to rewrite or cast the expression. 注意:CREATE TABLE将为串行列“ fieldtally1.fieldtally1_id”创建隐式序列“ fieldtally1_fieldtally1_id_seq”注意:CREATE TABLE / PRIMARY KEY将为表“ fieldtally1”创建隐式索引“ fieldtally1_pkey”错误:列“ fieldtally1_id”是整数类型,但表达式类型的字符类型随行而异。LINE 34:New.pipeno,^提示:您将需要重写或强制转换表达式。

*** *** Error 错误 ** * ** *

ERROR: column "fieldtally1_id" is of type integer but expression is of type character varying SQL state: 42804 Hint: You will need to rewrite or cast the expression. 错误:“ fieldtally1_id”列的类型为整数,但表达式的类型因字符而异SQL状态:42804提示:您将需要重写或强制转换表达式。 Character: 1024 字元:1024

If your insert doesn't match the column exactly, you need to specify them. 如果您的插入内容与该列不完全匹配,则需要指定它们。 Your update includes a "new." 您的更新包括一个“新”。 on the left hand side of serialno: This works in http://sqlfiddle.com/#!1/c9ef3/1/0 在serialno的左侧:在http://sqlfiddle.com/#!1/c9ef3/1/0中有效

CREATE TABLE fieldtally
(fieldtally_id serial NOT NULL primary key,
  pipeno character varying,
  wthick real,
  heatno1 character varying(32),
  pipeno2 character varying(32),
  heatno2 character varying(32),
  Djointno character varying(32),
  ContractorNo character varying(32),
  measuredlength double precision,
  serialno character varying(32),
  CoatingType character varying(32),
  coatingno character varying(32),
  mnfcno character varying(32),
  FactoryLength double precision,
  pipeod_in numeric,
  pipeod_mm numeric,
  pipeweight double precision,
  pipegrade numeric,
  loadtally numeric,
  dateweilded date,
  datereceived date,
  dataenteredby character varying(50),
  deliveryno character varying(50),
  manufacturer character varying(50),
  Remarks character varying(100),
  ManualUser text,
  log_when timestamp
  );
  Create rule fieldtally_ins as on INSERT to fieldtally
  Do Instead
  Insert into fieldtally (pipeno, wthick, heatno1, pipeno2, heatno2, djointno, contractorno, measuredlength, serialno, coatingtype, coatingno,
                         mnfcno, factorylength, pipeod_in, pipeod_mm, pipeweight, pipegrade, manualuser, log_when) values (
                New.pipeno,
                New.wthick,
                New.heatno1,
                New.pipeno2,
                New.heatno2,
                New.Djointno,
                New.ContractorNo,
                New.measuredlength,
                New.serialno,
                New.coatingtype,
                New.coatingno,
                New.mnfcno,
                New.factorylength,
                New.pipeod_in,
                New.pipeod_mm,
                New.pipeweight,
                New.pipegrade,
                New.ManualUser, 
                current_timestamp
                );

CREATE RULE fieldtally_upd AS ON UPDATE TO fieldtally
    DO INSTEAD
    UPDATE fieldtally
    SET PIPENO = New.pipeno,
    wthick = New.wthick,
    heatno1 = New.heatno1,
    pipeno2 = New.pipeno2,
    heatno2 = New.heatno2,
    Djointno = New.Djointno,
    ContractorNo = New.ContractorNo,
    measuredlength = New.measuredlength,
    serialno = New.serialno,
    coatingtype = New.coatingtype,
    coatingno = New.coatingno,
    mnfcno = New.mnfcno,
    factorylength = New.factorylength,
    pipeod_in = New.pipeod_in,
    pipeod_mm = New.pipeod_mm,
    pipeweight = New.pipeweight,
    pipegrade =  New.pipegrade,
    ManualUser = New.ManualUser 
    WHERE pipeno = OLD.pipeno;      

CREATE RULE fieldtally_del AS ON DELETE TO fieldtally
    DO INSTEAD
    DELETE FROM fieldtally
     WHERE pipeno = OLD.pipeno;

user GO - so sql will not check the valitation before the last go 用户GO-因此sql在最后一次执行之前将不检查评估

CREATE TABLE fieldtally
    (fieldtally_id serial NOT NULL primary key,
      pipeno character varying,
      wthick real,
      heatno1 character varying(32),
      pipeno2 character varying(32),
      heatno2 character varying(32),
      Djointno character varying(32),
      ContractorNo character varying(32),
      measuredlength double precision,
      serialno character varying(32),
      CoatingType character varying(32),
      coatingno character varying(32),
      mnfcno character varying(32),
      FactoryLength double precision,
      pipeod_in numeric,
      pipeod_mm numeric,
      pipeweight double precision,
      pipegrade numeric,
      loadtally numeric,
      dateweilded date,
      datereceived date,
      dataenteredby character varying(50),
      deliveryno character varying(50),
      manufacturer character varying(50),
      Remarks character varying(100),
      ManualUser text,
      log_when timestamp
      );
GO
      Create rule fieldtally_ins as on INSERT to fieldtally
      Do Instead
      Insert into fieldtally values (
                    New.pipeno,
                    New.wthick,
                    New.heatno1,
                    New.pipeno2,
                    New.heatno2,
                    New.Djointno,
                    New.ContractorNo,
                    New.measuredlength,
                    New.serialno,
                    New.coatingtype,
                    New.coatingno,
                    New.mnfcno,
                    New.factorylength,
                    New.pipeod_in,
                    New.pipeod_mm,
                    New.pipeweight,
                    New.pipegrade,
                    New.ManualUser, 
                    current_timestamp
                    );
GO
        CREATE RULE fieldtally_upd AS ON UPDATE TO fieldtally
            DO INSTEAD
            UPDATE fieldtally
            SET PIPENO = New.pipeno,
            wthick = New.wthick,
            heatno1 = New.heatno1,
            pipeno2 = New.pipeno2,
            heatno2 = New.heatno2,
            Djointno = New.Djointno,
            ContractorNo = New.ContractorNo,
            measuredlength = New.measuredlength,
            New.serialno = New.serialno,
            coatingtype = New.coatingtype,
            coatingno = New.coatingno,
            mnfcno = New.mnfcno,
            factorylength = New.factorylength,
            pipeod_in = New.pipeod_in,
            pipeod_mm = New.pipeod_mm,
            pipeweight = New.pipeweight,
            pipegrade =  New.pipegrade,
            ManualUser = New.ManualUser 
            WHERE pipeno = OLD.pipeno;      
    GO
        CREATE RULE fieldtally_del AS ON DELETE TO fieldtally
            DO INSTEAD
            DELETE FROM fieldtally
             WHERE pipeno = OLD.pipeno;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM