### Get Postgres Tips and Tricks

Subscribe to get advanced Postgres how-tos.

2 The SQL Language : 2.2 Data Types : 2.2.1 Numeric Types

2.2.1 Numeric Types

Signed integer, Alias for INTEGER Signed integer, Alias for INTEGER 2.2.1.1 Integer TypesThe BINARY_INTEGER, INTEGER, PLS_INTEGER, and ROWID types store whole numbers (without fractional components) as specified in Table 2-2 Numeric Types. Attempts to store values outside of the allowed range will result in an error.2.2.1.2 Arbitrary Precision NumbersThe type, NUMBER, can store practically an unlimited number of digits of precision and perform calculations exactly. It is especially 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.In what follows we use these terms: 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. To declare a column of type NUMBER use the syntaxselects 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, i.e., coercion to integer precision. For maximum portability, it is best to specify the precision and scale explicitly.)2.2.1.3 Floating-Point TypesThe 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.If you require exact storage and calculations (such as for monetary amounts), use the NUMBER type instead.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.Advanced Server 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. Advanced Server 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.2.2.2 Character TypesThe following table lists the general-purpose character types available in Advanced Server.

Where n is a positive integer; these types can store strings up to n characters in length. An attempt to assign a value that exceeds the length of n will result in an error, unless the excess characters are all spaces, in which case the string will be truncated to the maximum length.The storage requirement for data of these types is the actual string plus 1 byte if the string is less than 127 bytes, or 4 bytes if the string is 127 bytes or greater. In the case of CHAR, the padding also requires storage. Long strings are compressed by the system automatically, so the physical requirement on disk may be less. Long values are stored in background tables so they do not interfere with rapid access to the shorter column values.If you do not specify a value for n, n will default to 1. If the string to be assigned is shorter than n, values of type CHAR will be space-padded to the specified width (n), and will be stored and displayed that way.Padding spaces are treated as semantically insignificant. That is, trailing spaces are disregarded when comparing two values of type CHAR, and they will be removed when converting a CHAR value to one of the other string types.If you explicitly cast an over-length value to a CHAR(n) type, the value will be truncated to n characters without raising an error (as specified by the SQL standard).If the string to be assigned is shorter than n, values of type VARCHAR, VARCHAR2, NVARCHAR and NVARCHAR2 will store the shorter string without padding.If you explicitly cast a value to a VARCHAR type, an over-length value will be truncated to n characters without raising an error (as specified by the SQL standard).You can store a large character string in a CLOB type. CLOB is semantically equivalent to VARCHAR2 except no length limit is specified. Generally, you should use a CLOB type if the maximum string length is not known.The longest possible character string that can be stored in a CLOB type is about 1 GB.Note: The CLOB data type is actually a DOMAIN based on the PostgreSQL TEXT data type. For information on a DOMAIN, see the PostgreSQL core documentation at:Thus, usage of the CLOB type is limited by what can be done for TEXT such as a maximum size of approximately 1 GB.For usage of larger amounts of data, instead of using the CLOB data type, use the PostgreSQL Large Objects feature that relies on the pg_largeobject system catalog. For information on large objects, see the PostgreSQL core documentation at:

2 The SQL Language : 2.2 Data Types : 2.2.1 Numeric Types