Store Fixed-Length and Variable-Length Strings in MySQL Table



As we know that CHAR is used to store fixed length string and VARCHAR is used to store variable length strings. Hence we can store a fixed length as well as variable length string in the same table by declaring a column as CHAR and other as VARCHAR.

Example

mysql> Create Table Employees(FirstName CHAR(10), LastName VARCHAR(10));
Query OK, 0 rows affected (0.64 sec)

mysql> Desc Employees;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| FirstName | char(10)    | YES  |     | NULL    |       |
| LastName  | varchar(10) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
Updated on: 2020-06-20T07:08:50+05:30

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements