INSERT INTO Statement
Tags: beginners, opengauss, discuss
Published: 2023-03-02 09:58:09

A new table contains no data. You need to insert data to the table before using it. You can execute the INSERT INTO statement to insert data into the table. This statement can be used to insert a row of data or multiple rows of data at the same time.

**Syntax**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaxvlyt89qyhs52pid6e.png)

**Parameter Description**

**·table_name**

Specifies the name of the target table where data will be inserted.

Value range: an existing table name

**·column_name**

Specifies the name of a column in the target table.

·The column name can be qualified with a subcolumn name or an array subscript, if needed.
·Each column not present in the column list will be filled with a default value, either its declared default value or NULL if there is none. Inserting data into only some columns of a composite type leaves the other columns NULL.
·The target column names (specified by column_name) can be listed in any order. If no list of column names is given at all, the default is all the columns of the table in their declared order.
·The target columns are the first N column names, if there are only N columns provided by the value clause.
·The values provided by the value clause are associated with the corresponding columns from left to right in the table.
Value range: an existing column

**·expression**

Specifies an expression or a value to assign to the corresponding column.

·If single-quotation marks are inserted in a column, the single-quotation marks need to be used for escape.
·If the expression for any column is not of the correct data type, automatic type conversion will be attempted. If the attempt fails, data insertion fails, and the system returns an error message.

**·DEFAULT**

Specifies the default value of a column. The value is NULL if no specified default value has been assigned to it.

**Examples**

·Insert a row of data into the customer_t1 table.

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 they can also be scalar expressions.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kvp04xfnqjkr7t3h6fp.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bd4v52gfal9xu802u6xb.png)

If you do not know some of the column values, you can omit them. If no value is specified for a column, the column is set to the default value. Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0aqtzxb5qd0t7algnllv.png)

You can also specify the default value of a column or row.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ve8lf2j7ljsj9in1gob.png)

·To insert multiple rows of data into a table, run the following command:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ew0x2b8y34f4c84issyg.png)

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