Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Show all

Numeric Types

Updated on 2024-05-07 GMT+08:00

Table 1 lists all available types. For arithmetic operators and related built-in functions, see Arithmetic Functions and Operators.

Table 1 Integer types

Name

Description

Storage Space

Range

TINYINT

Tiny integer, also called INT1

1 byte

0 ~ +255

SMALLINT

Small integer, also called INT2

2 bytes

–32,768 to +32,767

INTEGER

Typical choice for integers, also called INT4

4 bytes

–2,147,483,648 to +2,147,483,647

BINARY_INTEGER

Alias of INTEGER.

4 bytes

–2,147,483,648 to +2,147,483,647

BIGINT

Big integer, also called INT8

8 bytes

–9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

int16

A 16-byte integer cannot be used to create tables.

16 bytes

–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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
-- Create a table containing TINYINT data.
gaussdb=# CREATE TABLE int_type_t1
           (
            IT_COL1 TINYINT
           );

-- Insert data to the created table.
gaussdb=# INSERT INTO int_type_t1 VALUES(10);

-- View data.
gaussdb=# SELECT * FROM int_type_t1;
 it_col1  
--------- 
 10
(1 row)

-- Drop the table.
gaussdb=# DROP TABLE int_type_t1;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
-- Create a table containing TINYINT, INTEGER, and BIGINT data.
gaussdb=# CREATE TABLE int_type_t2 
(
    a TINYINT, 
    b TINYINT,
    c INTEGER,
    d BIGINT
);

-- Insert data.
gaussdb=# INSERT INTO int_type_t2 VALUES(100, 10, 1000, 10000);

-- View data.
gaussdb=# SELECT * FROM int_type_t2;
  a  | b  |  c   |   d   
-----+----+------+-------
 100 | 10 | 1000 | 10000
(1 row)

-- Drop the table.
gaussdb=# 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

Name

Description

Storage Space

Range

NUMERIC[(p[,s])],

DECIMAL[(p[,s])]

The value range of p is [1,1000], and the value range of s is [0,p].

NOTE:

p indicates the total digits, and s indicates the decimal digit.

The precision is specified by users. Every four decimal digits occupy two bytes, and an extra eight-byte overhead is added to the entire data.

Up to 131,072 digits before the decimal point, and up to 16,383 digits after the decimal point when no precision is specified.

NUMBER[(p[,s])]

Alias of the NUMERIC type.

The precision is specified by users. Every four decimal digits occupy two bytes, and an extra eight-byte overhead is added to the entire data.

Up to 131,072 digits before the decimal point, and up to 16,383 digits after the decimal point when no precision is specified.

Example:

-- Create a table.
gaussdb=# CREATE TABLE decimal_type_t1 
(
    DT_COL1 DECIMAL(10,4)
);

-- Insert data.
gaussdb=# INSERT INTO decimal_type_t1 VALUES(123456.122331);

-- Query data in the table.
gaussdb=# SELECT * FROM decimal_type_t1;
   dt_col1   
-------------
 123456.1223
(1 row)

-- Drop the table.
gaussdb=# DROP TABLE decimal_type_t1;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
-- Create a table.
gaussdb=# CREATE TABLE numeric_type_t1 
(
    NT_COL1 NUMERIC(10,4)
);

-- Insert data.
gaussdb=# INSERT INTO numeric_type_t1 VALUES(123456.12354);

-- Query data in the table.
gaussdb=# SELECT * FROM numeric_type_t1;
   nt_col1   
-------------
 123456.1235
(1 row)

-- Drop the table.
gaussdb=# 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 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

Name

Description

Storage Space

Range

SMALLSERIAL

Two-byte serial integer

2 bytes.

–32,768 to +32,767.

SERIAL

Four-byte serial integer

4 bytes.

–2,147,483,648 to +2,147,483,647.

BIGSERIAL

Eight-byte serial integer

8 bytes.

–9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

LARGESERIAL

By default, a 16-byte auto-incrementing integer is inserted. The actual value type is the same as that of NUMERIC.

Variable-length type. Every four decimal digits occupy two bytes, and an extra eight-byte overhead is added to the entire data.

There can be a maximum of 131072 digits before the decimal point and 16383 digits after the decimal point.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- Create a table.
gaussdb=# CREATE TABLE smallserial_type_tab(a SMALLSERIAL);

