Character types (char, varchar, text) in PostgreSQL
📖 Scenario: You are creating a simple database table to store information about books in a library. Each book has a fixed-length code, a title with a maximum length, and a description that can be very long.
🎯 Goal: Build a PostgreSQL table named books using the character types char, varchar, and text correctly for different columns.
📋 What You'll Learn
Create a table named books
Add a column book_code with type char(5) for fixed-length codes
Add a column title with type varchar(100) for titles with a maximum length
Add a column description with type text for long descriptions
💡 Why This Matters
🌍 Real World
Character types are used to store text data in databases, such as codes, names, and descriptions.
💼 Career
Database developers and administrators must choose appropriate character types for efficient storage and data integrity.
Progress0 / 4 steps
1
Create the books table with book_code column
Write a SQL statement to create a table named books with one column called book_code of type char(5).
PostgreSQL
Hint
The char(5) type stores fixed-length strings of length 5.
2
Add the title column with varchar(100)
Add a column named title of type varchar(100) to the books table definition.
PostgreSQL
Hint
The varchar(100) type stores variable-length strings up to 100 characters.
3
Add the description column with text
Add a column named description of type text to the books table definition.
PostgreSQL
Hint
The text type stores strings of unlimited length.
4
Complete the table definition with a primary key
Modify the books table to add a primary key constraint on the book_code column.
PostgreSQL
Hint
The primary key uniquely identifies each book by its code.
Practice
(1/5)
1. Which PostgreSQL character type stores text with a fixed length, padding with spaces if the input is shorter?
easy
A. CHAR(n)
B. VARCHAR(n)
C. TEXT
D. INT
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 A
Quick Check:
Fixed length with padding = CHAR(n) [OK]
Hint: Fixed length with padding means CHAR(n) [OK]
Common Mistakes:
Confusing VARCHAR(n) as fixed length
Thinking TEXT has length limit
Choosing numeric types by mistake
2. Which of the following is the correct syntax to declare a variable-length character column with a maximum of 50 characters in PostgreSQL?
easy
A. CHAR(50)
B. VARCHAR(50)
C. TEXT(50)
D. STRING(50)
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 B
Quick Check:
Variable length with max limit = VARCHAR(n) [OK]
Hint: Use VARCHAR(n) for variable length with max size [OK]
Common Mistakes:
Using TEXT with length limit (invalid)
Confusing CHAR as variable length
Using non-existent STRING type
3. Consider the following table and insert statements:
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?
medium
A. name = 'Tom ', length = 5
B. name = 'Tom', length = 3
C. name = 'Tom', length = 5
D. name = 'Tom ', length = 3
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 A
Quick Check:
CHAR pads spaces, LENGTH counts them [OK]
Hint: CHAR pads spaces; LENGTH counts all characters [OK]
Common Mistakes:
Assuming LENGTH ignores trailing spaces
Thinking CHAR stores variable length
Confusing CHAR with VARCHAR behavior
4. You have a table with a column defined as VARCHAR(10). You try to insert a string of 15 characters. What will happen?
medium
A. The string is truncated to 10 characters and inserted
B. The string is padded with spaces to 15 characters
C. An error is raised and insertion fails
D. The string is inserted fully ignoring the limit