search
HomeDatabaseMysql TutorialMySQL学习6:MySQL基本数据类型

数据类型是指列、存储过程参数、表达式和局部变量的数据特征,它决定了数据的存储方式,代表了不同的信息 类型。MySQL中常用的的数据类型包括:数类型、日期和时间类型和字符串类型等。 一数类型 MySQL支持所有标准SQL中的数类型,其中包括严数据类型(INTEGE

        数据类型是指列、存储过程参数、表达式和局部变量的数据特征,它决定了数据的存储方式,代表了不同的信息

类型。MySQL中常用的的数据类型包括:数值类型、日期和时间类型和字符串类型等。

       一数值类型

       MySQL支持所有标准SQL中的数值类型,其中包括严格数据类型(INTEGER、SMALLINT、DECIMAL、

NUMBERIC),以及近似数值数据类型(FLOAT、REAL、DOUBLE、PRESISION),并在此基础上进行扩展。扩展后增

加了TINYINT、MEDIUMINT、BIGINT这3种长度不同的整形,并增加了BIT类型,用来存放位数据。

       (1)整型

       在MySQL中支持的5个主要整数类型是TINYINT、SMALLINT、MEDIUMINT、INT和BIGINT。这些类型在很大程

度上是相同的,只有它们存储的值的大小是不相同的。

       MySQL以一个可选的显示宽度指示器的形式对SQL标准进行扩展,这样当从数据库检索一个值时,可以把这个值

加长到指定的长度。例如,指定一个字段的类型为INT(6),就可以保证所包含数字少于 6 个的值从数据库中检索出来

时能够自动地用空格填充。需要注意的是,使用一个宽度指示器不会影响字段的大小和它可以存储的值的范围。万一

我们需要对一个字段存储一个超出许可范围的数字,MySQL会根据允许范围最接近它的一端截短后再进行存储。还有

一个比较特别的地方是,MySQL会在不合规定的值插入表前自动修改为0。

       下面的表显示了需要的每个整数类型的存储和范围:


       上面定义的都是有符号的,当然了,也可以加上UNSIGNED关键字,定义成无符号的类型,那么对应的取值范围

就要翻翻了,比如:TINYINT UNSIGNED的取值范围为0~255。

       例如我们存储人的年龄,就是使用TINYINT UNSIGNED:

CREATE TABBLE tb1(
    age TINYINT UNSIGNED,
);

       (2)浮点型

       MySQL支持的三个浮点类型是FLOAT、DOUBLE和DECIMAL类型。FLOAT数值类型用于表示单精度浮点数值,

而DOUBLE数值类型用于表示双精度浮点数值。与整数一样,这些类型也带有附加参数:一个显示宽度指示器和一个

小数点指示器。比如语句 FLOAT(7,3) 规定显示的值不会超过7位数字,小数点后面带有3位数字。对于小数点后面的

位数超过允许范围的值,MySQL会自动将它四舍五入为最接近它的值,再插入它。

       DECIMAL数据类型用于精度要求非常高的计算中,这种类型允许指定数值的精度和计数方法作为选择参数。精度

在这里指为这个值保存的有效数字的总个数,而计数方法表示小数点后数字的位数。比如语句DECIMAL(7,3) 规定了

存储的值不会超过7位数字,并且小数点后不超过3位。

      下表给出了它们的存储范围:


       我在MySQL中建立了一个表,有一列为FLOAT(5, 3);做了以下试验:

       1)插入123.45678,最后查询得到的结果为99.999;

       2)插入123.456,最后查询结果为99.999;

       3)插入12.34567,最后查询结果为12.346;

       所以,在使用浮点型的时候,还是要注意陷阱的,要以插入数据库中的实际结果为准。

       例如我们存储工作人员的工资就是使用FLOAT(8,2) UNSIGNED:

CREATE TABBLE tb1(
    salary FLOAT(8,2) UNSIGNED
);

       二日期时间类型

       在处理日期和时间类型的值时,MySQL带有5个不同的数据类型可供选择。它们可以被分成简单的日期、时间类

型,和混合日期、时间类型。根据要求的精度,子类型在每个分类型中都可以使用,并且MySQL带有内置功能可以把

多样化的输入格式变为一个标准格式。

       下表解释了五种日期和时间类型:


        1)MySQL用DATE和YEAR类型存储简单的日期值,使用TIME类型存储时间值。这些类型可以描述为字符串或不

带分隔符的整数序列。如果描述为字符串,DATE类型的值应该使用连字号作为分隔符分开,而TIME类型的值应该使

用冒号作为分隔符分开。需要注意的是,没有冒号分隔符的TIME类型值,将会被MySQL理解为持续的时间,而不是

时间戳。

       2)除了日期和时间数据类型,MySQL还支持DATEYIME和TIMESTAMP这两种混合类型。它们可以把日期和时间

作为单个的值进行存储。这两种类型通常用于自动存储包含当前日期和时间的时间戳,并可在需要执行大量数据库事

务和需要建立一个调试和审查用途的审计跟踪的应用程序中发挥良好作用。如果我们对TIMESTAMP类型的字段没有

明确赋值,或是被赋与了NULL值。MySQL会自动使用系统当前的日期和时间来填充它。

       例如我们存储添加用户的出生年月就是使用:

