EnterpriseDB has a rich set of data types available to users.
Table 7-1 shows all built-in general-purpose data types.
Table 7-1. Data Types
Name | Aliases | Description |
---|---|---|
BIGINT | INT8, ROWID | signed eight-byte integer |
BOOLEAN | BIT | logical boolean (true/false) |
BYTEA | BINARY, BLOB, BYTE, IMAGE, LONG RAW, RAW (n), VARBINARY | binary data |
CHAR [ (n) ] | CHARACTER [ (n) ] | fixed-length character string of n characters |
DATE | date and time up to second accuracy | |
DOUBLE PRECISION | FLOAT, FLOAT (25) - FLOAT (53) | double precision floating-point number |
INTEGER | INT | signed four-byte integer |
NUMERIC | DEC, DECIMAL, NUMBER | exact numeric with optional decimal places |
NUMERIC (p [, s ]) | DEC (p [, s ]), DECIMAL (p [, s ]), MONEY, NUMBER (p [, s ]), SMALLMONEY, YEAR | exact numeric of maximum selectable precision, p, and optional scale, s |
REAL | FLOAT (1) - FLOAT (24), SMALLFLOAT | single precision floating-point number |
SMALLINT | TINYINT | signed two-byte integer |
TEXT | CLOB, LONG, LONG VARCHAR, LONGTEXT, LVARCHAR, MEDIUMTEXT | long character string |
TIMESTAMP [ (p) ] | DATETIME, SMALLDATETIME | date and time with optional, fractional second precision, p |
VARCHAR (n) | CHAR VARYING (n), CHARACTER VARYING (n), TINYTEXT, VARCHAR2 (n) | variable-length character string with a maximum length of n characters |
The following sections describe each data type in more detail.
Numeric types consist of two-byte, four-byte, and eight-byte integers, four-byte and eight-byte floating-point numbers, and fixed-precision decimals. Table 7-2 lists the available types.
Table 7-2. Numeric Types
Name | Storage Size | Description | Range |
---|---|---|---|
SMALLINT | 2 bytes | small-range integer | -32,768 to +32,767 |
INTEGER | 4 bytes | usual choice for integer | -2,147,483,648 to +2,147,483,647 |
BIGINT | 8 bytes | large-range integer | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
NUMERIC | variable | user-specified precision, exact | no limit |
REAL | 4 bytes | variable-precision, inexact | 6 decimal digits precision |
DOUBLE PRECISION | 8 bytes | variable-precision, inexact | 15 decimal digits precision |
The syntax of constants for the numeric types is described in Section 3.1.2. The numeric types have a full set of corresponding arithmetic operators and functions. Refer to Chapter 8 for more information. The following sections describe the types in detail.
The types SMALLINT, INTEGER, and BIGINT store whole numbers, that is, numbers without fractional components, of various ranges. Attempts to store values outside of the allowed range will result in an error.
The type INTEGER is the usual choice, as it offers the best balance between range, storage size, and performance. The SMALLINT type is generally only used if disk space is at a premium. The BIGINT type should only be used if the INTEGER range is not sufficient since the latter is faster.
The BIGINT type may not function correctly on all platforms, since it relies on compiler support for eight-byte integers. On a machine without such support, BIGINT acts the same as INTEGER, but still takes up eight bytes of storage.
SQL only specifies the integer types, INTEGER (or INT) and SMALLINT. The type BIGINT is an EnterpriseDB extension.
TINYINT is a synonym for SMALLINT. INT is a synonym for INTEGER. INT8 and ROWID are synonyms for BIGINT.
The type NUMERIC can store numbers with up to 1000 digits of precision and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required. However, the NUMERIC type is very slow compared to the floating-point types described in the next section.
In what follows we use these terms: The scale of a NUMERIC is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a NUMERIC 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 NUMERIC type can be configured. To declare a column of type NUMERIC use the syntax
NUMERIC(precision, scale)
The precision must be positive, the scale zero or positive. Alternatively,
NUMERIC(precision)
selects a scale of 0. Specifying
NUMERIC
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 NUMERIC columns with a declared scale will coerce input values to that scale. (The SQL standard requires a default scale of 0, i.e., 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 so as to satisfy the declared limits, an error is raised.
The types DEC, DECIMAL, NUMBER, and NUMERIC are equivalent. DECIMAL and NUMERIC are part of the SQL standard.
Note: The type MONEY may appear in a column declaration, however internally, it is translated to, and treated as type NUMERIC(19,4). The type SMALLMONEY may appear in a column declaration, however, it is internally translated to, and treated as type NUMERIC(10,4).
Note: The type YEAR may appear in a column declaration, however, it is internally translated to, and treated as type NUMERIC(4). Date arithmetic and date functions are not supported on a column with YEAR data type.
The data types REAL and DOUBLE PRECISION 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 NUMERIC type instead.
If you want to do complicated calculations with these types for anything important, especially if you rely on certain behavior in boundary cases (infinity, underflow), you should 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.
EnterpriseDB 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. EnterpriseDB accepts FLOAT(1) to FLOAT(24) as selecting the REAL type, while FLOAT(25) to FLOAT(53) as selecting DOUBLE PRECISION. Values of p outside the allowed range draw an error. FLOAT with no precision specified is taken to mean DOUBLE PRECISION.
The data type SMALLFLOAT is a synonym for REAL.