Coginiti menu Coginiti menu

Precision and Scale

Precision and scale in SQL ensure data is stored and retrieved accurately. Precision defines the total number of digits that can be stored, while scale establishes the number of digits that can be held to the right of the decimal point. Both precision and scale are essential for defining numeric data types in SQL and ensuring data is stored and retrieved accurately.
For example, you can use precision and scale when defining numeric data types in the CREATE TABLE or ALTER TABLE statements. Here’s an example of how to use precision with the DECIMAL data type:

CREATE TABLE mytable ( id INT, amount DECIMAL(10,2) );

In the example above, we’ve defined a table called mytable with two columns: id of type INT and amount of type DECIMAL with a precision of 10 and a scale of 2. This means that amount can store up to 10 digits, with two digits to the right of the decimal point.
When inserting data into a table with a DECIMAL column, ensure the value has the correct number of digits and decimal places. For example, to insert a value of 1234.56 into the amount column, you would use the following SQL statement:

INSERT INTO mytable (id, amount) VALUES (1, 1234.56);

You’ll receive an error if you insert a value with more digits than the precision and the scale permits. Similarly, you may lose some precision due to rounding if you try to retrieve a value with more digits than the precision specifies.

The primary reason to lower the precision and/or scale is to reduce the storage size of a column. For modern columnar-storage database platforms, this small optimization may not outweigh the potential disruption to data flows when a value exceeds the precision or scale. Also consider the potential effects of precision and scale as this data is consumed by downstream applications.