Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Redundant NOT NULL Constraint

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?

like image 289
RSort Avatar asked Mar 04 '12 12:03

RSort


People also ask

IS NOT NULL redundant for PRIMARY KEY?

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.

How do you remove a NOT NULL constraint?

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.

Can we drop NOT NULL constraint existing table?

We can remove a NOT NULL constraint from a column of an existing table by using the ALTER TABLE statement.

How do you add NOT NULL constraints in existing columns?

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.


1 Answers

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.

like image 114
ypercubeᵀᴹ Avatar answered Sep 28 '22 00:09

ypercubeᵀᴹ