How can I store the fixed length string as well as variable length string in the same 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: 20-Jun-2020

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements