Postgres ENUM data type or CHECK CONSTRAINT?

前端 未结 6 1569
失恋的感觉
失恋的感觉 2021-01-29 23:24

I have been migrating a MySQL db to Pg (9.1), and have been emulating MySQL ENUM data types by creating a new data type in Pg, and then using that as the column definition. My q

6条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 00:03

    From my point of view, given a same set of values

    1. Enum is a better solution if you will use it on multiple column
    2. If you want to limit the values of only one column in your application, a check constraint is a better solution.

    Of course, there is a whole lot of other parameters which could creep in your decision process (typically, the fact that builtin operators are not available), but I think these two are the most prevalent ones.

提交回复
热议问题