Introduction
ENUM and SET types help you store a list of allowed values in a column. They make sure only certain values can be saved.
ENUM and SET types help you store a list of allowed values in a column. They make sure only certain values can be saved.
CREATE TABLE table_name ( column_name ENUM('value1', 'value2', ...), another_column SET('value1', 'value2', ...) );
ENUM stores one value from the list.
SET stores zero or more values from the list, combined.
CREATE TABLE shirts ( size ENUM('small', 'medium', 'large') );
CREATE TABLE products ( colors SET('red', 'green', 'blue') );
INSERT INTO shirts (size) VALUES ('medium');
INSERT INTO products (colors) VALUES ('red,blue');
This creates a table with ENUM and SET columns, inserts two rows, and selects all data.
CREATE TABLE example ( mood ENUM('happy', 'sad', 'neutral'), hobbies SET('reading', 'sports', 'music') ); INSERT INTO example (mood, hobbies) VALUES ('happy', 'reading,music'); INSERT INTO example (mood, hobbies) VALUES ('sad', 'sports'); SELECT * FROM example;
ENUM stores only one value, SET can store multiple values separated by commas.
Values in ENUM and SET are case sensitive.
Trying to insert a value not in the list will cause an error or store an empty string.
ENUM limits a column to one value from a list.
SET allows multiple values from a list in one column.
Both help keep data clean and save space.
ENUM and SET types in MySQL?ENUM typeENUM restricts a column to one value from a predefined list.SET typeSET allows storing zero or more values from a predefined list in one column.ENUM allows only one value, SET allows multiple values -> Option CENUM = single, SET = multiple [OK]colors that can store multiple colors using SET type?products with a column features SET('wifi', 'bluetooth', 'gps'), what will be the result of this query?SELECT features FROM products WHERE FIND_IN_SET('gps', features);CREATE TABLE users (status ENUM('active', 'inactive', 'pending'));SET type allows storing multiple predefined values efficiently in one column.ENUM allows only one value, VARCHAR wastes space and needs parsing, separate BOOLEANs increase columns.Pass the test and this pattern moves unknown