MySQL字段数据类型_MySQL
MySQL的列类型主要有三种:数字、字串和日期。
数字类型
数字列类型用于储存各种数字数据,如价格、年龄或者数量。数字列类型主要分为两种:整数型和浮点型。所有的数字列类型都允许有两个选项:UNSIGNED和ZEROFILL。选择UNSIGNED的列不允许有负数,选择了ZEROFILL的列会为数值添加零。下面是MySQL中可用的数字列类型
- TINYINT——一个微小的整数,支持 -128到127(SIGNED),0到255(UNSIGNED),需要1个字节存储
- BIT——同TINYINT(1)
- BOOL——同TINYINT(1)
- SMALLINT——一个小整数,支持 -32768到32767(SIGNED),0到65535(UNSIGNED),需要2个字节存储 MEDIUMINT——一个中等整数,支持 -8388608到8388607(SIGNED),0到16777215(UNSIGNED),需要3个字节存储
- INT——一个整数,支持 -2147493648到2147493647(SIGNED),0到4294967295(UNSIGNED),需要4个字节存储
- INTEGER——同INT
- BIGINT——一个大整数,支持 -9223372036854775808到9223372036854775807(SIGNED),0到18446744073709551615(UNSIGNED),需要8个字节存储
- FLOAT(precision)——一个浮点数。precision
- FLOAT——一个小的菜单精度浮点数。支持 -3.402823466E+38到-1.175494351E-38,0和1.175494351E-38 to 3.402823466E+38,需要4个字节存储。如果是UNSIGNED,正数的范围保持不变,但负数是不允许的。
- DOUBLE——一个双精度浮点数。支持 -1.7976931348623157E+308到-2.2250738585072014E-308,0和2.2250738585072014E-308到1.7976931348623157E+308。如果是FLOAT,UNSIGNED不会改变正数范围,但负数是不允许的。
- DOUBLE PRECISION——同DOUBLE
- REAL——同DOUBLE
- DECIMAL——将一个数像字符串那样存储,每个字符占一个字节
- DEC——同DECIMAL
- NUMERIC——同DECIMAL
字符串列类型
字符串列类型用于存储任何类型的字符数据,如名字、地址或者报纸文章。下面是MySQL中可用的字符串列类型
- CHAR——字符。固定长度的字串,在右边补齐空格,达到指定的长度。支持从0到155个字符。搜索值时,后缀的空格将被删除。
- VARCHAR——可变长的字符。一个可变长度的字串,其中的后缀空格在存储值时被删除。支持从0到255字符
- TINYBLOB——微小的二进制对象。支持255个字符。需要长度+1字节的存储。与TINYTEXT一样,只不过搜索时是区分大小写的。(0.25KB)
- TINYTEXT——支持255个字符。要求长度+1字节的存储。与TINYBLOB一样,只不过搜索时会忽略大小写。(0.25KB)
- BLOB——二进制对象。支持65535个字符。需要长度+2字节的存储。 (64KB)
- TEXT——支持65535个字符。要求长度+2字节的存储。 (64KB)
- MEDIUMBLOB——中等大小的二进制对象。支持16777215个字符。需要长度+3字节的存储。 (16M)
- MEDIUMTEXT——支持16777215个字符。需要长度+3字节的存储。 (16M)
- LONGBLOB——大的的二进制对象。支持4294967295个字符。需要长度+4字节的存储。 (4G)
- LONGTEXT——支持4294967295个字符。需要长度+4字节的存储。(4G)
- ENUM——枚举。只能有一个指定的值,即NULL或””,最大有65535个值
- SET——一个集合。可以有0到64个值,均来自于指定清单
日期和时间列类型
日期和时间列类型用于处理时间数据,可以存储当日的时间或出生日期这样的数据。格式的规定:Y表示年、M(前M)表示月、D表示日、H表示小时、M(后M)表示分钟、S表示秒。下面是MySQL中可用的日期和时间列类型
- DATETIME——格式:’YYYY-MM-DD HH:MM:SS’,范围:’1000-01-01 00:00:00′到’9999-12-31 23:59:59′
- DATE——格式:’YYYY-MM-DD’,范围:’1000-01-01′到’9999-12-31′
- TIMESTAMP——格式:’YYYYMMDDHHMMSS’、’YYMMDDHHMMSS’、’YYYYMMDD’、’YYMMDD’,范围:’1970-01-01 00:00:00′到’2037-01-01 00:00:00′
- TIME——格式:’HH:MM:SS’
- YEAR——格式:’YYYY,范围:’1901′到’2155′
数字类型
列类型 | 需要的存储量 |
TINYINT |
1 字节 |
SMALLINT |
2 个字节 |
MEDIUMINT |
3 个字节 |
INT |
4 个字节 |
INTEGER |
4 个字节 |
BIGINT |
8 个字节 |
FLOAT(X) |
4 如果 X |
FLOAT |
4 个字节 |
DOUBLE |
8 个字节 |
DOUBLE PRECISION |
8 个字节 |
REAL |
8 个字节 |
DECIMAL(M,D) |
M 字节(D +2 , 如果M )
|
NUMERIC(M,D) |
M 字节(D +2 , 如果M )
|
日期和时间类型
列类型 | 需要的存储量 |
DATE |
3 个字节 |
DATETIME |
8 个字节 |
TIMESTAMP |
4 个字节 |
TIME |
3 个字节 |
YEAR |
1 字节 |
字符串类型
列类型 | 需要的存储量 |
CHAR(M) |
M 字节,1
|
VARCHAR(M) |
L +1 字节, 在此L 和<code>1
|
TINYBLOB , TINYTEXT
|
L +1 字节, 在此L
|
BLOB , TEXT
|
L +2 字节, 在此L
|
MEDIUMBLOB , MEDIUMTEXT
|
L +3 字节, 在此L
|
LONGBLOB , LONGTEXT
|
L +4 字节, 在此L
|
ENUM('value1','value2',...) |
1 或 2 个字节, 取决于枚举值的数目(最大值65535) |
SET('value1','value2',...) |
1,2,3,4或8个字节, 取决于集合成员的数量(最多64个成员) |

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.

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 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.

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.

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.
