使用触发器比较来自不同表的值
Comparing values from different tables using triggers
所以问题是我有两个 table,在插入或更新其中一个 table 的值之前,我必须检查另一个 table 是否不包含新值我正在尝试 insert/update 像这样:
CREATE TABLE categoriasimples(
nome varchar(64) PRIMARY KEY REFERENCES categoria(nome) ON DELETE CASCADE);
CREATE TABLE supercategoria(
nome varchar(64) PRIMARY KEY REFERENCES categoria(nome) ON DELETE CASCADE);
CREATE FUNCTION check_categoria_type_sup() RETURNS trigger AS $check_categoria_type_sup$
BEGIN
IF nome FROM categoriasimples WHERE (NEW.nome = CategoriaSimples.nome) IS NOT NULL THEN
RAISE EXCEPTION 'Uma categoria nao pode ser super e simples ao mesmo tempo!';
END IF;
RETURN NEW;
END;
$check_categoria_type_sup$ LANGUAGE plpgsql;
CREATE TRIGGER check_categoria_type_sup BEFORE INSERT OR UPDATE ON supercategoria FOR EACH ROW EXECUTE PROCEDURE check_categoria_type_sup();
CREATE FUNCTION check_categoria_type_simp() RETURNS trigger AS $check_categoria_type_simp$
BEGIN
IF nome FROM supercategoria WHERE (supercategoria.nome = NEW.nome) IS NOT NULL THEN
RAISE EXCEPTION 'Uma categoria nao pode ser simples e super ao mesmo tempo!';
END IF;
RETURN NEW;
END;
$check_categoria_type_simp$ LANGUAGE plpgsql;
CREATE TRIGGER check_categoria_type_simp BEFORE INSERT OR UPDATE ON categoriasimples FOR EACH ROW EXECUTE PROCEDURE check_categoria_type_simp();
我可以将值插入一个 table 但如果我尝试插入另一个 table 它说:
错误:布尔类型的输入语法无效:"bife"(另一个 table 中的值)
上下文:PL/pgSQL 函数 check_categoria_type_sup() 第 3 行在 IF
SQL状态:22P02
评论太长了。
我很确定您可以在不使用触发器的情况下做您想做的事。我建议您调查 inheritance,Postgres 支持 tables.
您可以只在父 table 上定义一个唯一约束,而不必担心尝试使两个不同的 table 同步。
所以问题是我有两个 table,在插入或更新其中一个 table 的值之前,我必须检查另一个 table 是否不包含新值我正在尝试 insert/update 像这样:
CREATE TABLE categoriasimples(
nome varchar(64) PRIMARY KEY REFERENCES categoria(nome) ON DELETE CASCADE);
CREATE TABLE supercategoria(
nome varchar(64) PRIMARY KEY REFERENCES categoria(nome) ON DELETE CASCADE);
CREATE FUNCTION check_categoria_type_sup() RETURNS trigger AS $check_categoria_type_sup$
BEGIN
IF nome FROM categoriasimples WHERE (NEW.nome = CategoriaSimples.nome) IS NOT NULL THEN
RAISE EXCEPTION 'Uma categoria nao pode ser super e simples ao mesmo tempo!';
END IF;
RETURN NEW;
END;
$check_categoria_type_sup$ LANGUAGE plpgsql;
CREATE TRIGGER check_categoria_type_sup BEFORE INSERT OR UPDATE ON supercategoria FOR EACH ROW EXECUTE PROCEDURE check_categoria_type_sup();
CREATE FUNCTION check_categoria_type_simp() RETURNS trigger AS $check_categoria_type_simp$
BEGIN
IF nome FROM supercategoria WHERE (supercategoria.nome = NEW.nome) IS NOT NULL THEN
RAISE EXCEPTION 'Uma categoria nao pode ser simples e super ao mesmo tempo!';
END IF;
RETURN NEW;
END;
$check_categoria_type_simp$ LANGUAGE plpgsql;
CREATE TRIGGER check_categoria_type_simp BEFORE INSERT OR UPDATE ON categoriasimples FOR EACH ROW EXECUTE PROCEDURE check_categoria_type_simp();
我可以将值插入一个 table 但如果我尝试插入另一个 table 它说:
错误:布尔类型的输入语法无效:"bife"(另一个 table 中的值) 上下文:PL/pgSQL 函数 check_categoria_type_sup() 第 3 行在 IF SQL状态:22P02
评论太长了。
我很确定您可以在不使用触发器的情况下做您想做的事。我建议您调查 inheritance,Postgres 支持 tables.
您可以只在父 table 上定义一个唯一约束,而不必担心尝试使两个不同的 table 同步。