search
HomeDatabaseMysql Tutorial对MySQL数据类型与相关建库策略进行分析

我们大家都知道免费数据库空间或者大型的电子商务网站,其都有合理的设计表结构与其充分利用空间。这些就要求我们对数据库系统的常用http://database.51cto.com/art/200511/12568.htm数据类型有一个充分的认识。下面我就将我的一点心得写出来跟大家分享。 一

我们大家都知道免费数据库空间或者大型的电子商务网站,其都有合理的设计表结构与其充分利用空间。这些就要求我们对数据库系统的常用http://database.51cto.com/art/200511/12568.htm数据类型有一个充分的认识。下面我就将我的一点心得写出来跟大家分享。

一、数字类型

数字类型按照我的分类方法分为三类:整数类、小数类和数字类。

我所谓的“数字类”,就是指DECIMAL和NUMERIC,它们是同一种类型。它严格的说不是一种数字类型,因为他们实际上是将数字以字符串形式保存的;他的值的每一位(包括小数点)占一个字节的存储空间,因此这种类型耗费空间比较大。

但是它的一个突出的优点是小数的位数固定,在运算中不会“失真”,所以比较适合用于“价格”、“金额”这样对精度要求不高但准确度要求非常高的字段。

小数类,即浮点数类型,根据精度的不同,有FLOAT和DOUBLE两种。它们的优势是精确度,FLOAT可以表示绝对值非常小、小到约1.17E-38(0.000...0117,小数点后面有37个零)的小数,而DOUBLE更是可以表示绝对值小到约2.22E-308(0.000...0222,小数点后面有307个零)的小数。

FLOAT类型和DOUBLE类型占用存储空间分别是4字节和8字节。如果需要用到小数的字段,精度要求不高的,当然用FLOAT了。可是说句实在话,我们“民用”的MySQL数据型,哪有要求精度那么高的呢?这两种类型至今我没有用过――我还没有遇到适合于使用它们的事例。

用的最多的,最值得精打细算的,是整数类型。从只占一个字节存储空间的TINYINT到占8个字节的BIGINT,挑选一个“够用”并且占用存储空间最小的类型是设计数据库时应该考虑的。TINYINT、SMALLINT、MEDIUMINT、INT和BIGINT占用存储空间分别为1字节、2字节、3字节、4字节和8字节,就无符号的整数而言,这些类型能表示的最大整数分别为255、65535、16777215、4294967295和18446744073709551615。

如果用来保存用户的年龄(举例来说,数据库中保存年龄是不可取的),用TINYINT就够了;九城的《纵横》里,各项技能值,用SMALLINT也够了;如果要用作一个肯定不会超过16000000行的表的AUTO_INCREMENT的IDENTIFY字段,当然用MEDIUMINT不用INT,试想,每行节约一个字节,16000000行可以节约10兆多呢!

二、日期时间类型

日期和时间类型比较简单,无非是DATE、TIME、DATETIME、TIMESTAMP和YEAR等几个类型。只对日期敏感,而对时间没有要求的字段,就用DATE而不用DATETIME是不用说的了;单独使用时间的情况也时有发生――使用TIME;但最多用到的还是用DATETIME。在日期时间类型上没有什么文章可做,这里就不再详述。

三、字符(串)类型

不要以为字符类型就是CHAR,CHAR和VARCHAR的区别在于CHAR是固定长度,只要你定义一个字段是CHAR(10),那么不论你存储的数据是否达到了10个字节,它都要占去10个字节的空间;而VARCHAR则是可变长度的,如果一个字段可能的值是不固定长度的,我们只知道它不可能超过10个字符,把它定义为VARCHAR(10)是最合算的,VARCHAR类型的占用空间是它的值的实际长度+1。

为什么要+1呢?这一个字节用于保存实际使用了多大的长度。从这个+1中也应该看到,如果一个字段,它的可能值最长是10个字符,而多数情况下也就是用到了10个字符时,用VARCHAR就不合算了:因为在多数情况下,实际占用空间是11个字节,比用CHAR(10)还多占用一个字节。

举个例子,就是一个存储股票名称和代码的表,股票名称绝大部分是四个字的,即8个字节;股票代码,上海的是六位数字,深圳的是四位数字。这些都是固定长度的,股票名称当然要用CHAR(8);股票代码虽然是不固定长度,但如果使用VARCHAR(6),一个深圳的股票代码实际占用空间是5个字节,而一个上海的股票代码要占用7个字节!考虑到上海的股票数目比深圳的多,那么用VARCHAR(6)就不如CHAR(6)合算了。

虽然一个CHAR或VARCHAR的最大长度可以到255,我认为大于20的CHAR是几乎用不到的――很少有大于20个字节长度的固定长度的东东吧?不是固定长度的就用VARCHAR。大于100的VARCHAR也是几乎用不到的――比这更大的用TEXT就好了。

TINYTEXT,最大长度为255,占用空间也是实际长度+1;TEXT,最大长度65535,占用空间是实际长度+2;MEDIUMTEXT,最大长度16777215,占用空间是实际长度+3;LONGTEXT,最大长度4294967295,占用空间是实际长度+4。为什么+1、+2、+3、+4?你要是还不知道就该打PP了。

这些可以用在论坛啊、新闻啊,什么的,用来保存文章的正文。根据实际情况的不同,选择从小到大的不同类型。

四、枚举和集合类型

枚举(ENUM)类型,最多可以定义65535种不同的字符串从中做出选择,只能并且必须选择其中一种,占用存储空间是一个或两个字节,由枚举值的数目决定;集合(SET)类型,最多可以有64个成员,可以选择其中的零个到不限定的多个,占用存储空间是一个到八个字节,由集合可能的成员数目决定。

举个例子来说,在sql server(WINDOWS平台上强大的数据库平台)中,你可以节约到用一个BIT类型来表示性别(男/女),但MySQL(和PHP搭配之最佳组合)没有BIT,用TINTINT吗?不,可以用ENUM(’帅哥’,’美眉’),只有两种选择,所以只需一个字节――跟TINYINT一样大,但却可以直接用字符串’帅哥’和’美眉’来存取。真是太方便啦!

好了,MySQL(和PHP搭配之最佳组合)的MySQL数据类型介绍得差不多,我的建库策略也随着介绍MySQL数据类型介绍给大家一些。但这只是其中一部分,篇幅有限,不能再细说;其他的,就靠各人在对数据类型理解的基础上,多多实践、多多讨论。


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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.