CHAR and VARCHAR
- SQL course
- Beginner
- Lesson 26/115
- 4 min
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
-- name column
CREATE TABLE customers (
nameSample solution
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
CREATE TABLE t (name ____(100));
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.