sql / intermediate
Snippet
Preventing Rounding Errors with Exact Fixed-Point Numeric Types
Floating-point types (FLOAT, DOUBLE) introduce binary representation errors in arithmetic. Using DECIMAL or NUMERIC guarantees exact precision storage essential for financial calculations.
snippet.sql
sql
1
2
3
4
5
6
CREATE TABLE financial_ledger (entry_id INT NOT NULL,amount DECIMAL(15, 4) NOT NULL,posted_at TIMESTAMP NOT NULL,PRIMARY KEY (entry_id));
Breakdown
1
CREATE TABLE financial_ledger (
Creates a new structured database table definition for ledger entries.
2
entry_id INT NOT NULL,
Defines an integer primary key mandatory column.
3
amount DECIMAL(15, 4) NOT NULL,
Enforces exact fixed-point storage: up to 15 digits total, with exactly 4 decimal places.
4
posted_at TIMESTAMP NOT NULL,
Stores transaction timestamp values precisely.
5
PRIMARY KEY (entry_id)
Enforces uniqueness and creates a primary clustering index on entry ID.
6
);
Closes the table schema definition statement.