Skip to content

Tables and fields

Record IDs and addressing

SurrealDB record IDs are composed of a table name and a record identifier separated by a : in between, allowing for a simple and consistent way to reference records across the database. Record IDs are used to uniquely identify records within a table, to query, update, and delete records, and serve as links from one record to another.

Record IDs can be constructed in a number of ways, including alphanumeric text, complex Unicode text and symbols, numbers, arrays, objects, built-in ID generation functions, and a function to generate an ID from values.

The examples on this page use the record IDs of a small shop, with customers, products and orders. All of the following are valid record IDs in SurrealQL.

customer:ana;
//- customer:ana
customer:6h3ksdnq0x7b2p4mzf1e;
//- customer:6h3ksdnq0x7b2p4mzf1e
tag:`🎁`;
//- tag:`🎁`
daily_sales:['London', d'2026-11-16'];
//- daily_sales:['London', d'2026-11-16T00:00:00Z']

As all record IDs are unique, trying to create a new record with an existing record ID will return an error. To create a record or modify it if the ID already exists, use an UPSERT statement or an INSERT statement with an ON DUPLICATE KEY UPDATE clause.

When you create a record without specifying the full ID, a random identifier is assigned after the table name.

CREATE customer SET name = "Ana Silva";
Example output
[{ id: customer:ezs644u19mae2p68404j, name: 'Ana Silva' }]

Record IDs can be generated with a number of built-in ID generation functions, which are cryptographically secure and suitable for dispersion across a distributed datastore. These include a 20 digit alphanumeric ID (the default), sequentially incrementing and temporally sortable ULID Record identifiers, and UUID version 7 Record identifiers. Orders are created often and read back in the order they were placed, so a time-sortable ID suits them:

-- The default: 20 characters from `abcdefghijklmnopqrstuvwxyz0123456789`
CREATE order:rand() SET placed_at = time::now(), total = 42.5;
-- The same as the statement above
CREATE order SET placed_at = time::now(), total = 42.5;

-- A ULID, which sorts by the time it was generated
CREATE order:ulid() SET placed_at = time::now(), total = 42.5;
-- A UUID version 7, which also sorts by time
CREATE order:uuid() SET placed_at = time::now(), total = 42.5;

Text record IDs can contain letters, numbers and _ characters. A meaningful key such as a username is easier to read in a link than a random one.

CREATE customer:ana SET name = 'Ana Silva';
//- [{ id: customer:ana, name: 'Ana Silva' }]
CREATE product:travel_mug_350 SET name = 'Travel mug';
//- [{ id: product:travel_mug_350, name: 'Travel mug' }]

To create a record ID with complex characters, use ` (backticks) around the table name and/or record identifier. Here an order number contains hyphens, and a product category in French contains accented letters in both its table name and its key.

CREATE order:`2026-11-0042` SET customer = customer:ana;
//- [{ customer: customer:ana, id: order:`2026-11-0042` }]

CREATE `catégorie`:`thé-vert` SET name = 'Thé vert';
//- [{ id: `catégorie`:`thé-vert`, name: 'Thé vert' }]

If you create a record ID with a number as a string, it will be stored with ` backticks to differentiate it from a number. This matters when order numbers are imported from another system, which may give them as numbers or as strings.

CREATE order SET id = 1001;
CREATE order SET id = "1001";
CREATE order SET id = "order1001";
SELECT VALUE id FROM order;

As the record ID order:1001 is different from order:`1001` , no errors are returned when creating and both records turn up in the output of the SELECT statement. Meanwhile, the order with the identifier order1001 does not use backticks as there is no order1001 number to differentiate it from.

Output
[order:1001, order:`1001`, order:order1001]

If a numeric value is specified without any decimal point suffix and is within the range -9223372036854775808 to 9223372036854775807 then the value will be parsed, stored, and treated as a 64-bit signed integer.

Any numeric numbers outside of the range of a signed 64-bit integer will be stored as a string. A supplier's 20-digit product code is one of these:

/**[test]

[[test.results]]
value = "[{ id: order:1002, total: 18.9f }]"

[[test.results]]
value = "[{ id: supplier_code:`40012345678901234567`, product: product:mug }]"

*/
CREATE order:1002 SET total = 18.9;
//- [{ id: order:1002, total: 18.9f }]
CREATE supplier_code:40012345678901234567 SET product = product:mug;
//- [{ id: supplier_code:`40012345678901234567`, product: product:mug }]

Record IDs can be constructed out of arrays and even objects. This sort of record ID is most used when you have a field or two that will be used to look up records inside a record range, which is extremely performant. This is in contrast to using a WHERE clause to filter, which involves a table scan.

Records in SurrealDB can store arrays of values, including other nested arrays or objects within them. Different types of values can be stored within the same array, unless defined otherwise. The shop keeps one record of sales for each store and day, with the store and the date as its ID:

/**[test]

[[test.results]]
value = "[{ id: daily_sales:['London', d'2026-11-16T00:00:00Z'], orders: 42, revenue: 1830.5dec }]"

*/
CREATE daily_sales:['London', d'2026-11-16'] SET
    orders = 42,
    revenue = 1830.5dec;
