创建更新视图sql错误的规则

时间:2015-05-14 16:31:45

标签: sql postgresql

我正在尝试创建 UpdaeTable视图

我有表CREATE TABLE "category" ( "category_id" SERIAL NOT NULL, "name" varchar(15) NOT NULL, "description" varchar(150) NOT NULL, PRIMARY KEY("category_id") ); CREATE TABLE "product" ( "product_id" SERIAL NOT NULL, "name" varchar(20) NOT NULL, "price" int4 NOT NULL, "description" varchar(200), "country_of_origin" varchar(20) NOT NULL, PRIMARY KEY("product_id") ); CREATE TABLE "product_has_category" ( "NMID" SERIAL NOT NULL, "product_id" int4 NOT NULL, "category_id" int4 NOT NULL ); ALTER TABLE "product_has_category" ADD CONSTRAINT "Ref_Product_has_Category_to_Product" FOREIGN KEY ("Product_product_id") REFERENCES "Product"("product_id") MATCH SIMPLE ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE;

CREATE VIEW products_with_categories AS
SELECT product.name AS product_name, product.price, category.name AS     category
FROM product, category, product_has_category
WHERE product_has_category.product_id = product.product_id AND
product_has_category.category_id = category.category_id
ORDER BY product_name;

以下是选择产品,价格和类别的观点:

CREATE RULE prod_cat_upd AS ON UPDATE TO products_with_categories 
DO INSTEAD 
UPDATE product 
   SET product.name=NEW.product.name 
WHERE product.name=OLD.product.name

我希望视图可以更新并创建规则:

property checkURL : "yahoo"
try
    tell application "Safari" to set theTab to first tab of window 1 whose URL contains checkURL
    display dialog "That site is loaded"
on error
    display dialog "That site is not loaded"
end try

我收到以下错误:

  

对表"产品"的FROM子句条目的无效引用       提示:表" product"有一个条目,但不能从查询的这一部分引用它。

我无法理解这个错误意味着什么,以及如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

尝试使用此语法创建规则,Refer以获取更多文档

CREATE RULE prod_cat_upd AS ON UPDATE TO products_with_categories 
DO INSTEAD 
UPDATE product 
       SET name=NEW.name
WHERE name=OLD.name

我使用样本表创建了两个SQL小提琴

1) Sample SQL FIDDLE 有解决方案,我提供: - 工作正常

    CREATE TABLE shoelace_data (
    sl_name    text,          -- primary key
    sl_avail   integer,       -- available number of pairs
    sl_color   text,          -- shoelace color
    sl_len     real,          -- shoelace length
    sl_unit    text           -- length unit
);

CREATE TABLE unit (
    un_name    text,          -- primary key
    un_fact    real           -- factor to transform to cm
);

CREATE VIEW shoelace AS
    SELECT s.sl_name,
           s.sl_avail,
           s.sl_color,
           s.sl_len,
           s.sl_unit,
           s.sl_len * u.un_fact AS sl_len_cm
      FROM shoelace_data s, unit u
     WHERE s.sl_unit = u.un_name;

CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
    DO INSTEAD
    UPDATE shoelace_data
       SET sl_name = NEW.sl_name,
           sl_avail = NEW.sl_avail,
           sl_color = NEW.sl_color,
           sl_len = NEW.sl_len,
           sl_unit = NEW.sl_unit
     WHERE sl_name = OLD.sl_name;

2)你有CREATE RULE的代码: - 失败并且你得到的错误相同

不工作

CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
    DO INSTEAD
    UPDATE shoelace_data
       SET shoelace_data.sl_name = NEW.shoelace_data.sl_name,
           shoelace_data.sl_avail = NEW.shoelace_data.sl_avail,
           shoelace_data.sl_color = NEW.shoelace_data.sl_color,
           shoelace_data.sl_len = NEW.shoelace_data.sl_len,
           shoelace_data.sl_unit = NEW.shoelace_data.sl_unit
     WHERE shoelace_data.sl_name = OLD.shoelace_data.sl_name;

修改: -  您收到此错误: -

  

错误:列new.name不存在

由于您的视图没有“名称”字段,因此您的视图只包含以下三个字段: - product_name, price and category因此请仅使用这些字段

CREATE RULE prod_cat_upd AS ON UPDATE TO products_with_categories 
DO INSTEAD 
UPDATE product 
       SET name=NEW.product_name
WHERE name=OLD.product_name