Character types store text data in a database. They help keep words, names, or any text information organized and easy to find.
Character types (char, varchar, text) in PostgreSQL
CHAR(n) VARCHAR(n) TEXT
CHAR(n) stores fixed-length text, padded with spaces if shorter.
VARCHAR(n) stores variable-length text up to n characters.
TEXT stores variable-length text with no specific limit.
CREATE TABLE example ( code CHAR(3), name VARCHAR(50), description TEXT );
INSERT INTO example (code, name, description) VALUES ('USA', 'United States', 'A country in North America.');
SELECT code, name FROM example WHERE name LIKE 'United%';
This example creates a table for countries with a fixed 3-letter code, a name up to 50 characters, and notes with no length limit. It inserts three countries and then selects all data ordered by the country code.
CREATE TABLE countries ( country_code CHAR(3), country_name VARCHAR(50), notes TEXT ); INSERT INTO countries (country_code, country_name, notes) VALUES ('USA', 'United States', 'Located in North America'), ('CAN', 'Canada', 'Known for maple syrup'), ('MEX', 'Mexico', 'Famous for its cuisine'); SELECT country_code, country_name, notes FROM countries ORDER BY country_code;
CHAR pads text with spaces to fill the fixed length, so 'USA' stored as CHAR(5) becomes 'USA '.
VARCHAR is more flexible and usually preferred unless you need fixed length.
TEXT can store very long text but does not enforce length limits.
CHAR(n) stores fixed-length text, padding with spaces if needed.
VARCHAR(n) stores variable-length text up to n characters.
TEXT stores variable-length text with no size limit.
Practice
Solution
Step 1: Understand fixed-length storage
CHAR(n) stores exactly n characters, padding with spaces if input is shorter.Step 2: Compare with other types
VARCHAR(n) stores variable length up to n, TEXT has no limit, INT is numeric.Final Answer:
CHAR(n) -> Option AQuick Check:
Fixed length with padding = CHAR(n) [OK]
- Confusing VARCHAR(n) as fixed length
- Thinking TEXT has length limit
- Choosing numeric types by mistake
Solution
Step 1: Identify variable-length syntax
VARCHAR(n) declares variable-length text with max n characters.Step 2: Check other options
CHAR(50) is fixed length, TEXT has no length limit, STRING is invalid in PostgreSQL.Final Answer:
VARCHAR(50) -> Option BQuick Check:
Variable length with max limit = VARCHAR(n) [OK]
- Using TEXT with length limit (invalid)
- Confusing CHAR as variable length
- Using non-existent STRING type
CREATE TABLE users (name CHAR(5));
INSERT INTO users VALUES ('Tom');
SELECT name, LENGTH(name) FROM users;What will be the output of the SELECT query?
Solution
Step 1: Understand CHAR(5) behavior on insert
CHAR(5) pads 'Tom' with 2 spaces to make length 5.Step 2: LENGTH function counts all characters including spaces
LENGTH('Tom ') returns 5 because spaces count.Final Answer:
name = 'Tom ', length = 5 -> Option AQuick Check:
CHAR pads spaces, LENGTH counts them [OK]
- Assuming LENGTH ignores trailing spaces
- Thinking CHAR stores variable length
- Confusing CHAR with VARCHAR behavior
Solution
Step 1: Understand VARCHAR(n) length limit enforcement
VARCHAR(10) limits input to 10 characters strictly.Step 2: Behavior on exceeding length
PostgreSQL raises an error if input exceeds declared length.Final Answer:
An error is raised and insertion fails -> Option CQuick Check:
Exceeding VARCHAR limit causes error [OK]
- Assuming automatic truncation happens
- Thinking VARCHAR pads with spaces
- Believing PostgreSQL ignores length limits
Solution
Step 1: Identify requirement for unlimited length
User comments can be very long, so no fixed or limited length is suitable.Step 2: Compare data types for length limits
TEXT stores variable-length text with no size limit, unlike CHAR or VARCHAR.Step 3: Exclude irrelevant types
INT is numeric and unsuitable for text storage.Final Answer:
TEXT, because it stores variable-length text with no size limit -> Option DQuick Check:
No size limit needed = TEXT [OK]
- Choosing CHAR or VARCHAR with fixed limits
- Confusing numeric types with text
- Assuming VARCHAR can store unlimited text
Ready to prove it?
Pass the test and this pattern moves unknown
