MySQL-2 column type and column properties

1. Data Type

The so-called data types: a unified classification data, from the perspective of the system to be able to use a unified way to manage: a better use of the limited space
SQL data types will be divided into three categories: numeric types, string type and date type
Here Insert Picture Description

1.1 Numeric

Numerical data: all values
the system is divided into numeric integer and fractional type

1.1.1 Integer

The system will become integral subdivided class 5:
Here Insert Picture Description

  • It can only be inserted within the range of integer
  • Values are all types of default SQL signed: positive and negative points
    will need to use unsigned data: the need to define the data types: int unsigned;
    unsigned: from 0

1.1.2 decimals

Decimal Type: decimal point or a value outside the range of integer type.

SQL, the decimal type subdivided into two types: floating-point and fixed-point type

  • Float: floating decimal point, limited precision, and lose precision
  • Spot type: decimal point fixation, precision, without losing precision

(1) float
floating-point data type is a data accuracy: because then beyond the specified range, accuracy is lost (automatic rounding)
float: Theory divided into two precision

  • Float: single precision, 4 bytes of data storage, accuracy range of about 7 to about
  • Double: 双精度,占用8个字节存储数据, 精度方位大概为15位左右
    Here Insert Picture Description
    创建浮点数:
    float(M,D): M代表总长度,D代表小数部分长度, 整数部分长度为M-D

插入数据: 可以是直接小数,也可以是科学计数法。整型部分是不能超出长度的,但是小数部分可以超出长度(系统会自动四舍五入)

结果: 浮点数一定会进行四舍五入(超出精度范围); 浮点数如果是因为系统进位导致整数部分超出指定的长度,那么系统也允许成立.

(2)定点型:decimal(更安全)
Here Insert Picture Description
在MySQL中,定点数以字符串形式存储,因此,其精度比浮点数要高,而且浮点数会出现误差,这是浮点数一直存在的缺陷。如果要对数据的精度要求比较高,还是选择定点数decimal比较安全。

定点型: 绝对的保证整数部分不会被四舍五入(不会丢失精度),小数部分有可能(理论小数部分也不会丢失精度)

插入数据: 定点数的整数部分一定不能超出长度(进位不可以),小数部分的长度可以随意超出(系统自动四舍五入)

浮点数如果进位导致长度溢出没有问题,但是定点数不行

1.2 时间日期

Here Insert Picture Description
插入数据: 时间time可以是负数,而且可以是很大的负数, year可以使用2位数插入,也可以使用4位数,但是前面添加的前两位有区别。

Timestamp字段: 只要当前所在的记录被更新, 该字段一定会自动更新成当前时间

1.3 字符串

在SQL中,将字符串类型分成了6类: char,varchar,text , blob, enum和set
Here Insert Picture Description

1.3.1 定长字符串

定长字符串: char, 磁盘(二维表)在定义结构的时候,就已经确定了最终数据的存储长度.
Char(L): L代表length, 可以存储的长度, 单位为字符, 最大长度值可以为255.
Char(4): 在UTF8 环境下,需要4 * 3 = 12个字节

1.3.2 变长字符串

变长字符串: varchar, 在分配空间的时候, 按照最大的空间分配: 但是实际上最终用了多少,是根据具体的数据来确定.

Varchar(L): L表示字符长度 理论长度是65536个字符, 但是会多出1到2个字节来确定存储的实际长度: 但是实际上如果长度超过255,既不用定长也不用变长, 使用文本字符串text

Varchar(10): 的确存了10个汉字, utf8环境, 10 * 3 + 1 = 31(bytes)
存储了3个汉字: 3 * 3 + 1 = 10(bytes)

如何选择定长或者是变长字符串呢?
定长的磁盘空间比较浪费, 但是效率高: 如果数据基本上确定长度都一样, 就是使用定长, 如身份证, 电话号码, 手机号码等

变长的磁盘空间比较节省, 但是效率低: 如果数据不能确定长度(不同数据有变化), 如姓名, 地址等

1.3.3 文本字符串

如果数据量非常大, 通常说超过255个字符就会使用文本字符串
文本字符串根据存储的数据的格式进行分类: text和blob
Text: 存储文字(二进制数据实际上都是存储路径)
Blob: 存储二进制数据(通常不用)

1.3.4 枚举字符串

枚举: enum, 事先将所有可能出现的结果都设计好, 实际上存储的数据必须是规定好的数据中的一个.

枚举的使用方式
定义: enum(可能出现的元素列表); 如enum(‘男’,’女’,’不男不女’,’妖’,’保密’);
使用: 存储数据,只能存储上面定义好的数据

作用:
一、规范数据格式: 数据只能是规定的数据中的其中一个
二、节省存储空间(枚举通常有一个别名: 单选框): 枚举实际存储的是数值而不是字符串本身.

1.3.5 集合字符串

集合跟枚举很类似: 实际存储的是数值,而不是字符串(集合是多选)。

集合使用方式:
定义: Set(元素列表)
使用: 可以使用元素列表中的元素(多个), 使用逗号分隔

集合中每一个元素都是对应一个对应二进制位。集合的强大在于能够规范数据和节省空间。

1.4 Mysql记录长度

Mysql记录长度: 65535个字节, varchar达不到理论长度, NULL占用一个字节, text文本不占用记录长度(但是本身占据十个字节)

2.列属性

列属性: 真正约束字段的是数据类型, 但是数据类型的约束很单一. 需要有一些额外的约束, 来更加保证数据的合法性.

列属性有很多: NULL/NOT NULL, default, Primary key, unique key, auto_increment,comment

2.1 空属性:NULL(默认的)和NOT NULL(不为空)

虽然默认的, 数据库基本都是字段为空, 但是实际上在真实开发的时候, 尽可能的要保证所有的数据都不应该为空: 空数据没有意义; 空数据没有办法参与运算.

2.2 列描述: comment

描述, 没有实际含义: 是专门用来描述字段,会根据表创建语句保存: 用来给程序猿(数据库管理员)来进行了解的.

2.3 默认值:default

A certain kind of data is a specific value of regular appearance, it can be good to specify at the outset: when you need real data, the user can use the default value of selectivity.

Published 135 original articles · won praise 5 · Views 7072

Guess you like

Origin blog.csdn.net/qq_27921205/article/details/104634402