SurrealDB Docs Logo

Enter a search query

Data manipulation

SurrealDB supports a number of methods for interacting with the database and performing CRUD operations.

MethodDescription
db.select()Selects all records in a table, or a specific record, from the database
db.create()Creates a record in the database
db.insert()Inserts one or multiple records in the database
db.insert_relation()Inserts one or multiple relations in the database
db.update()Updates all records in a table, or a specific record, in the database
db.merge()Modifies all records in a table, or a specific record, in the database
db.patch()Applies JSON Patch changes to all records, or a specific record, in the database
db.delete()Deletes all records in a table, or a specific record, from the database

.select()

Selects all records in a table, or a specific record, from the database.

Method Syntax
db.select(thing)

Arguments

ArgumentsDescription
thing required

The table name or a RecordID to select.

Example usage

# Select all records from a table people = db.select('person') # Select a specific record from a table person = db.select(RecordID('person', 'h5wxrf2ewk8xjxosxtyc'))

Translated query

This function will run the following query in the database.

SELECT * FROM $thing;

.create()

Creates a record in the database.

Method Syntax
db.create(thing, data)

Arguments

ArgumentsDescription
thing required

The table name or a RecordID to create.

data optional

The document / record data to create.

Example usage

# Create a record with a random ID person = db.create('person') # Create a record with a specific ID person = db.create(RecordID('person', 'tobie'), { "name": 'Tobie', "settings": { "active": True, "marketing": True, } })

Translated query

This function will run the following query in the database.

CREATE $thing CONTENT $data;

.insert()

Inserts one or multiple records in the database.

Method Syntax
db.insert(table, data)

Arguments

ArgumentsDescription
thing required

The table name to insert to.

data required

Either a single document/record or an array of documents/records to insert

Example usage

# Insert a single record db.insert('person', { "name": 'Tobie', "settings": { "active": True, "marketing": True, }, }) # Insert multiple records db.insert('person', [ { "name": 'Tobie', "settings": { "active": True, "marketing": True, }, }, { "name": 'Jaime', "settings": { "active": True, "marketing": True, }, }, ])

Translated query

This function will run the following query in the database.

INSERT INTO $table $data;

.insert_relation()

Inserts one or multiple relations in the database.

Method Syntax
db.insert_relation(table, data)

Arguments

ArgumentsDescription
table required

The table name to insert to.

data required

Either a single document/record or an array of documents/records to insert

Example usage

# Insert a single record db.insert_relation('likes', { "in": RecordID('person', 'tobie'), "out": RecordID('post', 123) }) # Insert multiple records across tables people = db.insert_relation('likes', [ { "in": RecordID('person', 'tobie'), "out": RecordID('post', 123), }, { "in": RecordID('person', 'jaime'), "out": RecordID('post', 456), } ])

Translated query

This function will run the following query in the database.

INSERT RELATION INTO $table $data;

.update()

Updates all records in a table, or a specific record, in the database.

Method Syntax
db.update(thing, data)
Note

This function replaces the current document / record data with the specified data.

Arguments

ArgumentsDescription
thing required

The table name or the specific RecordID to update.

data optional

The document / record data to update.

Example usage

# Update all records in a table db.update('person', {name: "Jaime"}) # Update a record with a specific ID db.update(RecordID('person', 'tobie'), { "name": 'Tobie', "settings": { "active": True, "marketing": True, } })

Translated query

This function will run the following query in the database.

UPDATE $thing CONTENT $data;

.merge()

Modifies all records in a table, or a specific record, in the database.

Method Syntax
db.merge(thing, data)
Note

This function merges the current document / record data with the specified data. If no merge data is passed it will simply trigger an update.

Arguments

ArgumentsDescription
thing required

The table name or the specific RecordID to merge.

data optional

The document / record data to merge.

Example usage

# Update all records in a table db.merge('person', { "updated_at": datetime.datetime.now().isoformat() }) # Update a record with a specific ID db.merge(RecordID('person', 'tobie'), { "updated_at": datetime.datetime.now().isoformat(), "settings": { "active": True, } })

Translated query

This function will run the following query in the database.

UPDATE $thing MERGE $data;

.patch()

Applies JSON Patch changes to all records, or a specific record, in the database.

Method Syntax
db.patch(thing, data)
Note

This function patches the current document / record data with the specified JSON Patch data.

Arguments

ArgumentsDescription
thing required

The table name or the specific RecordID to patch.

data required

The JSON Patch data with which to patch the records.

Example usage

# Update all records in a table db.patch('person', [ { "op": 'replace', "path": '/created_at', "value": datetime.datetime.now().isoformat() }, ]) # Update a record with a specific ID db.patch(RecordID('person', 'tobie'), [ { "op": 'replace', "path": '/settings/active', "value": False }, { "op": 'add', "path": '/tags', "value": ['developer', 'engineer'] }, { "op": 'remove', "path": '/temp' }, ])

Translated query

This function will run the following query in the database.

UPDATE $thing PATCH $data;

.delete()

Deletes all records in a table, or a specific record, from the database.

Method Syntax
db.delete(thing,data)

Arguments

ArgumentsDescription
thing required

The table name or a RecordID to delete.

Example usage

# Delete all records from a table db.delete('person') # Delete a specific record from a table db.delete(RecordID('person', 'h5wxrf2ewk8xjxosxtyc'))

Translated query

This function will run the following query in the database.

DELETE $thing;