7๏ธโƒฃ ๐Ÿ”ฃ MySQL Data Types
Estimated reading: 4 minutes 27 views

๐Ÿ”ก MySQL Textual Types (VARCHAR, TEXT, ENUM, SET) โ€“ Complete Guide with Syntax & Examples


๐Ÿงฒ Introduction โ€“ Why Textual Data Types Matter in MySQL?

In MySQL, textual data types are essential for storing names, descriptions, user inputs, tags, statuses, labels, and much more. MySQL provides several string-based column types like VARCHAR, TEXT, ENUM, and SET, each optimized for different storage, size, and query scenarios.

Choosing the right one ensures your applicationโ€™s data is:

  • Stored efficiently
  • Validated correctly
  • Queried quickly

๐ŸŽฏ In this guide, youโ€™ll learn:

  • The syntax, use cases, and behaviors of VARCHAR, TEXT, ENUM, and SET
  • How to choose the best type for your data
  • Real-world examples and performance tips

๐Ÿ”ค 1. VARCHAR(n) โ€“ Variable-Length Strings

๐Ÿ”น Syntax

VARCHAR(n)
  • n: Maximum number of characters (not bytes).
  • Maximum total row length: 65,535 bytes (varies by charset and other columns).

๐Ÿ”น Example

CREATE TABLE users (
  username VARCHAR(50),
  email VARCHAR(100)
);

Explanation:

  • username: Can store up to 50 characters.
  • email: Stores user email up to 100 characters.

โœ… Best for short-to-medium length strings where the length varies per row.


๐Ÿ“ 2. TEXT โ€“ Large Text Blocks

๐Ÿ”น Variants

TypeMax SizeDescription
TINYTEXT255 bytesFor short notes
TEXT64 KBStandard long-form content
MEDIUMTEXT16 MBFor full articles, logs
LONGTEXT4 GBVery large documents or dumps

๐Ÿ”น Example

CREATE TABLE articles (
  title VARCHAR(255),
  body TEXT
);

Explanation:

  • title: A short string title.
  • body: A long-form article body (up to 64 KB).

โš ๏ธ Limitations:

  • Cannot be used with DEFAULT values.
  • Requires prefix length for indexing (e.g., CREATE INDEX idx_body ON articles(body(255));).
  • Stored outside the row, with a pointer in the main row.

๐Ÿ“‹ 3. ENUM โ€“ Single-Choice Value from Predefined List

๐Ÿ”น Syntax

ENUM('value1', 'value2', 'value3')

๐Ÿ”น Example

CREATE TABLE orders (
  order_id INT,
  status ENUM('pending', 'shipped', 'delivered', 'cancelled') DEFAULT 'pending'
);

Explanation:

  • status: Only accepts one value from the ENUM list.
  • Stored as a numeric index, which saves space.

โœ… Great for limited, fixed-status fields like user roles, order states, or binary flags.


๐Ÿงท 4. SET โ€“ Multi-Choice Value from Predefined List

๐Ÿ”น Syntax

SET('value1', 'value2', 'value3')

๐Ÿ”น Example

CREATE TABLE users (
  id INT,
  interests SET('php', 'mysql', 'css', 'javascript')
);

Explanation:

  • interests: Allows multiple values from the set, such as 'php,javascript'.

โœ… Useful for tag-like, multiple-choice fields.
โš ๏ธ Hard to normalize, and not suited for relational joins.


๐Ÿ“Š Comparison โ€“ VARCHAR vs TEXT vs ENUM vs SET

FeatureVARCHARTEXTENUMSET
Max Length65,535 bytes (total)Up to 4 GB65,535 values64 values
Default Valuesโœ… SupportedโŒ Not Supportedโœ… Supportedโœ… Supported
Indexableโœ… Fully๐Ÿ”ธ With prefix onlyโœ… Fullyโœ… Bitmask-based
Multiple ValuesโŒโŒโŒโœ…
Use CaseNames, emailsArticles, logsStatus, flagsTags, roles, preferences
StorageIn-rowOff-rowCompact integer (1 byte)Bitmask (1โ€“8 bytes)

๐Ÿ“˜ Best Practices

โœ… Tip๐Ÿ’ก Reason
Use VARCHAR for short stringsEfficient and indexable
Use TEXT only for long-form contentAvoids row size limits and truncation
Use ENUM for strict value enforcementPrevents bad inputs for statuses or categories
Avoid SET in normalized schemasDifficult to filter and join properly
Always define charset (utf8mb4)Ensures multilingual and emoji compatibility
Donโ€™t overuse large text fieldsImpacts performance on joins and indexes

๐Ÿš€ Real-World Use Cases

ScenarioRecommended TypeReason
Usernames and EmailsVARCHARVariable length, searchable
Product DescriptionsTEXTMay exceed 255 characters
Order Status (pending, complete)ENUMLimits input and improves query performance
User Tags or RolesSETAllows storing multiple predefined values
Blog Post Title + BodyVARCHAR + TEXTTitle is indexed, body is long content

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

MySQLโ€™s textual types provide flexible storage options for character-based data. Use the right type based on length, indexing needs, allowed values, and application logic.

๐Ÿ” Key Takeaways

  • VARCHAR: Best for short-to-medium text (fully indexable)
  • TEXT: Use for long content (no default, partial indexing only)
  • ENUM: Enforces predefined single-choice input
  • SET: Stores multiple values from a fixed list (use with caution)

โš™๏ธ Real-World Relevance

These types power real-world use cases like form fields, metadata, blog systems, tagging engines, status tracking, and more. Choosing the right type ensures better performance, structure, and safety.


โ“ FAQ โ€“ MySQL Textual Types


โ“ Can I set a default value for a TEXT column?

โŒ No. MySQL does not allow default values on TEXT or BLOB types.


โ“ Can I index a TEXT field?

โœ… Partially. Use a prefix index:

CREATE INDEX idx_content ON articles(body(255));

โ“ Can I store multiple values in an ENUM?

โŒ No. ENUM supports only one value. Use SET if you need multiple.


โ“ When should I avoid SET?

Avoid when:

  • You need normalized tables
  • You perform complex searches or joins
  • You require dynamic lists

โ“ How many values can ENUM and SET hold?

  • ENUM: Up to 65,535 values
  • SET: Up to 64 unique values

Share Now :

Leave a Reply

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

Share

๐Ÿ”ก MySQL Textual Types (VARCHAR, TEXT, ENUM, SET)

Or Copy Link

CONTENTS
Scroll to Top