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?
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.
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.
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.
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)
CREATE TABLE enum_test (
enum_fld ENUM('Yes', 'No') DEFAULT 'No'
);
or something like this
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With