sql / intermediate
Snippet
Fixed-Precision Numeric Storage for Financial Calculations
Using exact numeric data types like DECIMAL instead of floating-point representations guarantees exact precision and avoids binary rounding errors when handling monetary balances.
snippet.sql
sql
1
2
3
4
CREATE TABLE account_ledger (account_id INTEGER PRIMARY KEY,balance DECIMAL(15, 2) NOT NULL DEFAULT 0.00);
Breakdown
1
CREATE TABLE account_ledger (
Initiates creation of a new table named account_ledger.
2
account_id INTEGER PRIMARY KEY,
Defines an integer column as the primary key identifying each account uniquely.
3
balance DECIMAL(15, 2) NOT NULL DEFAULT 0.00
Defines a fixed-point numeric column with 15 total digits and 2 decimal places, defaulting to zero.
4
);
Ends table creation statement.