Table of Contents
MySQL的列类型主要有三种:数字、字串和日期。
数字类型
字符串列类型
日期和时间列类型
?日期和时间类型" >?日期和时间类型
字符串类型
Home php教程 php手册 MySQL字段数据类型

MySQL字段数据类型

Jun 06, 2016 pm 08:12 PM
mysql main Field data type

MySQL的列类型主要有三种:数字、字串和日期。 数字类型 数字列类型用于储存各种数字数据,如价格、年龄或者数量。数字列类型主要分为两种:整数型和浮点型。所有的数字列类型都允许有两个选项:UNSIGNED和ZEROFILL。选择UNSIGNED的列不允许有负数,选择了ZE

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个成员)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

See all articles