Numeric data type is mainly used to store numbers. mysql
provides a variety of numerical data types. Different data types provide different value ranges and can be stored The larger the value range, the larger the storage space required.
mysql
The main integer types provided are TINYINT
, SMALLINT
, MEDIUMINT
, INT
, BIGINT
. Integer type attribute fields can add AUTO_INCREMENT
auto-increment constraints.
Look at the following two tables;
Table 1: Integer data types in mysql
Table 2, mysql
Value ranges of different integer types
For example:
year int(4);
This statement indicates that the data in the year
field generally only displays 4
digits The width;
Note:The display width has nothing to do with the data type value range. The display width only specifies the maximum number of numbers that may be displayed. If the number of digits is less than the specified width, it will be filled with spaces; if a value larger than the display width is inserted, as long as the value does not exceed the type of integer Within the value range, values can still be inserted and displayed.
: Insert a value 12345 into the year
field. When using select
to query the column value, mysql
will be displayed as the complete 12345
with 5 digits instead of the 4 digit value. Other integer data types can also specify the required display width when defining the table structure. If not specified, the system specifies a default width for each type.
Different integer types have different value ranges and require different storage space. Therefore, the most appropriate type should be selected according to actual needs. This will help improve query efficiency and save storage space. In real life, you need to use numbers with a decimal part, and integer types do not contain a decimal part. Next we will introduce the decimal types.
Note:Floating point number and fixed point number are used in mysqlThe display width is only used for display and cannot limit the value range and occupied space.
Floating point number type and fixed point number type
to represent decimals.
as precision
, which represents the total number of digits; N is called scale, represents
number of decimal digits. DECIMAL
FLOAT and
DOUBLE,
DECIMAL is actually stored as a string, the maximum possible The value range is the same as
DOUBLRE, but its valid value range is determined by the values of
M and
D. how to say?
If
M
D is fixed, its value range will become larger as
M becomes larger.
So in mysql, what is the value range of floating-point and fixed-point types?
FLOAT
type Value range:
##DOUBLE
Unsigned value range:
##Notes:
Create a data table asIt may be a bit confusing, let’s give an example:
yunweijia_1, the field type is as follows;
mysql> create table yunweijia_1 (x float(5,1), y double(5,1), z decimal(5,1)); Query OK, 0 rows affected, 2 warnings (0.04 sec) mysql>
Then we insert a piece of data into this table,
sqlThe statement is as follows:
mysql> insert into yunweijia_1 values (6.66, 7.77, 8.888); Query OK, 1 row affected, 1 warning (0.01 sec) mysql>
Look at the above prompt message, it is Not when we execute the insert statement, a warning message is given, and then let’s take a look at what the warning message is;
mysql> show warnings; +-------+------+----------------------------------------+ | Level | Code | Message | +-------+------+----------------------------------------+ | Note | 1265 | Data truncated for column 'z' at row 1 | +-------+------+----------------------------------------+ 1 row in set (0.00 sec) mysql>can see that he prompted that the z field is in It was truncated during insertion, but no warning was raised when
x
andy were truncated.
Then check the results:
mysql> select * from yunweijia_1;
+------+------+------+
| x | y | z |
+------+------+------+
| 6.7 | 7.8 | 8.9 |
+------+------+------+
1 row in set (0.00 sec)
mysql>
可以看到分别都进行了四舍五入,这点需要注意。
FLOAT
和DOUBLE
在不指定精度操作时,默认会按照实际的精度(由计算机硬件和操作系统决定),DECIMAL
若不指定精度则默认为(10,0);
浮点数相对于定点数的优点是在长度一定的情况下,浮点数能够表示更大的数据范围;他的缺点是会引起精度问题;
注意点:在
mysql
中,定点数以字符串形式存储,在对精度要求比较高的时候,使用DECIMAL
的类型比较好,另外,两个浮点数进行减法和比较运算时,容易出现问题,所以在使用浮点数时需要注意,并尽量避免做浮点数比较。
mysql
中有多种表示日期的数据类型,主要有:
DATETIME
DATE
TIMESTAMP
TIME
YEAR
比如说,当只记录年信息的时候,可以只使用YEAR
类型,而没有必要使用DATE
。
每一个类型都有合法的取值范围,当指定确实不合法的值时系统将“零”值插入到数据库中。
year
类型是一个单字节类型,用于表示年,在存储时只需要1字节。可以使用各种格式指定year
值,如下:
1、以4位字符串或者4位数字格式表示的year
,范围为1901
~2155
。输入格式为yyyy
或者'yyyy',例如:输入‘2022’或者2022,插入到数据库的值均为2022。
2、以2位字符串格式表示的year
,范围为“00”到“99”。“00”~“69”和“70”~“99”范围的值分别被转换为“2022”~“2069”和“1970”~“1999”范围的year
值。“0”与“00”的作用相同。插入超过取值范围的值将被转换成2000
。
3、以两位数字表示的YEAR
,范围为1~99。1~69和70~99范围的值分别被转换为200·~2069和1970~1999范围的YEAR
值。和字符相比较而言,这里的0值,将被转换成0000,而不是2000。
注意点:
两位整数范围与两位字符串范围稍有不同。例如,插入2000年,有人可能会使用数字格式的0表示
year
,实际上,插入数据库的值为0000,而不是所希望的2000。只有使用字符串格式的“0”或者“00”,才可以被正确的解释为2000,。非法YEAR
值将被转换为0000。
栗子:
我们创建一个名为yunweijia_2
的数据表,使用以下sql
语句:
mysql> create table yunweijia_2 (y year); Query OK, 0 rows affected (0.04 sec) mysql>
然后我们往表中插入一条数据;
mysql> insert into yunweijia_2 values(2022), ('2022'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql>
再次尝试插入一条数据;
mysql> insert into yunweijia_2 values ('2166'); ERROR 1264 (22003): Out of range value for column 'y' at row 1 mysql>
我们发现‘2166’超出了YEAR
类型的取值范围,所以不能插入数据,这个时候我们看下mysql
数据库中的数据;
mysql> select * from yunweijia_2; +------+ | y | +------+ | 2022 | | 2022 | +------+ 2 rows in set (0.00 sec) mysql>
栗子:
首先我们先删除yunweijia_2
数据表中的数据;
mysql> delete from yunweijia_2; Query OK, 2 rows affected (0.01 sec) mysql>
可以看到提示删除了两条数据;
然后我们再插入以下数据;
mysql> insert into yunweijia_2 values ('0'), ('00'), ('88'), ('22'); Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql>
然后看下结果是什么:
mysql> select * from yunweijia_2;
+------+
| y |
+------+
| 2000 |
| 2000 |
| 1988 |
| 2022 |
+------+
4 rows in set (0.00 sec)
mysql>
从上面i看到,字符串‘0’和‘00’的作用相同,分别都转换成了2000年;‘88’转换成了‘1988’;‘22’转换成了‘2022’。
栗子:
老规矩,先删除表中现有的数据;
mysql> delete from yunweijia_2; Query OK, 4 rows affected (0.01 sec) mysql>
然后再插入数据:
mysql> insert into yunweijia_2 values (0), (99), (22); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>
看下结果:
mysql> select * from yunweijia_2;
+------+
| y |
+------+
| 0000 |
| 1999 |
| 2022 |
+------+
3 rows in set (0.00 sec)
mysql>
可以看到,0被转换成了0000,99被转换成了1999,22被转换成了2022。
time
类型用在只需要时间信息的值,在存储时需要3字节,格式为HH:MM:SS
。
其中,HH
表示小时,MM
表示分钟,SS
表示秒。
mysql
中time
类型的取值范围为-838:59:59~838:59:598,小时部分为什么会这么大呢,这是因为time
类型不仅可以用来表示一天的时间,还可能是某个事件过去的时间或者两个事件之间的时间间隔。
(1)‘D HH:MM:S’格式的字符串,可以使用下面任何一种“非严格”的语法:
‘HH:MM:SS’
‘HH:MM’
‘D HH:MM’
这里的D表示日,可以取0~34的值,在插入数据库时,D被转换成小时保存,格式为“D*24+HH”;
(2)‘HH:MM:SS’格式的、没有间隔符的字符串或者HHMMSS格式的数值,假定是有意义的时间。例如:'223344'被理解成‘22:33:44’,但是‘223366’是不合法的(他有一个没有意义的秒部分),存储时将变成00:00:00。
注意点:
为
time
列分配简写时应该注意,如果没有冒号,mysql
解释值时,假定最右边的两位表示秒。如果time
值中使用了冒号,则肯定被单做是当天的时间。例如:
11:22 会被
mysql
解释成 00:11:22'11:22'会被
mysql
解释成11:22:00
创建一个名为yunweijia_3
的数据表;
mysql> create table yunweijia_3(t time); Query OK, 0 rows affected (0.03 sec) mysql>
往这个表中插入数据:
mysql> insert into yunweijia_3 values ('10:06:06'), ('22:22'), ('2 10:10'), ('3 00'), ('20'); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql>
看下表中的数据:
mysql> select * from yunweijia_3; +----------+ | t | +----------+ | 10:06:06 | | 22:22:00 | | 58:10:00 | | 72:00:00 | | 00:00:20 | +----------+ 5 rows in set (0.00 sec) mysql>
小提示:在使用‘D HH’格式时,小时一定要使用双位数值,如果是小于10的小时数,应在前面加0以用来补全双位数值。
举个栗子 :
首先我们删除数据表yunweijia_3
里面的数据;
mysql> delete from yunweijia_3; Query OK, 5 rows affected (0.01 sec) mysql>
然后再表中插入数据:
mysql> insert into yunweijia_3 values ('101112'), (111213), ('0'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>
再次向表中插入一条数据;
mysql> insert into yunweijia_3 values (107010); ERROR 1292 (22007): Incorrect time value: '107010' for column 't' at row 1 mysql>
可以看到这条数据没有插入成功,我看看下提示信息;
mysql> show warnings; +-------+------+--------------------------------------------------------+ | Level | Code | Message | +-------+------+--------------------------------------------------------+ | Error | 1292 | Incorrect time value: '107010' for column 't' at row 1 | +-------+------+--------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
为什么这次会插入失败呢,是不是因为我们的分钟应该是小于60的啊,但是这里是70,所以报错了。
我们查看下这个数据库中的数据现在的展示吧:
mysql> select * from yunweijia_3; +----------+ | t | +----------+ | 10:11:12 | | 11:12:13 | | 00:00:00 | +----------+ 3 rows in set (0.00 sec) mysql>
又举个栗子:
我们还可以使用系统函数向time
字段列插入值;
删除数据表yunweijia_3
里面的数据;
mysql> delete from yunweijia_3; Query OK, 3 rows affected (0.01 sec) mysql>
然后在表中插入数据;
mysql> insert into yunweijia_3 values (CURRENT_TIME), (NOW()); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql>
查看下数据库中的结果;
mysql> select * from yunweijia_3;
+----------+
| t |
+----------+
| 23:03:41 |
| 23:03:41 |
+----------+
2 rows in set (0.00 sec)mysql>
从结果中可以看到,获取系统当前的日志插入到time
类型的列中。
The above is the detailed content of mysql data type example analysis. For more information, please follow other related articles on the PHP Chinese website!