我将如何将 MySQL 枚举数据类型默认值设置为“否”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21908428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:05:29  来源:igfitidea点击:

How willl I set MySQL enum datatype default value as 'No'?

mysqldatabase

提问by user2609417

I have a field in my Mysql table whose values are ('Yes','No') which is enum data type.

我的 Mysql 表中有一个字段,其值为 ('Yes','No') ,它是枚举数据类型。

Here I want to set its default value as 'No'. But when I am setting it as 'No', it takes no value. How will I do this?

在这里,我想将其默认值设置为“否”。但是当我将其设置为“否”时,它没有任何价值。我将如何做到这一点?

回答by onedevteam.com

CREATE TABLE enum_test (
    enum_fld ENUM('Yes', 'No') DEFAULT 'No'
);

or something like this

或类似的东西

回答by Toheeb

If an ENUM column is declared to permit NULL, the NULLvalue is a legal value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.

如果 ENUM 列声明为 permit NULL,则该 NULL值是该列的合法值,默认值为NULL。如果声明了 ENUM 列NOT NULL,则其默认值是允许值列表的第一个元素。

So something simple like this will help:

所以像这样简单的事情会有所帮助:

CREATE TABLE enum_test (enum_fld ENUM ('No', 'Yes'));

https://dev.mysql.com/doc/refman/5.0/en/enum.html

https://dev.mysql.com/doc/refman/5.0/en/enum.html

回答by Abdul Manaf

DROP TABLE IF EXISTS test_enum;
Query OK, 0 rows affected, 1 warning (0.00 sec)

CREATE TABLE test_enum(ID INT , Name CHAR(30), IsActive ENUM('Yes','No') DEFAULT 'No');
Query OK, 0 rows affected (0.29 sec)

INSERT INTO test_enum(ID,Name) VALUES(1,'Abdul');
Query OK, 1 row affected (0.00 sec)

SELECT * FROM test_enum;
+------+-------+----------+
| ID   | Name  | IsActive |
+------+-------+----------+
|    1 | Abdul | No       |
+------+-------+----------+
1 row in set (0.00 sec)

INSERT INTO test_enum(ID,Name,IsActive) VALUES(1,'Abdul','Yes');
Query OK, 1 row affected (0.00 sec)

SELECT * FROM test_enum;
+------+-------+----------+
| ID   | Name  | IsActive |
+------+-------+----------+
|    1 | Abdul | No       |
|    1 | Abdul | Yes      |
+------+-------+----------+
2 rows in set (0.00 sec)