CodeBlanks Logo Icon CodeBlanks

CHAR and VARCHAR

Learn fixed and variable length text types.

CHAR and VARCHAR

CHAR(n) is fixed-length, VARCHAR(n) is variable-length text. CHAR always stores n characters; VARCHAR uses only what is needed.

CREATE TABLE customers (
  code CHAR(3),
  name VARCHAR(100)
);

Code is always 3 characters; name is at most 100 characters but uses only what is needed.

The table customers is created.

CHAR and VARCHAR — details

CHAR pads with spaces; VARCHAR keeps a length prefix. Use CHAR for short fixed codes and VARCHAR for variable text.

Practice CHAR and VARCHAR

Add a name VARCHAR(100) column to customers.

Starter code

query.sql
-- name column
CREATE TABLE customers (
  name

Sample solution

query.sql
CREATE TABLE customers (
  name VARCHAR(100)
)

Expected output

Tablo oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 CHAR and VARCHAR: match the terms

Term Meaning
CHAR fixed text
VARCHAR variable text
TEXT long text
INT integer

2 Variable text

Question
CREATE TABLE t (name ____(100));
Answer
CREATE TABLE t (name VARCHAR(100));

Explanation: VARCHAR is the right answer.

3 Which is variable-length text?

  • CHAR
  • VARCHAR Correct answer
  • INT
  • DATE

Explanation: VARCHAR is variable-length.

All lessons in Beginner