char
Learning Focus
Use this lesson to understand PostgreSQL char(n) (fixed-length) and when it is appropriate.
char(n) Overview
- Fixed-length string
- Values are padded with spaces to length
n - Best for: fixed-size codes (country codes, status codes) when you want strict length
Example
CREATE TABLE currencies (
code char(3) PRIMARY KEY,
name text NOT NULL
);
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
| Using char for variable user input | Wasted space and surprises | Prefer text |