MySQL的数据类型和建库策略_MySQL
无论是在小得可怜的免费数据库空间或是大型电子商务网站,合理的设计表结构、充分利用空间是十分必要的。这就要求我们对数据库系统的常用数据类型有充分的认识。下面我就将我的一点心得写出来跟大家分享。
一、数字类型。数字类型按照我的分类方法分为三类:整数类、小数类和数字类。
我所谓的“数字类”,就是指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了!可是说句实在话,我们“民用”的数据,哪有要求精度那么高的呢?这两种类型至今我没有用过——我还没有遇到适合于使用它们的事例。
用的最多的,最值得精打细算的,是整数类型。从只占一个字节存储空间的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个字节的空间;而VARVHAR则是可变长度的,如果一个字段可能的值是不固定长度的,我们只知道它不可能超过10个字符,把它定义为 VARCHAR(10)是最合算的,VARCHAR 类型的实际长度是它的值的(实际长度 1)。为什么“ 1”呢?这一个字节用于保存实际使用了多大的长度呀!从这个“ 1”中也应该看到,如果一个字段,它的可能值最长是10个字符,而多数情况下也就是用到了10个字符时,用VARCHAR就不合算了:因为在多数情况下,实际占用空间是11个字节,比用CHAR(10)还多占用一个字节!
举个例子,就是一个存储股票名称和代码的表,股票名称绝大部分是四个字的,即8个字节;股票代码,上海的是六位数字,深圳的是四位数字。这些都是固定长度的,股票名称当然要用 CHAR(8) ;股票代码虽然是不固定长度,但如果使用VARVHAR(6),一个深圳的股票代码实际占用空间是5个字节,而一个上海的股票代码要占用7个字节!考虑到上海的股票数目比深圳的多,那么用VARCHAR(6)就不如CHAR(6)合算了。
虽然一个CHAR或VARVHAR的最大长度可以到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个成员,可以选择其中的零个到不限定的多个,占用存储空间是一个到八个字节,由集合可能的成员数目决定。
举个例子来说,在SQLServer中,你可以节约到用一个Bit类型来表示性别(男/女),但MySQL没有Bit,用TINTINT?不,可以用ENUM('帅哥','美眉')!只有两种选择,所以只需一个字节——跟TINYINT一样大,但却可以直接用字符串'帅哥'和'美眉'来存取。真是太方便啦!
好了,MySQL的数据类型介绍得差不多,我的建库策略也随着介绍数据类型介绍给大家一些。但这只是其中一部分,篇幅有限,不能再细说;其他的,就靠各人在对数据类型理解的基础上,多多实践、多多讨论。

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

AI Hentai Generator
Generate AI Hentai for free.

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



DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

New SOTA for multimodal document understanding capabilities! Alibaba's mPLUG team released the latest open source work mPLUG-DocOwl1.5, which proposed a series of solutions to address the four major challenges of high-resolution image text recognition, general document structure understanding, instruction following, and introduction of external knowledge. Without further ado, let’s look at the effects first. One-click recognition and conversion of charts with complex structures into Markdown format: Charts of different styles are available: More detailed text recognition and positioning can also be easily handled: Detailed explanations of document understanding can also be given: You know, "Document Understanding" is currently An important scenario for the implementation of large language models. There are many products on the market to assist document reading. Some of them mainly use OCR systems for text recognition and cooperate with LLM for text processing.

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile
