INSERT INTO
Adds rows to the table. If the target table already exists and is not sorted, the operation INSERT INTO adds rows at the end of the table. For a sorted table, YQL tries to preserve sorting by starting a sorted merge.
Search for the table by name in the database specified by the USE operator.
INSERT INTO lets you perform the following operations:
-
Adding constant values using
VALUES.INSERT INTO my_table (Column1, Column2, Column3, Column4) VALUES (345987,'ydb', 'Pied piper', 1414); COMMIT;INSERT INTO my_table (Column1, Column2) VALUES ("foo", 1), ("bar", 2); -
Saving the
SELECTresult.INSERT INTO my_table SELECT SourceTableColumn1 AS MyTableColumn1, "Empty" AS MyTableColumn2, SourceTableColumn2 AS MyTableColumn3 FROM source_table;
Using modifiers
Write operation can be performed with one or more modifiers. The modifier is specified after the WITH keyword after the table name: INSERT INTO ... WITH SOME_HINT. For example, to clear existing data from the table before the write operation, just add a modifier WITH TRUNCATE:
INSERT INTO my_table WITH TRUNCATE
SELECT key FROM my_table_source;
The following rules apply when working with modifiers:
- If the modifier has a value, it's specified after the
=sign:INSERT INTO ... WITH SOME_HINT=value. - If you need to specify multiple modifiers, they must be enclosed in parentheses:
INSERT INTO ... WITH (SOME_HINT1=value, SOME_HINT2, SOME_HINT3=value).
The full list of supported write modifiers:
TRUNCATEto clear existing data from the table.COMPRESSION_CODEC=codec_nameto write data with the specified compression codec. Takes priority over theyt.PublishedCompressionCodecpragma. For permitted values, see the Compression section.ERASURE_CODEC=erasure_nameto write data with the specified erasure codec. Takes priority over theyt.PublishedErasureCodecpragma. For permitted values, see the Erasure coding section.EXPIRATION=valueto the set expiration period for the table being created. The value can be set asdurationor astimestampin ISO 8601 format. Takes priority over theyt.ExpirationDeadline/yt.ExpirationIntervalpragmas. This modifier can only be set together withTRUNCATE.REPLICATION_FACTOR=factorto set the replication factor for the table being created. The value must be in the [1, 10] range.USER_ATTRS=ysonto set the user attribute that will be added to the table. Attributes must be set as a yson dictionary.MEDIA=ysonto set the media. Takes priority over theyt.PublishedMediapragma. For permitted values, see the Media section.PRIMARY_MEDIUM=medium_nameto set the primary medium. Takes priority over theyt.PublishedPrimaryMediumpragma.KEEP_METAto save the current schema, user attributes, and retention attributes (codecs, replications, media) of the target table. This modifier can only be set together withTRUNCATE.
Note
We do not recommend using system attributes in USER_ATTRS because the result of their application to the written data is not defined.
Examples
INSERT INTO my_table WITH (TRUNCATE, EXPIRATION="15m")
SELECT key FROM my_table_source;
INSERT INTO my_table WITH USER_ATTRS="{attr1=value1; attr2=value2;}"
SELECT key FROM my_table_source;