Mysql 简明教程

MySQL - VARCHAR

The MySQL Varchar Data Type

MySQL VARCHAR 数据类型用于存储变长字符字符串,其长度最长为 65,535 个字节。

The MySQL VARCHAR data type is used to store variable-length character strings, having a length up to 65,535 bytes.

在 MySQL 中,当您将文本存储在 VARCHAR 列中时,它需要一些额外的空间来跟踪文本的长度。此额外空间可以是 1 或 2 个字节,具体取决于文本的长度。如果文本较短(少于 255 个字符),则它使用 1 个字节作为长度。对于较长的文本,它使用 2 个字节。

In MySQL, when you store text in a VARCHAR column, it needs a little extra space to keep track of how long the text is. This extra space can be either 1 or 2 bytes, depending on the length of the text. If the text is short (less than 255 characters), it uses 1 byte for length. For longer text, it uses 2 bytes.

每行中数据总大小加上长度信息不能超过表中的一行 65535 字节。

The total size of data plus the length info cannot exceed 65,535 bytes for a row in a table.

Example

在以下查询中,我们正在创建一个名为 test_table 的新表,其中有两个列 column1column2

In the following query, we are creating a new table named test_table that has two columns column1 and column2.

正如我们可以在下面的代码块中看到的,这些列(column1 = 32765 和 column2 = 32766)占用了 65531 字节。这些列将分别占用 2 个字节作为长度前缀。因此,这些列总共占用 32765+2+32766+2 = 65535 字节 −

As we can see in the below code block, the columns (column1 = 32765 and column2 = 32766) makes 65531 bytes. These columns will take 2 bytes each as a length prefix. Therefore, the columns totally make 32765+2+32766+2 = 65535 bytes −

CREATE TABLE test_table (
   column1 VARCHAR(32765) NOT NULL,
   column2 VARCHAR(32766) NOT NULL
)CHARACTER SET 'latin1' COLLATE LATIN1_DANISH_CI;

Output

以下是上面代码的输出: -

Following is the output of the above code −

Query OK, 0 rows affected (0.03 sec)

Example

现在,让我们创建另一个表 test_table2 ,并将 32766 和 32766 提供给两个列(column1 和 column2) −

Now, let us create another table test_table2 and provide 32766 and 32766 to both the columns (column1 and column2) −

CREATE TABLE test_table2 (
   column1 VARCHAR(32766) NOT NULL, --error
   column2 VARCHAR(32766) NOT NULL
)CHARACTER SET 'latin1' COLLATE LATIN1_DANISH_CI;

Output

正如我们可以在下面的输出来看到的,生成了一个错误,因为行大小(32766 +2 +32766 +2 = 65536)超过了最大限制(65,535) −

As we can see in the output below, an error is generated because the row size (32766 +2 +32766 +2 = 65536) exceeds the maximum limit (65,535) −

ERROR 1118 (42000): Row size too large. The maximum row size for the used
table type, not counting BLOBs, is 65535. This includes storage overhead,
check the manual. You have to change some columns to TEXT or BLOBs

Example

在这里,我们正在使用以下查询创建一个名为 CUSTOMERS 的另一个表 −

Here, we are creating another table named CUSTOMERS using the following query −

CREATE TABLE CUSTOMERS (
   ID int PRIMARY KEY AUTO_INCREMENT,
   NAME VARCHAR(3)
);

以下是所获得的输出 −

Following is the output obtained −

Query OK, 0 rows affected (0.03 sec)

现在,我们正在向 NAME 列中插入一个字符串,其中长度大于 VARCHAR 列的长度 −

Now, we are inserting a string into NAME column where the length is greater than the length of VARCHAR column −

INSERT INTO CUSTOMERS (NAME) VALUES ('Rahul');

Output

因此,MySQL 将生成以下给出的错误 −

As a result, MySQL will generate an error given below −

ERROR 1406 (22001): Data too long for column 'NAME' at row 1

Example

MySQL 在插入值时不会计算尾随空格。相反,它截断尾随空格。

MySQL does not count the trailing spaces when inserting a value. Instead it truncates the trailing spaces.

让我们向具有尾随空格的 NAME 列中插入一个值 −

Let us insert a value into the NAME column that has trailing spaces −

INSERT INTO CUSTOMERS (NAME) VALUES ('ABC ');

Output

正如我们在下面的输出中看到的,MySQL 发出警告 −

As we can see in the output below, MySQL issued a warning −

Query OK, 1 row affected, 1 warning (0.02 sec)

Example

在以下查询中,我们正在尝试检查 NAME 列中的值的长度 −

In the following query, we are trying to check the length of the values in NAME column −

SELECT ID, NAME, length(NAME) FROM CUSTOMERS;

产生的结果如下 −

The result produced is as follows −

现在,让我们执行以下查询以显示在上述插入操作中发出的警告 −

Now, let us execute the below query to display the warnings that issued on the above insertion operation −

SHOW warnings;

生成的结果为 −

The result produced is −

Varchar Datatypes Using a Client Program

除了使用 mysql 查询执行数据类型外,我们还可以使用客户端程序创建 Varchar 数据类型的列。

In addition to performing datatypes using mysql query, we can also create column of the Varchar datatypes using the client program.

Syntax

Example

以下是这些程序 −

Following are the programs −