Numeric Types
Table 1 lists all available types. For digit operators and related built-in functions, see Mathematical Functions and Operators.
Table 1 Integer types
–170,141,183,460,469,231,731,687,303,715,884,105,728 to +170,141,183,460,469,231,731,687,303,715,884,105,727 |
Example:
-- Create a table containing TINYINT data.
openGauss=# CREATE TABLE int_type_t1
(
IT_COL1 TINYINT
);
-- Insert data to the created table.
openGauss=# INSERT INTO int_type_t1 VALUES(10);
-- View data.
openGauss=# SELECT * FROM int_type_t1;
it_col1
---------
10
(1 row)
-- Delete the table.
openGauss=# DROP TABLE int_type_t1;
-- Create a table containing TINYINT, INTEGER, and BIGINT data.
openGauss=# CREATE TABLE int_type_t2
(
a TINYINT,
b TINYINT,
c INTEGER,
d BIGINT
);
-- Insert data.
openGauss=# INSERT INTO int_type_t2 VALUES(100, 10, 1000, 10000);
-- View data.
openGauss=# SELECT * FROM int_type_t2;
a | b | c | d
-----+----+------+-------
100 | 10 | 1000 | 10000
(1 row)
-- Delete the table.
openGauss=# DROP TABLE int_type_t2;
NOTE:
- Only numbers of the TINYINT, SMALLINT, INTEGER, BIGINT, or INT16 type, that is, integers can be stored. Saving a number with a decimal in any of the data types will result in errors.
- The INTEGER type is the common choice, as it offers the best balance between range, storage size, and performance. Generally, use the SMALLINT type only if you are sure that the value range is within the SMALLINT value range. The storage speed of INTEGER is much faster. BIGINT is used only when the range of INTEGER is not large enough.
Table 2 Arbitrary precision types
Example:
-- Create a table.
openGauss=# CREATE TABLE decimal_type_t1
(
DT_COL1 DECIMAL(10,4)
);
-- Insert data.
openGauss=# INSERT INTO decimal_type_t1 VALUES(123456.122331);
-- Query data in the table.
openGauss=# SELECT * FROM decimal_type_t1;
dt_col1
-------------
123456.1223
(1 row)
-- Delete the table.
openGauss=# DROP TABLE decimal_type_t1;
-- Create a table.
openGauss=# CREATE TABLE numeric_type_t1
(
NT_COL1 NUMERIC(10,4)
);
-- Insert data.
openGauss=# INSERT INTO numeric_type_t1 VALUES(123456.12354);
-- Query data in the table.
openGauss=# SELECT * FROM numeric_type_t1;
nt_col1
-------------
123456.1235
(1 row)
-- Delete the table.
openGauss=# DROP TABLE numeric_type_t1;
NOTE:
- Compared to the integer types, the arbitrary precision numbers require larger storage space and have lower storage efficiency, operation efficiency, and poorer compression ratio results. The INTEGER type is the common choice when number types are defined. Arbitrary precision numbers are used only when numbers exceed the maximum range indicated by the integers.
- When NUMERIC/DECIMAL is used for defining a column, you are advised to specify the precision (p) and scale (s) for the column.
Table 3 Sequence integer
Example:
-- Create a table.
openGauss=# CREATE TABLE smallserial_type_tab(a SMALLSERIAL);
-- Insert data.
openGauss=# INSERT INTO smallserial_type_tab VALUES(default);
-- Insert data again.
openGauss=# INSERT INTO smallserial_type_tab VALUES(default);
-- View data.
openGauss=# SELECT * FROM smallserial_type_tab;
a
---
1
2
(2 rows)
-- Create a table.
openGauss=# CREATE TABLE serial_type_tab(b SERIAL);
-- Insert data.
openGauss=# INSERT INTO serial_type_tab VALUES(default);
-- Insert data again.
openGauss=# INSERT INTO serial_type_tab VALUES(default);
-- View data.
openGauss=# SELECT * FROM serial_type_tab;
b
---
1
2
(2 rows)
-- Create a table.
openGauss=# CREATE TABLE bigserial_type_tab(c BIGSERIAL);
-- Insert data.
openGauss=# INSERT INTO bigserial_type_tab VALUES(default);
-- Insert data.
openGauss=# INSERT INTO bigserial_type_tab VALUES(default);
-- View data.
openGauss=# SELECT * FROM bigserial_type_tab;
c
---
1
2
(2 rows)
-- Create a table.
openGauss=# CREATE TABLE largeserial_type_tab(c LARGESERIAL);
-- Insert data.
openGauss=# INSERT INTO largeserial_type_tab VALUES(default);
-- Insert data.
openGauss=# INSERT INTO largeserial_type_tab VALUES(default);
-- View data.
openGauss=# SELECT * FROM largeserial_type_tab;
c
---
1
2
(2 rows)
-- Delete the table.
openGauss=# DROP TABLE smallserial_type_tab;
openGauss=# DROP TABLE serial_type_tab;
openGauss=# DROP TABLE bigserial_type_tab;
NOTE:
SMALLSERIAL, SERIAL, BIGSERIAL, and LARGESERIAL are not real types. They are concepts used for setting a unique identifier for a table. Therefore, an integer column is created and its default value plans to be read from a sequencer. A NOT NULL constraint is used to ensure NULL is not inserted. In most cases you would also want to attach a UNIQUE or PRIMARY KEY constraint to prevent duplicate values from being inserted unexpectedly, but this is not automatic. The sequencer is set so that it belongs to the column. In this case, when the column or the table is deleted, the sequencer is also deleted. Currently, the SERIAL column can be specified only when you create a table. You cannot add the SERIAL column in an existing table. In addition, SERIAL columns cannot be created in temporary tables. Because SERIAL is not a data type, columns cannot be converted to this type.
Table 4 Floating point types
Example:
-- Create a table.
openGauss=# CREATE TABLE float_type_t2
(
FT_COL1 INTEGER,
FT_COL2 FLOAT4,
FT_COL3 FLOAT8,
FT_COL4 FLOAT(3),
FT_COL5 BINARY_DOUBLE,
FT_COL6 DECIMAL(10,4),
FT_COL7 INTEGER(6,3)
);
-- Insert data.
openGauss=# INSERT INTO float_type_t2 VALUES(10,10.365456,123456.1234,10.3214, 321.321, 123.123654, 123.123654);
-- View data.
openGauss=# SELECT * FROM float_type_t2 ;
ft_col1 | ft_col2 | ft_col3 | ft_col4 | ft_col5 | ft_col6 | ft_col7
---------+---------+-------------+---------+---------+----------+---------
10 | 10.3655 | 123456.1234 | 10.3214 | 321.321 | 123.1237 | 123.124
(1 row)
-- Delete the table.
openGauss=# DROP TABLE float_type_t2;