What Are ER Diagrams? – Entity-Relationship Diagrams Explained
Introduction
In SQL, text data types are used to store alphanumeric values like names, addresses, emails, and descriptions. Choosing the correct text type — CHAR, VARCHAR, or TEXT — is important for optimizing storage space, query speed, and database performance.
In this section, you'll learn the definitions, differences, and best use cases for each text data type.
1. CHAR (Fixed-Length String)
CHAR is used to store fixed-length strings. If the stored string is shorter than the defined length, SQL automatically pads it with spaces to match the specified size.
Features:
- Fixed length
- Fast and predictable performance
- Uses extra storage if the data is often shorter than the specified length
Syntax:
column_name CHAR(length);
length = number of characters (1 to 255 depending on the database system)
Example:
CREATE TABLE countries (
country_code CHAR(2),
country_name CHAR(50)
);
country_code like 'US', 'IN', 'UK' will always take 2 characters.
When to Use CHAR:
- Data with a constant size, such as country codes, gender ('M', 'F'), state abbreviations
- Fixed-format fields like credit card types ('VISA', 'MC')
- When exact storage size is known and consistent
2. VARCHAR (Variable-Length String)
VARCHAR stands for Variable Character. It stores variable-length strings, meaning only the actual characters are stored without unnecessary padding.
Features:
- Variable length
- More space-efficient than CHAR for varying-length text
- Slightly slower than CHAR when processing large volumes (because of extra calculations for string lengths)
Syntax:
column_name VARCHAR(length);
length = maximum number of characters allowed
Example:
CREATE TABLE employees (
first_name VARCHAR(50),
email VARCHAR(100)
);
Names and emails can vary in length, making VARCHAR ideal.
When to Use VARCHAR:
- Data with unpredictable or variable length
- Names, emails, addresses, and descriptions under 255-65535 characters
- Most general-purpose text fields
3. TEXT (Large Text Field)
TEXT is used to store large amounts of text like long descriptions, blog posts, comments, or articles.
Features:
- Meant for large text storage (up to 65,535 characters for standard TEXT in MySQL)
- Cannot have a default value (in some databases like MySQL)
- TEXT fields are stored outside the main table with a pointer reference
- Different variants exist (TINYTEXT, MEDIUMTEXT, LONGTEXT) for various sizes
Syntax:
column_name TEXT;
Example:
CREATE TABLE articles (
id INT,
title VARCHAR(255),
body TEXT
);
body will store the full article content, which can be very large.
When to Use TEXT:
- Long-form text fields (comments, articles, reviews, reports)
- Data that exceeds normal VARCHAR limits
- When exact storage requirements are unknown or potentially very large
Quick Comparison: CHAR vs VARCHAR vs TEXT
Feature | CHAR | VARCHAR | TEXT |
---|---|---|---|
Storage | Fixed length | Variable length | Variable, large storage |
Max Size | Up to 255 chars | 65,535 bytes (typically) | 65,535+ chars (depends on type) |
Performance | Fast for fixed-size | Efficient for variable text | Slightly slower for queries |
Indexing | Full index support | Full index support | Limited in some DBs |
Best Use Case | Codes, fixed formats | Names, addresses, emails | Articles, long descriptions |
Important Tips
- Use CHAR only when all values will be exactly the same length
- VARCHAR is the best choice for most standard text fields
- Reserve TEXT for content that exceeds VARCHAR limits
- Consider VARCHAR(MAX) in SQL Server for large text that might need indexing
- Be aware that TEXT fields may have limitations on default values and full-text indexing
Definition: What Is an ER Diagram?
An Entity-Relationship Diagram (ER Diagram or ERD) is a visual representation of a database's structure, showing how entities (tables) relate to each other. ERDs are essential in database design because they help model real-world data into a structured format before writing any SQL.
Purpose of ER Diagrams
- Plan and design a relational database schema
- Identify entities, relationships, and attributes
- Spot and resolve redundancy or data conflicts
- Communicate database design to teams and stakeholders
- Serve as documentation for developers and DBAs
Key Components of ER Diagrams
1. Entities
Represented as rectangles, entities are objects or concepts you want to store data about (e.g., User, Product, Order).
2. Attributes
Represented as ellipses (or listed within the entity), attributes are the properties of entities (e.g., name, email, price).
3. Primary Key
A unique identifier for an entity, often underlined in ERDs.
4. Relationships
Represented as diamonds (or labeled lines in modern tools), showing how entities are related (e.g., enrolls, places, owns).
5. Foreign Keys
Attributes that reference the primary key of another table, used to define relationships.
6. Cardinality
Shows the number of instances in one entity that can relate to instances in another:
- One-to-One (1:1)
- One-to-Many (1:N)
- Many-to-Many (M:N)
ER Diagram Example
Use Case: A student enrolls in courses.
Entities:
- Student (student_id, name)
- Course (course_id, title)
- Enrollment (student_id, course_id)
Relationships:
- A student can enroll in many courses.
- A course can have many students.
ERD Relationship Type: Many-to-Many
Tools to Create ER Diagrams
- dbdiagram.io (simple and fast)
- Lucidchart (drag-and-drop interface)
- Draw.io (free diagramming tool)
- MySQL Workbench (automatically generates ERDs from existing databases)
- ER/Studio, Visual Paradigm (enterprise tools)
Best Practices
- Start with entities before defining relationships.
- Ensure unique primary keys for each entity.
- Avoid redundant relationships or attributes.
- Use naming conventions (e.g., singular names, lowercase with underscores).
Benefits of ERDs
Advantage | Description |
---|---|
Visual Clarity | Helps visualize complex systems and relationships |
Communication | Makes it easier to explain the schema to stakeholders |
Early Problem Detection | Detects design flaws before implementation |
Foundation for SQL Code | Translates directly to CREATE TABLE statements |