我的表中有一个枚举类型列。我现在决定将其设为 varchar 类型并施加一些约束。
Q1)这是一个很好的做法:使用枚举或在列上放置 constarint。
Q2)如何将我的枚举类型列更改为 varchar。正好与这个问题相反。我尝试使用这个:
ALTER TABLE tablename ALTER COLUMN columnname TYPE VARCHAR
Run Code Online (Sandbox Code Playgroud)
但这给了我错误:No operator matches the given name and argument type(s). You might need to add explicit type casts.
这是表定义:
CREATE TABLE tablename (
id1 TEXT NOT NULL,
id2 VARCHAR(100) NOT NULL,
enum_field table_enum,
modified_on TIMESTAMP NOT NULL DEFAULT NOW(),
modified_by VARCHAR(100),
PRIMARY key (id1, id2)
);
Run Code Online (Sandbox Code Playgroud)
至于最佳实践,最好定义一个单独的可能值表,并使您的列成为该表的外键。这样做有以下好处:
INSERT更改可能的值是操作数据行( 、UPDATE或)的问题DELETE,这比更改约束或枚举值更容易访问和管理。