
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can ENUM Type Values Contain Spaces in MySQL?
Yes, you can include a string value with spaces in ENUM type. This is particularly useful when we add descriptive options that contain multiple words. Let us explore how to create, describe, and use ENUM columns containing spaces, with supporting examples.
ENUM vs VARCHAR:While both ENUM and VARCHAR can store strings with spaces, ENUM has an advantage: limiting of the number of entries that can be added into a column to a fixed set. Because of this, it is useful in situations where the values are limited and static, such as size categories, and status codes. Whereas VARCHAR is better for fields where the data isn't limited to a fixed set. However, ENUM prevents the database from invalid data compared to VARCHAR.
Creating an ENUM column with values Containing Spaces
Following is the query to create an ENUM column with values containing spaces, you simply need to enclose the values in single quotes while defining the table.
CREATE TABLE DemoTable ( Size ENUM('SMALL SIZE','LARGE SIZE','XL SIZE') );
From the above query, we can observe that the column 'Size' which is ENUM data type contains values that include spaces.
Let us check the description of the table using DESC command to confirm that the ENUM values with spaces are set up correctly ?
DESC DemoTable;
This will produce the following output ?
Field | Type | NULL | Key | Default | Extra |
---|---|---|---|---|---|
Size | enum('SMALL SIZE','LARGE SIZE','XL SIZE') | YES | NULL |
This output confirms that the ENUM column allows the specified values with spaces.
Insert and retrieve ENUM valuesInsert some records in the table using INSERT command ?
INSERT INTO DemoTable (size) VALUES ('XL SIZE');
Display all records from the table using SELECT statement ?
SELECT * FROM DemoTable;
This will produce the following output. Here, we have displayed an ENUM type value with a space ?
Size |
---|
XL SIZE |
Key points to remember when dealing with ENUM in MySQL.
-
ENUM values allow spaces if they are enclosed during table creation by single quotes.
-
MySQL interprets ENUM values with spaces as just another simple ENUM value during storage and retrieval.
-
Make sure the values inserted are exactly as defined in an ENUM list, with appropriate space inclusion since it is case-sensitive and space-sensitive.