CREATE TABBLE tb1(
    brithday DATE
);

       三字符串类型

       MySQL提供了6个基本的字符串类型,分别为CHAR、VARCHAR、TINYTEST、TEXT、MEDIUMTEXT、

LONGTEXT六种字符串类型。可以存储的范围从简单的一个字符到巨大的文本块或二进制字符串数据。

      下标解释了六种基本字符串类型:


       1)CHAR(n)和VARCHAR(n)中括号中n代表字符的个数,并不代表字节个数,所以当使用了中文的时候(UTF8)意

着可以插入m个中文,但是实际会占用m*3个字节。

       2)同时CHAR和VARCHAR最大的区别就在于CHAR不管实际VALUE都会占用n个字符的空间,而VARCHAR只会

用实际字符应该占用的空间+1,并且实际空间+1

       3)超过CHAR和VARCHAR的n设置后,字符串会被截断。

       4)CHAR在存储的时候会截断尾部的空格,VARCHAR和TEXT不会。

       5)VARCHAR会使用1-3个字节来存储长度,TEXT不会。

       6)CHAR类型用于定长字符串,并且必须在圆括号内用一个大小修饰符来定义。这个大小修饰符的范围从 0-

255。比指定长度大的值将被截短,而比指定长度小的值将会用空格作填补。

       例如我们存储登录用户的用户名就是使用VARCHAR(20):

CREATE TABLE tb1(
    username VARCHAR(20)
);

       实例:

       我们现在来验证上述四个字段是否能创建成功:

CREATE TABLE tb1 (
    username VARCHAR(20),
    age TINYINT UNSIGNED,
    salary FLOAT(8,2) UNSIGNED,
    brithday DATE
);

       创建数据表tb1和查看数据表结构的结果为:


       我们向数据表tb1中插入记录:

INSERT tb1 VALUES('Tom',22,5555.55,'1994-10-01');

       执行的结果显示为:


       四复合类型

  MySQL还支持两种复合数据类型ENUM和SET,它们扩展了SQL规范。虽然这些类型在技术上是字符串类型,但

是可以被视为不同的数据类型。一个ENUM类型只允许从一个集合中取得一个值;而SET类型允许从一个集合中取得

任意多个值。

       (1)ENUM类型

       ENUM(“member1″, "member2″, … “member65535″)

ENUM数据类型就是定义了一种枚举,最多包含65535个不同的成员。当定义了一个ENUM的列时,该列的值限制为

列定义中声明的值。如果列声明包含NULL属性,则NULL将被认为是一个有效值,并且是默认值。如果声明了NOT

 NULL,则列表的第一个成员是默认值。

  ENUM类型因为只允许在集合中取得一个值,有点类似于单选项。在处理相互排拆的数据时容易让人理解,比如

人类的性别。ENUM类型字段可以从集合中取得一个值或使用NULL值,除此之外的输入将会使MySQ在这个字段中插

入一个空字符串。另外如果插入值的大小写与集合中值的大小写不匹配,MySQL会自动使用插入值的大小写转换成与

集合中大小写一致的值。

       ENUM类型在系统内部可以存储为数字,并且从1开始用数字做索引。一个ENUM类型最多可以包含65536个元

素,其中一个元素被MySQL保留,用来存储错误现的值是合法输入,除此之外其它任何输入都将失败。这说明通过搜

索包含空字符串或对应数字索引为0的行就可以很容易地找到错误记录的位置。

       (2)SET类型

       SET(“member", "member2″, … “member64″)

       SET数据类型为指定一组预定义值中的零个或多个值提供了一种方法,这组值最多包括64个成员。值的选择限制

为列定义中声明的值。

     SET类型与ENUM类型相似但不相同。SET类型可以从预定义的集合中取得任意数量的值。并且与ENUM类型相

同的是任何试图在SET类型字段中插入非预定义的值都会使 MySQL插入一个空字符串。如果插入一个即有合法的元

素又有非法的元素的记录,MySQL将会保留合法的元素,除去非法的元素。

     一个 SET 类型最多可以包含64项元素。在SET元素中值被存储为一个分离的“位”序列,这些“位”表示与它相对应

的元素。“位”是创建有序元素集合的一种简单而有效的方式。并且它还去除了重复的元素,所以SET类型中不可能包

含两个相同的元素。希望从SET类型字段中找出非法的记录只需查找包含空字符串或二进制值为0的行。

        复合数据类型的例子我们会在以后的博文中使用到,这里不再进行举例。

       五总结

       通过对每种数据类型的用途,物理存储,表示范围等有一个概要的了解。这样在面对具体应用时,就可以根据相

应的特来来选择合适的数据类型,使得我们能够争取在满足应用的基础上,用较小的存储代价换来较高的数据库性

能。

      选用数据类型的原则:

      1)选择最小的可用的类型;

      2)从速度方面考虑,选择固定的列  使用CHAR类型;

      3)从节省空间考虑,选择动态的列,使用VARCHAR类型。

      高性能数据库设计原则:

      第一步:大致确定字段的数据类型,数字,字符串,时间等,比较直观。

      第二步:确定特定的类型,比如说 :数字里有TINYINT、SMALLINT、INT、LONGINT等,选择最合适的一个(更

小通常更好)

      第三步:如有必要,请为字段设置默认值。

      当然,索引优化肯定是必不可少的,不过这属于设计表完成之后的优化范围了。



Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.