Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Fixed vs. Variable Length Character Types | Deep Dive: Numeric and Character Types
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL Data Types Explained

bookFixed vs. Variable Length Character Types

When you need to store text in SQL, you often choose between two main character types: fixed-length (CHAR) and variable-length (VARCHAR). Understanding the difference between these types is key to designing efficient databases. Imagine a row of mailboxes in an apartment building: each mailbox is the same size, even if some residents only receive postcards and others get large letters. This is like CHAR—every entry takes up the same amount of space, no matter how much is actually used. On the other hand, VARCHAR is like a set of flexible boxes that expand or shrink to fit the amount of mail each resident gets. With VARCHAR, storage adjusts to the length of the actual data, which can save space for entries that are shorter.

CREATE TABLE places (
    country_code CHAR(2),
    city_name VARCHAR(50)
);

Choosing between CHAR and VARCHAR depends on how predictable your data length is. In the places table above, the country_code column uses CHAR(2) because country codes are always two characters long, like "US" or "FR". This makes CHAR ideal: storage is simple and performance can be slightly faster for fixed-length values, since the database knows exactly how much space to allocate. The city_name column, however, uses VARCHAR(50) because city names vary greatly in length, from "Oslo" to "San Francisco". Using VARCHAR here avoids wasting space on short names while still accommodating longer ones. If you used CHAR(50) for city names, every entry would take up 50 characters, even if the actual name was much shorter, leading to wasted storage.

INSERT INTO places (country_code, city_name) VALUES
('US', 'New York'),
('FR', 'Paris'),
('JP', 'Osaka');

1. Which type is more efficient for storing short, fixed-length codes?

2. What is a potential drawback of using CHAR for variable-length data?

question mark

Which type is more efficient for storing short, fixed-length codes?

Select the correct answer

question mark

What is a potential drawback of using CHAR for variable-length data?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookFixed vs. Variable Length Character Types

Swipe um das Menü anzuzeigen

When you need to store text in SQL, you often choose between two main character types: fixed-length (CHAR) and variable-length (VARCHAR). Understanding the difference between these types is key to designing efficient databases. Imagine a row of mailboxes in an apartment building: each mailbox is the same size, even if some residents only receive postcards and others get large letters. This is like CHAR—every entry takes up the same amount of space, no matter how much is actually used. On the other hand, VARCHAR is like a set of flexible boxes that expand or shrink to fit the amount of mail each resident gets. With VARCHAR, storage adjusts to the length of the actual data, which can save space for entries that are shorter.

CREATE TABLE places (
    country_code CHAR(2),
    city_name VARCHAR(50)
);

Choosing between CHAR and VARCHAR depends on how predictable your data length is. In the places table above, the country_code column uses CHAR(2) because country codes are always two characters long, like "US" or "FR". This makes CHAR ideal: storage is simple and performance can be slightly faster for fixed-length values, since the database knows exactly how much space to allocate. The city_name column, however, uses VARCHAR(50) because city names vary greatly in length, from "Oslo" to "San Francisco". Using VARCHAR here avoids wasting space on short names while still accommodating longer ones. If you used CHAR(50) for city names, every entry would take up 50 characters, even if the actual name was much shorter, leading to wasted storage.

INSERT INTO places (country_code, city_name) VALUES
('US', 'New York'),
('FR', 'Paris'),
('JP', 'Osaka');

1. Which type is more efficient for storing short, fixed-length codes?

2. What is a potential drawback of using CHAR for variable-length data?

question mark

Which type is more efficient for storing short, fixed-length codes?

Select the correct answer

question mark

What is a potential drawback of using CHAR for variable-length data?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
some-alt