HomeMYSQLDifference between ENUM and SET.

Difference between ENUM and SET.

Here’s a detailed comparison between ENUM and SET in MySQL:

FeatureENUMSET
DefinitionA string object that can have only one value chosen from a list of predefined values.A string object that can have multiple values, each chosen from a predefined list.
Number of ValuesOnly one value from the list can be stored.Can store zero or more values from the list.
StorageStores internally as 1 or 2 bytes depending on the number of possible values.Stores internally as bitmap (up to 8 bytes) for multiple selections.
SyntaxENUM('value1', 'value2', 'value3')SET('value1', 'value2', 'value3', ...)
Use CaseWhen you want to restrict a column to one choice among many predefined options.When you want to allow a column to have multiple choices simultaneously.
Examplesql CREATE TABLE users (status ENUM('active', 'inactive', 'pending'));sql CREATE TABLE products (colors SET('red', 'green', 'blue'));
InsertionOnly one value allowed: INSERT INTO users (status) VALUES('active');Multiple values allowed: INSERT INTO products (colors) VALUES('red,blue');
QueryingCheck for exact match: WHERE status='active'Check for a value using FIND_IN_SET or bit operations: WHERE FIND_IN_SET('red', colors)
AdvantagesLightweight, easy validation, ensures only one valid value.Flexible, allows multiple selections, still compact storage.
LimitationsOnly one value; cannot select multiple options.Limited to 64 members; querying multiple values can be less intuitive.

Summary:

  • ENUM: Single choice from a list.
  • SET: Multiple choices from a list.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *