When I'm adding some constraints, e.g:
create table Test(
IDTest int primary key,
Credit int not null constraint Credit check (Credit >= 0)
);
In this case isn't the not null
in Credit
redundant as I'm adding a constraint that Credit
must be higher than 0?
The PRIMARY KEY column(s) will become NOT NULL. More correct "columns mentioned in PK expression" - PK may be built by any valid expression and not refer to column separately and directly. This is truly redundant.
To remove a NOT NULL constraint for a column in SQL Server, you use the ALTER TABLE .... ALTER COLUMN command and restate the column definition.
We can remove a NOT NULL constraint from a column of an existing table by using the ALTER TABLE statement.
To enforce NOT NULL for a column in SQL Server, use the ALTER TABLE .. ALTER COLUMN command and restate the column definition, adding the NOT NULL attribute.
No, it is not redundant.
A CHECK
constraint accepts a value if the condition is not FALSE
, so whether it is TRUE
or UNKNOWN
.
If you allow Nulls in your column, then a NULL >= 0
will evaluate to UNKNOWN
and will pass the test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With