-- Insert data.
gaussdb=# INSERT INTO smallserial_type_tab VALUES(default);

-- Insert data again.
gaussdb=# INSERT INTO smallserial_type_tab VALUES(default);

-- View data.
gaussdb=# SELECT * FROM smallserial_type_tab;  
 a 
---
 1
 2
(2 rows)

-- Create a table.
gaussdb=# CREATE TABLE serial_type_tab(b SERIAL);

-- Insert data.
gaussdb=# INSERT INTO serial_type_tab VALUES(default);

-- Insert data again.
gaussdb=# INSERT INTO serial_type_tab VALUES(default);

-- View data.
gaussdb=# SELECT * FROM serial_type_tab; 
 b 
---
 1
 2
(2 rows)

-- Create a table.
gaussdb=# CREATE TABLE bigserial_type_tab(c BIGSERIAL);

-- Insert data.
gaussdb=# INSERT INTO bigserial_type_tab VALUES(default);

-- Insert data again.
gaussdb=# INSERT INTO bigserial_type_tab VALUES(default);

-- View data.
gaussdb=# SELECT * FROM bigserial_type_tab;
 c 
---
 1
 2
(2 rows)

-- Create a table.
gaussdb=# CREATE TABLE largeserial_type_tab(c LARGESERIAL);

-- Insert data.
gaussdb=# INSERT INTO largeserial_type_tab VALUES(default);

-- Insert data again.
gaussdb=# INSERT INTO largeserial_type_tab VALUES(default);

-- View data.
gaussdb=# SELECT * FROM largeserial_type_tab;
 c 
---
 1
 2
(2 rows)

-- Drop the table.
gaussdb=# DROP TABLE smallserial_type_tab;

gaussdb=# DROP TABLE serial_type_tab;

gaussdb=# 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. Finally, the sequencer belongs to the column. In this case, when the column or the table is deleted, the sequencer is also deleted. Currently, you can specify a SERIAL column when creating a table or add a SERIAL column to an ordinary table in PG-compatible mode. 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

Name

Description

Storage Space

Range

REAL,

FLOAT4

Single precision floating points, which is not very precise.

4 bytes.

–3.402E+38 to +3.402E+38, 6-digit decimal digits.

DOUBLE PRECISION,

FLOAT8

Double precision floating points, which is not very precise.

8 bytes.

–1.79E+308 to +1.79E+308, 15-bit decimal digits.

FLOAT[(p)]

Floating-point number, which is not very precise. The value range of precision (p) is [1,53].

4 bytes or 8 bytes.

REAL or DOUBLE PRECISION is selected as an internal identifier based on precision (p). If no precision is specified, DOUBLE PRECISION is used as the internal identifier.

BINARY_DOUBLE

Alias for DOUBLE PRECISION, compatible with Oracle.

8 bytes.

–1.79E+308 to +1.79E+308, 15-bit decimal digits.

DEC[(p[,s])]

The value range of p is [1,1000], and the value range of s is [0,p].

The precision is specified by users. Every four decimal digits occupy two bytes, and an extra eight-byte overhead is added to the entire data.

Maximum 131,072 digits before the decimal point and 16,383 digits after the decimal point when the precision and scale are specified to the maximum.

INTEGER[(p[,s])]

The value range of p (precision) is [1,1000], and the value range of s (scale) is [0,p].

If the precision and scale are not specified, the precision p is 10 and the scale s is 0 by default.

If the precision and scale are not specified, this type is mapped to INTEGER. If the precision and scale are specified, this type is mapped to NUMERIC.

The precision is specified by users. Every four decimal digits occupy two bytes, and an extra eight-byte overhead is added to the entire data.

Maximum 131,072 digits before the decimal point and 16,383 digits after the decimal point when the precision and scale are specified to the maximum.

If the precision and scale are not specified, the value ranges from –2,147,483,648 to +2,147,483,647.

NOTE:

In Table 4, p is the precision, indicating the minimum acceptable total number of integral places, and s indicates the decimal digit.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- Create a table.
gaussdb=# 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.
gaussdb=# INSERT INTO float_type_t2 VALUES(10,10.365456,123456.1234,10.3214, 321.321, 123.123654, 123.123654);

-- View data.
gaussdb=# 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)

-- Drop the table.
gaussdb=# DROP TABLE float_type_t2;

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback