

Is there a built-in way to limit a column in the sql server to a list of explici...
source link: https://www.codesd.com/item/is-there-a-built-in-way-to-limit-a-column-in-the-sql-server-to-a-list-of-explicit-values-in-the-table-design-view.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Is there a built-in way to limit a column in the sql server to a list of explicit values in the table design view?
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.
create table whatever (
...
Status varchar(12) not null default 'Married'
check (Status in ('Single', 'Married', 'Divorced', 'Widower')),
...
);
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.
create table marital_statuses (
status_code char(1) primary key,
status_text varchar(12) not null unique
);
insert into marital_statuses values
('s', 'Single'),
('m', 'Married'),
('d', 'Divorced'),
('w', 'Widower');
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'.
Recommend
-
39
Learning SQL can feel intimidating, even to some experienced DB2 DBAs. For years, you could administer Db2 without using SQL. There are a lot of developers, too who want to learn SQL. The set-thinking of SQL can be confus...
-
68
Any information stored in a blockchain is supposed to be preserved forever, nobody will be able to change it or even less erase it. But is this really true? Is there any chance that governments or private groups with enou...
-
29
From time to time you may have experienced that MySQL was not able to find the best execution plan for a query. You felt the query should have...
-
6
The ALTER TABLE ... ALTER COLUMN command is very powerful. You can use it to change a column's data type, length, precision, scale, nullability, collation…and many other things besides. It is certai...
-
2
Virtual reality: 'There's no max, there's no limit!'Virtual reality: 'There's no max, there's no limit!'Seeing your favourite artists live on stage hasn't been eas...
-
7
Concatenate a single column in a comma-delimited list advertisements This question already has an answer here: Concatenate m...
-
6
Developers & PractitionersIs there a limit to Cloud VMs? A conversation Carter Morgan Developer Advocate Brian Dorsey Dev...
-
10
Vinita Kasliwal June 16, 2022 2 minute read...
-
8
There’s no speed limit Derek Sivers from the book “
-
12
January 3, 2023 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK