INSERT Statement
<sql> ::= <insert_statement>
<insert_statement> ::= "INSERT INTO" <table_name> "(" <column_list> ")" <insert_values>
<insert_values> ::= "VALUES" "(" <value_list> ")" | <query>
<query> ::= "SELECT" <select_list> "FROM" <from_item> ["JOIN" <join_clause>] ["WHERE" <condition>] ["ORDER BY" <order_clause>] ["TYPE" <data_type>]
<select_statement> ::= "SELECT" <column_list> "FROM" <table_name> ["WHERE" <condition>]
<column_list> ::= <column_name> ["," <column_name>]*
<value_list> ::= <value> ["," <value>]*
<set_clause> ::= <column_name> "=" <value> ["," <column_name> "=" <value>]*
<condition> ::= <expression> ["AND" <expression>]*
<expression> ::= <column_name> <operator> <value>
<operator> ::= "=" | "<>" | "<" | ">" | "<=" | ">=" | "IS NOT NULL"
<table_name> ::= [a-zA-Z_][a-zA-Z0-9_.]*
<column_name> ::= [a-zA-Z_][a-zA-Z0-9_]*
<value> ::= <literal> | <parameter>
<literal> ::= '"[^"]*"' | "'[^']*'" | [0-9]+
<parameter> ::= "&" [a-zA-Z_][a-zA-Z0-9_]*
<from_item> ::= <table_name>
<join_clause> ::= <table_name> ["ON" <condition>]
<order_clause> ::= <column_name> ["ASC" | "DESC"]
<data_type> ::= "OBJECT" | "LIST"
Explanation
-
SQL Statement Types (
<sql>
): The BNF defines two primary types of SQL statements -INSERT
andSELECT
. -
INSERT Statement (
<insert_statement>
): AnINSERT
statement starts withINSERT INTO
, followed by a table name, a list of columns, and the values to be inserted. Values can either be directly specified (VALUES
) or derived from aSELECT
query (<query>
). -
Query (
<query>
): Represents aSELECT
statement used as part of anINSERT
operation. The query may include joins, conditions, ordering, and a data type specification. -
Components:
- Column List (
<column_list>
): Defines one or more columns to be used in the statement. - Value List (
<value_list>
): Defines one or more values to be used in theVALUES
clause. - Set Clause (
<set_clause>
): Used inUPDATE
statements to specify column-value assignments. - Condition (
<condition>
): Specifies filtering conditions forWHERE
clauses. - Expression (
<expression>
): Represents a conditional expression that compares a column with a value using an operator. - Operators (
<operator>
): Defines the comparison operators, such as=
,<>
,<
,>
,<=
,>=
, andIS NOT NULL
. - Join Clause (
<join_clause>
): Represents a table join, optionally specifying a condition withON
. - Order Clause (
<order_clause>
): Specifies the columns to order the results by, with optional sorting direction (ASC
orDESC
). - Data Type (
<data_type>
): Specifies the type of the result, such asOBJECT
orLIST
.
- Column List (
-
Table and Column Naming:
- Table Name (
<table_name>
): Represents the name of the table, following typical SQL naming conventions. - Column Name (
<column_name>
): Represents the name of a column, following typical SQL naming conventions.
- Table Name (
-
Values:
- Literal (
<literal>
): Represents a value such as a string or number. - Parameter (
<parameter>
): Represents a parameter that starts with&
.
- Literal (
Example 1
INSERT INTO dictionary.users(ref, id, name, name1, name2, name3)
SELECT uuid(x.id), x.id, x.name AS name, x.name AS name1, x.name AS name2, x.name AS name3
FROM ¶ms AS x(id int, name text, name1 text, name2 text, name3 text)
ON CONFLICT(ref) DO UPDATE SET name = x.name, id = EXCLUDED.id;
Example 2
INSERT INTO Dictionary.syncevents("from", "to", "in", "Out")
SELECT &returnValues.sender, &returnValues.to, &returnValues.amount1In, &returnValues.amount0Out
WHERE &returnValues.amount1In IS NOT NULL
TYPE OBJECT;
Example 3
INSERT INTO Operation.operation_order(price, value, storage, goods)
SELECT 10 AS price, 22 AS val,
(SELECT st.ref
FROM Dictionary.storages AS st
WHERE st.name = "main"
LIMIT 1) AS storage,
(SELECT gd.ref
FROM Dictionary.goods AS gd
WHERE gd.name = "Teapot 1"
LIMIT 1) AS goods
TYPE LIST;