About the answer to my problem, I was able to solve it with the help of Diego Marques:
CREATE TABLE usuario(
id serial,
cpf varchar(20),
cnpj varchar(20)
);
create or replace function cpfcnpjVerification() returns trigger language plpgsql as $$
begin
IF (NEW.cpf IS NULL AND NEW.cnpj IS NULL) THEN
RAISE SQLSTATE '09000'
USING MESSAGE = 'Column CPF and CNPJ cannot both be null';
END IF;
return NEW;
end $$;
create trigger InsertCpfCnpjNotNull before insert on usuario
for each row execute procedure cpfcnpjVerification();
INSERT INTO usuario(cpf, cnpj) VALUES('22','33') -- normal
INSERT INTO usuario(cpf, cnpj) VALUES(NULL,'44') -- normal
INSERT INTO usuario(cpf, cnpj) VALUES('55',NULL) -- normal
INSERT INTO usuario(cpf, cnpj) VALUES(NULL,NULL) -- ERROR: Column CPF and CNPJ cannot both be null
--SQL state: 09000
--Context: PL/pgSQL function cpfcnpjverification() line 4 at RAISE
One question I was left was in relation to SQL STATE, which to use, but based on Postgres I chose the 09000 even though I don’t think it makes that much difference to me.
Thank you
Ball show but in Postgres does not work correctly?
– Hermus
The logic works yes, just create a Trigger that returns null. I put an example of Trigger with null return in the answer.
– Diego Marques
I don’t know that part of triggers, I’ve never actually used it, although I know it’s very useful, but if you could give me a hand, I’d appreciate it. Obviously Postgres uses some different Mysql functions, but can you help me place a message? What I’ve done so far is: [https://www.db-fiddle.com/f/sJLeGX8tQybn4o87hnN8n1/0] This for Postgres worked here, it just didn’t return any messages because:
USING MESSAGE = cpf || ' and ' || cnpf || ' cannot both be null';
Generates a message nothing to do, you have how to help me in this? Gracias– Hermus