String type in column type in MySQL tutorial
This article mainly introduces you to the relevant knowledge of the string type in mysql. I hope it will be helpful to friends in need!
Recommended reference tutorial: "mysql tutorial"
The so-called column type actually refers to data Type, that is, a unified classification of data, from a system perspective is to be able to manage it in a unified way and make better use of limited space.
In SQL, data types are divided into three major categories: Numeric type, string type and date and time type.
In SQL, the string type is divided into String type in column type in MySQL tutorial categories, namely: char
, varchar
, text
, blob
, enum
and set
.
Category String type in column type in MySQL tutorial: Fixed-length string
Fixed-length string: char
, that is, the disk (two-dimensional table) has already determined the final data storage when defining the structure. length.
char(L)
: L represents Length, that is, the length that can be stored, in characters, the maximum length is String type in column type in MySQL tutorialString type in column type in MySQL tutorialString type in column type in MySQL tutorial;char(String type in column type in MySQL tutorial)
: Indicates that in UTFString type in column type in MySQL tutorial environment, String type in column type in MySQL tutorial*String type in column type in MySQL tutorial=String type in column type in MySQL tutorialString type in column type in MySQL tutorial bytes are required.
Category String type in column type in MySQL tutorial: Variable-length string
Variable-length string: varchar
, that is, allocating storage space When the space is allocated, it is allocated according to the maximum space, but the actual amount used is determined based on specific data.
varchar(L)
: L represents Length, the theoretical length is String type in column type in MySQL tutorialString type in column type in MySQL tutorialString type in column type in MySQL tutorialString type in column type in MySQL tutorialString type in column type in MySQL tutorial, but there will be String type in column type in MySQL tutorial to String type in column type in MySQL tutorial more bytes to determine the actual length of storage;varchar(String type in column type in MySQL tutorial0)
: For example, to store String type in column type in MySQL tutorial0 Chinese characters, in UTFString type in column type in MySQL tutorial environment, String type in column type in MySQL tutorial0*String type in column type in MySQL tutorial String type in column type in MySQL tutorial=String type in column type in MySQL tutorialString type in column type in MySQL tutorial bytes are required.
In fact, if the storage length exceeds String type in column type in MySQL tutorialString type in column type in MySQL tutorialString type in column type in MySQL tutorial characters, neither a fixed-length string nor a variable-length string is used, but a text stringtext
.
How to choose a fixed-length string or a variable-length string?
Fixed-length strings are a waste of disk space, but are highly efficient: if the data is basically the same length, use fixed-length strings, such as ID cards, phone numbers, etc.;
Variable-length strings save disk space, but are inefficient: If the length of the data cannot be determined (different data changes), use variable-length strings, such as addresses and names. wait.
Category String type in column type in MySQL tutorial: Text string
If the amount of data is very large, usually more than String type in column type in MySQL tutorialString type in column type in MySQL tutorialString type in column type in MySQL tutorial characters, a text string will be used.
Text strings are classified according to the storage format and can be divided into:
text
: store text;blob
: stores binary data (which are actually storage paths), usually not used.
Category String type in column type in MySQL tutorial: Enumeration string
Enumeration string:enum
, you need to combine all possibilities in advance The results that appear are all designed, and the actual data stored must be one of the specified data.
How to use enumeration strings:
Definition:
enum('Element String type in column type in MySQL tutorial', 'Element String type in column type in MySQL tutorial', 'Element String type in column type in MySQL tutorial'... )
, for exampleenum('male','female','confidential')
;- ##Usage: The stored data can only be defined in advance Good data.
-- 创建枚举表create table my_enum( gender enum(&#String type in column type in MySQL tutorial9;男&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;女&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;保密&#String type in column type in MySQL tutorial9;) )charset utfString type in column type in MySQL tutorial;
: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>-- 插入测试数据insert into my_enum values (&#String type in column type in MySQL tutorial9;男&#String type in column type in MySQL tutorial9;),(&#String type in column type in MySQL tutorial9;女&#String type in column type in MySQL tutorial9;),(&#String type in column type in MySQL tutorial9;保密&#String type in column type in MySQL tutorial9;);insert into my_enum values (&#String type in column type in MySQL tutorial9;male&#String type in column type in MySQL tutorial9;);</pre><div class="contentsignin">Copy after login</div></div>
Through the above test, we can find that there is an advantage to using enumeration strings, that is:
In addition, enumeration strings also have another function, that is:
Save storage space (enumeration data usually has an alias). Enumerations actually store numerical values instead of The string itself.In MySQL, the system has the function of automatically converting data formats. Here, we can prove that the enumeration field stores numerical values. The specific method is: take out the data
0. If it is a string, the final result will always be 0
, otherwise it will be other values. . <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>-- 验证枚举字段实际存储的格式select gender + 0,gender from my_enum;</pre><div class="contentsignin">Copy after login</div></div>
Observing the above results, we can find out the actual rules for enumerating elements, that is, numbering starts from
according to the order in which the elements appear. Next, let’s understand the principle of 枚举在进行数据规范(定义)的时候,系统会自动建立一个数字与枚举元素的对应关系(放在日志中);在进行数据插入的时候,系统自动将字符串转换为对应的数值进行存储;在进行数据提取的时候,系统自动将数值转换成对应的字符串进行显示。 通过阅读以上枚举的原理,咱们可以知道:使用枚举的效率并不高(低于其他类型的数据),但能规范数据和节省存储空间。 第 String type in column type in MySQL tutorial 类:集合字符串 集合字符串: 集合字符串的使用方式: 定义: 使用:可以使用元素列表中的多个元素,用逗号分隔。 执行如下 SQL 语句创建枚举表,进行测试: 再执行如下 SQL 语句,向表 再执行如下 SQL 语句,查看表 观察上面的结果,相信大部分童鞋也懵啦!对于 再将上面的二进制反过来: 不妨算算,上述二进制对应的十进制数,即为 到这里,相信大家已经恍然大悟啦,原来:集合字符串中每一个元素都对应一个二进制位,其中被选中的为 此外,集合字符串中插入元素的顺序并没有影响,最终系统都会自动去匹配集合的顺序,即:enumeration
:set
,跟枚举类似,实际存储的是数值而不是字符串。set
,元素列表;-- 创建集合表create table my_set(
hobby set(&#String type in column type in MySQL tutorial9;音乐&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;电影&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;旅行&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;美食&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;摄影&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;运动&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;宠物&#String type in column type in MySQL tutorial9;)
)charset utfString type in column type in MySQL tutorial;
my_set
中插入测试数据:-- 插入测试数据insert into my_set values (&#String type in column type in MySQL tutorial9;电影,美食,宠物&#String type in column type in MySQL tutorial9;);insert into my_set values (String type in column type in MySQL tutorial);
my_set
中的数据:-- 查看数据select hobby + 0,hobby from my_set;
String type in column type in MySQL tutorial
还好理解,String type in column type in MySQL tutorial=String type in column type in MySQL tutorial+String type in column type in MySQL tutorial
,对应于集合中数据的编号,也正是音乐
和电影
;但是String type in column type in MySQL tutorialString type in column type in MySQL tutorial
是什么鬼啊?在此,咱们不妨将集合(&#String type in column type in MySQL tutorial9;音乐&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;电影&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;旅行&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;美食&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;摄影&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;运动&#String type in column type in MySQL tutorial9;,&#String type in column type in MySQL tutorial9;宠物&#String type in column type in MySQL tutorial9;
)中的元素选中的记为String type in column type in MySQL tutorial
,没有选中的记为0
,表示成二进制,也就是:0String type in column type in MySQL tutorial0String type in column type in MySQL tutorial00String type in column type in MySQL tutorial
String type in column type in MySQL tutorial00String type in column type in MySQL tutorial0String type in column type in MySQL tutorial0
String type in column type in MySQL tutorialString type in column type in MySQL tutorial
.String type in column type in MySQL tutorial
,未选中的为0
,最后在反过来,这个二进制数对应的十进制数即为其数据库中实际存储的是数值。-- 插入测试数据insert into my_set values (&#String type in column type in MySQL tutorial9;电影,美食,旅行&#String type in column type in MySQL tutorial9;);insert into my_set values (&#String type in column type in MySQL tutorial9;旅行,电影,美食&#String type in column type in MySQL tutorial9;);
上述两个 SQL 语句会产生相同的结果:
如上图所示,显然咱们的结论得到了验证。
最后,集合的原理同枚举类似,因此可以的到相同的结论,即:使用集合的效率并不高(低于其他类型的数据),但能规范数据和节省存储空间。
The above is the detailed content of String type in column type in MySQL tutorial. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
