devxlogo

Using ENUM to Pre-define Column Values in MySQL

Using ENUM to Pre-define Column Values in MySQL

You may choose to validate the data that you enter in a table either, at the database level or at the application level.

Application level is feasible only if the source of data is the application. Many instances are present in which the data can be directly uploaded to the database from a script that will be time-saving.

MySQL supports ENUM and using the values for a particular column can be pre-defined during the table definition and this will be validated when the records are inserted or updated in the table.

Example:

CREATE TABLE GRADES (    NAME VARCHAR(40),    GRADE ENUM('A', 'B', 'C', 'D', 'E'));

and now using the pre-defined values of the ENUM for column GRADE which can take values as defined.

Correct SQL:

INSERT INTO GRADES (NAME, GRADE) VALUES 		('STUDENT 1', 'B'), 		('STUDENT 2', 'A'), 		('STUDENT 3', 'A'),		('STUDENT 4', 'B');

Incorrect SQL:

INSERT INTO GRADES (NAME, GRADE) VALUES 		('STUDENT 5', 'G');
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist