How can I store the fixed length string as well as variable length string in the same MySQL table?

MySQLMySQLi Database

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)
raja
Published on 19-Feb-2018 16:48:33
Advertisements