0 day streak
sql / beginner
Snippet

Selecting Fixed and Variable Character Lengths in Tables

When defining text columns, CHAR stores fixed-length text padding short inputs with spaces, while VARCHAR stores variable-length strings efficiently. Choosing the right data type optimizes storage and data integrity.

snippet.sql
sql
1
2
3
4
CREATE TABLE employee_directory (
country_code CHAR(2),
employee_name VARCHAR(100)
);
Breakdown
1
CREATE TABLE employee_directory (
Begins the creation of a new table structure named employee_directory.
2
country_code CHAR(2),
Defines a fixed-length text column exactly 2 characters long, ideal for ISO country codes.
3
employee_name VARCHAR(100)
Defines a variable-length text column allowing up to 100 characters for names of differing length.
4
);
Closes the table definition block.