UPI Study › Courses › Database Programming › Free lessons › Lesson 3.4 Free with a free account
Character Data Types: CHAR vs. VARCHAR
Database Programming · Chapter 3: Data Definition Language and Schema DesignSection 3.4 · lesson 18 of 769 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Whole Number Data Types: INT and SMALLINT →← Schemas in SQL: Definitions and Design FundamentalsNext lesson: Whole Number Data Types: INT and SMALLINT →
A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →
In this lesson
Choose fixed or variable text types, encodings, and collations.
- Compare CHAR and VARCHAR storage, padding, and trailing-space behavior
- Apply MySQL and SQL Server length limits, including TEXT and MAX types
- Choose Unicode encodings such as utf8mb4 and NVARCHAR appropriately
- Explain how collations control case and accent sensitivity in comparisons
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (999 words). Read along, or search it.
Opening Question
Names, Phones, and Country Codes
Sofia Alvarez, sales associate at Lantern Books, enters a customer's name, phone number, and country code. They are all text, but their requirements differ. A country code has a stable length. A name varies, and a phone number may begin with a zero or a plus sign. Which declaration preserves the meaning, and which settings decide whether a lowercase search finds an uppercase name?
Declarations
Fixed-Length CHAR, Variable VARCHAR
CHAR is a fixed-length character type. In MySQL, short values are padded with spaces, and trailing padding is normally removed when retrieved. VARCHAR is variable length. It stores the text plus a small length counter, retaining trailing spaces that were actually supplied. Use CHAR for genuinely fixed-length codes, and VARCHAR for bounded text whose length varies. A declared size is a policy, so check real application requirements. These are non-run MySQL declarations for a disposable profile extension. The existing course customer table remains our shared baseline. Phone numbers and postal codes are labels, even when they contain digits. Arithmetic would lose meaningful formatting and possibly leading zeros.
Storage Math
Storing Cat in CHAR and VARCHAR
Take the word cat in a single-byte MySQL storage illustration. CHAR of ten reserves ten character positions, with seven spaces of padding. VARCHAR of ten uses three bytes for cat and one byte for its length: four bytes in this example. For tiger, the comparison is ten against six. A full ten-character value needs ten against eleven, so variable length is not automatically smaller. MySQL VARCHAR uses one or two length bytes, depending on maximum byte capacity. These simplified counts exclude record overhead and physical storage-engine details.
Size Limits
MySQL and SQL Server Length Limits
In MySQL, CHAR permits up to two hundred fifty-five characters. VARCHAR lengths count characters, but the shared row limit is sixty-five thousand five hundred thirty-five bytes. Other columns and overhead consume that budget. With a four-byte character set, one character can need several bytes, so a character limit is not a byte guarantee. SQL Server CHAR and VARCHAR sizes count bytes, up to eight thousand for ordinary declarations. NVARCHAR sizes count byte-pairs, up to four thousand. SQL Server MAX forms support much larger values, up to approximately two gigabytes. Its older text and ntext types are deprecated. For MySQL strict mode, excess nonspace content produces an error rather than an accepted shortened name. Relaxed modes can truncate with warnings; excess trailing spaces have special rules.
Portability Test
SQLite Ignores the Declared Length
SQLite gives us an important portability test. A declaration that resembles another product's syntax may not enforce the same rule. This disposable table declares VARCHAR of three, then inserts six letters. Run the file before assuming the length was enforced. SQLite returns the entire value, with length six. Its VARCHAR annotation establishes text affinity, rather than a maximum string length. SQLite also does not implement MySQL's CHAR padding behavior. Use explicit constraints when a SQLite application needs a length rule.
Long Text
Choosing a MySQL TEXT Type
MySQL's TEXT family supplies byte-based capacities for longer content. Choose a family member according to expected encoded size, rather than the word text alone. TINYTEXT permits two hundred fifty-five bytes; TEXT permits sixty-five thousand five hundred thirty-five. MEDIUMTEXT and LONGTEXT allow much larger documents. An article with one hundred thousand characters can need four hundred thousand bytes, under utf8mb4. MEDIUMTEXT fits that budget; TEXT does not. Physical off-row storage depends on engine and row format. Ordinary MySQL indexes on TEXT need a prefix; full-text indexing is a separate mechanism.
Unicode
Unicode Encodings in MySQL and SQL Server
Unicode assigns codes to characters across writing systems, including many symbols and emoji. The encoding determines how those codes become stored bytes. Choose utf8mb4 for MySQL Unicode text. It uses up to four bytes per character; utf8mb3 excludes supplementary characters. MySQL NVARCHAR uses utf8mb3, its national character set. Prefer explicitly choosing utf8mb4. SQL Server NVARCHAR uses UTF sixteen under supplementary-character-aware collations. Some characters require two byte-pairs, so two bytes per character is not a universal rule. Prefix SQL Server Unicode literals with N. Modern SQL Server also supports Unicode VARCHAR through UTF eight collations; check the column's actual collation.
Collations
Case and Accent Sensitivity Rules
A character set specifies the repertoire and encoding. A collation specifies comparison and sorting rules, including case and accent sensitivity. In MySQL collation names, ci means case-insensitive, and ai means accent-insensitive. Their sensitive counterparts are cs and as. The name also identifies a character set and rules version. A binary character collation compares character codes; binary strings compare bytes. MySQL settings inherit through server, database, table, and column, with a more specific choice overriding an inherited default. SQL Server combines encoding-related choices and comparison rules in its collation system. Mixed collations can cause errors, although MySQL can resolve some combinations automatically.
Live Comparison
BINARY Versus NOCASE in SQLite
Watch comparison rules change a real result. This SQLite query compares the same two spellings under two built-in collations. BINARY distinguishes Pune from lowercase pune. NOCASE folds ASCII letters, so the case difference can compare equal. The first result is false, shown as zero; the second is true, shown as one. The stored text did not change. SQLite's built-in NOCASE is not a general Unicode case-folding system. Do not equate it with MySQL's Unicode-aware collations.
Quick Check
Size Annotation Meets NOCASE
Sofia tests a fresh SQLite table with both a type annotation and a comparison rule. The value contains five letters. The final query searches using lowercase text. Consider both the declared size and the collation before predicting its count. Pause, and decide. One matching record. SQLite accepts the five-letter value despite the size annotation, and NOCASE matches the ASCII case variation. Run the file to confirm both rules working together. The comparison finds a row; it does not count the letters inside that row. Five confuses characters with records. Zero assumes either enforced VARCHAR length or a case-sensitive comparison, neither of which applies to this SQLite definition.
Key Takeaways
Size, Encoding, and Comparison Choices
Sofia's country code fits a fixed-length policy; names need room to vary. Phone numbers stay text so their formatting survives. Size text to actual requirements, with headroom. Oversized declarations can affect row budgets, indexes, and temporary work, even when stored values are short. Choose Unicode encoding and comparison rules separately. Then test limits and comparisons on the database product that will actually store the data.
About Database Programming at UPI Study
Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Character Data Types: CHAR vs. VARCHAR
What is the difference between CHAR and VARCHAR?
Does VARCHAR always use less storage than CHAR?
What is the difference between VARCHAR and NVARCHAR in SQL Server?
What controls whether a text comparison is case-sensitive?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Database Programming.
76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →