Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

database

mysql

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

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?

like image 847
user2609417 Avatar asked Feb 20 '14 12:02

user2609417


People also ask

How can I change ENUM value in MySQL?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.

How do I change the default value in MySQL?

To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values must be constants. For example, you cannot set the default for a date-valued column to NOW( ) , although that would be very useful.

Can ENUM be NULL MySQL?

If an ENUM column is declared to permit NULL , the NULL value is a valid 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.

How ENUM values are stored in MySQL?

The ENUM data type is stored in two locations: the set of values is stored in the table metadata; in each row, only the set index is stored, as integer, which requires one byte for enums up to 255 entries large, then two for up to 65535 entries (see MySQL reference)


3 Answers

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

or something like this

like image 144
onedevteam.com Avatar answered Nov 15 '22 15:11

onedevteam.com


If an ENUM column is declared to permit NULL, the NULL value 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.

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

like image 33
Toheeb Avatar answered Nov 15 '22 14:11

Toheeb


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)
like image 28
Abdul Manaf Avatar answered Nov 15 '22 16:11

Abdul Manaf