Restrict varchar() column to specific values?

后端 未结 4 2150
长发绾君心
长发绾君心 2020-12-02 08:13

Is there a way to specify, for example 4 distinct values for a varchar column in MS SQL Server 2008?

For example, I need a column called Frequency (varchar) that onl

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 08:29

    Personally, I'd code it as tinyint and:

    • Either: change it to text on the client, check constraint between 1 and 4
    • Or: use a lookup table with a foreign key

    Reasons:

    • It will take on average 8 bytes to store text, 1 byte for tinyint. Over millions of rows, this will make a difference.

    • What about collation? Is "Daily" the same as "DAILY"? It takes resources to do this kind of comparison.

    • Finally, what if you want to add "Biweekly" or "Hourly"? This requires a schema change when you could just add new rows to a lookup table.

提交回复
热议问题