Updated on 2024-04-29 GMT+08:00

INSERT INTO

This section describes how to insert data.

Basic Syntax

  • The following code shows the syntax of an INSERT INTO statement:
    INSERT INTO [db.]table [(c1, c2, c3)] VALUES (v11, v12, v13), (v21, v22, v23), ...

    For a field that is defined in the table structure but not specified in an INSERT INTO statement, the system fills in the field based on the following rules:

    • If an expression is defined for the default values of the field, the system calculates the default values by using this expression and then inserts the default values into the field.
    • If no expression is defined for the default values of the field, the system inserts 0 or empty strings into the field.

    Insert data by referring to Creating a Table by Copying the Structure of an Existing Table.

    insert into demo_t values(1,'Candy','23','M'),(2,'cici','33','F');
  • Insert data by using the SELECT result.
    INSERT INTO [db.]table [(c1, c2, c3)] SELECT ...

    Data in the fields that are queried in the SELECT clause are inserted into the fields that are specified in the INSERT INTO statement in strict order. The names of the two sets of fields do not have to be the same. The system converts the data types of the fields as needed.

    Except for the VALUES type, all other data types do not support expressions such as now() and 1 + 2. The VALUES type allows you to use such expressions. However, these expressions are not recommended because the execution of these expressions is inefficient.