Mysql 简明教程
MySQL - FLOAT
FLOAT 数据类型是数字数据类型的一部分。数字数据类型用于存储数字,并且可以根据其特性(例如存储大小和精度)将其分类为各种子类型。
The FLOAT data type is a part of the numeric data type. Numeric data types are used to store numbers, and they can be categorized into various subtypes based on their characteristics, such as storage size and precision.
The MySQL FLOAT Data Type
MySQL FLOAT 数据类型是一个浮点数类型,用于存储近似数字值。它在 4 个字节中存储近似数字值,并表示单精度值。
The MySQL FLOAT datatype is a floating-point number type that stores approximate numeric values. It stores approximate numeric values in 4 bytes and represents single-precision values.
FLOAT 适用于广泛的数字值,但由于 IEEE 754 标准的限制,会以近似方式存储它们。
FLOAT is suitable for a wide range of numeric values but stores them in an approximate manner due to the IEEE 754 standard limitations.
Syntax
以下是设置字段数据类型为 FLOAT 的基本语法——
Following is the basic syntax to set the datatype of a field as FLOAT −
CREATE TABLE (column_name FLOAT, ...);
Example
在这个示例中,让我们使用 CREATE TABLE 语句创建一个名为“datatype_demo”的新数据库表,其中包含表示 FLOAT 值的列——
In this example, let us create a new database table named 'datatype_demo' using CREATE TABLE statement with columns representing FLOAT values −
CREATE TABLE datatype_demo(
ID INT,
NAME VARCHAR(50),
HEIGHT FLOAT,
WEIGHT FLOAT
);
以下是所获得的输出 −
Following is the output obtained −
Query OK, 0 rows affected (0.03 sec)
Verification
创建表后,我们可以通过按如下所示检索表的定义来验证“HEIGHT”和“WEIGHT”字段的数据类型——
Once the table is created, we can verify the data types of the 'HEIGHT' and 'WEIGHT' fields by retrieving the table’s definition as shown below −
DESC datatype_demo;
DESC 命令的结果会显示“HEIGHT”和“WEIGHT”字段具有 FLOAT 数据类型 -
The result of the DESC command will show that the 'HEIGHT' and 'WEIGHT' fields have the FLOAT data type −
为了进一步验证,让我们使用以下 INSERT 语句将一些值插入表中 -
To verify further, let us insert some values into the table using the following INSERT statement −
INSERT INTO datatype_demo VALUES
(1, 'John', 171.3, 65.7),
(2, 'Rob', 45, 75),
(3, 'Salman', 12.74839, 54.262),
(4, 'Arush', NULL, NULL),
(5, 'James', 'h', 'w');
以下是所获得的输出 −
Following is the output obtained −
ERROR 1265 (01000): Data truncated for column 'HEIGHT' at row 1
正如预期的那样,FLOAT 字段可以毫无问题地接受单精度浮点数。然而,当试图向这些字段中插入非数字值时,如“h”和“w”,MySQL 会引发一个错误,表示数据被截断。
As expected, the FLOAT fields accept single precision floating-point numbers without any issues. However, when attempting to insert non-numeric values into these fields, such as 'h' and 'w,' MySQL raises an error, indicating data truncation.
最后,要查看已插入表中的数据,我们可以使用如下所示的 SELECT 语句 -
Finally, to view the data that has been inserted into the table, we can use the SELECT statement as shown below −
SELECT * FROM datatype_demo;
结果表如下 −
The resultant table is as follows −
Other Representations of MySQL FLOAT
MySQL 有一个规定,以位形式为 FLOAT 数据类型指定精度范围(不是指数)。这些位指定在关键字 FLOAT 之后的括号内,即 FLOAT(p)。
MySQL has a provision to specify the range of precision (not the exponent) for the FLOAT datatype in the form of bits. These bits are specified within the parenthesis following the keyword FLOAT, i.e. FLOAT(p).
但是,此精度值仅用于确定存储大小,并且仅容纳 7 位小数,范围从 0 到 23 位。如果精度位超过 23,则数据类型变为 DOUBLE。
However, this precision value is only used to determine the storage size and only holds up to 7 decimal places, with the range from 0 to 23 bits. If the precision bit exceeds 23, the data type becomes DOUBLE.
Example
首先,我们将删除现有的“datatype_demo”表 -
First, we will drop the existing 'datatype_demo' table −
DROP TABLE datatype_demo;
获得的输出如下 −
The output obtained is as follows −
Query OK, 0 rows affected (0.01 sec)
然后,我们将创建一个新表“datatype_demo”,指定“HEIGHT”列的精度为 20 位 -
Then, we will create a new table 'datatype_demo' specifying a precision of 20 bits for the 'HEIGHT' column −
CREATE TABLE datatype_demo(
ID INT,
NAME VARCHAR(50),
HEIGHT FLOAT(20)
);
以下是上面代码的输出: -
Following is the output of the above code −
Query OK, 0 rows affected (0.02 sec)
Verification
尽管我们指定了 20 位的精度,“HEIGHT”列仍将在单精度范围内存储浮点值,最多容纳 7 位小数。为了验证表定义,我们可以使用如下所示的 DESC 命令 -
Even though we specified a precision of 20 bits, the 'HEIGHT' column will still store float values within the single-precision range, holding up to 7 decimal places. To verify the table’s definition, we can use the DESC command as shown below −
DESC datatype_demo;
生成的表如下 -
The table produced is as follows −
如果精度位超过 23,则数据类型变为 DOUBLE。查看下面的查询 -
If the precision bit exceeds 23, the datatype becomes DOUBLE. Look at the query below −
CREATE TABLE datatype_demo1(
ID INT,
NAME VARCHAR(50),
HEIGHT FLOAT(30)
);
我们得到以下输出 -
we get the following output −
Query OK, 0 rows affected (0.02 sec)
同样,我们可以使用 DESC 命令验证表的定义 -
Again, we can verify the table’s definition using the DESC command −
DESC datatype_demo1;
以下是要获得的表:
Following is the table obtained −