Numeric types consist of four-byte integers, four-byte and eight-byte floating-point numbers, and fixed-precision decimals.
The following table lists the available types.
Data type | Storage size | Description | Range |
---|---|---|---|
BINARY INTEGER | 4 bytes | Signed integer. It is an alias for INTEGER. | -2,147,483,648 to 2,147,483,647 |
DOUBLE PRECISION | 8 bytes | A variable precision. This data type is inexact. | 15 decimal digits precision |
INTEGER | 4 bytes | Typical choice for integers. | -2,147,483,648 to 2,147,483,647 |
NUMBER | Variable | A user-specified precision. This data type is exact. | Up to 1,000 digits precision |
NUMBER(p [, s ] ) | Variable | The exact number for the maximum precision p and the optional scale s. | Up to 1,000 digits precision |
PLS INTEGER | 4 bytes | Signed integer. It is an alias for INTEGER. | -2,147,483,648 to 2,147,483,647 |
REAL | 4 bytes | A variable precision. This data type is inexact. | 6 decimal digits precision |
ROWID | 8 bytes | The signed 8-byte integer. | -9223372036854775808 to 9223372036854775807 |
SMALLINT | 2 bytes | An integer within a small range. | -32768 to 32767 |
BIGINT | 8 bytes | An integer within a large range. | -9223372036854775808 to 9223372036854775807 |
DECIMAL | Variable length. | A user-specified precision. This data type is exact. | 131072 digits before the decimal point to 16383 digits after the decimal point. |
NUMERIC | Variable length. | A user-specified precision. This data type is exact. | 131072 digits before the decimal point to 16383 digits after the decimal point. |
SMALLSERIAL | 2 bytes | 2-byte integer. | 1 to 32767 |
SERIAL | 4 bytes | The auto-increment integer. | 1 to 2147483647 |
BIGSERIAL | 8 bytes | The large auto-increment integer. | 1 to 9223372036854775807 |
Integer types
The INTEGER type stores whole numbers without fractional components from -2,147,483,648 to 2,147,483,647. Attempts to store values outside the allowed range will result in an error.
SMALLINT is generally used only when the disk space is low. BIGINT is used only when the value range of INTEGER is exceeded.
Columns of the ROWID type holds fixed-length binary data that describes the physical address of a record. ROWID is an unsigned, four-byte INTEGER that stores whole numbers without fractional components between the values of 0 and 4,294,967,295. Attempts to store values outside the allowed range will result in an error.
Arbitrary precision numeric types
The NUMBER type can store practically an unlimited number of digits of precision and perform calculations exactly. It is recommended for storing monetary amounts and other quantities where exactness is required. However, the NUMBER type is very slow compared to the floating-point types described in the next section.
The scale of a NUMBER is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a NUMBER is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.
Both the precision and the scale of the NUMBER type can be configured. You can use the following syntax to declare a column of the NUMBER type:
NUMBER(precision, scale)
The precision must be positive, the scale zero or positive. The following syntax can also be used:
NUMBER(precision)
This syntax selects a scale of 0. Specifying NUMBER without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. A column of this kind will not coerce input values to any particular scale, whereas NUMBER columns with a declared scale will coerce input values to that scale. The SQL standard requires a default scale of 0, for example, coercion to integer precision. For maximum portability, it is best to specify the precision and scale explicitly.
If the precision or scale of a value is greater than the declared precision or scale of a column, the system will attempt to round the value. If the value cannot be rounded to satisfy the declared limits, an error is raised.
NUMERIC and DECIMAL are equivalent to NUMBER.
Floating-point types
The REAL and DOUBLE PRECISION data types are inexact, variable-precision numeric types. In practice, these types are usually implementations of IEEE Standard 754 for Binary Floating-Point Arithmetic (single and double precision, respectively), to the extent that the underlying processor, operating system, and compiler support it.
Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations,
so that storing and printing back out a value may show slight discrepancies. Managing these errors and how they propagate through calculations is the subject of an entire branch of mathematics and computer science and will not be discussed further here, except for the following points:
If you require exact storage and calculations such as for monetary amounts, use the NUMBER type instead.
If you want to do complicated calculations by using these types for anything important, especially if you rely on certain behavior in boundary cases such as infinity and underflow, you must evaluate the implementation carefully.
Comparing two floating-point values for equality may or may not work as expected. On most platforms, the REAL type has a range of at least 1E-37 to 1E+37 with a precision of at least 6 decimal digits. The DOUBLE PRECISION type typically has a range of around 1E-307 to 1E+308 with a precision of at least 15 digits. Values that are too large or too small will cause an error. Rounding may take place if the precision of an input number is too high. Numbers too close to zero that are not representable as distinct from zero will cause an underflow error.
POLARDB also supports the SQL standard notations FLOAT and FLOAT(p) for specifying inexact numeric types. Here, p specifies the minimum acceptable precision in binary digits. POLARDB accepts FLOAT(1) to FLOAT(24) as selecting the REAL type, while FLOAT(25) to FLOAT(53) as selecting DOUBLE PRECISION. Values of p that exceed the allowed range draw an error. FLOAT with no precision specified is taken as DOUBLE PRECISION type.
Serial types
CREATE TABLE tablename (
colname SERIAL
);
The preceding statement is equivalent to the following statements: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;
Therefore, an integer field is introduced and its default value to be read from a sequence generator. The NOT NULL constraint is applied to ensure that NULL is not inserted. Although you can attach a UNIQUE or PRIMARY KEY constraint to avoid accidentally inserting duplicate values in most cases, this is not automatic. If you set the sequencer generator subordinate to the integer field, the sequencer generator is deleted when the field or table is deleted. When you insert the next value in the sequence into the SERIALl field, you must assign a default value to the SERIAL field. This can be implemented by excluding the field from the field list in the INSERT statement, or by using the DEFAULT keyword.
SERIALl and SERIALl4 are equivalent and both can be used to create INTEGER fields. BIGSERIAL and SERIALl8 are equivalent, but BIGSERIAL creates a BIGINT field. BIGSERIAL must be selected if more than 231 identifiers may be used over the lifetime of the table. SMALLSERIAL and SERIAL2 are equivalent, but SMALLSERIAL creates SMALLINT fields.
A sequence of the SERIAL type is automatically deleted when its field is deleted. Although you can delete just the sequence and leave the field, the default value expression for the field is still deleted in this way.