Em 12/07/13, Douglas Fabiano Specht<[email protected]> escreveu:
> Bom dia pessoal,
>
> estou com uma dificuldade para criar uma regra que não permite insert de
> linhas duplicadas.
> fiz um teste simples, criei uma tabela test e a regra, mas quando vou
> inserir mesmo sem ter nada na tabela ele ja me acusa o erro.
>
> a regra é simples, nao pode ter o tipoacesso e numero iguais..
> será que alguem teria alguma ideia?
>
>
> CREATE TABLE test
> (
>   chave integer,
>   tipoacesso character(1),
>   numero integer
> )
>
>
> create or replace rule protect_double as
>   on INSERT to test
>     where chave in(
> select res.chave from dah.test res
> where new.tipoacesso='R' and new.numero=res.numero)
>
>   do instead select erroDouble();
>
>
> CREATE OR REPLACE FUNCTION erroDouble() RETURNS VOID AS $$
> BEGIN
> RAISE EXCEPTION 'Numero Duplicada não permitido!!!';
> END;
> $$
> LANGUAGE plpgsql;
>
>
> testando o insert
> insert into test values(-5,'R', 1)
>


Não é mais simples criar um índice único nesta dupla de campos?
http://www.postgresql.org/docs/current/interactive/sql-createindex.html

Osvaldo
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a