Thursday, March 22, 2012

easy (?) SQL constraint question

I have a table where there are tow columns, 'B_Deleted' and 'SDT_DeletedOn'.

Can I create a constraint that says that, if B_Deleted is 1, then a date must be put in SDT_DeletedOn.

Here is what I have so far:

CREATE TABLE myTable
(
B_Deleted bit DEFAULT(0),
SDT_DeletedOn smalldatetime
);Unfortunately you can't as the primary purpose of CONSTRAINT is integrity of your table, and not to mention it expects a TRUE or FALSE condition, so adding another step of adding a date upon modification of a column isn't possible. You have to use a trigger on that one. :)

No comments:

Post a Comment