Check BIT Column [closed]

Posted on

Question :

I have the following table:

  • Id – unique number for every user. There will be no more than 2^63-1 users. (Auto incremented)
  • Username – unique identifier of the user will be no more than 30 characters (non Unicode). (Required)
  • Password – password will be no longer than 26 characters (non Unicode). (Required)
  • ProfilePicture – image with size up to 900 KB.
  • LastLoginTime
  • IsDeleted – shows if the user deleted his/her profile. Possible states are true or false.

This is my SQL query:

CREATE TABLE Users (
Id INT PRIMARY KEY IDENTITY,
Username VARCHAR(30) NOT NULL,
[Password] VARCHAR(26) NOT NULL,
ProfilePicture VARBINARY(MAX) CHECK (DATALENGTH(ProfilePicture) <= 900000),
LastLoginTime DATETIME,
IsDeleted BIT
)

Is there a way to check (validate) the ‘IsDeleted’ column? (if it is true or false)

Answer :

Alter the column definition to NOT NULL and SQL Server will do it for you, implicitly.

A column defined as …

IsDeleted BIT NOT NULL 

… can ONLY hold the values 0 or 1, representing False or True.

A column defined as …

IsDeleted BIT 

… can also hold the value NULL.

However, this field should never require explicit “validation”!

It should not be editable by (nor even shown to) any User.

This is an “application” field that controls application behaviour, not a “data” field that a User can actively muck about with.

If they delete their Profile then, as far as they’re concerned, it’s gone.

Leave a Reply

Your email address will not be published. Required fields are marked *