Which one to use or which one is the better? There's any difference?
searchtype_id bigint NOT NULL
or
CONSTRAINT searchtype_id_nn CHECK ((searchtype_id IS NOT NULL))
Which one to use or which one is the better? There's any difference?
searchtype_id bigint NOT NULL
or
CONSTRAINT searchtype_id_nn CHECK ((searchtype_id IS NOT NULL))
Is there a difference? Yes. NOT NULL
is part of the storage definition of the type of the column. So, NOT NULL
can affect how the value is stored (is a NULL
-flag required?). A NOT NULL
definition can also be used for optimizations during the compilation phase of a query.
By contrast, a CHECK
constraint does validate that the data meets certain characteristics, but it is less likely that this information will be used in the compilation phase.
The NOT NULL
definition predates the CHECK
constraint and is standard across all databases.
NULL-ability is something that I think of as part of the type -- because it is a declaration built into the language that says "this column is required to have a value". An integer column that can take on NULL
values is subtly different from an integer that cannot.
I would recommend using the NOT NULL
syntax rather than a CHECK
constraint. It gives the database more information about the column.
Both are different and it's hard to choose between them as some things apply to one while some doesn't to other like NOT NULL constraint can only be declared inline while CHECK constraints can applied to out-of-line constraint. If I had to choose between one of them I'll choose out-of-line as: