哪些数据库系统支持 ENUM 数据类型,哪些不支持?

发布于 2024-07-09 13:32:31 字数 213 浏览 7 评论 0原文

跟进这个问题:“数据库枚举 - 优点和缺点”,我想了解哪些数据库系统支持枚举数据类型,以及它们如何实现的一些详细信息(例如内部存储的内容、限制是什么、查询语法含义、索引含义等)。

对用例或优缺点的讨论应在其他问题中进行。

Following up this question: "Database enums - pros and cons", I'd like to know which database systems support enumeration data types, and a bit of detail on how they do it (e.g. what is stored internally, what are the limits, query syntax implications, indexing implications, ...).

Discussion of use cases or the pros and cons should take place in the other questions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深海夜未眠 2024-07-16 13:32:31

我知道 MySQL 确实支持 ENUM:

  • 该数据类型被实现为带有关联字符串的整数值,
  • 单个枚举最多可以有 65.535 个元素,
  • 每个字符串都有一个等价的数字,从 1 开始,按照定义的顺序
  • 排列数值 在非严格 SQL 模式下,可以通过“SELECT enum_col+0”访问该字段的值
  • ,分配不在列表中的值不一定会导致错误,而是会分配一个特殊的错误值,数值 0
  • 排序按数字顺序(例如定义顺序)进行,而不是按字符串的字母顺序进行等价赋值,
  • 可以通过值字符串或索引号
  • 进行:ENUM('0','1 ','2') 应避免使用,因为 '0' 将具有整数值 1

I know that MySQL does support ENUM:

  • the data type is implemented as integer value with associated strings
  • you can have a maximum of 65.535 elements for a single enumeration
  • each string has a numerical equivalent, counting from 1, in the order of definition
  • the numerical value of the field is accessible via "SELECT enum_col+0"
  • in non-strict SQL mode, assigning not-in-list values does not necessarily result in an error, but rather a special error value is assigned instead, having the numerical value 0
  • sorting occurs in numerical order (e.g. order of definition), not in alphabetical order of the string equivalents
  • assignment either works via the value string or the index number
  • this: ENUM('0','1','2') should be avoided, because '0' would have integer value 1
很酷又爱笑 2024-07-16 13:32:31

PostgreSQL 从 8.3 开始支持 ENUM。 对于旧版本,您可以使用 :

您可以通过执行以下操作来模拟 ENUM:

CREATE TABLE persons (
  person_id int not null primary key,
  favourite_colour varchar(255) NOT NULL,
  CHECK (favourite_colour IN ('red', 'blue', 'yellow', 'purple'))
);

您还可以使用 :

CREATE TABLE colours (
  colour_id int not null primary key,
  colour varchar(255) not null
)
CREATE TABLE persons (
  person_id int not null primary key,
  favourite_colour_id integer NOT NULL references colours(colour_id),
);

当您了解最喜欢的颜色时添加一个连接,但优点是您可以简单地添加颜色通过向颜色表添加一个条目,并不是说您不需要每次都更改架构。 您还可以向颜色添加属性,例如 HTML 代码或 RVB 值。

您还可以创建自己的类型来执行枚举,但我认为它不会比 varchar 和 CHECK 更快。

PostgreSQL supports ENUM from 8.3 onwards. For older versions, you can use :

You can simulate an ENUM by doing something like this :

CREATE TABLE persons (
  person_id int not null primary key,
  favourite_colour varchar(255) NOT NULL,
  CHECK (favourite_colour IN ('red', 'blue', 'yellow', 'purple'))
);

You could also have :

CREATE TABLE colours (
  colour_id int not null primary key,
  colour varchar(255) not null
)
CREATE TABLE persons (
  person_id int not null primary key,
  favourite_colour_id integer NOT NULL references colours(colour_id),
);

which would have you add a join when you get to know the favorite colour, but has the advantage that you can add colours simply by adding an entry to the colour table, and not that you would not need to change the schema each time. You also could add attribute to the colour, like the HTML code, or the RVB values.

You also could create your own type which does an enum, but I don't think it would be any more faster than the varchar and the CHECK.

能怎样 2024-07-16 13:32:31

Oracle 根本不支持 ENUM。

Oracle doesn't support ENUM at all.

失与倦" 2024-07-16 13:32:31

AFAIK,IBM DB2 和 IBM Informix Dynamic Server 都不支持 ENUM 类型。

AFAIK, neither IBM DB2 nor IBM Informix Dynamic Server support ENUM types.

┊风居住的梦幻卍 2024-07-16 13:32:31

与 mat 所说的不同,PostgreSQL 确实支持 ENUM (从版本
8.3,最后一个):

essais=> CREATE TYPE rcount AS ENUM (
essais(>   'one',
essais(>   'two',
essais(>   'three'
essais(> );
CREATE TYPE
essais=> 
essais=> CREATE TABLE dummy (id SERIAL, num rcount);
NOTICE:  CREATE TABLE will create implicit sequence "dummy_id_seq" for serial column "dummy.id"
CREATE TABLE
essais=> INSERT INTO dummy (num) VALUES ('one');
INSERT 0 1
essais=> INSERT INTO dummy (num) VALUES ('three');
INSERT 0 1
essais=> INSERT INTO dummy (num) VALUES ('four');
ERROR:  invalid input value for enum rcount: "four"
essais=> 
essais=> SELECT * FROM dummy WHERE num='three';
 id |  num  
----+-------
  2 | three
  4 | three

有些函数专门用于枚举< /a>.

索引在枚举类型上工作得很好。

根据手册,实现如下:

枚举值在磁盘上占用四个字节。 枚举值的文本标签的长度受到编译到 PostgreSQL 中的 NAMEDATALEN 设置的限制; 在标准版本中,这意味着最多 63 个字节。

枚举标签区分大小写,因此“happy”与“HAPPY”不同。 标签中的空格也很重要。

Unlike what mat said, PostgreSQL does support ENUM (since version
8.3, the last one):

essais=> CREATE TYPE rcount AS ENUM (
essais(>   'one',
essais(>   'two',
essais(>   'three'
essais(> );
CREATE TYPE
essais=> 
essais=> CREATE TABLE dummy (id SERIAL, num rcount);
NOTICE:  CREATE TABLE will create implicit sequence "dummy_id_seq" for serial column "dummy.id"
CREATE TABLE
essais=> INSERT INTO dummy (num) VALUES ('one');
INSERT 0 1
essais=> INSERT INTO dummy (num) VALUES ('three');
INSERT 0 1
essais=> INSERT INTO dummy (num) VALUES ('four');
ERROR:  invalid input value for enum rcount: "four"
essais=> 
essais=> SELECT * FROM dummy WHERE num='three';
 id |  num  
----+-------
  2 | three
  4 | three

There are functions which work specifically on enums.

Indexing works fine on enum types.

According to the manual, implementation is as follows:

An enum value occupies four bytes on disk. The length of an enum value's textual label is limited by the NAMEDATALEN setting compiled into PostgreSQL; in standard builds this means at most 63 bytes.

Enum labels are case sensitive, so 'happy' is not the same as 'HAPPY'. Spaces in the labels are significant, too.

呢古 2024-07-16 13:32:31

MSSQL 不支持 ENUM。

使用实体框架 5 时,可以使用枚举(请参阅:实体框架中的枚举支持EF5 枚举类型演练),但即使这样,值也会以 int 形式存储在数据库中。

MSSQL doesn't support ENUM.

When you use Entity Framework 5, you can use enums (look at: Enumeration Support in Entity Framework and EF5 Enum Types Walkthrough), but even then the values are stored as int in the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文