During the interview my friend was asked, why SQL Server allows NULLs for the BIT datatype.
I like to explain things with analogies and this is no different. Consider a table holding job applicants details for a Job Board Website. Something Like this:-
I like to explain things with analogies and this is no different. Consider a table holding job applicants details for a Job Board Website. Something Like this:-
CREATE TABLE dbo.tblJobApplicants (
CandidateID int,
JobID int,
DateApplied date,
isApproved bit
)
-- Applicant is approved
INSERT INTO dbo.tblJobApplicants
SELECT
1,
1,
GETDATE(),
1
-- Applicant is rejected
INSERT INTO dbo.tblJobApplicants
SELECT
2,
1,
GETDATE(),
0
-- Applicant is in progress means just applied
INSERT INTO dbo.tblJobApplicants
SELECT
3,
1,
GETDATE(),
NULL
The key field to note here is of course the BIT field which indicates the approval or disapproval of the applicant. Obviously, when a candidate applies to a job, the status of the applicant isn't known means the applicant has not been accepted, nor rejected. I think, it is the only end of the application in which this field can make a meaningful value. Hopefully, this example helps explain just when you might require a NULL bit field.
That’s it!!…..Happy Programming...