Output
[{ id: daily_sales:['London', d'2026-11-16T00:00:00Z'], orders: 42, revenue: 1830.5dec }]

The main reason why record ranges are so performant is simply because the database knows ahead of time in which area to look for records in a query, and therefore has a smaller "surface area" to work in.

This can be demonstrated by seeing what happens when a single record range query encompasses all of the records in a database. The example below creates 10,000 order records that have an array-based record ID that begins with 'london', the store that took the order, allowing them to be used in a record range query, as well as a field called store that is also 'london', which will be used in a WHERE clause to compare performance.

Interestingly, in this case a record range query is only somewhat more performant. This is because both queries end up iterating over 10,000 records, with the only difference being that the query with a WHERE clause also checks to see if the value of the store field is equal to 'london'.

/**[test]

[[test.results]]
error = false

[[test.results]]
value = "10000"

[[test.results]]
value = "10000"

*/
FOR $_ IN 0..10000 {
    CREATE order:['london', rand::id()] SET store = 'london';
};

count(SELECT * FROM order:['london', NONE]..['london', ..]);
//- 10000
count(SELECT * FROM order WHERE store = 'london');
//- 10000

If the shop has ten stores, however, the difference in performance will be much larger. In this case the record range query is still only iterating a relatively small surface area of 10,000 records, while the second one has ten times this number to go through in addition to the WHERE clause on top.

/**[test]

[[test.results]]
error = false

[[test.results]]
value = "10000"

[[test.results]]
value = "10000"

*/
FOR $_ IN 0..10000 {
    CREATE order:['london', rand::id()] SET store = 'london';
    CREATE order:['paris', rand::id()]  SET store = 'paris';
    CREATE order:['berlin', rand::id()] SET store = 'berlin';
    CREATE order:['madrid', rand::id()] SET store = 'madrid';
    CREATE order:['rome', rand::id()]   SET store = 'rome';
    CREATE order:['lisbon', rand::id()] SET store = 'lisbon';
    CREATE order:['vienna', rand::id()] SET store = 'vienna';
    CREATE order:['prague', rand::id()] SET store = 'prague';
    CREATE order:['oslo', rand::id()]   SET store = 'oslo';
    CREATE order:['dublin', rand::id()] SET store = 'dublin';
};

count(SELECT * FROM order:['london', NONE]..['london', ..]);
//- 10000
count(SELECT * FROM order WHERE store = 'london');
//- 10000

The type name of a record ID is record, which by default allows any sort of record. This type can be set inside a DEFINE FIELD statement. A customer's saved list below can hold any record, such as products and gift cards, while favourites only holds products.

/**[test]

[[test.results]]
error = false

[[test.results]]
error = false

[[test.results]]
value = "[{ favourites: [product:mug], id: customer:ana, saved: [product:mug, gift_card:g50] }]"

[[test.results]]
error = true

*/
DEFINE FIELD saved ON TABLE customer TYPE option<array<record>>;
DEFINE FIELD favourites ON TABLE customer TYPE option<array<record<product>>>;

CREATE customer:ana SET
    saved = [product:mug, gift_card:g50],
    favourites = [product:mug];
//- [{ favourites: [product:mug], id: customer:ana, saved: [product:mug, gift_card:g50] }]

CREATE customer:ben SET favourites = [gift_card:g50];
Error output
"Couldn't coerce value for field `favourites` of `customer:ben`: Expected `none | array<record<product>>` but found `[gift_card:g50]`"

A record ID is unique within its table, so product:4521 and gift_card:4521 can both exist. To make a key unique across several tables, and to find a record by its key alone, keep a registry table whose records have the same key and link to the record. An event on each table creates the registry entry, and because two records cannot have the same ID, a second table using the key fails and its write is rolled back:

/**[test]

[[test.results]]
error = false

[[test.results]]
error = false

[[test.results]]
error = false

[[test.results]]
error = "Database record `code:4521` already exists"

*/
DEFINE EVENT register ON product WHEN $event = "CREATE" THEN {
	CREATE type::record("code", record::id($after.id)) SET record = $after.id;
};
DEFINE EVENT register ON gift_card WHEN $event = "CREATE" THEN {
	CREATE type::record("code", record::id($after.id)) SET record = $after.id;
};

CREATE product:4521 SET name = "Travel mug";
CREATE gift_card:4521 SET amount = 50;
Error output
Database record `code:4521` already exists

The registry then finds a record without knowing its table:

/**[test]

[[test.results]]
error = false

[[test.results]]
error = false

[[test.results]]
value = "{ id: product:4521, name: 'Travel mug' }"

*/
-- Setup from the example above
DEFINE EVENT register ON product WHEN $event = "CREATE" THEN {
	CREATE type::record("code", record::id($after.id)) SET record = $after.id;
};

CREATE product:4521 SET name = "Travel mug";

code:4521.record.*;
Output
{ id: product:4521, name: 'Travel mug' }

With random keys, such as the default IDs or uuid(), two tables are very unlikely to choose the same key, and the registry is then mainly a way to find a record by its key.

Was this page helpful?

Last updated