Em 19 de janeiro de 2017 10:55, Rafael Sousa <[email protected]> escreveu:
>
> é possivel colocar um not null apenas se outro campo for por exemplo true ?

É possível criando um check constraint:

postgres=# create database checks;
CREATE DATABASE

postgres=# \c checks;
Você está conectado agora ao banco de dados "checks" como usuário "postgres".

checks=# create table public.teste_check(att1a boolean not null
default false, att2b integer);
CREATE TABLE

## Criação da Check Constraint
checks=# alter table public.teste_check add constraint ck_teste_2b
check ( case when att1a is true then att2b is not null end  );
ALTER TABLE

## Registro válido
checks=# insert into public.teste_check(att1a, att2b) values(false,null);
INSERT 0 1

## Registro inválido, já que attr1a é TRUE, attr2b não pode ser null
checks=# insert into public.teste_check(att1a, att2b) values(true,null);
ERROR:  new row for relation "teste_check" violates check constraint
"ck_teste_2b"
DETALHE:  Failing row contains (t, null).



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

Responder a