INSERT INTO
Warning
Currently, mixing column-oriented tables and row-oriented tables in a single transaction is supported only if the transaction performs read operations; no writes are allowed. Support for read-write transactions involving both table types is under development.
If a write transaction includes both types of tables, it fails with the following error: Write transactions between column and row tables are disabled at current time
.
Adds rows to the table. If you try to insert a row into a table with an existing primary key value, the operation fails with the PRECONDITION_FAILED
error code and the Operation aborted due to constraint violation: insert_pk
message returned.
INSERT INTO
lets you perform the following operations:
-
Adding constant values using
VALUES
.INSERT INTO my_table (Key1, Key2, Value1, Value2) VALUES (345987,'ydb', 'Pied piper', 1414); COMMIT;
INSERT INTO my_table (key, value) VALUES ("foo", 1), ("bar", 2);
-
Saving the
SELECT
result.INSERT INTO my_table SELECT Key AS Key1, "Empty" AS Key2, Value AS Value1 FROM my_table1;
INSERT INTO ... RETURNING
Inserts rows and returns their values in a single operation. It allows to retrieve data from the rows being inserted without needing to perform a separate SELECT query afterwards.
Examples
- Return all values of modified rows
INSERT INTO some_table (id, year, color, price)
VALUES (1103, 2023, 'blue', 400)
RETURNING *;
- Return specific columns
INSERT INTO some_table (id, color, price)
VALUES
(1101, 'red', 200),
(1102, 'green', 300)
RETURNING id, price;