数据库枚举 - 利弊 [英] Database enums - pros and cons

查看:346
本文介绍了数据库枚举 - 利弊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个users表,它的枚举列type有两个可能的值:individual和organization。它们是相互排斥的和强制性的(每一行必须从可能的两个都必须有一个值。这将是一个很好的例子,使用枚举?为什么这样/不?



什么对数据库字段使用ENUM(set)类型有什么利弊?

解决方案

是的,这似乎是一个很好的候选人一个枚举。



优点:




  • 小(磁盘空间)

  • 快速(整数比较)

  • 容易理解(大部分)



缺点:




  • 无法在不更改表格结构的情况下添加值

  • 有要小心区域敏感度

  • 如果使用不当,可能会非常混乱...不要使用active = enum('1','0'),因为我必须放

  • 不支持所有数据库


For example, I have a "users" table which has an enum column "type" with two possible values: "individual" and "organization." They are mutually exclusive and mandatory (each row must have exactly one value from the possible two. Would this be a good case to use enums? Why so/not?

What are some pros and cons on using ENUM (set) types for database fields?

解决方案

Yes, this seems like a good candidate for an enum.

Pros:

  • Small (disk space)
  • Fast (integer comparison)
  • Easy to understand (mostly)

Cons:

  • Can't add a value without changing the table structure
  • Have to be careful with case sensitivity in places
  • Can be very confusing if used badly... don't use active = enum('1', '0') as I have to put up with!
  • Not supported by all databases

这篇关于数据库枚举 - 利弊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