What type does mysql use for decimals?
Mysql decimal available types: 1. FLOAT type, which can store single-precision floating-point numbers; 2. DOUBLE type, which can store double-precision floating-point numbers; 3. DECIMAL type, used to store precise values, such as accounting Currency data in the system.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Floating point numbers and fixed point numbers are used to represent decimals in MySQL.
There are two floating-point types, single-precision floating-point numbers (FLOAT) and double-precision floating-point numbers (DOUBLE); there is only one fixed-point type, which is DECIMAL.
Both floating-point types and fixed-point types can be represented by (M, D), where M is called precision, indicating the total number of digits; D is called scale, indicating the number of decimal digits.
The value range of floating point number type is M (1~255) and D (1~30, and cannot be greater than M-2), which represent the display width and number of decimal places respectively. M and D are optional in FLOAT and DOUBLE, and the FLOAT and DOUBLE types will be saved to the maximum precision supported by the hardware. The default D value for DECIMAL is 0 and M value is 10.
The following table lists the decimal types and storage requirements in MySQL.
The DECIMAL type is different from FLOAT and DOUBLE. DOUBLE is actually stored in the form of a string. The possible maximum value range of DECIMAL is the same as DOUBLE, but the effective value range is determined by M and D. If M is changed and D is fixed, the value range will become larger as M becomes larger.
As can be seen from the above table, the storage space of DECIMAL is not fixed, but is determined by the precision value M, occupying M 2 bytes.
The value range of the FLOAT type is as follows:
The signed value range: -3.402823466E 38~-1.175494351E-38.
Unsigned value range: 0 and -1.175494351E-38~-3.402823466E 38.
The value range of the DOUBLE type is as follows:
The signed value range: -1.7976931348623157E 308~ -2.2250738585072014E-308.
Unsigned value range: 0 and -2.2250738585072014E-308~-1.7976931348623157E 308.
Note: Whether it is a fixed-point or floating-point type, if the user-specified precision exceeds the precision range, it will be rounded for processing.
FLOAT and DOUBLE will default to the actual precision when the precision is not specified. DECIMAL will default to (10, 0) if the precision is not specified.
The advantage of floating-point numbers over fixed-point numbers is that they can represent a larger range when the length is constant; the disadvantage is that it can cause accuracy problems.
DECIMAL type
DECIMAL
The data type is used to store precise values in the database. We often use the DECIMAL
data type for columns that retain exact precision, such as currency data in accounting systems.
To define a column with data type DECIMAL
, use the following syntax:
column_name DECIMAL(P,D);
In the above syntax:
P
represents the precision of the number of significant digits.P
range is1~65
.D
represents the number of digits after the decimal point. The range ofD
is0
~30
. MySQL requiresD
to be less than or equal to (<=
)P
.
DECIMAL(P,D)
Indicates that the column can store P
digits of D
decimal places. The actual range of a decimal column depends on precision and scale.
Like the INT data type, the DECIMAL
type also has UNSIGNED
and ZEROFILL
attributes. If the UNSIGNED
attribute is used, the column for DECIMAL UNSIGNED
will not accept negative values.
If ZEROFILL
is used, MySQL will pad the display value to 0
to display the width specified by the column definition. Additionally, if we use ZERO FILL
on the DECIMAL
column, MySQL will automatically add the UNSIGNED
attribute to the column.
The following example uses a column called amount
defined using the DECIMAL
data type.
amount DECIMAL(6,2);
In this example, the amount
column can store up to 6
digits with a decimal place of 2
; therefore, ## The #amount column ranges from
-9999.99 to
9999.99.
column_name DECIMAL(P);
column_name DECIMAL(P,0);
column_name DECIMAL;
P is
10.
MySQL DECIMAL STORAGE
MySQL分别为整数和小数部分分配存储空间。 MySQL使用二进制格式存储DECIMAL
值。它将9
位数字包装成4
个字节。
对于每个部分,需要4
个字节来存储9
位数的每个倍数。剩余数字所需的存储如下表所示:
剩余数字 | 位 |
---|---|
0 | 0 |
1–2 | 1 |
3–4 | 2 |
5–6 | 3 |
7-9 | 4 |
例如,DECIMAL(19,9)
对于小数部分具有9
位数字,对于整数部分具有19
位= 10
位数字,小数部分需要4
个字节。 整数部分对于前9
位数字需要4
个字节,1
个剩余字节需要1
个字节。DECIMAL(19,9)
列总共需要9
个字节。
MySQL DECIMAL数据类型和货币数据
经常使用DECIMAL
数据类型的货币数据,如价格,工资,账户余额等。如果要设计一个处理货币数据的数据库,则可参考以下语法 -
amount DECIMAL(19,2);
但是,如果您要遵守公认会计原则(GAAP)规则,则货币栏必须至少包含4
位小数,以确保舍入值不超过$0.01
。 在这种情况下,应该定义具有4
位小数的列,如下所示:
amount DECIMAL(19,4);
MySQL DECIMAL数据类型示例
首先,创建一个名为test_order
的新表,其中包含三列:id
,description
和cost
。
CREATE TABLE test_order ( id INT AUTO_INCREMENT PRIMARY KEY, description VARCHAR(255), cost DECIMAL(19,4) NOT NULL );
第二步,将资料插入test_order表。
INSERT INTO test_order(description,cost) VALUES('Bicycle', 500.34),('Seat',10.23),('Break',5.21);
第三步,从test_order表查询数据。
SELECT * from test_order
第四步,更改cost
列以包含ZEROFILL
属性。
ALTER TABLE test_order MODIFY cost DECIMAL(19,4) zerofill;
第五步,再次查询test_order表。
SELECT * from test_order
查询结果:
如上所见,在输出值中填充了许多零。
因为zerofill,当我们插入负值会报错:
INSERT INTO test_order(description,cost) VALUES('test', -100.11); 提示: [SQL]INSERT INTO test_order(description,cost) VALUES('test', -100.11) [Err] 1264 - Out of range value for column 'cost' at row 1
其它插入测试结论:
当数值在其取值范围之内,小数位多了,则四舍五入后直接截断多出的小数位。
若数值在其取值范围之外,则直接报Out of range value错误。
【相关推荐:mysql视频教程】
The above is the detailed content of What type does mysql use for decimals?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
