PolarDB for Oracle supports four categories of numeric types: integer types, arbitrary-precision numeric types, floating-point types, and serial types.
| Data type | Storage size | Description | Range |
|---|---|---|---|
| BINARY INTEGER | 4 bytes | Signed integer. Alias for INTEGER. | -2,147,483,648 to 2,147,483,647 |
| DOUBLE PRECISION | 8 bytes | Variable precision, inexact. | 15 decimal digits precision |
| INTEGER | 4 bytes | Standard integer type. | -2,147,483,648 to 2,147,483,647 |
| NUMBER | Variable | User-specified precision, exact. | Up to 1,000 digits precision |
| NUMBER(p [, s]) | Variable | Exact numeric with max precision p and optional scale s. | Up to 1,000 digits precision |
| PLS INTEGER | 4 bytes | Signed integer. Alias for INTEGER. | -2,147,483,648 to 2,147,483,647 |
| REAL | 4 bytes | Variable precision, inexact. | 6 decimal digits precision |
| ROWID | 8 bytes | Signed 8-byte integer. | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| SMALLINT | 2 bytes | Small-range integer. | -32,768 to 32,767 |
| BIGINT | 8 bytes | Large-range integer. | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| DECIMAL | Variable | User-specified precision, exact. | 131,072 digits before the decimal point; 16,383 digits after |
| NUMERIC | Variable | User-specified precision, exact. | 131,072 digits before the decimal point; 16,383 digits after |
| SMALLSERIAL | 2 bytes | Auto-incrementing 2-byte integer. | 1 to 32,767 |
| SERIAL | 4 bytes | Auto-incrementing 4-byte integer. | 1 to 2,147,483,647 |
| BIGSERIAL | 8 bytes | Auto-incrementing 8-byte integer. | 1 to 9,223,372,036,854,775,807 |
Integer types
INTEGER stores whole numbers from -2,147,483,648 to 2,147,483,647. Values outside this range cause an error.
Use SMALLINT only when disk space is at a premium. Use BIGINT only when values exceed the INTEGER range.
ROWID columns hold the physical address of a row as fixed-length binary data. Despite the table entry listing ROWID as a signed 8-byte integer, ROWID actually stores unsigned 4-byte integers in the range 0 to 4,294,967,295. Values outside this range cause an error.
Arbitrary-precision numeric types
NUMBER stores numbers with virtually unlimited precision and performs calculations exactly. Use NUMBER for monetary amounts and other values where precision loss is unacceptable. Note that NUMBER is significantly slower than the floating-point types described in the next section.
Precision and scale — The precision of a NUMBER is the total count of significant digits on both sides of the decimal point. The scale is the count of digits to the right of the decimal point. For example, 23.5141 has a precision of 6 and a scale of 4. Integers have a scale of zero.
Declare a NUMBER column using one of the following forms:
NUMBER(precision, scale)
NUMBER(precision) -- scale defaults to 0
NUMBER -- accepts any precision and scale up to the implementation limitThe precision must be positive; the scale must be zero or positive.
A NUMBER column with a declared scale coerces input values to that scale. A NUMBER column without precision or scale does not coerce input. For maximum portability, specify both precision and scale explicitly.
If an input value exceeds the declared precision or scale, PolarDB rounds it. If the value cannot be rounded to fit, an error is raised.
Example: A column declared as NUMBER(3, 1) rounds values to one decimal place and accepts values between -99.9 and 99.9 inclusive.
NUMERIC and DECIMAL are equivalent to NUMBER.
Floating-point types
REAL and DOUBLE PRECISION are inexact, variable-precision types. They implement IEEE Standard 754 for binary floating-point arithmetic (single and double precision, respectively), subject to the capabilities of the underlying processor, operating system, and compiler.
Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations, so storing and retrieving a value may show slight discrepancies. Keep the following in mind when using floating-point types:
For exact storage and calculations — such as for monetary amounts — use NUMBER instead.
For complex calculations where boundary behavior (infinity, underflow) matters, evaluate the implementation carefully before relying on it.
Comparing two floating-point values for equality may not work as expected.
On most platforms, REAL has a range of at least 1E-37 to 1E+37 with at least 6 decimal digits of precision. DOUBLE PRECISION typically has a range of around 1E-307 to 1E+308 with at least 15 digits of precision. Values too large or too small cause an error. Input with too many significant digits is rounded. Values too close to zero to be represented as distinct from zero cause an underflow error.
PolarDB also supports the SQL standard notations FLOAT and FLOAT(p), where p specifies the minimum acceptable precision in binary digits:
| FLOAT specification | Resolves to |
|---|---|
FLOAT(1) to FLOAT(24) | REAL |
FLOAT(25) to FLOAT(53) | DOUBLE PRECISION |
FLOAT (no precision) | DOUBLE PRECISION |
Values of p outside the supported range cause an error.
Serial types
SMALLSERIAL, SERIAL, and BIGSERIAL are not true numeric types. They are a convenience shorthand for creating unique identifier columns, similar to AUTO_INCREMENT in other databases.
The following declaration:
CREATE TABLE tablename (
colname SERIAL
);is equivalent to:
CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;This creates an integer column with a NOT NULL constraint and a default value drawn from a sequence generator. Add a UNIQUE or PRIMARY KEY constraint separately if you need to prevent duplicate values.
When inserting a row, assign the next sequence value to the SERIAL column by either omitting it from the column list or using the DEFAULT keyword.
Type equivalents:
| Short form | Equivalent | Column type created |
|---|---|---|
| SERIAL / SERIAL4 | SERIAL | INTEGER |
| BIGSERIAL / SERIAL8 | BIGSERIAL | BIGINT |
| SMALLSERIAL / SERIAL2 | SMALLSERIAL | SMALLINT |
Use BIGSERIAL if the table may generate more than 2^31 identifiers over its lifetime.
When a SERIAL column is dropped, its underlying sequence is also dropped automatically. Dropping only the sequence leaves the column intact but removes its default value expression.
Because SERIAL types use sequences internally, values in a serial column may not be contiguous even if no rows are deleted. Sequence values are consumed when assigned — if an insert transaction is rolled back, those values are not reused.