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

Inserting Data to Tables

Updated on 2024-08-20 GMT+08:00

A new table contains no data. You need to insert data to the table before using it. This section describes how to insert a row or multiple rows of data by running the INSERT command and to insert data from a specified table. Contact the administrator if a large amount of data needs to be imported.

Context

In case of different character sets used on the client and server, the length of a character on both ends may differ. A string entered on the client will be processed on the server based on the server's character set, and therefore the result may differ from the expected result.

Table 1 Comparison of outputs varying depending on character sets on the client and server

Procedure

Same Encoding on the Server and Client

Different Encoding on the Server and Client

No operations are performed on the string while it is saved and read.

Your expected result is returned.

Your expected result is returned (only if the encoding on the client remains unchanged).

Operations (such as executing string functions) are performed on the string while it is saved and read.

Your expected result is returned.

The result may differ from the expected result, depending on the operations performed on the string.

An ultra long string is truncated while it is saved.

Your expected result is returned.

If the character sets used on the client and server have different character length, the result may differ from the expected result.

More than one of the preceding operations may be performed on a string. For example, if the character sets of the client and server are different and the string is truncated after being processed, the result will also be unexpected. For details, see Table 2.

NOTE:

Ultra long strings are truncated only if DBCOMPATIBILITY is set to TD and the GUC parameter td_compatible_truncation is set to on.

Create table1 and table2 to be used in the examples.

1
2
gaussdb=# CREATE TABLE table1(id int, a char(6), b varchar(6),c varchar(6));
gaussdb=# CREATE TABLE table2(id int, a char(20), b varchar(20),c varchar(20));
Table 2 Examples

No.

Character Set on the Server

Character Set on the Client

Automatic Truncation Enabled

Example

Result

Description

1

SQL_ASCII

UTF8

Yes

1
gaussdb=# INSERT INTO table1 VALUES(1,reverse('123AA78'),reverse('123AA78'),reverse('123AA78'));
1
2
3
id |a|b|c
----+------+------+------
1 | 87| 87| 87

A string is reversed on the server and then truncated. Because character sets used by the server and client are different, character A is displayed in multiple bytes on the server and the result is unexpected.

2

SQL_ASCII

UTF8

Yes

1
gaussdb=# INSERT INTO table1 VALUES(2,reverse('123A78'),reverse('123A78'),reverse('123A78'));
1
2
3
id |a|b|c
----+------+------+------
2 | 873| 873| 873

A string is reversed and then automatically truncated. Therefore, the result is unexpected.

3

SQL_ASCII

UTF8

Yes

1
gaussdb=# INSERT INTO table1 VALUES(3,'87A123','87A123','87A123');
1
2
3
id |   a   |   b   |   c
----+-------+-------+-------
  3 | 87A1 | 87A1 | 87A1

The column length in the string type is an integer multiple of the length in client character encoding. Therefore, the result is as expected after truncation.

4

SQL_ASCII

UTF8

No

1
2
gaussdb=# INSERT INTO table2 VALUES(1,reverse('123AA78'),reverse('123AA78'),reverse('123AA78'));
gaussdb=# INSERT INTO table2 VALUES(2,reverse('123A78'),reverse('123A78'),reverse('123A78'));
1
2
3
4
id |a|b|c
----+-------------------+--------+--------
1 | 87 321| 87 321 | 87 321
2 | 87321| 87321| 87321

Similar to the first example, multi-byte characters are different from the original characters after being reversed.

Procedure

You need to create a table before inserting data to it. For details about how to create a table, see Creating and Managing Tables.

  • Insert a row to table customer_t1.
    Data values are arranged in the same order as the columns in the table and are separated by commas (,). Generally, column values are text values (constants), but scalar expressions are also allowed.
    1
    gaussdb=# INSERT INTO customer_t1(c_customer_sk, c_customer_id, c_first_name) VALUES (3769, 'hello', 'Grace');
    
    If you know the sequence of the columns in the table, you can obtain the same result without listing these columns. For example, the following command generates the same result as the preceding command:
    1
    gaussdb=# INSERT INTO customer_t1 VALUES (3769, 'hello', 'Grace');
    
    If you do not know some of the column values, you can omit them. In the INSERT statement, if the column name of the specified target table is not displayed, the values to be inserted in the VALUES clause correspond to the columns of the target table based on the column number. That is, the first value in the VALUES clause corresponds to the first column of the target table, the second value of the VALUES clause corresponds to the second column of the target table, and so on. Columns that do not have corresponding values in the VALUES clause are automatically filled with default values or NULL. For example:
    1
    2
    3
    gaussdb=# INSERT INTO customer_t1 (c_customer_sk, c_first_name) VALUES (3769, 'Grace');
    
    gaussdb=# INSERT INTO customer_t1 VALUES (3769, 'hello');
    
    You can also specify the default value of a column or row.
    1
    2
    3
    gaussdb=# INSERT INTO customer_t1 (c_customer_sk, c_customer_id, c_first_name) VALUES (3769, 'hello', DEFAULT);
    
    gaussdb=# INSERT INTO customer_t1 DEFAULT VALUES;
    
  • To insert multiple rows to a table, run the following command:
    1
    2
    3
    4
    gaussdb=# INSERT INTO customer_t1 (c_customer_sk, c_customer_id, c_first_name) VALUES 
        (6885, 'maps', 'Joes'),
        (4321, 'tpcds', 'Lily'),
        (9527, 'world', 'James');
    

    You can also insert multiple rows by running the command for inserting one row for multiple times. However, you are advised to run this command to improve efficiency.

  • Assume that you have created a backup table customer_t2 for table customer_t1. To insert data from customer_t1 to customer_t2, run the following commands:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    gaussdb=# CREATE TABLE customer_t2
    (
        c_customer_sk             integer,
        c_customer_id             char(5),
        c_first_name              char(6),
        c_last_name               char(8)
    );
    
    gaussdb=# INSERT INTO customer_t2 SELECT * FROM customer_t1;
    
    NOTE:

    If implicit conversion is not implemented between the column data types of the specified table and those of the current table, the two tables must have the same column data types when data is inserted from the specified table to the current table.

  • Insert data into a table by using a table alias without the keyword AS.
    1
    2
    3
    gaussdb=# CREATE TABLE T1(A INT,B TEXT,C TIMESTAMP,D FLOAT);
    gaussdb=# INSERT INTO T1 T VALUES(1,'HA','1997-01-01 10:00:00'::TIMESTAMP,1.234);
    gaussdb=# INSERT INTO T1 TT(B,C,D) VALUES('HA','1997-01-01 10:00:00'::TIMESTAMP,1.234);
    
    NOTE:
    • When the INSERT statement is used without AS, the table alias cannot be a keyword (such as SELECT or VALUE) or an expression. The alias must comply with the identifier naming rule.
    • When the INSERT statement is used without AS, the table alias does not support this format: INSERT INTO table_name alias_name(alias_name.col1,...,alias_name.coln) VALUES(xxx).
    • To insert data into a partitioned table by using a table alias without the keyword AS, the target partition cannot be specified.
  • Delete a backup table.
    1
    gaussdb=# DROP TABLE customer_t2 CASCADE;
    
    NOTE:

    If the table to be deleted is dependent on other tables, you need to delete the corresponding tables first.

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