sql / intermediate
Snippet
Precision Financial Data Storage with DECIMAL Data Type
The DECIMAL data type stores exact numeric values with defined precision and scale. Unlike floating-point types, DECIMAL prevents binary rounding errors, making it mandatory for monetary amounts and strict calculations.
snippet.sql
sql
1
2
3
4
5
CREATE TABLE product_prices (product_id INTEGER PRIMARY KEY,unit_price DECIMAL(10, 2) NOT NULL,discount_rate DECIMAL(3, 2) DEFAULT 0.00);
Breakdown
1
CREATE TABLE product_prices (
Defines a new relation schema named product_prices.
2
product_id INTEGER PRIMARY KEY,
Sets product_id as an integer field serving as the primary identifier.
3
unit_price DECIMAL(10, 2) NOT NULL,
Defines unit_price as an exact decimal with up to 10 total digits and 2 fractional decimal places.
4
discount_rate DECIMAL(3, 2) DEFAULT 0.00
Defines discount_rate with 3 total digits and 2 decimal places, defaulting to 0.00.
5
);
Closes the table definition block.