Insert String value as ENUM : ENUM « Data Type « SQL / MySQL






Insert String value as ENUM

  
/*
mysql> Drop table Classes;

mysql> CREATE TABLE Classes
    -> (
    ->    ClassID SMALLINT NOT NULL PRIMARY KEY,
    ->    Dept CHAR(4) NOT NULL,
    ->    Level ENUM('Upper', 'Lower') NOT NULL,
    ->    TotalStudents TINYINT UNSIGNED NOT NULL
    -> );
Query OK, 0 rows affected (0.25 sec)

mysql> INSERT INTO Classes
    -> VALUES (1001, 'ANTH', 'Upper', 25),
    ->        (1002, 'ANTH', 'Lower', 25),
    ->        (1003, 'MATH', 'Upper', 18),
    ->        (1004, 'ANTH', 'Lower', 19),
    ->        (1005, 'ENGL', 'Upper', 28);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from Classes;
+---------+------+-------+---------------+
| ClassID | Dept | Level | TotalStudents |
+---------+------+-------+---------------+
|    1001 | ANTH | Upper |            25 |
|    1002 | ANTH | Lower |            25 |
|    1003 | MATH | Upper |            18 |
|    1004 | ANTH | Lower |            19 |
|    1005 | ENGL | Upper |            28 |
+---------+------+-------+---------------+
5 rows in set (0.00 sec)


*/  

Drop table Classes;

CREATE TABLE Classes
(
   ClassID SMALLINT NOT NULL PRIMARY KEY,
   Dept CHAR(4) NOT NULL,
   Level ENUM('Upper', 'Lower') NOT NULL,
   TotalStudents TINYINT UNSIGNED NOT NULL
);


INSERT INTO Classes
VALUES (1001, 'ANTH', 'Upper', 25),
       (1002, 'ANTH', 'Lower', 25),
       (1003, 'MATH', 'Upper', 18),
       (1004, 'ANTH', 'Lower', 19),
       (1005, 'ENGL', 'Upper', 28);

select * from Classes;
           
         
    
  








Related examples in the same category

1.How to use enum
2.Use two ENUM data type in a table
3. Storage Comparisons of ENUM and SET
4.Define and use ENUM data type
5.Define 'Male' and 'Female' as Enum
6.ENUM: Enumeration of up to 65,535 strings
7.With ENUM you can manage a list of up to 65,535 character strings.
8.Sorting ENUM Values
9.MySQL uses the internal numeric values for sorting for enum value
10.Sort ENUM values in lexical order
11.Consider changing the column type to ENUM, if you always sort a non-enumeration column in a specific non-lexic
12.Use ALTER TABLE to convert the name column to an ENUM that lists the colors in the desired sort order:
13.Add to mytbl an ENUM column e that has several members
14.Enumerating a Many-to-Many Relationship
15.Configure an ENUM column and a SET column
16.If you add an invalid value to ENUM columns, an empty string ("") is inserted instead
17.Perform queries on enumerated fields based on their indexes
18.Sets work in a similar way to enumerated fields
19.The ENUM and SET Column Types
20.Enumeration values aren't limited to being single letters or uppercase.
21.An ENUM column definition may list up to 65,535 members.
22.Insert value to enum type column
23.The SET datatype, like ENUM, is declared using a comma-separated list of quoted strings that define its valid
24.enum value index
25.Enum for month names
26.Enum type column
27.Order by enum value
28.Using foreign key as an enum type
29.Use operators to specify a range of values.