VARCHAR(n)
The variable-length character type (VARCHAR) is a data type that can store variable-length string data. This type allocates space based on the actual content length when storing strings, thereby effectively saving storage space. The length range of VARCHAR type strings is from 1 to 65535 characters.
Syntax
Among them, n represents the maximum length of the string, with a value range of 1 to 1048576.
Example
- Create a VARCHAR type table:
In this example, we create a table named example_table
with three fields: id
(integer type, primary key), name
(variable-length character type, maximum length of 50 characters), and age
(integer type).
- Insert data into a VARCHAR type field:
In this example, we insert a piece of data into the example_table, where the value of the name field is '张三', with a length of 3 characters.
- Query data of VARCHAR type field:
In this example, we query the data of the name field in the example_table table.