← Back to MySQL Course | Chapter 3: Data Types | Lesson 2 of 8

String Data Types

CHAR एक fixed size वाली seat जैसा है जो हमेशा same space इस्तेमाल करती है, जबकि VARCHAR एक stretchy seat जैसा है जो सिर्फ उतनी room लेती है जितनी word को चाहिए।
Syntax
sql
column_name CHAR(length)
column_name VARCHAR(max_length)
column_name TEXT

CHAR बनाम VARCHAR

CHAR हमेशा प्रति row same fixed संख्या में bytes reserve करता है और छोटी values को spaces से pad करता है, जबकि VARCHAR सिर्फ उतना space इस्तेमाल करता है जितना actual string को चाहिए — VARCHAR names, emails, और दूसरे variable-length text के लिए बेहतर default है।

उदाहरण: CHAR vs VARCHAR

sql
CREATE TABLE users (
  code CHAR(5),
  name VARCHAR(50)
);

TEXT Type

TEXT columns articles या product descriptions जैसे बड़े, unbounded blocks of content के लिए बने हैं, और VARCHAR के उलट वे main row data से अलग stored होते हैं, जो affect करता है कि आप उन्हें कितनी efficiently index कर सकते हैं।

उदाहरण: TEXT Type

sql
CREATE TABLE articles (
  title VARCHAR(100),
  body TEXT
);

String Length Functions

CHAR_LENGTH() और LENGTH() जैसे functions क्रमशः characters बनाम bytes में एक string का size report करते हैं — वे emoji जैसे multi-byte Unicode characters शामिल होते ही अलग हो जाते हैं।

उदाहरण: String Length Functions

sql
SELECT CHAR_LENGTH('café') AS chars, LENGTH('café') AS bytes;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Binary Strings

BINARY और VARBINARY human-readable text के बजाय raw byte sequences store करते हैं, जो उन्हें ordinary strings के बजाय hashes, encrypted blobs, या fixed-format binary identifiers के लिए उपयुक्त बनाता है।

उदाहरण: Binary Strings

sql
CREATE TABLE files (
  hash BINARY(32),
  data VARBINARY(255)
);

String Collation

Collation settings control करती हैं कि string comparisons apple और Apple को equal या distinct treat करें या नहीं, और sorting locale-specific alphabetical rules follow करे या नहीं — इसे गलत करें तो search features टूटे हुए महसूस हो सकते हैं।

उदाहरण: String Collation

sql
CREATE TABLE names (name VARCHAR(50)) COLLATE utf8mb4_general_ci;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. variable-length text के लिए CHAR इस्तेमाल करना और यह सोचना कि values pad क्यों होती हैं, जबकि VARCHAR सिर्फ actual length store करता है।
  2. VARCHAR(50) set करना और एक लंबी value insert करना, जो strict mode में इसे store करने के बजाय error देता है।
  3. accents या emoji वाले text पर LENGTH() इस्तेमाल करना और characters की उम्मीद करना, जबकि यह bytes return करता है और CHAR_LENGTH() characters count करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.