dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
Oct 11, 2019

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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');
M S Sridhar
 
Thanks for your registration, follow us on our social networks to keep up-to-date