<small id='DDDRU'></small><noframes id='DDDRU'>

<legend id='DDDRU'><style id='DDDRU'><dir id='DDDRU'><q id='DDDRU'></q></dir></style></legend>

        <bdo id='DDDRU'></bdo><ul id='DDDRU'></ul>
    1. <i id='DDDRU'><tr id='DDDRU'><dt id='DDDRU'><q id='DDDRU'><span id='DDDRU'><b id='DDDRU'><form id='DDDRU'><ins id='DDDRU'></ins><ul id='DDDRU'></ul><sub id='DDDRU'></sub></form><legend id='DDDRU'></legend><bdo id='DDDRU'><pre id='DDDRU'><center id='DDDRU'></center></pre></bdo></b><th id='DDDRU'></th></span></q></dt></tr></i><div id='DDDRU'><tfoot id='DDDRU'></tfoot><dl id='DDDRU'><fieldset id='DDDRU'></fieldset></dl></div>

      <tfoot id='DDDRU'></tfoot>
      1. 在 MySQL 中创建 ENUM 变量类型

        时间:2024-04-15

            <bdo id='YdgxP'></bdo><ul id='YdgxP'></ul>
          • <small id='YdgxP'></small><noframes id='YdgxP'>

              <legend id='YdgxP'><style id='YdgxP'><dir id='YdgxP'><q id='YdgxP'></q></dir></style></legend>
                <tbody id='YdgxP'></tbody>
              <tfoot id='YdgxP'></tfoot>
            • <i id='YdgxP'><tr id='YdgxP'><dt id='YdgxP'><q id='YdgxP'><span id='YdgxP'><b id='YdgxP'><form id='YdgxP'><ins id='YdgxP'></ins><ul id='YdgxP'></ul><sub id='YdgxP'></sub></form><legend id='YdgxP'></legend><bdo id='YdgxP'><pre id='YdgxP'><center id='YdgxP'></center></pre></bdo></b><th id='YdgxP'></th></span></q></dt></tr></i><div id='YdgxP'><tfoot id='YdgxP'></tfoot><dl id='YdgxP'><fieldset id='YdgxP'></fieldset></dl></div>

                  本文介绍了在 MySQL 中创建 ENUM 变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 MySQL 中使用 ENUM 数据类型并想重用它,但不重新输入值.在 MySQL 中是否有等效于 C、C++ 定义类型的方式?

                  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?

                  我想执行以下操作:

                  DEFINE ETYPE ENUM('a','b','c','d');
                  CREATE TABLE Table_1 (item1 ETYPE, item2 ETYPE);
                  

                  这可能吗?

                  谢谢

                  推荐答案

                  没有.MySQL 不支持 CREATE DOMAINCREATE TYPE,例如 PostgreSQL 确实.

                  No. MySQL does not support CREATE DOMAIN or CREATE TYPE as, for example, PostgreSQL does.

                  您可能需要再次输入所有名称.您可以通过使用 copy & 来减轻执行此操作所需的工作量.粘贴或 SQL 脚本.

                  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.

                  您还可以使用 INFORMATION_SCHEMA 表来获取 ENUM 定义的文本,然后将其插入到新的 CREATE TABLE 语句中.

                  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.

                  您还可以创造性地使用 CREATE TABLE AS 来复制类型定义.这是一个演示:

                  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;
                  

                  输出:

                  CREATE TABLE `bar` (
                    `b` enum('abc','xyz') default NULL
                  ) 
                  

                  最后,我建议如果您的 ENUM 中有很多值(我猜这是真的,因为您正在寻找避免输入它们的解决方案),您可能应该使用查找表而不是 ENUM数据类型.

                  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.

                  来自@bliako 的重新评论:

                  Re comment from @bliako:

                  您可以按照您的描述进行操作:

                  You can do what you describe this way:

                  CREATE TABLE bar (pop INT NOT NULL, name VARCHAR(100))
                  AS SELECT 0 AS pop, NULL AS name, f FROM foo;
                  

                  我在 MySQL 5.7.27 上试过这个,它奏效了.

                  I tried this on MySQL 5.7.27, and it worked.

                  有趣的是,您不必在 CREATE TABLE 行中声明所有三列.第三列 f 将自动添加.

                  It's interesting to note that you don't have to declare all three columns in the CREATE TABLE line. The third column f will be added automatically.

                  有趣的是,我不得不在 SELECT 语句中提供列别名,以确保列名称与 CREATE TABLE 中声明的名称相匹配.否则,如果列名不匹配,您最终会得到额外的列,并且它们的数据类型不是您所期望的:

                  It's also interesting that I had to give column aliases in the SELECT statement to make sure the column names match those declared in the CREATE TABLE. Otherwise if the column names don't match, you end up with extra columns, and their data types are not what you expect:

                  create table bar (pop int not null, name varchar(100)) 
                  as select 0 as c1, null as c2, f from foo;
                  
                  show create table barG
                  
                  CREATE TABLE `bar` (
                    `pop` int(11) NOT NULL,
                    `name` varchar(100) DEFAULT NULL,
                    `c1` binary(0) DEFAULT NULL,
                    `c2` binary(0) DEFAULT NULL,
                    `f` enum('abc','xyz') DEFAULT NULL
                  )
                  

                  这篇关于在 MySQL 中创建 ENUM 变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ORDER BY “ENUM 字段"在 MYSQL 下一篇:在 MySQL 中返回枚举值的最佳方法是什么?

                  相关文章

                    <bdo id='jiUS2'></bdo><ul id='jiUS2'></ul>

                1. <legend id='jiUS2'><style id='jiUS2'><dir id='jiUS2'><q id='jiUS2'></q></dir></style></legend><tfoot id='jiUS2'></tfoot>

                      <small id='jiUS2'></small><noframes id='jiUS2'>

                    1. <i id='jiUS2'><tr id='jiUS2'><dt id='jiUS2'><q id='jiUS2'><span id='jiUS2'><b id='jiUS2'><form id='jiUS2'><ins id='jiUS2'></ins><ul id='jiUS2'></ul><sub id='jiUS2'></sub></form><legend id='jiUS2'></legend><bdo id='jiUS2'><pre id='jiUS2'><center id='jiUS2'></center></pre></bdo></b><th id='jiUS2'></th></span></q></dt></tr></i><div id='jiUS2'><tfoot id='jiUS2'></tfoot><dl id='jiUS2'><fieldset id='jiUS2'></fieldset></dl></div>