在MySQL中创建ENUM变量类型 [英] Creating ENUM variable type in MySQL
问题描述
我想执行以下操作:
'pre>
DEFINE ETYPE ENUM('a','b','c','d');
CREATE TABLE Table_1(item1 ETYPE,item2 ETYPE);
这是可能吗?
谢谢
否。 MySQL不支持 CREATE DOMAIN
或 CREATE TYPE
,例如PostgreSQL does 。
你可能会有再次输入所有的名字。您可以通过使用copy& amp; amp;粘贴或SQL脚本。
您还可以使用 INFORMATION_SCHEMA
表获取ENUM定义的文本,然后将其插入新的 CREATE TABLE
语句。
您还可以使用 CREATE TABLE AS
以创造性的方式复制类型定义。这是一个演示:
CREATE TABLE foo(f ENUM('abc','xyz'));
CREATE TABLE bar AS SELECT f AS b FROM foo;
SHOW CREATE TABLE bar;
输出:
code> CREATE TABLE`bar`(
`b` enum('abc','xyz')default NULL
)ENGINE = MyISAM DEFAULT CHARSET = latin1
最后,我建议如果您的ENUM有很多值(我猜这是真的,因为你正在寻找一个解决方案为避免键入它们),您应该使用查找表而不是ENUM数据类型。
I am using an ENUM data type in MySQL and would like to reuse it, but not retype in the values. Is there an equivalent to the C, C++ way of defining types in MySQL?
I would like to do the following:
DEFINE ETYPE ENUM('a','b','c','d');
CREATE TABLE Table_1 (item1 ETYPE, item2 ETYPE);
Is this possible?
Thanks
No. MySQL does not support CREATE DOMAIN
or CREATE TYPE
as, for example, PostgreSQL does.
You'll probably have to enter all the names again. You can mitigate the work it takes to do this by using copy & paste, or SQL scripts.
You could also use the INFORMATION_SCHEMA
tables to get the text of the ENUM definition, and then interpolate that into a new CREATE TABLE
statement.
You can also use CREATE TABLE AS
in creative ways to copy a type definition. Here's a demonstration:
CREATE TABLE foo ( f ENUM('abc', 'xyz') );
CREATE TABLE bar AS SELECT f AS b FROM foo;
SHOW CREATE TABLE bar;
Outputs:
CREATE TABLE `bar` (
`b` enum('abc','xyz') default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
Finally, I suggest that if your ENUM has many values in it (which I'm guessing is true since you're looking for a solution to avoid typing them), you should probably be using a lookup table instead of the ENUM data type.
这篇关于在MySQL中创建ENUM变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!