Using domains to add a non-nullable field
Instead of specifying data types and constraints directly, you can also use domains, e.g. like this:
create domain inn as int not null;
alter table Adventures add id inn;
Or like this:
create domain icnn as int check (value is not null);
alter table Adventures add id icnn;
For the presence of NULL
s in the added columns, returning of false 0
's, effects of default values etc., it makes no difference at all whether you take the domain route or the direct approach.The only difference is that domain-based constraints can’t be removed at the column level.So if you ever want to drop the constraint later, you must either switch the column to another domain or built-in type again, or remove the constraint from the entire domain.The latter operation is described in the section [nullguide-change-domain-nullability].