enums in SQL Server database

前端 未结 3 776
北海茫月
北海茫月 2021-02-13 06:44

Is there a better or easier way to store the enums (Enumerations available in programming languages like C#) in SQL Server database other than simply creating a lookup table (wi

3条回答
  •  伪装坚强ぢ
    2021-02-13 07:17

    1. Personally, I like to define one lookup table per enum, because it is a kind of documentation as well. If someone wants to know what an id stands for, he would find it easily in a table. Looking for this information in a column constraint is not evident.

    2. It is also much easier to add new values in a table than in a constraint.

    3. If you create a database diagram, individual lookup tables appear more logical.

    4. You can add additional information to individual lookup tables besides id and text, if required (like a comment, a sort column, some sort of flag etc.).

    5. And as you have said, it is better for referential integrity

    However; if you are using an o/r-mapper with the code-first approach, using enums provided by the programming language feels quite natural. This is because you are not designing a database but an object model. The o/r-mapper creates the database automatically for you.

提交回复
热议问题