Home  >  Article  >  Database  >  How to store fixed length string and variable length string in the same MySQL table?

How to store fixed length string and variable length string in the same MySQL table?

PHPz
PHPzforward
2023-09-05 22:09:161421browse

如何在同一个 MySQL 表中存储固定长度字符串和可变长度字符串?

We know that CHAR is used to store fixed-length strings, and VARCHAR is used to store variable-length strings. Therefore, we can store both fixed-length and variable-length strings in the same table by declaring one column as CHAR and other columns 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)

The above is the detailed content of How to store fixed length string and variable length string in the same MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete