Set ENUM in MySQL for column values

MySQLMySQLi Database

While creating a table, set ENUM type for the column you want ENUM values. Let us first create a table −

mysql> create table DemoTable2019
   -> (
   -> StudentMarks int,
   -> StudentStatus ENUM('First','Second','Fail')
   -> );
Query OK, 0 rows affected (1.75 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2019 values(96,'First');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable2019 values(28,'Fail');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable2019 values(45,'Second');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable2019;

This will produce the following output −

+--------------+---------------+
| StudentMarks | StudentStatus |
+--------------+---------------+
| 96           | First         |
| 28           | Fail          |
| 45           | Second        |
+--------------+---------------+
3 rows in set (0.00 sec)

Following is the query to fetch records from a table with ENUM column value −

mysql> select *from DemoTable2019
   -> where StudentMarks=28
   -> order by StudentStatus limit 1;

This will produce the following output −

+--------------+---------------+
| StudentMarks | StudentStatus |
+--------------+---------------+
| 28           | Fail          |
+--------------+---------------+
1 row in set (0.00 sec)
raja
Published on 06-Apr-2020 13:15:03
Advertisements