I have a table named myTable on SQL Server 2008 R2 Express.
I would like to have a (marital) Status column with the only explicit values: ‘Single’, ‘Married’, ‘Divorced’, ‘Widower’.
The default should be ‘Married’.
Is there a way to limit the field to the above values ON THE SQL Server without additional tables?
You can do that with a CHECK() constraint.
The problem with using a CHECK constraint like this is that it’s harder for the user interface to present a list of valid choices to the user for selection. If these four were stored in a table of marital statuses, you could just
select status from marital_statuses order by status;. If they were stored in a table, you could still use ‘Married’ as the default.If I were going to store these in a table, it might look like this.
Using human-readable codes means you usually won’t need an additional join. (If you use ID numbers, you always need an additional join.) If I did that, I’d use a foreign key in the “whatever” table, and change the default to ‘m’.