---
title: "Data manipulation"
description: "Read and write records with select(), create(), insert(), insertRelation(), update(), upsert(), merge(), patch() and delete() in version 1 of the PHP SDK."
canonical_url: "https://surrealdb.com/docs/reference/php/v1/concepts/data-manipulation"
sdk: "PHP SDK v1"
surrealdb_version: "4.x"
last_modified: "2026-11-24T05:29:47Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# Data manipulation

Read and write records with select(), create(), insert(), insertRelation(), update(), upsert(), merge(), patch() and delete() in version 1 of the PHP SDK.

The methods below create, read, update and delete records. Each one takes a table name or a record ID as its first argument. To run [SurrealQL statements](/docs/reference/query-language/statements/overview.md) directly, see [executing queries](/docs/reference/php/v1/concepts/executing-queries.md).

| Method | Description |
| --- | --- |
| [`$db->select($thing)`](#select) | Selects all records in a table, or a specific record |
| [`$db->create($thing, $data)`](#create) | Creates a record in the database |
| [`$db->insert($thing, $data)`](#insert) | Inserts one or multiple records in the database |
| [`$db->insertRelation($thing, $data)`](#insertrelation) | Inserts one or multiple relations in the database |
| [`$db->update($thing, $data)`](#update) | Replaces the data of all records in a table, or a specific record |
| [`$db->upsert($thing, $data)`](#upsert) | Creates a record if it does not exist, or updates it if it does |
| [`$db->merge($thing, $data)`](#merge) | Merges data into all records in a table, or a specific record |
| [`$db->patch($thing, $data, $diff)`](#patch) | Applies JSON Patch changes to all records in a table, or a specific record |
| [`$db->delete($thing)`](#delete) | Deletes all records in a table, or a specific record |

## `->select()` {#select}

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

```php title="Method Syntax"
$db->select($thing)
```

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>, <code>RecordId</code> or <code>StringRecordId</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or a [`RecordId`](/docs/reference/php/v1/concepts/value-types.md#recordid) to select.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
// Select all records from a table
$people = $db->select('person');

// Select a specific record from a table
$person = $db->select(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc'));
$person = $db->select(new StringRecordId('person:h5wxrf2ewk8xjxosxtyc'));
```

After a record is created, pass its ID to fetch it. A `StringRecordId` or a `RecordId` made with `create()` selects the same record.

```php
$person = $db->select($person->id);

// using the StringRecordId
$id = StringRecordId::create("person:tobie");
$person = $db->select($id);

// using the RecordId
$id = RecordId::create("person", "tobie");
$person = $db->select($id);
```

### Translated query
This function will run the following query in the database.

```surql
SELECT * FROM $thing;
```

## `->create()` {#create}

Creates a record in the database.

```php title="Method Syntax"
$db->create($thing, $data)
```

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>, <code>RecordId</code> or <code>StringRecordId</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or a `RecordId` to create.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>data</code>
               <label label="optional" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>mixed</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The document / record data to create.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
// Create a record with a random ID
[$person] = $db->create('person');

// Create a record with a specific ID
$person = $db->create(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

// The content you are creating the record with might differ from the return type
[$record] = $db->create(
    new RecordId('person', 'tobie'),
    ["name" => "Tobie"]
);
```

The second argument is an associative array with the field names and values.

```php
$person = $db->create("person:tobie", [
	"name" => "Tobie",
	"lastname" => "Morgan Hitchcock",
	"age" => 30,
	"hobbies" => ["reading", "coding"]
]);
```

### Translated query
This function will run the following query in the database.

```surql notest
CREATE $thing CONTENT $data;
```

## `->insert()` {#insert}

Inserts one or multiple records in the database.

```php title="Method Syntax"
$db->insert($thing, $data)
```

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or `RecordId` to insert to.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>data</code>
               <label label="optional" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>associative array</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                Either a single document/record or an array of documents/records to insert
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
// Insert a single record
[$person] = $db->insert('person', [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

$person = $db->insert(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

// Insert multiple records
$people = $db->insert('person', [
	[
		"name" => 'Tobie',
		"settings" => [
			"active" => true,
			"marketing" => true,
		],
	],
	[
		"name" => 'Jaime',
		"settings" => [
			"active" => true,
			"marketing" => true,
		],
	],
]);

// The content you are creating the record with might differ from the return type
$people = $db->insert('person', [
	[ "name" => 'Tobie' ],
	[ "name" => 'Jaime' ],
]);
```

### Translated query
This function will run the following query in the database.

```surql notest
INSERT INTO $thing $data;
```

## `->insertRelation()` {#insertrelation}

Inserts one or multiple relations in the database.

```php title="Method Syntax"
$db->insertRelation($thing, $data)
```

### Arguments
<table>
	<thead>
		<tr>
			<th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
			<th colspan="2" scope="col">Description</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td colspan="2" scope="row" data-label="Arguments">
				<code>thing</code>
				<label label="required" />
			</td>
			<td colspan="2" scope="row" data-label="Description">
				`string` or `Table`
			</td>
			<td colspan="2" scope="row" data-label="Description">
				Target table to insert the relation to.
			</td>
		</tr>
		<tr>
			<td colspan="2" scope="row" data-label="Arguments">
				<code>data</code>
			   <label label="optional" />
			</td>
			<td colspan="2" scope="row" data-label="Type">
				`array`
			</td>
			<td colspan="2" scope="row" data-label="Description">
				An array of relations to insert.
			</td>
		</tr>
	</tbody>
</table>

### Example usage
```php
// Insert a single relation
$relation = $db->insertRelation('person', [
	"id" => new RecordId('person', 'tobie'),
	"in" => new RecordId('company', 'surreal'),
	"out" => new RecordId('role', 'founder'),
]);

// Insert multiple relations
$relations = $db->insertRelation('person', [
	[
		"id" => new RecordId('person', 'tobie'),
		"in" => new RecordId('company', 'surreal'),
		"out" => new RecordId('role', 'founder'),
	],
	[
		"id" => new RecordId('person', 'jaime'),
		"in" => new RecordId('company', 'surreal'),
		"out" => new RecordId('role', 'cofounder'),
	],
]);
```

## `->update()` {#update}

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

```php title="Method Syntax"
$db->update($thing, $data)
```

> [!NOTE]
> This function replaces the current document / record data with the specified data.

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>, <code>RecordId</code> or <code>StringRecordId</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or the specific `RecordId` to update.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>data</code>
               <label label="optional" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>mixed</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The document / record data to update.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
// Update all records in a table
$people = $db->update('person');

// Update a record with a specific ID
$person = $db->update(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

// The content you are updating the record with might differ from the return type
$record = $db->update(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
]);
```

This call replaces the entire record with the new values, so it lists every field. See [the `UPDATE` statement reference](/docs/reference/query-language/statements/update.md) for more.

```php
$person = $db->update($person->id, [
	"name" => "Tobie",
	"lastname" => "Morgan Hitchcock",
	"age" => 31
]);
```

### Translated query
This function will run the following query in the database.

```surql notest
UPDATE $thing CONTENT $data;
```

## `->upsert()` {#upsert}

Creates or updates a record in a table.

```php title="Method Syntax"
$db->upsert($thing, $data);
```

### Arguments
<table>
	<thead>
		<tr>
			<th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
			<th colspan="2" scope="col">Description</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td colspan="2" scope="row" data-label="Arguments">
				<code>$thing</code>
				<label label="required" />
			</td>
			<td colspan="2" scope="row" data-label="Type">
				`RecordId`. `StringRecordId` or `string`
			</td>
			<td colspan="2" scope="row" data-label="Description">
				The collection to upsert the record to.
			</td>
		</tr>
		<tr>
			<td colspan="2" scope="row" data-label="Arguments">
				<code>$data</code>
				<label label="required" />
			</td>
			<td colspan="2" scope="row" data-label="Type">
				`mixed`
			</td>
			<td colspan="2" scope="row" data-label="Description">
				The record to upsert.
			</td>
		</tr>
	</tbody>
</table>

### Example usage
```php title="Example"
$id = new RecordId('users', 'tobie');

// Upsert a record to the "users" collection.
$response = $db->upsert($id, [
	'firstname' => 'Tobie',
	'lastname' => 'Hitchcock',
]);
```

## `->merge()` {#merge}

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

```php title="Method Syntax"
$db->merge($thing, $data)
```

> [!NOTE]
> This function merges the current document / record data with the specified data.

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>, <code>RecordId</code> or <code>StringRecordId</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or the specific `RecordId` to merge.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>data</code>
               <label label="optional" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>mixed</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The document / record data to merge.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
// Update all records in a table
$people = $db->merge('person', [
	"updated_at" => new Date(),
]);

// Update a record with a specific ID
$person = $db->merge(new RecordId('person', 'tobie'), [
	"updated_at" => new Date(),
	"settings" => [
		"active" => true,
	],
]);

// The content you are merging the record with might differ from the return type
$record = $db->merge(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
]);
```

The new values are merged with the existing record, and a field that already exists is replaced with the new value. See [the RPC protocol reference](/docs/reference/protocols/rpc-protocol.md#merge) for more.

```php
$person = $db->merge($person->id, [
	"age" => 31
]);
```

### Translated query
This function will run the following query in the database.

```surql notest
UPDATE $thing MERGE $data;
```

## `->patch()` {#patch}

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

```php title="Method Syntax"
$db->patch($thing, $data, $diff)
```

> [!NOTE]
> This function patches the current document / record data with the specified JSON Patch data.

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>, <code>RecordId</code> or <code>StringRecordId</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or the specific `RecordId` to patch.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>data</code>
               <label label="optional" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>associative array</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The JSON Patch data with which to patch the records.
            </td>
        </tr>
		<tr>
			<td colspan="2" scope="row" data-label="Arguments">
				<code>diff</code>
			   <label label="optional" />
			</td>
			<td colspan="2" scope="row" data-label="Type">
				<code>boolean</code>
			</td>
			<td colspan="2" scope="row" data-label="Description">
				Whether to return the diff of the patched record.
			</td>
		</tr>
    </tbody>
</table>

### Example usage
```php
// Update all records in a table
$people = $db->patch('person', [
		[ "op" => 'replace', "path" => '/created_at',
	    "value" => new Date() ],
]);

// Update a record with a specific ID
$person = $db->patch(new 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.

```surql notest
UPDATE $thing PATCH $data;
```

## `->delete()` {#delete}

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

```php title="Method Syntax"
$db->delete($thing)
```

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>thing</code>
                <label label="required" />
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>, <code>RecordId</code> or <code>StringRecordId</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or a `RecordId` to delete.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
// Delete all records from a table
$db->delete('person');

// Delete a specific record from a table
$db->delete(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc'));
```

The first argument can also be a `RecordId` or a `StringRecordId`.

```php
$db->delete($person->id);

$id = StringRecordId::create("person:tobie");
$db->delete($id);

$id = RecordId::create("person", "tobie");
$db->delete($id);
```

### Translated query
This function will run the following query in the database.

```surql notest
DELETE $thing;
```
