JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

MySQL currency data type

Author:JIYIK Last Updated:2025/04/22 Views:

This article introduces the DECIMAL(P,D) data type which is best suited for storing monetary values ​​in MySQL.


MySQL currency data type

Monetary values ​​need to be represented accurately. We don't need to use only approximate data types such as float.

To insert monetary data, we can use fixed-point numeric data types such as DECIMAL(P,D).

DECIMAL(P,D)Indicates that a column can store at most P digits and D decimal places. P and D are explained after the syntax.

We can define a DECIMAL(P,D)column with data type .

# Syntax
columnName DECIMAL(P,D)

Where P represents the precision of the number of significant digits, and its range is 1 to 65.

Although D is a scale, it represents the number of digits after the decimal point. The value range of D is 0 to 30.

DECIMAL(P,D)The condition D<=P must be met when using ; otherwise, we will get an error. If P and D are not specified, the maximum number of digits in a DECIMAL type column is 65.

The range of a DECIMAL type column depends on the precision (P) and scale (D). Let us understand it by creating a table demonstration with two attributes ID and MONEY and fill it with some data.

You can follow up with us using the following enquiries.

Sample code:

# create a table
CREATE table demo(
    ID INT NOT NULL PRIMARY KEY,
    MONEY DECIMAL(12,2) NOT NULL
);

# insert data
INSERT INTO demo VALUES
(1,1254367892.50),
(2,8754367893.60),
(3,9854367895.65),
(4,9224367896.70);

# display data
SELECT * FROM demo;

Output:

+----+---------------+
| ID | MONEY         |
+----+---------------+
|  1 | 1254367892.50 |
|  2 | 8754367893.60 |
|  3 | 9854367895.65 |
|  4 | 9224367896.70 |
+----+---------------+
4 rows in set (0.00 sec)

We can also use FIXED, DEC or NUMERIC keywords instead of DECIMAL keyword as all mentioned keywords are synonyms of DECIMAL. See the following example as a demonstration.

Sample code:

# create a table
CREATE table demo1(
    ID INT NOT NULL PRIMARY KEY,
    MONEY FIXED(12,2) NOT NULL
);

# insert data
INSERT INTO demo1 VALUES
(1,1254367892.50),
(2,8754367893.60),
(3,9854367895.65),
(4,9224367896.70);

# display data
SELECT * FROM demo;

Output:

+----+---------------+
| ID | MONEY         |
+----+---------------+
|  1 | 1254367892.50 |
|  2 | 8754367893.60 |
|  3 | 9854367895.65 |
|  4 | 9224367896.70 |
+----+---------------+
4 rows in set (0.00 sec)

As with INT, we can use the ZEROFILL and UNSIGNED attributes of the DECIMAL data type. With the UNSIGNED attribute, DECIMAL(P,D) UNSIGNEDthe table field of the type cannot take negative values.

For example, the table created with the following query does not accept negative values ​​for the MONEY field.

Sample code:

# create a table
CREATE table demo2(
    ID INT NOT NULL PRIMARY KEY,
    MONEY DECIMAL(12,2) UNSIGNED NOT NULL
);

With the ZEROFILL attribute, MySQL fills the result value with zeros until the width specified by the column definition.

请记住, if we use the ZEROFILL attribute, MySQL will automatically add UNSIGNED. We can check it using DESCRIBE tableName; query.

See the following code as an example.

# create a table
CREATE table demo3(
    ID INT NOT NULL PRIMARY KEY,
    MONEY DECIMAL(12,2) ZEROFILL NOT NULL
);

# describe a table
DESCRIBE demo3;

Output:

+-------+---------------------------------+------+-----+---------+-------+
| Field | Type                            | Null | Key | Default | Extra |
+-------+---------------------------------+------+-----+---------+-------+
| ID    | int                             | NO   | PRI | NULL    |       |
| MONEY | decimal(12,2) unsigned zerofill | NO   |     | NULL    |       |
+-------+---------------------------------+------+-----+---------+-------+
2 rows in set (0.26 sec)

Always remember that DECIMAL(P) is equivalent to DECIMAL(P,0), DECIMAL is equivalent to DECIMAL(P,0). By using DECIMAL(P,0), we will have no decimal part (fractional portion).

The implementations DECIMAL(P) and DECIMAL(P,0) let us decide the value of P. If we use the default implementation of DECIMAL, the default value of P is 10.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:192 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Zerofill usage, advantages and alternatives in MySQL

Publish Date:2025/04/22 Views:195 Category:MySQL

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input

Compare only MySQL timestamp dates to date parameters

Publish Date:2025/04/22 Views:64 Category:MySQL

In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi

Calculating Percentages in MySQL

Publish Date:2025/04/22 Views:66 Category:MySQL

We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re

Selecting multiple values using WHERE in MySQL

Publish Date:2025/04/22 Views:185 Category:MySQL

This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi

Changing the connection timeout in MySQL

Publish Date:2025/04/22 Views:59 Category:MySQL

We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti

MySQL fix Data Is Truncated for a Column error

Publish Date:2025/04/22 Views:101 Category:MySQL

This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca

MySQL Error Server PID File Could Not Be Found Solution

Publish Date:2025/04/22 Views:192 Category:MySQL

In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My

Get the last inserted ID using PHP MySQLi function

Publish Date:2025/04/22 Views:99 Category:MySQL

This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial