Records can also be inserted by using the VALUES keyword. This keyword is preceded by the name of the fields in question, and followed by comma-separated values matching the number of fields specified.
-- Insert a single record INSERTINTO company (name, founded) VALUES ('SurrealDB', '2021-09-10');
-- Insert multiple records INSERTINTO company (name, founded) VALUES ('Acme Inc.', '1967-05-03'), ('Apple Inc.', '1976-04-01');
It is possible to update records which already exist or violate a unique index by specifying an ON DUPLICATE KEY UPDATE clause. This clause also allows incrementing and decrementing numeric values, and adding or removing values from arrays. To increment a numeric value, or to add an item to an array, use the += operator. To decrement a numeric value, or to remove an value from an array, use the -= operator.
Field names inside ON DUPLICATE KEY UPDATE refer to the fields of the existing record. To access the fields of the new record that was attempted to be inserted, prefix the field name with $input:
Using the insert statement, it is possible to copy records easily between tables. The records being copied will have the same id in the new table, but the record id will signify the new table name.
INSERTINTOrecordings_san_francisco (SELECT * FROM temperature WHERE city = 'San Francisco');
Furthermore, it is possible to perform a bulk insert in a single query. The @what part of the syntax can be either a single object or an array of objects.
While attempting to insert one or more records via the INSERT statement, if the record ID is already present in the table, the query will encounter an error and fail. If the IGNORE clause is supplied, records with an already existing or duplicate ID will be silently ignored.
{id: "jaime", name: "Jaime", surname: "Morgan Hitchcock"}, -- will not throw an error ];
Return Values
By default, the INSERT statement returns the record once it has been inserted. To change what is returned, we can use the RETURN clause, specifying either NONE, BEFORE, AFTER, DIFF, or a comma-separated list of specific fields to return.
RETURN NONE can be useful to avoid excess output:
-- Insert a record and return nothing INSERTINTOcompany{ name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] }RETURNNONE;
RETURN DIFF returns the changeset diff:
-- Insert a record and return the diff INSERTINTOcompany{ name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] }RETURNDIFF;
RETURN BEFORE inside a INSERT statement is essentially a synonym for RETURN NONE, while RETURN AFTER is the default behaviour for INSERT.
-- Before insert will always return NONE as it is the same as the record being inserted INSERTINTOcompany{ name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] }RETURNBEFORE;
-- Return the record after creation INSERTINTOcompany{ name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] }RETURNAFTER;
You can also return specific fields from a created record, the value of a single field using VALUE, as well as ad-hoc fields to modify the output as needed.
The INSERT statement supports bulk insert, which allows multiple records to be inserted in a single query. The @what part of the syntax can be either a single object or an array of objects.
The INSERT statement can also be used to add records into relation tables. The @what part of the syntax can be either a single object or an array of objects.
Learn more about creating relationships between tables in the RELATE statement. For example:
-- Insert records into the person table INSERTINTOperson[ {id: 1}, {id: 2}, {id: 3}, ]; -- Insert a single relation INSERTRELATIONINTOlikes{ in: person:1, id: 'object', out: person:2, };