# Table of Contents - [SurrealDB](#surrealdb) - [Java | SDKs | Integration](#java-sdks-integration) - [Java | SDKs | Installation](#java-sdks-installation) - [Fastembed | Embeddings](#fastembed-embeddings) - [SurrealKV | Introduction](#surrealkv-introduction) - [Java | SDK | Concepts](#java-sdk-concepts) - [SurrealDB Labs](#surrealdb-labs) - [Java | SDK | Create a new connection](#java-sdk-create-a-new-connection) - [Working with SurrealDB over HTTP via Postman | Tutorials](#working-with-surrealdb-over-http-via-postman-tutorials) - [SurrealDB Labs](#surrealdb-labs) - [Integrate Auth0 as an Authentication Provider | Tutorials](#integrate-auth0-as-an-authentication-provider-tutorials) - [Integrate AWS Cognito as an Authentication Provider | Tutorials](#integrate-aws-cognito-as-an-authentication-provider-tutorials) - [Golang | SDK | Quick start](#golang-sdk-quick-start) - [Surrealist | Settings and Customisation](#surrealist-settings-and-customisation) - [Install | Data migration](#install-data-migration) - [Neo4J | Data migration](#neo4j-data-migration) - [Configure an Instance](#configure-an-instance) - [Get Started with Surreal Cloud](#get-started-with-surreal-cloud) - [Connect to Instance | Surreal Cloud](#connect-to-instance-surreal-cloud) - [JSON lines | Data migration](#json-lines-data-migration) - [MongoDB | Data migration](#mongodb-data-migration) - [Surrealist | Getting Started](#surrealist-getting-started) - [n8n | Data Management](#n8n-data-management) - [Surrealist | Exploring database records](#surrealist-exploring-database-records) - [Configure network access](#configure-network-access) - [Surrealist | Sending queries to the database](#surrealist-sending-queries-to-the-database) - [Dependency Injection | .NET | SDK | Concepts](#dependency-injection-net-sdk-concepts) - [.NET | SDK | Quick start](#-net-sdk-quick-start) - [.NET | SDK | Concepts](#-net-sdk-concepts) - [Data Manipulation | .NET | SDK | Concepts](#data-manipulation-net-sdk-concepts) - [PHP | SDKs | Integration | SurrealDB Docs](#php-sdks-integration-surrealdb-docs) - [Run SurrealQL queries | .NET | SDK | Concepts](#run-surrealql-queries-net-sdk-concepts) - [Set parameters | .NET | SDK | Concepts](#set-parameters-net-sdk-concepts) - [.NET | SDK | Methods | Version](#-net-sdk-methods-version) - [.NET | SDK | Methods | Patch](#-net-sdk-methods-patch) - [Arguments | Embedded scripting functions | SurrealQL](#arguments-embedded-scripting-functions-surrealql) - [Sequence functions | SurrealQL](#sequence-functions-surrealql) - [Value functions | SurrealQL](#value-functions-surrealql) - [Built-in functions | Embedded scripting functions | SurrealQL](#built-in-functions-embedded-scripting-functions-surrealql) - [Function context | Embedded scripting functions | SurrealQL](#function-context-embedded-scripting-functions-surrealql) - [IF ELSE statement | SurrealQL](#if-else-statement-surrealql) - [Parameters | SurrealQL](#parameters-surrealql) - [Machine learning functions | SurrealQL](#machine-learning-functions-surrealql) - [FOR statement | SurrealQL](#for-statement-surrealql) - [Sleep function | SurrealQL](#sleep-function-surrealql) - [Type conversion | Embedded scripting functions | SurrealQL](#type-conversion-embedded-scripting-functions-surrealql) - [Machine Learning functions | SurrealQL](#machine-learning-functions-surrealql) - [Comments | SurrealQL](#comments-surrealql) - [Vector functions | SurrealQL](#vector-functions-surrealql) - [SurrealQL functions | Embedded scripting functions | SurrealQL](#surrealql-functions-embedded-scripting-functions-surrealql) - [DEFINE SCOPE statement | SurrealQL](#define-scope-statement-surrealql) - [DEFINE NAMESPACE statement | SurrealQL](#define-namespace-statement-surrealql) - [INSERT statement | SurrealQL](#insert-statement-surrealql) - [DELETE statement | SurrealQL](#delete-statement-surrealql) - [DEFINE SEQUENCE statement | SurrealQL](#define-sequence-statement-surrealql) - [Session functions | SurrealQL](#session-functions-surrealql) - [USE statement | SurrealQL](#use-statement-surrealql) - [DEFINE PARAM statement | SurrealQL](#define-param-statement-surrealql) - [RETURN statement | SurrealQL](#return-statement-surrealql) - [INFO statement | SurrealQL](#info-statement-surrealql) - [WITH clause | SurrealQL](#with-clause-surrealql) - [BREAK statement | SurrealQL](#break-statement-surrealql) - [CANCEL statement | SurrealQL](#cancel-statement-surrealql) - [Geo functions | SurrealQL](#geo-functions-surrealql) - [FETCH clause | SurrealQL](#fetch-clause-surrealql) - [Clauses | SurrealQL](#clauses-surrealql) - [Statements | SurrealQL](#statements-surrealql) - [ORDER BY clause | SurrealQL](#order-by-clause-surrealql) - [COMMIT statement | SurrealQL](#commit-statement-surrealql) - [BEGIN statement | SurrealQL](#begin-statement-surrealql) - [LIVE SELECT statement | SurrealQL](#live-select-statement-surrealql) - [GROUP BY clause | SurrealQL](#group-by-clause-surrealql) - [DEFINE TABLE statement | SurrealQL](#define-table-statement-surrealql) - [Transactions | SurrealQL](#transactions-surrealql) - [LIMIT clause | SurrealQL](#limit-clause-surrealql) - [OMIT clause | SurrealQL](#omit-clause-surrealql) - [EXPLAIN clause | SurrealQL](#explain-clause-surrealql) - [Record links | SurrealQL](#record-links-surrealql) - [Datetimes | SurrealQL](#datetimes-surrealql) - [Sets | SurrealQL](#sets-surrealql) - [Booleans | SurrealQL](#booleans-surrealql) - [Ranges | SurrealQL](#ranges-surrealql) - [Regex | SurrealQL](#regex-surrealql) - [None and Null | SurrealQL](#none-and-null-surrealql) - [DEFINE BUCKET statement | SurrealQL](#define-bucket-statement-surrealql) - [Numbers | SurrealQL](#numbers-surrealql) - [DEFINE FUNCTION statement | SurrealQL](#define-function-statement-surrealql) - [SPLIT clause | SurrealQL](#split-clause-surrealql) - [FROM clause | SurrealQL](#from-clause-surrealql) - [DEFINE EVENT statement | SurrealQL](#define-event-statement-surrealql) - [Strings | SurrealQL](#strings-surrealql) - [LET statement | SurrealQL](#let-statement-surrealql) - [DEFINE TOKEN statement | SurrealQL](#define-token-statement-surrealql) - [UPDATE statement | SurrealQL](#update-statement-surrealql) - [Files | SurrealQL](#files-surrealql) - [Futures | SurrealQL](#futures-surrealql) - [Literals | SurrealQL](#literals-surrealql) - [Values | SurrealQL](#values-surrealql) - [Data types | SurrealQL](#data-types-surrealql) - [ACCESS statement | SurrealQL](#access-statement-surrealql) - [Demo data | SurrealQL](#demo-data-surrealql) - [Casting | SurrealQL](#casting-surrealql) - [DEFINE API statement | SurrealQL](#define-api-statement-surrealql) - [DEFINE CONFIG statement | SurrealQL](#define-config-statement-surrealql) - [Record IDs | SurrealQL](#record-ids-surrealql) --- # SurrealDB [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealdb) Introduction [Introduction](https://surrealdb.com/docs/surrealdb/introduction) [Getting started](https://surrealdb.com/docs/surrealdb/introduction/start) [Concepts](https://surrealdb.com/docs/surrealdb/introduction/concepts) [Namespace](https://surrealdb.com/docs/surrealdb/introduction/concepts/namespace) [Database](https://surrealdb.com/docs/surrealdb/introduction/concepts/database) [Architecture](https://surrealdb.com/docs/surrealdb/introduction/architecture) Data Models [Introduction](https://surrealdb.com/docs/surrealdb/models) [Document](https://surrealdb.com/docs/surrealdb/models/document) [Graph](https://surrealdb.com/docs/surrealdb/models/graph) [Vector](https://surrealdb.com/docs/surrealdb/models/vector) [Full Text Search](https://surrealdb.com/docs/surrealdb/models/full-text-search) [Time Series](https://surrealdb.com/docs/surrealdb/models/time-series) [Geospatial](https://surrealdb.com/docs/surrealdb/models/geospatial) Installation [Introduction](https://surrealdb.com/docs/surrealdb/installation) [Install on macOS](https://surrealdb.com/docs/surrealdb/installation/macos) [Install on Linux](https://surrealdb.com/docs/surrealdb/installation/linux) [Install on Windows](https://surrealdb.com/docs/surrealdb/installation/windows) [Install the Nightly](https://surrealdb.com/docs/surrealdb/installation/nightly) [Running](https://surrealdb.com/docs/surrealdb/installation/running) [Run with Docker](https://surrealdb.com/docs/surrealdb/installation/running/docker) [Run a disk based server](https://surrealdb.com/docs/surrealdb/installation/running/file) [Run an in memory server](https://surrealdb.com/docs/surrealdb/installation/running/memory) [Run a multi node server](https://surrealdb.com/docs/surrealdb/installation/running/tikv) [Upgrading](https://surrealdb.com/docs/surrealdb/installation/upgrading) [Upgrading from 1.x to 2.x](https://surrealdb.com/docs/surrealdb/installation/upgrading/migrating-data-to-2x) Querying [Introduction](https://surrealdb.com/docs/surrealdb/querying) [Querying via SDKs](https://surrealdb.com/docs/surrealdb/querying/sdks) [GraphQL](https://surrealdb.com/docs/surrealdb/querying/graphql) [GraphQL via Surrealist](https://surrealdb.com/docs/surrealdb/querying/graphql/surrealist) [GraphQL via HTTP](https://surrealdb.com/docs/surrealdb/querying/graphql/http) [SurrealQL](https://surrealdb.com/docs/surrealdb/querying/surrealql) [SurrealQL via Surrealist](https://surrealdb.com/docs/surrealdb/querying/surrealql/surrealist) [SurrealQL via HTTP](https://surrealdb.com/docs/surrealdb/querying/surrealql/http) [SurrealQL via CLI](https://surrealdb.com/docs/surrealdb/querying/surrealql/cli) Security [Introduction](https://surrealdb.com/docs/surrealdb/security) [Summary](https://surrealdb.com/docs/surrealdb/security/summary) [Authentication](https://surrealdb.com/docs/surrealdb/security/authentication) [Capabilities](https://surrealdb.com/docs/surrealdb/security/capabilities) [Troubleshooting](https://surrealdb.com/docs/surrealdb/security/troubleshooting) Deployment [Introduction](https://surrealdb.com/docs/surrealdb/deployment) [Deploy on Surreal Cloud](https://surrealdb.com/docs/surrealdb/deployment/surreal-cloud) [Deploy on Kubernetes](https://surrealdb.com/docs/surrealdb/deployment/kubernetes) [Deploy on Amazon EKS](https://surrealdb.com/docs/surrealdb/deployment/amazon) [Deploy on Google GKE](https://surrealdb.com/docs/surrealdb/deployment/google) [Deploy on Azure AKS](https://surrealdb.com/docs/surrealdb/deployment/azure) Embedding [Introduction](https://surrealdb.com/docs/surrealdb/embedding) [Embedding SurrealDB in Rust](https://surrealdb.com/docs/surrealdb/embedding/rust) [Embedding SurrealDB in JavaScript](https://surrealdb.com/docs/surrealdb/embedding/javascript) [Embedding SurrealDB in Python](https://surrealdb.com/docs/surrealdb/embedding/python) [Embedding SurrealDB in .NET](https://surrealdb.com/docs/surrealdb/embedding/dotnet) Integration [Introduction](https://surrealdb.com/docs/surrealdb/integration) [SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) [HTTP & Rest](https://surrealdb.com/docs/surrealdb/integration/http) [RPC Protocol](https://surrealdb.com/docs/surrealdb/integration/rpc) [CBOR Protocol](https://surrealdb.com/docs/surrealdb/integration/cbor) CLI tool [Introduction](https://surrealdb.com/docs/surrealdb/cli) [Start command](https://surrealdb.com/docs/surrealdb/cli/start) [SQL command](https://surrealdb.com/docs/surrealdb/cli/sql) [Help command](https://surrealdb.com/docs/surrealdb/cli/help) [Export command](https://surrealdb.com/docs/surrealdb/cli/export) [Fix command](https://surrealdb.com/docs/surrealdb/cli/fix) [Import command](https://surrealdb.com/docs/surrealdb/cli/import) [Isready command](https://surrealdb.com/docs/surrealdb/cli/isready) [ML commands](https://surrealdb.com/docs/surrealdb/cli/ml) [Import command](https://surrealdb.com/docs/surrealdb/cli/ml/import) [Export command](https://surrealdb.com/docs/surrealdb/cli/ml/export) [Upgrade command](https://surrealdb.com/docs/surrealdb/cli/upgrade) [Validate command](https://surrealdb.com/docs/surrealdb/cli/validate) [Version command](https://surrealdb.com/docs/surrealdb/cli/version) [Environment variables](https://surrealdb.com/docs/surrealdb/cli/env) Reference Guides [Introduction](https://surrealdb.com/docs/surrealdb/reference-guide) [Full Text Search](https://surrealdb.com/docs/surrealdb/reference-guide/full-text-search) [Graph Relations](https://surrealdb.com/docs/surrealdb/reference-guide/graph-relations) [Observability](https://surrealdb.com/docs/surrealdb/reference-guide/observability) [Security Best Practices](https://surrealdb.com/docs/surrealdb/reference-guide/security-best-practices) [Vector Search](https://surrealdb.com/docs/surrealdb/reference-guide/vector-search) [Performance Best Practices](https://surrealdb.com/docs/surrealdb/reference-guide/performance-best-practices) [Sample Industry Schemas](https://surrealdb.com/docs/surrealdb/reference-guide/sample-industry-schemas) [Schema Best Practices](https://surrealdb.com/docs/surrealdb/reference-guide/schema-creation-best-practices) FAQs [Introduction](https://surrealdb.com/docs/surrealdb/faqs) [Known Issues](https://surrealdb.com/docs/surrealdb/faqs/known-issues) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealdb) [Overview](https://surrealdb.com/docs/surrealdb) ![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_20slx6.svg) SurrealDB is a native, open-source, [multi-model database](https://surrealdb.com/features/multi-model-database) that lets you store and manage data across relational, document, graph, time-series, vector & search, and geospatial models—all in one place. Powered by a flexible, SQL-like query language called [SurrealQL](https://surrealdb.com/docs/surrealql) , you can write queries using familiar syntax while leveraging the capabilities of a modern, unified database. Built to be [distributed and scalable](https://surrealdb.com/docs/surrealdb/introduction/architecture) , SurrealDB makes it easy to grow with your application. You can also interact with SurrealDB through additional query methods like [GraphQL](https://surrealdb.com/docs/surrealdb/querying/graphql) , [HTTP](https://surrealdb.com/docs/surrealdb/integration/http) , and [RPC](https://surrealdb.com/docs/surrealdb/integration/rpc) , or integrate directly using a range of [SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) in your native development environment. [Get started with SurrealDB](https://surrealdb.com/docs/surrealdb/introduction/start) [Installation guide](https://surrealdb.com/docs/surrealdb/installation) Topics[](https://surrealdb.com/docs/surrealdb#topics) ====================================================== [#### ![Surreal Cloud](https://surrealdb.com/docs/_astro/cloud-light.BZu7Kbhl_2vfdcu.webp)\ \ Surreal Cloud Managed Cloud Hosting\ \ Effortlessly deploy, manage, and scale your SurrealDB instances in the cloud.](https://surrealdb.com/docs/cloud) [#### ![Surrealist](https://surrealdb.com/docs/_astro/surrealist.DifebjXY_ZvuE7v.webp)\ \ Surrealist Management Interface\ \ The official dashboard to query and manage your SurrealDB databases.](https://surrealdb.com/docs/surrealist) [#### ![SurrealQL](https://surrealdb.com/docs/_astro/ql-light.OfpRe4oD_Z2wJbH2.webp)\ \ SurrealQL Query Language\ \ Discover SurrealQL, a powerful and intuitive SQL-like language built for SurrealDB.](https://surrealdb.com/docs/surrealql) Data Models[](https://surrealdb.com/docs/surrealdb#data-models) ================================================================ [#### ![Document](https://surrealdb.com/docs/_astro/document.ZAU4Cdmm_Z1vQo0W.webp)\ \ Document\ \ Document database that is designed to make building and scaling realtime apps dramatically quicker and easier.](https://surrealdb.com/docs/surrealdb/models/document) [#### ![Graph](https://surrealdb.com/docs/_astro/graph.WmluwZ0z_Z1SODJL.webp)\ \ Graph\ \ Graph database that is designed to make building and scaling realtime apps dramatically quicker and easier.](https://surrealdb.com/docs/surrealdb/models/graph) [#### ![Vector](https://surrealdb.com/docs/_astro/vector.DJl8v-T3_Z1pfwS.webp)\ \ Vector\ \ Vector database that is designed to make building and scaling realtime apps dramatically quicker and easier.](https://surrealdb.com/docs/surrealdb/models/vector) [#### ![Full-Text Search](https://surrealdb.com/docs/_astro/full-text-search.BPFyMy9y_Z10fa7I.webp)\ \ Full-Text Search\ \ Full-Text Search database that is designed to make building and scaling realtime apps dramatically quicker and easier.](https://surrealdb.com/docs/surrealdb/models/full-text-search) [#### ![Time Series](https://surrealdb.com/docs/_astro/time-series.JKuM0hcZ_T35Mm.webp)\ \ Time Series\ \ Time Series database that is designed to make building and scaling realtime apps dramatically quicker and easier.](https://surrealdb.com/docs/surrealdb/models/time-series) [#### ![Geospatial](https://surrealdb.com/docs/_astro/geospatial.Bi5WFsyX_Za483b.webp)\ \ Geospatial\ \ Geospatial database that is designed to make building and scaling realtime apps dramatically quicker and easier.](https://surrealdb.com/docs/surrealdb/models/geospatial) Integrations[](https://surrealdb.com/docs/surrealdb#integrations) ================================================================== [#### ![Data Management](https://surrealdb.com/docs/_astro/integrations.Bp4SONs3_ZIH2hh.webp)\ \ Data Management\ \ SurrealDB integrates with Airbyte, Fivetran, and more to help you manage your data.](https://surrealdb.com/docs/integrations/data-management) [#### ![Embeddings](https://surrealdb.com/docs/_astro/integrations.Bp4SONs3_ZIH2hh.webp)\ \ Embeddings\ \ SurrealDB provides a number of different embeddings features that can be used to manage your data.](https://surrealdb.com/docs/integrations/embeddings) [#### ![Frameworks](https://surrealdb.com/docs/_astro/integrations.Bp4SONs3_ZIH2hh.webp)\ \ Frameworks\ \ SurrealDB integrates with a number of different frameworks to help you build your applications.](https://surrealdb.com/docs/integrations/frameworks) Authentication[](https://surrealdb.com/docs/surrealdb#authentication) ====================================================================== [#### ![System Access](https://surrealdb.com/docs/_astro/system-access.sglBF_iL_wJOM3.webp)\ \ System Access\ \ System access is used to authenticate and authorize users to access the SurrealDB server.](https://surrealdb.com/docs/surrealdb/security/authentication) [#### ![Record Access](https://surrealdb.com/docs/_astro/record-access.mpq9gGYd_1aUep2.webp)\ \ Record Access\ \ Record access is used to authenticate and authorize users to access the SurrealDB database.](https://surrealdb.com/docs/surrealql/statements/define/access/record) [#### ![Token Accesss](https://surrealdb.com/docs/_astro/token-access.PQ7S12fH_1jhM3C.webp)\ \ Token Accesss\ \ Token access is used to authenticate and authorize users to access the SurrealDB database.](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) Education[](https://surrealdb.com/docs/surrealdb#education) ============================================================ [#### ![SurrealDB University](https://surrealdb.com/docs/_astro/university.sZTpbgN3_Z2wHX6j.webp)\ \ SurrealDB University\ \ Watch videos, live streams, and expert talks to quickly learn key SurrealDB concepts and insights.](https://surrealdb.com/learn) [#### ![Aeon's Surreal Renaissance](https://surrealdb.com/docs/_astro/book.C670wprV_Z1me88d.webp)\ \ Aeon's Surreal Renaissance\ \ Learn SurrealDB through an engaging, story-driven experience that feels like watching a movie.](https://surrealdb.com/learn/book) [#### ![Surreal Sidekick](https://surrealdb.com/docs/_astro/sidekick.Cysr44z9_Z17hQnR.webp)\ \ Surreal Sidekick\ \ Surreal Sidekick is your go to AI assistant for all things SurrealDB related.](https://app.surrealdb.com/chat) SDKs[](https://surrealdb.com/docs/surrealdb#sdks) ================================================== [#### ![Rust](https://surrealdb.com/docs/_astro/axum.XM7ZlikV_Z1zBi9n.webp)\ \ Rust\ \ Connect to SurrealDB in Rust applications.\ \ HTTP & WebSocket\ \ In-memory and on-disk](https://surrealdb.com/docs/sdk/rust) [#### ![JavaScript](https://surrealdb.com/docs/_astro/javascript.sX9PYvGJ_Z27yyMU.webp)\ \ JavaScript\ \ Connect to SurrealDB from JavaScript environments.\ \ HTTP & WebSocket](https://surrealdb.com/docs/sdk/javascript) [#### ![Node.js](https://surrealdb.com/docs/_astro/nodejs.skyfKwdq_NR09V.webp)\ \ Node.js\ \ Node.js adapter for the JavaScript SDK.\ \ HTTP & WebSocket\ \ In-memory and on-disk](https://surrealdb.com/docs/sdk/javascript/engines/node) [#### ![WASM](https://surrealdb.com/docs/_astro/webassembly.CF9wKTKl_Z1R4zpC.webp)\ \ WASM\ \ WebAssembly adapter for the JavaScript SDK.\ \ In-memory and IndexedDB](https://surrealdb.com/docs/sdk/javascript/engines/wasm) [#### ![Java](https://surrealdb.com/docs/_astro/java.-D1_B-fU_Z7BACx.webp)\ \ Java\ \ Connect to SurrealDB in Java applications.\ \ HTTP & WebSocket\ \ In-memory and on-disk](https://surrealdb.com/docs/sdk/java) [#### ![Golang](https://surrealdb.com/docs/_astro/golang.6qxRzLGE_ajJ73.webp)\ \ Golang\ \ Connect to SurrealDB in Go applications.\ \ HTTP & WebSocket\ \ In-memory and on-disk](https://surrealdb.com/docs/sdk/golang) [#### ![Python](https://surrealdb.com/docs/_astro/python.UjMY2cJ7_CdzB5.webp)\ \ Python\ \ Connect to SurrealDB in Python applications.\ \ HTTP & WebSocket\ \ In-memory and on-disk](https://surrealdb.com/docs/sdk/python) [#### ![.NET](https://surrealdb.com/docs/_astro/dotnet.BCx1fozL_JdAal.webp)\ \ .NET\ \ Connect to SurrealDB in .NET applications.\ \ HTTP & WebSocket\ \ In-memory and on-disk](https://surrealdb.com/docs/sdk/dotnet) [#### ![PHP](https://surrealdb.com/docs/_astro/php.Cy4Sbx66_1Awa1W.webp)\ \ PHP\ \ Connect to SurrealDB in PHP applications.\ \ HTTP & WebSocket](https://surrealdb.com/docs/sdk/php) Resources[](https://surrealdb.com/docs/surrealdb#resources) ============================================================ [![SurrealDB](https://surrealdb.com/docs/_astro/labs.mRExFNtU_e1mvt.svg)\ \ Dive into the full range of tools, libraries, and integrations built around SurrealDB. SurrealDB Labs brings together first- and third-party resources to help you build, manage, and extend your SurrealDB applications with ease.\ \ Explore SurrealDB Labs](https://surrealdb.com/docs/labs) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealdb/index.mdx) Copy page as Markdown [Introduction](https://surrealdb.com/docs/surrealdb/introduction) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealdb/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Java | SDKs | Integration [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/java) [Installation](https://surrealdb.com/docs/sdk/java/installation) [Quick start](https://surrealdb.com/docs/sdk/java/start) [Data types](https://surrealdb.com/docs/sdk/java/data-types) [API Documentation](https://surrealdb.com/docs/sdk/java/api-documentation) Concepts [Introduction](https://surrealdb.com/docs/sdk/java/core) [Create a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/java/core/handling-authentication) [surrealdb/surrealdb.java](https://github.com/surrealdb/surrealdb.java) [](https://surrealdb.com/docs/sdk/java) [Overview](https://surrealdb.com/docs/sdk/java) ![Java](https://surrealdb.com/docs/_astro/java.-D1_B-fU_2hrNx7.webp) Java SDK[](https://surrealdb.com/docs/sdk/java#java-sdk) ========================================================= The SurrealDB SDK for Java is the primary SDK for interacting with SurrealDB from client or server-side applications, systems, and APIs. You can use the Java SDK to interact with your SurrealDB database instances, or to run SurrealDB as an embedded database within your Java application, with functionality for executing queries, managing data, running database functions, and authenticating to the database. When connecting to remote database instances, connections automatically reconnect when terminated. > Important > > The SDK requires Java JDK version `8` or greater, and is available as a [Maven package](https://mvnrepository.com/artifact/com.surrealdb/surrealdb) > . > Note > > The SDK works seamlessly with SurrealDB versions `v2.0.0` to `v2.3.7`, ensuring compatibility with the latest version. > Warning > > This API is not yet fully stabilized and may be subject to change until the SDK reaches `1.0.0`. To contribute to the SDK code, submit an Issue or Pull Request in the [surrealdb.java](https://github.com/surrealdb/surrealdb.java) repository. To contribute to this documentation, submit an Issue or Pull Request in the [docs.surrealdb.com](https://github.com/surrealdb/docs.surrealdb.com) repository. Example projects[](https://surrealdb.com/docs/sdk/java#example-projects) ------------------------------------------------------------------------- You can find example repositories that demonstrate how to integrate SurrealDB in a number of different environments: [#### \ \ Java Starter\ \ A simple starter project demonstrating the power of the SurrealDB Java SDK](https://github.com/surrealdb/examples/tree/main/java-starter) Sources[](https://surrealdb.com/docs/sdk/java#sources) ------------------------------------------------------- * [GitHub repository](https://github.com/surrealdb/surrealdb.java) * [JavaDoc](https://surrealdb.github.io/surrealdb.java/javadoc/) * [Maven package](https://mvnrepository.com/artifact/com.surrealdb/surrealdb) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/index.mdx) Copy page as Markdown [Installation](https://surrealdb.com/docs/sdk/java/installation) ### On this page * [Java SDK](https://surrealdb.com/docs/sdk/java#java-sdk) * [Example projects](https://surrealdb.com/docs/sdk/java#example-projects) * [Sources](https://surrealdb.com/docs/sdk/java#sources) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Java | SDKs | Installation [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/java) [Installation](https://surrealdb.com/docs/sdk/java/installation) [Quick start](https://surrealdb.com/docs/sdk/java/start) [Data types](https://surrealdb.com/docs/sdk/java/data-types) [API Documentation](https://surrealdb.com/docs/sdk/java/api-documentation) Concepts [Introduction](https://surrealdb.com/docs/sdk/java/core) [Create a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/java/core/handling-authentication) [surrealdb/surrealdb.java](https://github.com/surrealdb/surrealdb.java) [](https://surrealdb.com/docs/sdk/java) [Installation](https://surrealdb.com/docs/sdk/java/installation) Installation[](https://surrealdb.com/docs/sdk/java/installation#installation) ============================================================================== In this section, you will learn how to install the Java SDK in your project. ### Install the SDK[](https://surrealdb.com/docs/sdk/java/installation#install-the-sdk) First, install the [SurrealDB SDK](https://mvnrepository.com/artifact/com.surrealdb/surrealdb) from the Maven Central repository using [Gradle](https://gradle.org/) or [Maven](https://maven.apache.org/) . * [Gradle (Groovy)](https://surrealdb.com/docs/sdk/java/installation#tab-panel-33) * [Gradle (Kotlin)](https://surrealdb.com/docs/sdk/java/installation#tab-panel-34) * [Maven](https://surrealdb.com/docs/sdk/java/installation#tab-panel-35) ext { surrealdbVersion = "0.2.1" } dependencies { implementation "com.surrealdb:surrealdb:${surrealdbVersion}" } val surrealdbVersion by extra("0.2.1") dependencies { implementation("com.surrealdb:surrealdb:${surrealdbVersion}") } com.surrealdb surrealdb 0.2.1 ### Import the SDK to your project[](https://surrealdb.com/docs/sdk/java/installation#import-the-sdk-to-your-project) After installing, you can access the SDK by importing from the `com.surrealdb` package. import com.surrealdb.Surreal; ... Surreal surreal = new Surreal(); In the Quick Start guide, you will learn how to create a new connection and authenticate users using the SDK. Next Steps[](https://surrealdb.com/docs/sdk/java/installation#next-steps) -------------------------------------------------------------------------- After installing the SDK, check out the quick start guide to build your a simple application with the SDK. You can also learn more about carrying out common tasks with the SDK in the following sections: * [Quick Start](https://surrealdb.com/docs/sdk/java/start) * [Creating a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) * [Authenticating users](https://surrealdb.com/docs/sdk/java/core/handling-authentication) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/installation.mdx) Copy page as Markdown [Overview](https://surrealdb.com/docs/sdk/java) [Quick start](https://surrealdb.com/docs/sdk/java/start) ### On this page * [Install the SDK](https://surrealdb.com/docs/sdk/java/installation#install-the-sdk) * [Import the SDK to your project](https://surrealdb.com/docs/sdk/java/installation#import-the-sdk-to-your-project) * [Next Steps](https://surrealdb.com/docs/sdk/java/installation#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/installation.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Fastembed | Embeddings [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/integrations) Data Management [Introduction](https://surrealdb.com/docs/integrations/data-management) [Airbyte](https://surrealdb.com/docs/integrations/data-management/airbyte) [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) Embeddings [Introduction](https://surrealdb.com/docs/integrations/embeddings) [Quick start with Python](https://surrealdb.com/docs/integrations/embeddings/python) [Quick start with Rust](https://surrealdb.com/docs/integrations/embeddings/rust) [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) Frameworks [Introduction](https://surrealdb.com/docs/integrations/frameworks) [Agno](https://surrealdb.com/docs/integrations/frameworks/agno) [Camel](https://surrealdb.com/docs/integrations/frameworks/camel) [CrewAI](https://surrealdb.com/docs/integrations/frameworks/crewai) [Dagster](https://surrealdb.com/docs/integrations/frameworks/dagster) [DeepEval](https://surrealdb.com/docs/integrations/frameworks/deepeval) [Dynamiq](https://surrealdb.com/docs/integrations/frameworks/dynamiq) [Feast](https://surrealdb.com/docs/integrations/frameworks/feast) [Google Agent](https://surrealdb.com/docs/integrations/frameworks/googleagent) [Langchain](https://surrealdb.com/docs/integrations/frameworks/langchain) [Llama Index](https://surrealdb.com/docs/integrations/frameworks/llama-index) [Smolagents](https://surrealdb.com/docs/integrations/frameworks/smolagents) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/integrations) [Embeddings](https://surrealdb.com/docs/integrations/embeddings) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) Fastembed[](https://surrealdb.com/docs/integrations/embeddings/fastembed#fastembed) ==================================================================================== Fastembed is a library that allows you to generate vector embeddings locally, without needing an API key or calling into an external service. Fastembed uses the included ONNX runtime to run its embedding models, downloading the model once every time it is used for the first time. Fastembed libraries are available for the following languages: * [Python](https://github.com/qdrant/fastembed) * [Rust](https://crates.io/crates/fastembed) * [Go](https://github.com/Anush008/fastembed-go) * [JavaScript](https://github.com/Anush008/fastembed-js) Overview of available models[](https://surrealdb.com/docs/integrations/embeddings/fastembed#overview-of-available-models) -------------------------------------------------------------------------------------------------------------------------- The following is an overview of most of the models available for Fastembed. General use cases are: * Prototyping, development speed, cost savings: models with small vector embedding sizes tend to take up a relatively small amount of disk size as well (generally a few hundred MB) and can be quickly downloaded and run locally. * Small or edge devices: devices with no internet access can still take advantage of vector embeddings. * Particular use cases: some models are specialized for certain use cases such as images, certain languages, and contexts. ### MiniLM Series[](https://surrealdb.com/docs/integrations/embeddings/fastembed#minilm-series) Fast general-purpose embeddings. Choose L6 for speed, L12 for quality. Ideal for semantic search, clustering, and similarity tasks. A “quantized” model means that it is optimised for faster inference and lower memory usage, often with minimal quality loss. | Model name | Embedding size | Description | | --- | --- | --- | | AllMiniLML6V2 | 384 | Sentence Transformer model, MiniLM-L6-v2 | | AllMiniLML6V2Q | 384 | Quantized Sentence Transformer model, MiniLM-L6-v2 | | AllMiniLML12V2 | 384 | Sentence Transformer model, MiniLM-L12-v2 | | AllMiniLML12V2Q | 384 | Quantized Sentence Transformer model, MiniLM-L12-v2 | ### BGE Series[](https://surrealdb.com/docs/integrations/embeddings/fastembed#bge-series) Used for dense retrieval and semantic similarity. BGESmallENV15 is optimized for speed and tends to be the default choice for many applications. | Model name | Embedding size | Description | | --- | --- | --- | | BGEBaseENV15 | 768 | v1.5 release of the base English model | | BGEBaseENV15Q | 768 | Quantized v1.5 release of the base English model | | BGELargeENV15 | 1024 | v1.5 release of the large English model | | BGELargeENV15Q | 1024 | Quantized v1.5 release of the large English model | | BGESmallENV15 | 384 | v1.5 release of the fast and default English model | | BGESmallENV15Q | 384 | Quantized v1.5 release of the fast and default English model | ### Nomic Embed Text[](https://surrealdb.com/docs/integrations/embeddings/fastembed#nomic-embed-text) Used for large context window embeddings. Optimized for long-context English text (8K tokens). v1.5 improves quality over v1. | Model name | Embedding size | Description | | --- | --- | --- | | NomicEmbedTextV1 | 768 | 8192 context length english model | | NomicEmbedTextV15 | 768 | v1.5 release of the 8192 context length english model | | NomicEmbedTextV15Q | 768 | Quantized v1.5 release of the 8192 context length english model | ### Paraphrase Models[](https://surrealdb.com/docs/integrations/embeddings/fastembed#paraphrase-models) Used for paraphrase detection and multilingual similarity. Ideal for sentence equivalence and semantic matching tasks. | Model name | Embedding size | Description | | --- | --- | --- | | ParaphraseMLMiniLML12V2 | 384 | Multi-lingual model | | ParaphraseMLMiniLML12V2Q | 384 | Quantized Multi-lingual model | | ParaphraseMLMpnetBaseV2 | 768 | Sentence-transformers model for tasks like clustering or semantic search, based on the MPNet architecture. | ### Chinese BGE Models[](https://surrealdb.com/docs/integrations/embeddings/fastembed#chinese-bge-models) | Model name | Embedding size | Description | | --- | --- | --- | | BGESmallZHV15 | 512 | v1.5 release of the small Chinese model | | BGELargeZHV15 | 1024 | v1.5 release of the large Chinese model | ### ModernBert and Multilingual E5[](https://surrealdb.com/docs/integrations/embeddings/fastembed#modernbert-and-multilingual-e5) Used for context-rich multilingual embeddings. Great for cross-language retrieval and nuanced contextual understanding. | Model name | Embedding size | Description | | --- | --- | --- | | ModernBertEmbedLarge | 1024 | Large model of ModernBert Text Embeddings | | MultilingualE5Small | 384 | Small model of multilingual E5 Text Embeddings | | MultilingualE5Base | 768 | Base model of multilingual E5 Text Embeddings | | MultilingualE5Large | 1024 | Large model of multilingual E5 Text Embeddings | ### Mxbai and GTE[](https://surrealdb.com/docs/integrations/embeddings/fastembed#mxbai-and-gte) Used for high-quality English/multilingual embeddings. | Model name | Embedding size | Description | | --- | --- | --- | | MxbaiEmbedLargeV1 | 1024 | Large English embedding model from MixedBreed.ai | | MxbaiEmbedLargeV1Q | 1024 | Quantized Large English embedding model from MixedBreed.ai | | GTEBaseENV15 | 768 | Base multilingual embedding model from Alibaba | | GTEBaseENV15Q | 768 | Quantized base multilingual embedding model from Alibaba | | GTELargeENV15 | 1024 | Large multilingual embedding model from Alibaba | | GTELargeENV15Q | 1024 | Quantized large multilingual embedding model from Alibaba | ### CLIP and Code Models[](https://surrealdb.com/docs/integrations/embeddings/fastembed#clip-and-code-models) Use CLIP for image-text matching, Jina for code search and retrieval. JinaEmbeddingsV2BaseCode is optimised for embedding code snippets. | Model name | Embedding size | Description | | --- | --- | --- | | ClipVitB32 | 512 | CLIP text encoder based on ViT-B/32 | | JinaEmbeddingsV2BaseCode | 768 | Jina embeddings v2 base code | Language-specific example[](https://surrealdb.com/docs/integrations/embeddings/fastembed#language-specific-example) -------------------------------------------------------------------------------------------------------------------- The following example in Rust demonstrates how SurrealDB can be used to store the embeddings from the default language model for a number of phrases, after which it can be prompted to return the three closest results to a certain prompt. First add a few crates to Cargo.toml with the following command: cargo add anyhow fastembed serde tokio surrealdb --features surrealdb/kv-mem Then use the following code. use anyhow::Error; use fastembed::{EmbeddingModel, InitOptions, TextEmbedding}; use serde::Serialize; use surrealdb::{ Surreal, Value, engine::any::{Any, connect}, }; const SCHEMA: &str = "DEFINE TABLE document; DEFINE FIELD text ON document TYPE string; DEFINE FIELD embedding ON document TYPE array; // Uncomment this to use HNSW index, ensure that number after DIMENSION matches size of embedding // DEFINE INDEX hnsw_embed ON document FIELDS embedding HNSW DIMENSION 384 DIST COSINE"; const INSERT_QUERY: &str = "INSERT INTO document $docs"; const VECTOR_QUERY: &str = "SELECT text, vector::distance::knn() AS distance FROM document WHERE embedding <|3,COSINE|> $embeds ORDER BY distance"; #[derive(Serialize)] struct DocumentInput { text: String, embedding: Vec, } async fn store_docs( input: Vec<&str>, db: &Surreal, model: &mut TextEmbedding, ) -> Result<(), Error> { let docs = model .embed(input.clone(), None)? .into_iter() .zip(input.into_iter()) .map(|(embedding, text)| DocumentInput { text: text.to_string(), embedding, }) .collect::>(); db.query(INSERT_QUERY).bind(("docs", docs)).await?; Ok(()) } async fn test_embed( input: &str, db: &Surreal, model: &mut TextEmbedding, ) -> Result<(), Error> { let Some(embeds) = model.embed(vec![input], None)?.into_iter().next() else { return Err(anyhow::anyhow!("Nothing found at index 0")); }; let val = db .query(VECTOR_QUERY) .bind(("embeds", embeds.clone())) .await? .take::(0)?; println!("{val}\n"); Ok(()) } #[tokio::main] async fn main() -> Result<(), Error> { // Default model let mut model = TextEmbedding::try_new(InitOptions::new(EmbeddingModel::BGESmallENV15))?; let db = connect("memory").await?; db.use_ns("ns").use_db("db").await?; db.query(SCHEMA).await?; let input = vec![\ // Cities\ "Calgary is a city in the Canadian province of Alberta.",\ "Ljubljana is the capital and largest city of Slovenia.",\ // Historical / mythological figures\ "Xenophon of Athens was a Greek military leader, philosopher, and historian.",\ "King Arthur was a mythical king in the mythology of Great Britain.",\ // Planets\ "Venus is the second planet from the Sun.",\ "Ceres is a dwarf planet in the middle main asteroid belt between the orbits of Mars and Jupiter.",\ // Languages\ "Manx is a Gaelic language of the insular Celtic branch of the Celtic language family",\ "Interlingue, originally Occidental, is an international auxiliary language created in 1922.",\ // Sea animals\ "Octopuses have a complex nervous system and are among the most intelligent and behaviourally diverse invertebrates.",\ "Clams have no central nervous system at all and are near to plants in intelligence.",\ ]; store_docs(input, &db, &mut model).await?; println!("Edmonton is closest to:"); test_embed("Edmonton", &db, &mut model).await?; println!("Merlin is closest to:"); test_embed("Merlin", &db, &mut model).await?; println!("Earth is closest to:"); test_embed("Earth", &db, &mut model).await?; println!("Irish is closest to:"); test_embed("Irish language", &db, &mut model).await?; println!("Squid are closest to:"); test_embed("Squid", &db, &mut model).await?; Ok(()) } Output of the example with the default model: Edmonton is closest to: [\ { distance: 0.2596421358215669f, text: 'Calgary is a city in the Canadian province of Alberta.' },\ { distance: 0.5010449624435647f, text: 'Ljubljana is the capital and largest city of Slovenia.' },\ { distance: 0.5242241576926254f, text: 'Interlingue, originally Occidental, is an international auxiliary language created in 1922.' }\ ] Merlin is closest to: [\ { distance: 0.3653307924860497f, text: 'King Arthur was a mythical king in the mythology of Great Britain.' },\ { distance: 0.4515194174120666f, text: 'Manx is a Gaelic language of the insular Celtic branch of the Celtic language family' },\ { distance: 0.5317039966149415f, text: 'Calgary is a city in the Canadian province of Alberta.' }\ ] Earth is closest to: [\ { distance: 0.3380429615054925f, text: 'Venus is the second planet from the Sun.' },\ { distance: 0.3764237673020161f, text: 'Ceres is a dwarf planet in the middle main asteroid belt between the orbits of Mars and Jupiter.' },\ { distance: 0.444087039462282f, text: 'Calgary is a city in the Canadian province of Alberta.' }\ ] Irish is closest to: [\ { distance: 0.27517683002655635f, text: 'Manx is a Gaelic language of the insular Celtic branch of the Celtic language family' },\ { distance: 0.34080671701374754f, text: 'Interlingue, originally Occidental, is an international auxiliary language created in 1922.' },\ { distance: 0.5113325799682362f, text: 'King Arthur was a mythical king in the mythology of Great Britain.' }\ ] Squid are closest to: [\ { distance: 0.3439891425642231f, text: 'Octopuses have a complex nervous system and are among the most intelligent and behaviourally diverse invertebrates.' },\ { distance: 0.4707156750207915f, text: 'Manx is a Gaelic language of the insular Celtic branch of the Celtic language family' },\ { distance: 0.517311424260043f, text: 'Clams have no central nervous system at all and are near to plants in intelligence.' }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/Embeddings/fastembed.mdx) Copy page as Markdown [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) ### On this page * [Overview of available models](https://surrealdb.com/docs/integrations/embeddings/fastembed#overview-of-available-models) * [MiniLM Series](https://surrealdb.com/docs/integrations/embeddings/fastembed#minilm-series) * [BGE Series](https://surrealdb.com/docs/integrations/embeddings/fastembed#bge-series) * [Nomic Embed Text](https://surrealdb.com/docs/integrations/embeddings/fastembed#nomic-embed-text) * [Paraphrase Models](https://surrealdb.com/docs/integrations/embeddings/fastembed#paraphrase-models) * [Chinese BGE Models](https://surrealdb.com/docs/integrations/embeddings/fastembed#chinese-bge-models) * [ModernBert and Multilingual E5](https://surrealdb.com/docs/integrations/embeddings/fastembed#modernbert-and-multilingual-e5) * [Mxbai and GTE](https://surrealdb.com/docs/integrations/embeddings/fastembed#mxbai-and-gte) * [CLIP and Code Models](https://surrealdb.com/docs/integrations/embeddings/fastembed#clip-and-code-models) * [Language-specific example](https://surrealdb.com/docs/integrations/embeddings/fastembed#language-specific-example) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/Embeddings/fastembed.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # SurrealKV | Introduction [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Introduction](https://surrealdb.com/docs/surrealkv) [Performance characteristics](https://surrealdb.com/docs/surrealkv/performance) [surrealdb/surrealkv](https://github.com/surrealdb/surrealkv) [](https://surrealdb.com/docs/surrealkv) [Introduction](https://surrealdb.com/docs/surrealkv) ![SurrealKV](https://surrealdb.com/docs/_astro/kv.2f1opyYh_2tqieM.webp) SurrealKV[](https://surrealdb.com/docs/surrealkv#surrealkv) ============================================================ SurrealKV is an embedded ACID-compliant key-value storage engine with built-in versioning, that allows for historical or temporal querying. It is built entirely in Rust - like the rest of SurrealDB. Since SurrealKV supports historical/temporal querying, you can user the [VERSION](https://surrealdb.com/docs/surrealql/statements/select#the-version-clause) clause when selecting or creating data to access historical data. Learn more in the SurrealQL documentation. > Important > > Although included in SurrealDB `2.0`, the storage engine itself is considered beta, and may require additional development and testing before it is ready for production use. Key Features[](https://surrealdb.com/docs/surrealkv#key-features) ------------------------------------------------------------------ SurrealKV offers several key features that makes it a powerful and versatile database storage engine: * **Embedded Database**: SurrealKV is available for embedded environments. * **Built-in Versioning**: Start SurrealKV [in versioned mode](https://surrealdb.com/docs/surrealdb/cli/start#surrealkv-beta) to track and access historical versions of your data. * **Compaction**: Efficient storage management through compaction. * **Durability Guaranteed**: Persistent storage with protection against system failures. When do I use SurrealKV?[](https://surrealdb.com/docs/surrealkv#when-do-i-use-surrealkv) ----------------------------------------------------------------------------------------- While SurrealKV is particularly well-suited for certain use cases, it can differ in terms of performance and limitations from RocksDB. The next article will list the benefits and limitations of SurrealKV. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealkv/index.mdx) Copy page as Markdown [Performance characteristics](https://surrealdb.com/docs/surrealkv/performance) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealkv/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Java | SDK | Concepts [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/java) [Installation](https://surrealdb.com/docs/sdk/java/installation) [Quick start](https://surrealdb.com/docs/sdk/java/start) [Data types](https://surrealdb.com/docs/sdk/java/data-types) [API Documentation](https://surrealdb.com/docs/sdk/java/api-documentation) Concepts [Introduction](https://surrealdb.com/docs/sdk/java/core) [Create a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/java/core/handling-authentication) [surrealdb/surrealdb.java](https://github.com/surrealdb/surrealdb.java) [](https://surrealdb.com/docs/sdk/java) [SDK Concepts](https://surrealdb.com/docs/sdk/java/core) Concepts[](https://surrealdb.com/docs/sdk/java/core#concepts) ============================================================== In this section, we will go over the core concepts of the SurrealDB SDK for Java. You will learn how to connect to a SurrealDB instance, manage authentication, and interact with the database. * [Create a new Connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) * [Handle authentication](https://surrealdb.com/docs/sdk/java/core/handling-authentication) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/core/index.mdx) Copy page as Markdown [API Documentation](https://surrealdb.com/docs/sdk/java/api-documentation) [Create a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) ### On this page * [Concepts](https://surrealdb.com/docs/sdk/java/core#concepts) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/core/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # SurrealDB Labs [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) ![SurrealDB](https://surrealdb.com/docs/_astro/labs.mRExFNtU_e1mvt.svg) Dive into the full range of tools, libraries, and integrations built around SurrealDB. This page brings together first- and third-party resources—including client SDKs, videos, development tools, learning materials, and more—to help you build, manage, and extend your SurrealDB applications with ease. Whether you're getting started or scaling up, find everything you need to support your journey with SurrealDB. [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Aeon's Surreal Renaissance - Official book for in-depth learning through storytelling.\ \ Examples\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/learn/book "Aeon's Surreal Renaissance - Official book for in-depth learning through storytelling.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### allographer\ \ ![Itsumura H.](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Itsumura H.\ \ Software Engineer](https://github.com/itsumura-h/nim-allographer "allographer") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### .NET Aspire Integration\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Bottiau\ \ Software Engineer](https://www.nuget.org/packages/CommunityToolkit.Aspire.Hosting.SurrealDb ".NET Aspire Integration") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### AspNetCore.HealthChecks.SurrealDb\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Bottiau\ \ Software Engineer](https://www.nuget.org/packages/AspNetCore.HealthChecks.SurrealDb "AspNetCore.HealthChecks.SurrealDb") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### awaited-surrealdb\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/awaited-surrealdb "awaited-surrealdb") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Beyond Surreal? A closer look at NewSQL Relational Data - Beyond Fireship.\ \ Beginner\ \ Examples\ \ ![Fireship](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Fireship\ \ Content Creator - Software Engineer](https://www.youtube.com/watch?v=LCAIkx1p1k0 "Beyond Surreal? A closer look at NewSQL Relational Data - Beyond Fireship.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Blink - Build a Notion clone with SurrealDB and WebAssembly engine\ \ Beginner\ \ Examples\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/kearfy/blink "Blink - Build a Notion clone with SurrealDB and WebAssembly engine") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Build a realtime presence web application using SurrealDB Live Queries\ \ Beginner\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Bottiau\ \ Software Engineer](https://surrealdb.com/docs/tutorials/build-a-realtime-presence-web-application-using-surrealdb-live-queries "Build a realtime presence web application using SurrealDB Live Queries") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Build an AI RAG Agent\ \ AI\ \ Examples\ \ ![Karim Lalani](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Karim Lalani\ \ Software Engineer](https://github.com/colinmcnamara/austin_langchain/blob/main/labs/LangChain_104/104-langgraph-rag-agent.ipynb "Build an AI RAG Agent") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### CLI phone book in Python using SurrealDB as database.\ \ Data Management\ \ Examples\ \ ![Gabor Szabo](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Gabor Szabo\ \ Software Engineer](https://code-maven.com/surrealdb-python-cli-phonebook "CLI phone book in Python using SurrealDB as database.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Connect to SurrealDB via Ngrok tunnel\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/connect-to-surrealdb-via-ngrok-tunnel "Connect to SurrealDB via Ngrok tunnel") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Define a Schema in SurrealDB\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/define-a-schema "Define a Schema in SurrealDB") [![Deployment Tools](https://surrealdb.com/docs/_astro/deployment-tools.light.BnweCPOT_1rordJ.webp)\ \ Deployment Tools\ \ ### Dokku Surrealdb\ \ ![Diptesh Choudhuri](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Diptesh Choudhuri\ \ Software Engineer](https://github.com/IgnisDa/dokku-surrealdb "Dokku Surrealdb") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Getting started with SurrealDB! Future of cloud databases (maybe)?\ \ Beginner\ \ ![Chris Hay](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Chris Hay\ \ Content Creator](https://www.youtube.com/watch?v=D41jb4DDIdA "Getting started with SurrealDB! Future of cloud databases (maybe)?") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Getting started with SurrealDB using Python and Docker.\ \ Beginner\ \ ![Gabor Szabo](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Gabor Szabo\ \ Software Engineer](https://code-maven.com/surrealdb-getting-started "Getting started with SurrealDB using Python and Docker.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Getting started with SurrealDB.\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/getting-started-with-surrealdb "Getting started with SurrealDB.") [![Deployment Tools](https://surrealdb.com/docs/_astro/deployment-tools.light.BnweCPOT_1rordJ.webp)\ \ Deployment Tools\ \ ### GKE using Terraform\ \ Data Management\ \ ![Dylan Vanmali](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Dylan Vanmali\ \ Software Engineer](https://github.com/dvanmali/terraform-google-surrealdb "GKE using Terraform") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Hosting Surreal DB in Rust in Less Than 3 Minutes.\ \ Beginner\ \ ![Gui Bibeau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Gui Bibeau\ \ Software Engineer](https://www.youtube.com/watch?v=VoRoeL1tal4 "Hosting Surreal DB in Rust in Less Than 3 Minutes.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### How to Use SurrealDb with the Fresh Framework and Deno.\ \ Examples\ \ ![Rajdeep Singh](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Rajdeep Singh\ \ Software Engineer](https://www.freecodecamp.org/news/how-to-use-surrealdb-with-fresh-framework/ "How to Use SurrealDb with the Fresh Framework and Deno.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Improve database management with SurrealDB.\ \ Data Management\ \ ![Alexander Nnakwue](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Alexander Nnakwue\ \ Software Engineer](https://blog.logrocket.com/improve-database-management-surrealdb/ "Improve database management with SurrealDB.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Integrate Auth0 as an Authentication Provider\ \ Security\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider "Integrate Auth0 as an Authentication Provider") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Integrate AWS Cognito as an Authentication Provider\ \ Security\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider "Integrate AWS Cognito as an Authentication Provider") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### IoT telemetry example\ \ Examples\ \ ![Martin Schaer](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Martin Schaer\ \ Solutions Engineer](https://github.com/surrealdb/example-iot-telemetry "IoT telemetry example") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Kards Social - FOSS social media app.\ \ Examples\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/kards-social "Kards Social - FOSS social media app.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### kysely-surrealdb\ \ ![Igal Klebanov](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Igal Klebanov\ \ Software Engineer](https://github.com/igalklebanov/kysely-surrealdb "kysely-surrealdb") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### LangChain integration\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/integrations/frameworks/langchain "LangChain integration") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Livestream series documenting learning SurrealDB.\ \ ![Xkonti](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Xkonti\ \ Content Creator](https://www.youtube.com/playlist?list=PL5AVzKSngnt_xPGNuYdrbB7NZtJbQ046a "Livestream series documenting learning SurrealDB.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Make a GenAI chatbot using GraphRAG with SurrealDB + LangChain\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/make-a-genai-chatbot-using-graphrag-with-surrealdb-langchain "Make a GenAI chatbot using GraphRAG with SurrealDB + LangChain") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Make a medical chatbot in Rust using GraphRAG with SurrealDB + LangChain\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/make-a-medical-chatbot-using-graphrag-with-surrealdb-langchain "Make a medical chatbot in Rust using GraphRAG with SurrealDB + LangChain") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Minimal LangChain chatbot example with vector and graph\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/minimal-langchain-chatbot-example-with-vector-and-graph "Minimal LangChain chatbot example with vector and graph") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Nextjs + surrealdb demo - Basic blog that serves as a demo / template for your nextjs + surrealdb project.\ \ Examples\ \ Beginner\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/kearfy/demo-nextjs-surrealdb "Nextjs + surrealdb demo - Basic blog that serves as a demo / template for your nextjs + surrealdb project.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Playrbase - Event & player management system.\ \ Examples\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/playrbase "Playrbase - Event & player management system.") [![Deployment Tools](https://surrealdb.com/docs/_astro/deployment-tools.light.BnweCPOT_1rordJ.webp)\ \ Deployment Tools\ \ ### Pterodactyl Egg\ \ ![Stefan](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Stefan\ \ Freelance Developer](https://github.com/Stefanuk12/Pterodactyl/blob/master/eggs/misc/egg-surrealdb.json "Pterodactyl Egg") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### PySurrealDB\ \ ![Mike Aurelion](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Mike Aurelion\ \ Software Engineer](https://github.com/aurelion314/pysurrealdb "PySurrealDB") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### ra-surrealdb\ \ ![Moïse Valvassori](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Moïse Valvassori\ \ Software Engineer](https://github.com/djedi23/ra-surrealdb "ra-surrealdb") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Rust Powered Database SurrealDB (It's Pretty Ambitious) - Code to the Moon.\ \ ![Code to the Moon](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Code to the Moon\ \ Content Creator](https://www.youtube.com/watch?v=DPQbuW9dQ7w "Rust Powered Database SurrealDB (It's Pretty Ambitious) - Code to the Moon.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Semantic search in Rust with SurrealDB and Mistral AI\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/semantic-search-in-rust-with-surrealdb-and-mistral-ai "Semantic search in Rust with SurrealDB and Mistral AI") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Semantic search in Rust with SurrealDB and OpenAI\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/semantic-search-with-surrealdb-and-openai "Semantic search in Rust with SurrealDB and OpenAI") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Simple API with Gin/Gonic and SurrealDB (GO).\ \ ![Atharva Deshpande](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Atharva Deshpande\ \ Software Engineer](https://atoo.hashnode.dev/simple-api-with-gingonic-and-surrealdb "Simple API with Gin/Gonic and SurrealDB (GO).") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### Sirqle\ \ ![Tudor Andrei Dumitrascu](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Tudor Andrei Dumitrascu\ \ Lead AI Developer](https://github.com/PythiaSocialTech/sirqle "Sirqle") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### Starter Kit for SurrealDB + Tauri + Next.js.\ \ Examples\ \ Beginner\ \ ![Reymon](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Reymon\ \ Software Engineer](https://github.com/reymom/surrealdb-starter-taurikit "Starter Kit for SurrealDB + Tauri + Next.js.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### Surreal-4o Fine-tuned Model Datasets for SurrealQL Queries - Project to create structured datasets for OpenAI.\ \ AI\ \ ![Sebastian Fritsch](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sebastian Fritsch\ \ Fullstack Developer](https://github.com/sFritsch09/surreal-4o "Surreal-4o Fine-tuned Model Datasets for SurrealQL Queries - Project to create structured datasets for OpenAI.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### surreal-codegen\ \ ![Albert Marashi](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Albert Marashi\ \ Software Engineer](https://github.com/siteforge-io/surreal-codegen "surreal-codegen") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surreal-id\ \ ![Liam Woodleigh-Hardinge](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Liam Woodleigh-Hardinge\ \ Lead Engineer](https://github.com/liamwh/surreal-id "surreal-id") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surreal-ts\ \ ![horvbalint](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### horvbalint\ \ Software Engineer](https://github.com/horvbalint/surreal-ts "surreal-ts") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb/tree/main/crates/sdk "surrealdb") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB Admin\ \ ![Sachin Bhutani](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sachin Bhutani\ \ Software Engineer](https://github.com/sachinbhutani/surrealdb-admin "SurrealDB Admin") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB AI Docs Retrieval - Project to showcase: How to build a GPT-Based question-answering system on top of SurrealDB Docs.\ \ Examples\ \ AI\ \ ![Kyryl Truskovskyi](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Kyryl Truskovskyi\ \ Machine Learning Engineer](https://github.com/truskovskiyk/surrealdb-docs-retrieval "SurrealDB AI Docs Retrieval - Project to showcase: How to build a GPT-Based question-answering system on top of SurrealDB Docs.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB as a Vector Store for LangChain - A Jupyter notebook demonstrating how to use SurrealDB as a Vector Store.\ \ Embedding\ \ Examples\ \ ![Karim Lalani](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Karim Lalani\ \ Software Engineer](https://github.com/lalanikarim/notebooks/blob/main/SurrealDB-Langchain.ipynb "SurrealDB as a Vector Store for LangChain - A Jupyter notebook demonstrating how to use SurrealDB as a Vector Store.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.c\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.c "surrealdb.c") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### surrealdb-client-generator\ \ ![Sebastian Wessel](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sebastian Wessel\ \ Software Engineer](https://github.com/sebastianwessel/surrealdb-client-generator "surrealdb-client-generator") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-cloudflare\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/surrealdb-cloudflare "surrealdb-cloudflare") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.cr\ \ ![Muhammed Yaşar](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Muhammed Yaşar\ \ Backend Crafter](https://github.com/yorci/surrealdb.cr "surrealdb.cr") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-erlang\ \ ![Meppu](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Meppu\ \ Software Engineer](https://github.com/meppu/surreal "surrealdb-erlang") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB Explorer\ \ ![Sam Parton](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sam Parton\ \ Software Engineer](https://github.com/iDevelopThings/SurrealDB-Explorer "SurrealDB Explorer") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-extras\ \ ![Frederik Uni](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Frederik Uni\ \ Software Engineer](https://docs.rs/surrealdb-extras/latest/surrealdb_extras/ "surrealdb-extras") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + FastAPI-NextJS Starter.\ \ Examples\ \ Beginner\ \ ![Mardav Chirag and Richard Orido](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Mardav Chirag and Richard Orido\ \ Software Engineers](https://github.com/richie-omondi/fastapi-nextjs-surrealdb-starterkit "SurrealDB + FastAPI-NextJS Starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Flask Starter.\ \ Examples\ \ Beginner\ \ ![Syed Zubeen](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Syed Zubeen\ \ Software Engineer](https://github.com/syedzubeen/surrealdb_flask_starter_app "SurrealDB + Flask Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-flutter\ \ ![Duhan Balci](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Duhan Balci\ \ Software Engineer](https://github.com/duhanbalci/surrealdb_flutter "surrealdb-flutter") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### SurrealDB Fundamentals Course - Official course to efficiently learn SurrealDB in 3 hours.\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/learn/fundamentals "SurrealDB Fundamentals Course - Official course to efficiently learn SurrealDB in 3 hours.") [![CI/CD](https://surrealdb.com/docs/_astro/ci-cd.light.BDnry5sr_Z1pMSIp.webp)\ \ CI/CD\ \ ### SurrealDB GitHub Action\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/marketplace/actions/surrealdb-in-github-action "SurrealDB GitHub Action") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Go Driver Starter.\ \ Examples\ \ Beginner\ \ ![Salman Shah](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Salman Shah\ \ Software Engineer](https://github.com/sbshah97/surrealdb-go-starter-project "SurrealDB + Go Driver Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.go\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.go "surrealdb.go") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### SurrealDB Grafana datasource\ \ Data Management\ \ ![Grafana Labs](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Grafana Labs\ \ Observability](https://github.com/grafana/surrealdb-datasource "SurrealDB Grafana datasource") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### SurrealDB in 100 seconds.\ \ Beginner\ \ ![Fireship](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Fireship\ \ Content Creator](https://www.youtube.com/watch?v=C7WFwgDRStM "SurrealDB in 100 seconds.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.java\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.java "surrealdb.java") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.js\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.js "surrealdb.js") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Koa starter rest api.\ \ Examples\ \ Beginner\ \ ![Jerempy](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Jerempy\ \ Software Engineer](https://github.com/jerempy/surrealdb-starter-koa "SurrealDB + Koa starter rest api.") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### SurrealDB MCP Server\ \ AI\ \ ![David Whatley](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Whatley\ \ Software Engineer](https://github.com/nsxdavid/surrealdb-mcp-server "SurrealDB MCP Server") [![CI/CD](https://surrealdb.com/docs/_astro/ci-cd.light.BDnry5sr_Z1pMSIp.webp)\ \ CI/CD\ \ ### surrealdb-migrations Action\ \ Data Management\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Bottiau\ \ Software Engineer](https://github.com/marketplace/actions/surrealdb-migrations "surrealdb-migrations Action") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-migrations\ \ Data Management\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Bottiau\ \ Software Engineer](https://github.com/Odonno/surrealdb-migrations "surrealdb-migrations") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.net\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.net "surrealdb.net") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.node\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.node "surrealdb.node") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Nuxt 3 Starter.\ \ Examples\ \ Beginner\ \ ![Dylan Vanmali](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Dylan Vanmali\ \ Software Engineer](https://github.com/dvanmali/surrealdb-nuxt-starter "SurrealDB + Nuxt 3 Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.php\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.php "surrealdb.php") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB Presence Demo - Demo project on how to create a realtime presence web application using SurrealDB Live Queries.\ \ Examples\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Bottiau\ \ Software Engineer](https://github.com/Odonno/surrealdb-presence-demo "SurrealDB Presence Demo - Demo project on how to create a realtime presence web application using SurrealDB Live Queries.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.py\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.py "surrealdb.py") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + React-Nodejs starter.\ \ Examples\ \ Beginner\ \ ![Sai Kiran](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sai Kiran\ \ Software Engineer](https://github.com/MSaiKiran9/react.nodejs_starter "SurrealDB + React-Nodejs starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Rocket.\ \ Examples\ \ Beginner\ \ ![David Zabala](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### David Zabala\ \ Software Engineer](https://github.com/davidzr/surrealdb-rocket-starter "SurrealDB + Rocket.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### SurrealDB - Rust Embedded Database - Quick Tutorial.\ \ Data Management\ \ Beginner\ \ ![Jeremy Chone](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Jeremy Chone\ \ Software Engineer](https://www.youtube.com/watch?v=iOyvum0D3LM "SurrealDB - Rust Embedded Database - Quick Tutorial.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB Sandbox - An offline browser-based playground for experimenting with SurrealDB.\ \ ![Mark Connelly](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Mark Connelly\ \ Software Engineer](https://github.com/plasmatech8/surrealdb-sandbox "SurrealDB Sandbox - An offline browser-based playground for experimenting with SurrealDB.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SolidStart Starter.\ \ Examples\ \ Beginner\ \ ![Sam Zanca](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sam Zanca\ \ Software Engineer](https://github.com/metruzanca/surreal-solid-template "SurrealDB + SolidStart Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-spring-client\ \ ![Mukund Mundhra](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Mukund Mundhra\ \ Software Engineer](https://github.com/Mukund2900/surrealdb-spring-client "surrealdb-spring-client") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SpringBoot.\ \ Examples\ \ Beginner\ \ ![Priyadarshan Singh](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Priyadarshan Singh\ \ Software Engineer](https://github.com/PDROJACK/surrealdb-springboot-starter "SurrealDB + SpringBoot.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Streamlit Starter.\ \ Examples\ \ ![Lucifer Akirami](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Lucifer Akirami\ \ Software Engineer](https://github.com/LuciAkirami/surrealdb-streamlit-starter-kit "SurrealDB + Streamlit Starter.") [![Docker Images](https://surrealdb.com/docs/_astro/docker-images.light.CuHnp7-d_Z11MUAi.webp)\ \ Docker Images\ \ ### surrealdb/surrealdb\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://hub.docker.com/r/surrealdb/surrealdb "surrealdb/surrealdb") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SvelteKit Auth Example.\ \ Examples\ \ Security\ \ ![Albert Marashi](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Albert Marashi\ \ Software Engineer](https://github.com/AlbertMarashi/surrealdb-svelte-auth-template "SurrealDB + SvelteKit Auth Example.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SvelteKit Starter.\ \ Examples\ \ Beginner\ \ ![Jitesh Doshi](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Jitesh Doshi\ \ Software Engineer](https://github.com/spinspire/surrealdb-sveltekit-starter "SurrealDB + SvelteKit Starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SvelteKit.\ \ Examples\ \ Beginner\ \ ![Oskar Gmerek](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Oskar Gmerek\ \ Software Engineer](https://github.com/oskar-gmerek/surreal-sveltekit "SurrealDB + SvelteKit.") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### SurrealDB. The Kitchen Sink Document Store that might dethrone Firebase.\ \ ![Ray Villalobos](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Ray Villalobos\ \ Content Creator](https://www.youtube.com/watch?v=tWpj8Bc_jBQ "SurrealDB. The Kitchen Sink Document Store that might dethrone Firebase.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-valibot\ \ ![Levy van der Valk](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Levy van der Valk\ \ Web Developer](https://github.com/ShadowWolf308/surrealdb-valibot "surrealdb-valibot") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### SurrealDB Vector Store for LangChain\ \ Embedding\ \ ![LangChain](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### LangChain\ \ Open Source Project](https://python.langchain.com/docs/integrations/vectorstores/surrealdb "SurrealDB Vector Store for LangChain") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Vue Blog Starter.\ \ Examples\ \ ![Fadel SrWither](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Fadel SrWither\ \ Software Engineer](https://github.com/SrWither/surrealdb-vuejs "SurrealDB + Vue Blog Starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Vue Starter.\ \ Examples\ \ Beginner\ \ ![Dharani Inkollu](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Dharani Inkollu\ \ Software Engineer](https://github.com/inkollusireeshaadharani/vue-starter-kit "SurrealDB + Vue Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.wasm\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.wasm "surrealdb.wasm") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB x OpenAI - Example of RAG using SurrealDB and OpenAI.\ \ AI\ \ Examples\ \ ![Cellan Hall](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Cellan Hall\ \ MLOps Engineer](https://github.com/Ce11an/surrealdb-openai "SurrealDB x OpenAI - Example of RAG using SurrealDB and OpenAI.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-zod\ \ ![Levy van der Valk](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Levy van der Valk\ \ Web Developer](https://github.com/ShadowWolf308/surrealdb-zod "surrealdb-zod") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb "SurrealDB") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### SurrealDB\_DSL\ \ ![Mnbjhu](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Mnbjhu\ \ Software Engineer](https://github.com/mnbjhu/SurrealDB_DSL "SurrealDB_DSL") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb\_extra\ \ ![Jakin010](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Jakin010\ \ Software Engineer](https://github.com/jakin010/surrealdb_extra "surrealdb_extra") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb\_migration\_engine\ \ Data Management\ \ ![Henry](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Henry\ \ Software Engineer](https://github.com/mcmah309/surrealdb_migration_engine "surrealdb_migration_engine") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb\_query\_builder\ \ ![Ayush Chothe](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Ayush Chothe\ \ Software Engineer](https://github.com/AyushChothe/surrealdb_query_builder "surrealdb_query_builder") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### Surrealised\ \ ![Harry Carpenter](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Harry Carpenter\ \ Software Engineer](https://github.com/ozone-team/surrealised "Surrealised") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealist\ \ ![Kotolex](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Kotolex\ \ Software Engineer](https://github.com/kotolex/surrealist "surrealist") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### Surrealist\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://app.surrealdb.com/ "Surrealist") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealML vs PyTorch vs ONNX - Benchmarking the performance of SurrealML against PyTorch and ONNX - Vladimir Rotariu.\ \ AI\ \ ![Vladimir Rotariu](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Vladimir Rotariu\ \ AI & Backend engineer](https://github.com/vladimirrotariu/surrealml-vs-onnx-vs-pytorch/tree/main "SurrealML vs PyTorch vs ONNX - Benchmarking the performance of SurrealML against PyTorch and ONNX - Vladimir Rotariu.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealReact\ \ ![Rouan van der Ende](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Rouan van der Ende\ \ Software Engineer](https://github.com/rvdende/surrealreact "SurrealReact") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### A Tour of SurrealDB - A course to learn the ins and outs of SurrealDB in 30 minutes.\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/learn/tour "A Tour of SurrealDB - A course to learn the ins and outs of SurrealDB in 30 minutes.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### type-surrealdb\ \ ![Bilux](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Bilux\ \ Software Engineer](https://github.com/ibilux/type-surrealdb "type-surrealdb") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Unlocking SurrealDB: Building a Real-World Multi-Tenant RBAC System Made Easy (4 Part Series).\ \ Security\ \ ![Sebastian Wessel](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Sebastian Wessel\ \ Software Engineer](https://dev.to/sebastian_wessel/series/24535 "Unlocking SurrealDB: Building a Real-World Multi-Tenant RBAC System Made Easy (4 Part Series).") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Use SurrealDB in GitHub Actions\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/using-github-actions "Use SurrealDB in GitHub Actions") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### Use SurrealDB with LangChain\ \ AI\ \ Embedding\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/langchain-surrealdb "Use SurrealDB with LangChain") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Using SurrealDB to prove football statistics.\ \ ![Joseph McCarthy](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### Joseph McCarthy\ \ Content Creator](https://www.youtube.com/watch?v=6J1SPMXzOh4&t=5s "Using SurrealDB to prove football statistics.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Working with SurrealDB over HTTP via Postman\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=tutorials)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman "Working with SurrealDB over HTTP via Postman") --- # Java | SDK | Create a new connection [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/java) [Installation](https://surrealdb.com/docs/sdk/java/installation) [Quick start](https://surrealdb.com/docs/sdk/java/start) [Data types](https://surrealdb.com/docs/sdk/java/data-types) [API Documentation](https://surrealdb.com/docs/sdk/java/api-documentation) Concepts [Introduction](https://surrealdb.com/docs/sdk/java/core) [Create a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/java/core/handling-authentication) [surrealdb/surrealdb.java](https://github.com/surrealdb/surrealdb.java) [](https://surrealdb.com/docs/sdk/java) [SDK Concepts](https://surrealdb.com/docs/sdk/java/core) [Create a new connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection) Create a new connection[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#create-a-new-connection) ==================================================================================================================== The first step towards interacting with SurrealDB is to create a new connection to a database instance. This involves initializing a new instance of the `Surreal` class and connecting it to a database endpoint. You can then switch to a specific namespace and database, and pass required authentication credentials. This guide will walk you through the process of creating a new connection to a SurrealDB instance using the Java SDK. Related Methods[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#related-methods) ---------------------------------------------------------------------------------------------------- | Method | Description | | --- | --- | | [`driver.connect(String url)`](https://surrealdb.github.io/surrealdb.java/javadoc/com/surrealdb/Surreal.html#connect(java.lang.String)) | Connects to a local, remote, or embedded database endpoint | | [`driver.close()`](https://surrealdb.github.io/surrealdb.java/javadoc/com/surrealdb/Surreal.html#close()) | Closes the persistent connection to the database | | [`driver.useNs(String namespace)`](https://surrealdb.github.io/surrealdb.java/javadoc/com/surrealdb/Surreal.html#useNs(java.lang.String)) | Switch to a specific namespace | | [`driver.useDb(String database)`](https://surrealdb.github.io/surrealdb.java/javadoc/com/surrealdb/Surreal.html#useDb(java.lang.String)) | Switch to a specific database | Opening a connection[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#opening-a-connection) -------------------------------------------------------------------------------------------------------------- The `.connect()` method accepts a `String` pointing to the desired local or remote instance. Since the Java SDK supports running embedded databases, you can also specify an embedded endpoint such as `memory://` and `surrealkv://`. ### Supported protocols[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#supported-protocols) This is the complete list of supported connection protocols * `http://` - Plain HTTP * `https://` - Secure HTTP * `ws://` - Plain WebSocket * `wss://` - Secure WebSocket * `memory://` - In-memory database * `surrealkv://` - Disk-based database * `surrealkv+versioned://` - Disk-based database (with temporal data) Selecting a namespace and database[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#selecting-a-namespace-and-database) ------------------------------------------------------------------------------------------------------------------------------------------ Depending on the complexity of your use case, you can switch to a specific namespace and database using the `.useNs()` and `.useDb()` methods. This is particularly useful if you want to switch to a different setup after connecting. You can also stay in the same namespace but switch to a different database. ### Example usage[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#example-usage) driver.useNs("surrealdb").useDb("docs"); Closing the connection[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#closing-the-connection) ------------------------------------------------------------------------------------------------------------------ The `.close()` method closes the persistent connection to the database. You should always call this method when you are done with the connection to free up resources. You can use a try-with-resources block to ensure that the connection is closed automatically when the block is exited. // Close the connection manually driver.close(); // Using try-with-resources try (Surreal driver = new Surreal()) { // ... } Example[](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#example) ------------------------------------------------------------------------------------ Here is an example of the `.connect()`, `.useNs()`, `.useDb()`, and `.close()` methods in action. Example.javapackage com.surrealdb.example; import com.surrealdb.Surreal; public class Example { public static void main(String[] args) { try (final Surreal driver = new Surreal()) { // Connect to an in-memory database driver.connect("memory"); // Select a namespace and database driver.useNs("surrealdb").useDb("docs"); } } } [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/core/create-a-new-connection.mdx) Copy page as Markdown [Concepts](https://surrealdb.com/docs/sdk/java/core) [Handle authentication](https://surrealdb.com/docs/sdk/java/core/handling-authentication) ### On this page * [Related Methods](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#related-methods) * [Opening a connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#opening-a-connection) * [Supported protocols](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#supported-protocols) * [Selecting a namespace and database](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#selecting-a-namespace-and-database) * [Example usage](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#example-usage) * [Closing the connection](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#closing-the-connection) * [Example](https://surrealdb.com/docs/sdk/java/core/create-a-new-connection#example) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-java/core/create-a-new-connection.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Working with SurrealDB over HTTP via Postman | Tutorials [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Tutorials](https://surrealdb.com/docs/tutorials) [Integrate Auth0 as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider) [Integrate AWS Cognito as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider) [Use SurrealDB in GitHub Actions](https://surrealdb.com/docs/tutorials/using-github-actions) [Connect to SurrealDB via Ngrok tunnel](https://surrealdb.com/docs/tutorials/connect-to-surrealdb-via-ngrok-tunnel) [Working with SurrealDB over HTTP via Postman](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman) [Define a Schema in SurrealDB](https://surrealdb.com/docs/tutorials/define-a-schema) [Build a realtime presence web application](https://surrealdb.com/docs/tutorials/build-a-realtime-presence-web-application-using-surrealdb-live-queries) [surrealdb/examples](https://github.com/surrealdb/examples) [](https://surrealdb.com/docs/tutorials) [Working with SurrealDB over HTTP via Postman](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman) Working with SurrealDB over HTTP via Postman[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#working-with-surrealdb-over-http-via-postman) =============================================================================================================================================================================== SurrealDB provides a RESTful HTTP API for interacting with the database. In this tutorial, you will learn how to query SurrealDB endpoints via the [Postman collection](https://postman.com/surrealdb/workspace/surrealdb/collection/19100500-3da237f3-588b-4252-8882-6d487c11116a) . SurrealDB supports requests via HTTP & Rest with which you can handle different queries from creating simple tables to having Graph relationships between complex tables. Check out the [HTTP & Rest integration documentation](https://surrealdb.com/docs/surrealdb/integration/http)  for a detailed list of all the endpoints supported. Prerequisites[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#prerequisites) ----------------------------------------------------------------------------------------------------------------- This tutorial assumes that you have the following: * A Postman account to fork the [SurrealDB collection](https://postman.com/surrealdb/workspace/surrealdb/collection/19100500-3da237f3-588b-4252-8882-6d487c11116a) * SurrealDB installed - If you do not, see the [installation guide](https://surrealdb.com/docs/surrealdb/installation) specific to your machine. Getting Started.[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#getting-started) ---------------------------------------------------------------------------------------------------------------------- Before forking the Collection from Postman, ensure that you have your SurrealDB instance running locally because by default the collection endpoint is set to [`localhost:8000`](http://localhost:8000/) or [`http://127.0.0.1:8000`](http://127.0.0.1:8000/) To do so run the [Start command](https://surrealdb.com/docs/surrealdb/cli/start) in your terminal: surreal start --user root --pass secret The above command starts SurrealDB with [authentication](https://surrealdb.com/docs/surrealdb/cli/start#authentication) and specifies that the user and password are `root`. Note that this is just for demonstration purposes. You can replace `root` in both instances with any other value. After running locally, head over to Postman and create a fork for your workspace. You should see all the endpoints listed. You can also [check the endpoints in the documentation](https://surrealdb.com/docs/surrealdb/integration/http) . Using the `INFO` Statement[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#using-the-info-statement) ----------------------------------------------------------------------------------------------------------------------------------------- You can get information about your workspace by using the INFO statement. However, you need the right permissions in order to see the output. See the documentation for the [INFO statement](https://surrealdb.com/docs/surrealql/statements/info) . It is also important to note that the `namespace` and `database` values for the Postman collection are set to `test` by default. You can see the JSON output using the [INFO statement](https://surrealdb.com/docs/surrealql/statements/info) in the body of the request. INFO FOR ROOT / DB / NS Below is the output of querying for the info of the current namespace. INFO FOR NS [\ {\ "result": {\ "accesses": {},\ "databases": {\ "test": "DEFINE DATABASE test"\ },\ "users": {}\ },\ "status": "OK",\ "time": "190.166µs"\ }\ ] Defining a Record User[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#defining-a-record-user) ----------------------------------------------------------------------------------------------------------------------------------- To define a [record user](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) , first navigate to the `POST /sql` endpoint and in the header of the request add the following fields: Accept:application/json NS:{{namespace}} DB:{{database}} You can also [define](https://surrealdb.com/docs/surrealql/statements/define/user) a [system user](https://surrealdb.com/docs/surrealdb/security/authentication#system-users) with other credentials such as Root or Database user. For this tutorial, we will be using only [record users](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) . > Note > > The Namespace and Database fields are set to the value `test` by default. You can change this in the collection settings. In the request body, [define the record access method](https://surrealdb.com/docs/surrealql/statements/define/access/record) `human` with a session of 24 hours: DEFINE ACCESS human ON DATABASE TYPE RECORD SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) ) SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) DURATION FOR SESSION 24h ; The code above allows a user to sign up as a record user with their email and password. Then hash the password with the [`crypto::argon2::generate`](https://surrealdb.com/docs/surrealql/functions/database/crypto#cryptoargon2generate) function The sign in logic gets all the users where the emails match and uses the [`crypto::argon2::compare`](https://surrealdb.com/docs/surrealql/functions/database/crypto#cryptoargon2compare) function to check the hash value to the unhashed. Now when you get the info of the Database using `INFO for DB` you can see the access method `human` in the following output: [\ {\ "result": null,\ "status": "OK",\ "time": "102.458µs"\ },\ {\ "result": {\ "accesses": {\ "human": "DEFINE ACCESS human ON DATABASE TYPE RECORD SIGNUP (CREATE user SET email = $email, pass = crypto::argon2::generate($pass)) SIGNIN (SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass)) DURATION FOR TOKEN 1h, FOR SESSION 1d"\ },\ "analyzers": {},\ "functions": {},\ "models": {},\ "params": {},\ "tables": {},\ "users": {}\ },\ "status": "OK",\ "time": "78.084µs"\ }\ ] Signing Up a New Scope user[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#signing-up-a-new-scope-user) --------------------------------------------------------------------------------------------------------------------------------------------- After defining the `signup` and `signin` logic head over to the `POST /signup` endpoint to signup and in the header of the request add the following fields: Accept:application/json namespace:test database:test access:human In the body of the request add the following information: { "ns": "test", "db": "test", "ac": "human", "email": "test@surreal.com", "pass":"1234567886" } The result will be a JSON object: { "code": 200, "details": "Authentication succeeded", "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE3MDcxNDY2NTgsIm5iZiI6MTcwNzE0NjY1OCwiZXhwIjoxNzA3MjMzMDU4LCJpc3MiOiJTdXJyZWFsREIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJBQyI6Imh1bWFuIiwiSUQiOiJ1c2VyOnc2ZzBsNmh5eHpjZzlubTY2dGVjIn0.8Gud51cocThB8DMKD1zovtGiVgf5L1dAS6-pjWb6Lm6a7-4Spp7xXjD7JrHHdtJVNX1O0d8GdjZwRGTsP_NM9A" } Signing in a record user[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#signing-in-a-record-user) --------------------------------------------------------------------------------------------------------------------------------------- Now that we have defined a User we can now log in. To do so, head to the `POST /signin` and using the same credentials. In the body of the request, add the same information you used to sign up: { "ns": "test", "db": "test", "ac": "human", "email": "test3@surreal.com", "pass":"1234567886" } This should return a similar JSON object indicating successful authentication and providing a new token. { "code": 200, "details": "Authentication succeeded", "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE3MDcxNDY2NTgsIm5iZiI6MTcwNzE0NjY1OCwiZXhwIjoxNzA3MjMzMDU4LCJpc3MiOiJTdXJyZWFsREIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJBQyI6Imh1bWFuIiwiSUQiOiJ1c2VyOnc2ZzBsNmh5eHpjZzlubTY2dGVjIn0.8Gud51cocThB8DMKD1zovtGiVgf5L1dAS6-pjWb6Lm6a7-4Spp7xXjD7JrHHdtJVNX1O0d8GdjZwRGTsP_NM9A" } Using Table Endpoints[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#using-table-endpoints) --------------------------------------------------------------------------------------------------------------------------------- The Postman collection has a couple of endpoints for `POST` `GET` `PUT` `PATCH` `DEL` operations. For example, If you want to add a new entry to the table `Person` (Which is the default table in the collection) go to the [`POST /key/:table`](https://surrealdb.com/docs/surrealdb/integration/http#post-table) endpoint then in a body of the request and add the table content in the body of the request. E.g: { age: 32, name: 'John' } You should get a response as seen below, notice that there is a [record ID](https://surrealdb.com/docs/surrealql/datamodel/ids) , with this record ID you can now use any of the [`/key/:table/:id`](https://surrealdb.com/docs/surrealdb/integration/http#get-record) endpoints for `POST` `GET` `PUT` `PATCH` `DEL` operations. [\ {\ "result": [\ {\ "age": 32,\ "id": "person:p1cdf6cx89gnfboq5wye",\ "name": "John"\ }\ ],\ "status": "OK",\ "time": "105.667µs"\ }\ ] Conclusion[](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#conclusion) ----------------------------------------------------------------------------------------------------------- In this tutorial, we’ve walked through how to set up and use SurrealDB over HTTP using Postman. We’ve covered how to define a new record access method, sign up a new user, and sign in with a user. These steps are crucial for managing user authentication in your applications using SurrealDB. The Postman collection is still in active development for more information on using surrealDB via the HTTP endpoints. [Check out our documentation on HTTP and rest endpoints](https://surrealdb.com/docs/surrealdb/integration/http) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-tutorials/working-with-surrealdb-over-http-via-postman.mdx) Copy page as Markdown [Connect to SurrealDB via Ngrok tunnel](https://surrealdb.com/docs/tutorials/connect-to-surrealdb-via-ngrok-tunnel) [Define a Schema in SurrealDB](https://surrealdb.com/docs/tutorials/define-a-schema) ### On this page * [Prerequisites](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#prerequisites) * [Getting Started.](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#getting-started) * [Using the INFO Statement](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#using-the-info-statement) * [Defining a Record User](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#defining-a-record-user) * [Signing Up a New Scope user](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#signing-up-a-new-scope-user) * [Signing in a record user](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#signing-in-a-record-user) * [Using Table Endpoints](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#using-table-endpoints) * [Conclusion](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman#conclusion) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-tutorials/working-with-surrealdb-over-http-via-postman.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # SurrealDB Labs [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) ![SurrealDB](https://surrealdb.com/docs/_astro/labs.mRExFNtU_e1mvt.svg) Dive into the full range of tools, libraries, and integrations built around SurrealDB. This page brings together first- and third-party resources—including client SDKs, videos, development tools, learning materials, and more—to help you build, manage, and extend your SurrealDB applications with ease. Whether you're getting started or scaling up, find everything you need to support your journey with SurrealDB. [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Aeon's Surreal Renaissance - Official book for in-depth learning through storytelling.\ \ Examples\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/learn/book "Aeon's Surreal Renaissance - Official book for in-depth learning through storytelling.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### allographer\ \ ![Itsumura H.](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Itsumura H.\ \ Software Engineer](https://github.com/itsumura-h/nim-allographer "allographer") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### .NET Aspire Integration\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Bottiau\ \ Software Engineer](https://www.nuget.org/packages/CommunityToolkit.Aspire.Hosting.SurrealDb ".NET Aspire Integration") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### AspNetCore.HealthChecks.SurrealDb\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Bottiau\ \ Software Engineer](https://www.nuget.org/packages/AspNetCore.HealthChecks.SurrealDb "AspNetCore.HealthChecks.SurrealDb") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### awaited-surrealdb\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/awaited-surrealdb "awaited-surrealdb") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Beyond Surreal? A closer look at NewSQL Relational Data - Beyond Fireship.\ \ Beginner\ \ Examples\ \ ![Fireship](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Fireship\ \ Content Creator - Software Engineer](https://www.youtube.com/watch?v=LCAIkx1p1k0 "Beyond Surreal? A closer look at NewSQL Relational Data - Beyond Fireship.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Blink - Build a Notion clone with SurrealDB and WebAssembly engine\ \ Beginner\ \ Examples\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/kearfy/blink "Blink - Build a Notion clone with SurrealDB and WebAssembly engine") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Build a realtime presence web application using SurrealDB Live Queries\ \ Beginner\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Bottiau\ \ Software Engineer](https://surrealdb.com/docs/tutorials/build-a-realtime-presence-web-application-using-surrealdb-live-queries "Build a realtime presence web application using SurrealDB Live Queries") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Build an AI RAG Agent\ \ AI\ \ Examples\ \ ![Karim Lalani](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Karim Lalani\ \ Software Engineer](https://github.com/colinmcnamara/austin_langchain/blob/main/labs/LangChain_104/104-langgraph-rag-agent.ipynb "Build an AI RAG Agent") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### CLI phone book in Python using SurrealDB as database.\ \ Data Management\ \ Examples\ \ ![Gabor Szabo](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Gabor Szabo\ \ Software Engineer](https://code-maven.com/surrealdb-python-cli-phonebook "CLI phone book in Python using SurrealDB as database.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Connect to SurrealDB via Ngrok tunnel\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/connect-to-surrealdb-via-ngrok-tunnel "Connect to SurrealDB via Ngrok tunnel") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Define a Schema in SurrealDB\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/define-a-schema "Define a Schema in SurrealDB") [![Deployment Tools](https://surrealdb.com/docs/_astro/deployment-tools.light.BnweCPOT_1rordJ.webp)\ \ Deployment Tools\ \ ### Dokku Surrealdb\ \ ![Diptesh Choudhuri](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Diptesh Choudhuri\ \ Software Engineer](https://github.com/IgnisDa/dokku-surrealdb "Dokku Surrealdb") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Getting started with SurrealDB! Future of cloud databases (maybe)?\ \ Beginner\ \ ![Chris Hay](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Chris Hay\ \ Content Creator](https://www.youtube.com/watch?v=D41jb4DDIdA "Getting started with SurrealDB! Future of cloud databases (maybe)?") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Getting started with SurrealDB using Python and Docker.\ \ Beginner\ \ ![Gabor Szabo](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Gabor Szabo\ \ Software Engineer](https://code-maven.com/surrealdb-getting-started "Getting started with SurrealDB using Python and Docker.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Getting started with SurrealDB.\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/getting-started-with-surrealdb "Getting started with SurrealDB.") [![Deployment Tools](https://surrealdb.com/docs/_astro/deployment-tools.light.BnweCPOT_1rordJ.webp)\ \ Deployment Tools\ \ ### GKE using Terraform\ \ Data Management\ \ ![Dylan Vanmali](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Dylan Vanmali\ \ Software Engineer](https://github.com/dvanmali/terraform-google-surrealdb "GKE using Terraform") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Hosting Surreal DB in Rust in Less Than 3 Minutes.\ \ Beginner\ \ ![Gui Bibeau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Gui Bibeau\ \ Software Engineer](https://www.youtube.com/watch?v=VoRoeL1tal4 "Hosting Surreal DB in Rust in Less Than 3 Minutes.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### How to Use SurrealDb with the Fresh Framework and Deno.\ \ Examples\ \ ![Rajdeep Singh](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Rajdeep Singh\ \ Software Engineer](https://www.freecodecamp.org/news/how-to-use-surrealdb-with-fresh-framework/ "How to Use SurrealDb with the Fresh Framework and Deno.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Improve database management with SurrealDB.\ \ Data Management\ \ ![Alexander Nnakwue](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Alexander Nnakwue\ \ Software Engineer](https://blog.logrocket.com/improve-database-management-surrealdb/ "Improve database management with SurrealDB.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Integrate Auth0 as an Authentication Provider\ \ Security\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider "Integrate Auth0 as an Authentication Provider") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Integrate AWS Cognito as an Authentication Provider\ \ Security\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider "Integrate AWS Cognito as an Authentication Provider") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### IoT telemetry example\ \ Examples\ \ ![Martin Schaer](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Martin Schaer\ \ Solutions Engineer](https://github.com/surrealdb/example-iot-telemetry "IoT telemetry example") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Kards Social - FOSS social media app.\ \ Examples\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/kards-social "Kards Social - FOSS social media app.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### kysely-surrealdb\ \ ![Igal Klebanov](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Igal Klebanov\ \ Software Engineer](https://github.com/igalklebanov/kysely-surrealdb "kysely-surrealdb") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### LangChain integration\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/integrations/frameworks/langchain "LangChain integration") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Livestream series documenting learning SurrealDB.\ \ ![Xkonti](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Xkonti\ \ Content Creator](https://www.youtube.com/playlist?list=PL5AVzKSngnt_xPGNuYdrbB7NZtJbQ046a "Livestream series documenting learning SurrealDB.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Make a GenAI chatbot using GraphRAG with SurrealDB + LangChain\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/make-a-genai-chatbot-using-graphrag-with-surrealdb-langchain "Make a GenAI chatbot using GraphRAG with SurrealDB + LangChain") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Make a medical chatbot in Rust using GraphRAG with SurrealDB + LangChain\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/make-a-medical-chatbot-using-graphrag-with-surrealdb-langchain "Make a medical chatbot in Rust using GraphRAG with SurrealDB + LangChain") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Minimal LangChain chatbot example with vector and graph\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/minimal-langchain-chatbot-example-with-vector-and-graph "Minimal LangChain chatbot example with vector and graph") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Nextjs + surrealdb demo - Basic blog that serves as a demo / template for your nextjs + surrealdb project.\ \ Examples\ \ Beginner\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/kearfy/demo-nextjs-surrealdb "Nextjs + surrealdb demo - Basic blog that serves as a demo / template for your nextjs + surrealdb project.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Playrbase - Event & player management system.\ \ Examples\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/playrbase "Playrbase - Event & player management system.") [![Deployment Tools](https://surrealdb.com/docs/_astro/deployment-tools.light.BnweCPOT_1rordJ.webp)\ \ Deployment Tools\ \ ### Pterodactyl Egg\ \ ![Stefan](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Stefan\ \ Freelance Developer](https://github.com/Stefanuk12/Pterodactyl/blob/master/eggs/misc/egg-surrealdb.json "Pterodactyl Egg") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### PySurrealDB\ \ ![Mike Aurelion](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Mike Aurelion\ \ Software Engineer](https://github.com/aurelion314/pysurrealdb "PySurrealDB") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### ra-surrealdb\ \ ![Moïse Valvassori](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Moïse Valvassori\ \ Software Engineer](https://github.com/djedi23/ra-surrealdb "ra-surrealdb") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Rust Powered Database SurrealDB (It's Pretty Ambitious) - Code to the Moon.\ \ ![Code to the Moon](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Code to the Moon\ \ Content Creator](https://www.youtube.com/watch?v=DPQbuW9dQ7w "Rust Powered Database SurrealDB (It's Pretty Ambitious) - Code to the Moon.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Semantic search in Rust with SurrealDB and Mistral AI\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/semantic-search-in-rust-with-surrealdb-and-mistral-ai "Semantic search in Rust with SurrealDB and Mistral AI") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### Semantic search in Rust with SurrealDB and OpenAI\ \ AI\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/blog/semantic-search-with-surrealdb-and-openai "Semantic search in Rust with SurrealDB and OpenAI") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Simple API with Gin/Gonic and SurrealDB (GO).\ \ ![Atharva Deshpande](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Atharva Deshpande\ \ Software Engineer](https://atoo.hashnode.dev/simple-api-with-gingonic-and-surrealdb "Simple API with Gin/Gonic and SurrealDB (GO).") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### Sirqle\ \ ![Tudor Andrei Dumitrascu](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Tudor Andrei Dumitrascu\ \ Lead AI Developer](https://github.com/PythiaSocialTech/sirqle "Sirqle") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### Starter Kit for SurrealDB + Tauri + Next.js.\ \ Examples\ \ Beginner\ \ ![Reymon](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Reymon\ \ Software Engineer](https://github.com/reymom/surrealdb-starter-taurikit "Starter Kit for SurrealDB + Tauri + Next.js.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### Surreal-4o Fine-tuned Model Datasets for SurrealQL Queries - Project to create structured datasets for OpenAI.\ \ AI\ \ ![Sebastian Fritsch](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sebastian Fritsch\ \ Fullstack Developer](https://github.com/sFritsch09/surreal-4o "Surreal-4o Fine-tuned Model Datasets for SurrealQL Queries - Project to create structured datasets for OpenAI.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### surreal-codegen\ \ ![Albert Marashi](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Albert Marashi\ \ Software Engineer](https://github.com/siteforge-io/surreal-codegen "surreal-codegen") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surreal-id\ \ ![Liam Woodleigh-Hardinge](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Liam Woodleigh-Hardinge\ \ Lead Engineer](https://github.com/liamwh/surreal-id "surreal-id") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surreal-ts\ \ ![horvbalint](https://surrealdb.com/docs/labs?filters=examples)\ \ #### horvbalint\ \ Software Engineer](https://github.com/horvbalint/surreal-ts "surreal-ts") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb/tree/main/crates/sdk "surrealdb") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB Admin\ \ ![Sachin Bhutani](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sachin Bhutani\ \ Software Engineer](https://github.com/sachinbhutani/surrealdb-admin "SurrealDB Admin") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB AI Docs Retrieval - Project to showcase: How to build a GPT-Based question-answering system on top of SurrealDB Docs.\ \ Examples\ \ AI\ \ ![Kyryl Truskovskyi](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Kyryl Truskovskyi\ \ Machine Learning Engineer](https://github.com/truskovskiyk/surrealdb-docs-retrieval "SurrealDB AI Docs Retrieval - Project to showcase: How to build a GPT-Based question-answering system on top of SurrealDB Docs.") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB as a Vector Store for LangChain - A Jupyter notebook demonstrating how to use SurrealDB as a Vector Store.\ \ Embedding\ \ Examples\ \ ![Karim Lalani](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Karim Lalani\ \ Software Engineer](https://github.com/lalanikarim/notebooks/blob/main/SurrealDB-Langchain.ipynb "SurrealDB as a Vector Store for LangChain - A Jupyter notebook demonstrating how to use SurrealDB as a Vector Store.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.c\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.c "surrealdb.c") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### surrealdb-client-generator\ \ ![Sebastian Wessel](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sebastian Wessel\ \ Software Engineer](https://github.com/sebastianwessel/surrealdb-client-generator "surrealdb-client-generator") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-cloudflare\ \ ![Micha de Vries](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Micha de Vries\ \ Software Engineer](https://github.com/theopensource-company/surrealdb-cloudflare "surrealdb-cloudflare") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.cr\ \ ![Muhammed Yaşar](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Muhammed Yaşar\ \ Backend Crafter](https://github.com/yorci/surrealdb.cr "surrealdb.cr") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-erlang\ \ ![Meppu](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Meppu\ \ Software Engineer](https://github.com/meppu/surreal "surrealdb-erlang") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB Explorer\ \ ![Sam Parton](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sam Parton\ \ Software Engineer](https://github.com/iDevelopThings/SurrealDB-Explorer "SurrealDB Explorer") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-extras\ \ ![Frederik Uni](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Frederik Uni\ \ Software Engineer](https://docs.rs/surrealdb-extras/latest/surrealdb_extras/ "surrealdb-extras") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + FastAPI-NextJS Starter.\ \ Examples\ \ Beginner\ \ ![Mardav Chirag and Richard Orido](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Mardav Chirag and Richard Orido\ \ Software Engineers](https://github.com/richie-omondi/fastapi-nextjs-surrealdb-starterkit "SurrealDB + FastAPI-NextJS Starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Flask Starter.\ \ Examples\ \ Beginner\ \ ![Syed Zubeen](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Syed Zubeen\ \ Software Engineer](https://github.com/syedzubeen/surrealdb_flask_starter_app "SurrealDB + Flask Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-flutter\ \ ![Duhan Balci](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Duhan Balci\ \ Software Engineer](https://github.com/duhanbalci/surrealdb_flutter "surrealdb-flutter") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### SurrealDB Fundamentals Course - Official course to efficiently learn SurrealDB in 3 hours.\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/learn/fundamentals "SurrealDB Fundamentals Course - Official course to efficiently learn SurrealDB in 3 hours.") [![CI/CD](https://surrealdb.com/docs/_astro/ci-cd.light.BDnry5sr_Z1pMSIp.webp)\ \ CI/CD\ \ ### SurrealDB GitHub Action\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/marketplace/actions/surrealdb-in-github-action "SurrealDB GitHub Action") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Go Driver Starter.\ \ Examples\ \ Beginner\ \ ![Salman Shah](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Salman Shah\ \ Software Engineer](https://github.com/sbshah97/surrealdb-go-starter-project "SurrealDB + Go Driver Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.go\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.go "surrealdb.go") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### SurrealDB Grafana datasource\ \ Data Management\ \ ![Grafana Labs](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Grafana Labs\ \ Observability](https://github.com/grafana/surrealdb-datasource "SurrealDB Grafana datasource") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### SurrealDB in 100 seconds.\ \ Beginner\ \ ![Fireship](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Fireship\ \ Content Creator](https://www.youtube.com/watch?v=C7WFwgDRStM "SurrealDB in 100 seconds.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.java\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.java "surrealdb.java") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.js\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.js "surrealdb.js") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Koa starter rest api.\ \ Examples\ \ Beginner\ \ ![Jerempy](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Jerempy\ \ Software Engineer](https://github.com/jerempy/surrealdb-starter-koa "SurrealDB + Koa starter rest api.") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### SurrealDB MCP Server\ \ AI\ \ ![David Whatley](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Whatley\ \ Software Engineer](https://github.com/nsxdavid/surrealdb-mcp-server "SurrealDB MCP Server") [![CI/CD](https://surrealdb.com/docs/_astro/ci-cd.light.BDnry5sr_Z1pMSIp.webp)\ \ CI/CD\ \ ### surrealdb-migrations Action\ \ Data Management\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Bottiau\ \ Software Engineer](https://github.com/marketplace/actions/surrealdb-migrations "surrealdb-migrations Action") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-migrations\ \ Data Management\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Bottiau\ \ Software Engineer](https://github.com/Odonno/surrealdb-migrations "surrealdb-migrations") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.net\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.net "surrealdb.net") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.node\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.node "surrealdb.node") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Nuxt 3 Starter.\ \ Examples\ \ Beginner\ \ ![Dylan Vanmali](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Dylan Vanmali\ \ Software Engineer](https://github.com/dvanmali/surrealdb-nuxt-starter "SurrealDB + Nuxt 3 Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.php\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.php "surrealdb.php") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB Presence Demo - Demo project on how to create a realtime presence web application using SurrealDB Live Queries.\ \ Examples\ \ ![David Bottiau](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Bottiau\ \ Software Engineer](https://github.com/Odonno/surrealdb-presence-demo "SurrealDB Presence Demo - Demo project on how to create a realtime presence web application using SurrealDB Live Queries.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.py\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.py "surrealdb.py") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + React-Nodejs starter.\ \ Examples\ \ Beginner\ \ ![Sai Kiran](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sai Kiran\ \ Software Engineer](https://github.com/MSaiKiran9/react.nodejs_starter "SurrealDB + React-Nodejs starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Rocket.\ \ Examples\ \ Beginner\ \ ![David Zabala](https://surrealdb.com/docs/labs?filters=examples)\ \ #### David Zabala\ \ Software Engineer](https://github.com/davidzr/surrealdb-rocket-starter "SurrealDB + Rocket.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### SurrealDB - Rust Embedded Database - Quick Tutorial.\ \ Data Management\ \ Beginner\ \ ![Jeremy Chone](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Jeremy Chone\ \ Software Engineer](https://www.youtube.com/watch?v=iOyvum0D3LM "SurrealDB - Rust Embedded Database - Quick Tutorial.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB Sandbox - An offline browser-based playground for experimenting with SurrealDB.\ \ ![Mark Connelly](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Mark Connelly\ \ Software Engineer](https://github.com/plasmatech8/surrealdb-sandbox "SurrealDB Sandbox - An offline browser-based playground for experimenting with SurrealDB.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SolidStart Starter.\ \ Examples\ \ Beginner\ \ ![Sam Zanca](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sam Zanca\ \ Software Engineer](https://github.com/metruzanca/surreal-solid-template "SurrealDB + SolidStart Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb-spring-client\ \ ![Mukund Mundhra](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Mukund Mundhra\ \ Software Engineer](https://github.com/Mukund2900/surrealdb-spring-client "surrealdb-spring-client") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SpringBoot.\ \ Examples\ \ Beginner\ \ ![Priyadarshan Singh](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Priyadarshan Singh\ \ Software Engineer](https://github.com/PDROJACK/surrealdb-springboot-starter "SurrealDB + SpringBoot.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Streamlit Starter.\ \ Examples\ \ ![Lucifer Akirami](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Lucifer Akirami\ \ Software Engineer](https://github.com/LuciAkirami/surrealdb-streamlit-starter-kit "SurrealDB + Streamlit Starter.") [![Docker Images](https://surrealdb.com/docs/_astro/docker-images.light.CuHnp7-d_Z11MUAi.webp)\ \ Docker Images\ \ ### surrealdb/surrealdb\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://hub.docker.com/r/surrealdb/surrealdb "surrealdb/surrealdb") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SvelteKit Auth Example.\ \ Examples\ \ Security\ \ ![Albert Marashi](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Albert Marashi\ \ Software Engineer](https://github.com/AlbertMarashi/surrealdb-svelte-auth-template "SurrealDB + SvelteKit Auth Example.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SvelteKit Starter.\ \ Examples\ \ Beginner\ \ ![Jitesh Doshi](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Jitesh Doshi\ \ Software Engineer](https://github.com/spinspire/surrealdb-sveltekit-starter "SurrealDB + SvelteKit Starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + SvelteKit.\ \ Examples\ \ Beginner\ \ ![Oskar Gmerek](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Oskar Gmerek\ \ Software Engineer](https://github.com/oskar-gmerek/surreal-sveltekit "SurrealDB + SvelteKit.") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### SurrealDB. The Kitchen Sink Document Store that might dethrone Firebase.\ \ ![Ray Villalobos](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Ray Villalobos\ \ Content Creator](https://www.youtube.com/watch?v=tWpj8Bc_jBQ "SurrealDB. The Kitchen Sink Document Store that might dethrone Firebase.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-valibot\ \ ![Levy van der Valk](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Levy van der Valk\ \ Web Developer](https://github.com/ShadowWolf308/surrealdb-valibot "surrealdb-valibot") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### SurrealDB Vector Store for LangChain\ \ Embedding\ \ ![LangChain](https://surrealdb.com/docs/labs?filters=examples)\ \ #### LangChain\ \ Open Source Project](https://python.langchain.com/docs/integrations/vectorstores/surrealdb "SurrealDB Vector Store for LangChain") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Vue Blog Starter.\ \ Examples\ \ ![Fadel SrWither](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Fadel SrWither\ \ Software Engineer](https://github.com/SrWither/surrealdb-vuejs "SurrealDB + Vue Blog Starter.") [![Templates](https://surrealdb.com/docs/_astro/templates.light.BG-xm1DG_ZDxKz8.webp)\ \ Templates\ \ ### SurrealDB + Vue Starter.\ \ Examples\ \ Beginner\ \ ![Dharani Inkollu](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Dharani Inkollu\ \ Software Engineer](https://github.com/inkollusireeshaadharani/vue-starter-kit "SurrealDB + Vue Starter.") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### surrealdb.wasm\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb.wasm "surrealdb.wasm") [![Demos](https://surrealdb.com/docs/_astro/demos.light.QNbJPI8S_Z277QKg.webp)\ \ Demos\ \ ### SurrealDB x OpenAI - Example of RAG using SurrealDB and OpenAI.\ \ AI\ \ Examples\ \ ![Cellan Hall](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Cellan Hall\ \ MLOps Engineer](https://github.com/Ce11an/surrealdb-openai "SurrealDB x OpenAI - Example of RAG using SurrealDB and OpenAI.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb-zod\ \ ![Levy van der Valk](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Levy van der Valk\ \ Web Developer](https://github.com/ShadowWolf308/surrealdb-zod "surrealdb-zod") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealDB\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/surrealdb "SurrealDB") [![SDKs](https://surrealdb.com/docs/_astro/sdks.light.Drlu1Byh_Z1lhHNF.webp)\ \ SDKs\ \ ### SurrealDB\_DSL\ \ ![Mnbjhu](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Mnbjhu\ \ Software Engineer](https://github.com/mnbjhu/SurrealDB_DSL "SurrealDB_DSL") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb\_extra\ \ ![Jakin010](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Jakin010\ \ Software Engineer](https://github.com/jakin010/surrealdb_extra "surrealdb_extra") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb\_migration\_engine\ \ Data Management\ \ ![Henry](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Henry\ \ Software Engineer](https://github.com/mcmah309/surrealdb_migration_engine "surrealdb_migration_engine") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealdb\_query\_builder\ \ ![Ayush Chothe](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Ayush Chothe\ \ Software Engineer](https://github.com/AyushChothe/surrealdb_query_builder "surrealdb_query_builder") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### Surrealised\ \ ![Harry Carpenter](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Harry Carpenter\ \ Software Engineer](https://github.com/ozone-team/surrealised "Surrealised") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### surrealist\ \ ![Kotolex](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Kotolex\ \ Software Engineer](https://github.com/kotolex/surrealist "surrealist") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### Surrealist\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://app.surrealdb.com/ "Surrealist") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealML vs PyTorch vs ONNX - Benchmarking the performance of SurrealML against PyTorch and ONNX - Vladimir Rotariu.\ \ AI\ \ ![Vladimir Rotariu](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Vladimir Rotariu\ \ AI & Backend engineer](https://github.com/vladimirrotariu/surrealml-vs-onnx-vs-pytorch/tree/main "SurrealML vs PyTorch vs ONNX - Benchmarking the performance of SurrealML against PyTorch and ONNX - Vladimir Rotariu.") [![Development Tools](https://surrealdb.com/docs/_astro/development-tools.light.8jXa0GYe_HPr8s.webp)\ \ Development Tools\ \ ### SurrealReact\ \ ![Rouan van der Ende](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Rouan van der Ende\ \ Software Engineer](https://github.com/rvdende/surrealreact "SurrealReact") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### A Tour of SurrealDB - A course to learn the ins and outs of SurrealDB in 30 minutes.\ \ Beginner\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/learn/tour "A Tour of SurrealDB - A course to learn the ins and outs of SurrealDB in 30 minutes.") [![Libraries](https://surrealdb.com/docs/_astro/libraries.light.JCffqnO2_Z1bdgRx.webp)\ \ Libraries\ \ ### type-surrealdb\ \ ![Bilux](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Bilux\ \ Software Engineer](https://github.com/ibilux/type-surrealdb "type-surrealdb") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Unlocking SurrealDB: Building a Real-World Multi-Tenant RBAC System Made Easy (4 Part Series).\ \ Security\ \ ![Sebastian Wessel](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Sebastian Wessel\ \ Software Engineer](https://dev.to/sebastian_wessel/series/24535 "Unlocking SurrealDB: Building a Real-World Multi-Tenant RBAC System Made Easy (4 Part Series).") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Use SurrealDB in GitHub Actions\ \ Examples\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/using-github-actions "Use SurrealDB in GitHub Actions") [![Integrations](https://surrealdb.com/docs/_astro/integrations.light.D7TgFCdn_1Y1kTu.webp)\ \ Integrations\ \ ### Use SurrealDB with LangChain\ \ AI\ \ Embedding\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://github.com/surrealdb/langchain-surrealdb "Use SurrealDB with LangChain") [![Videos](https://surrealdb.com/docs/_astro/videos.light.6NlIAyXR_Z1W6RDj.webp)\ \ Videos\ \ ### Using SurrealDB to prove football statistics.\ \ ![Joseph McCarthy](https://surrealdb.com/docs/labs?filters=examples)\ \ #### Joseph McCarthy\ \ Content Creator](https://www.youtube.com/watch?v=6J1SPMXzOh4&t=5s "Using SurrealDB to prove football statistics.") [![Tutorials](https://surrealdb.com/docs/_astro/tutorials.light.Dw9feS_B_8HNuz.webp)\ \ Tutorials\ \ ### Working with SurrealDB over HTTP via Postman\ \ ![SurrealDB](https://surrealdb.com/docs/labs?filters=examples)\ \ #### SurrealDB\ \ Official](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman "Working with SurrealDB over HTTP via Postman") --- # Integrate Auth0 as an Authentication Provider | Tutorials [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Tutorials](https://surrealdb.com/docs/tutorials) [Integrate Auth0 as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider) [Integrate AWS Cognito as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider) [Use SurrealDB in GitHub Actions](https://surrealdb.com/docs/tutorials/using-github-actions) [Connect to SurrealDB via Ngrok tunnel](https://surrealdb.com/docs/tutorials/connect-to-surrealdb-via-ngrok-tunnel) [Working with SurrealDB over HTTP via Postman](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman) [Define a Schema in SurrealDB](https://surrealdb.com/docs/tutorials/define-a-schema) [Build a realtime presence web application](https://surrealdb.com/docs/tutorials/build-a-realtime-presence-web-application-using-surrealdb-live-queries) [surrealdb/examples](https://github.com/surrealdb/examples) [](https://surrealdb.com/docs/tutorials) [Integrate Auth0 as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider) Integrate Auth0 as an Authentication Provider[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#integrate-auth0-as-an-authentication-provider) =============================================================================================================================================================================== This guide will cover using [Auth0](https://auth0.com/) as the authentication provider for single-page web applications using SurrealDB as the only backend. Depending on the version of SurrealDB that you are on: `v1.x` or `v2.x`, you may have different options available with respect to using [Scope](https://surrealdb.com/docs/surrealql/statements/define/scope) and [Token](https://surrealdb.com/docs/surrealql/statements/define/token) or [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) methods to you for integrating Auth0 as an authentication provider. In this guide you will learn how to: * Configure Auth0 to issue tokens that can be used with SurrealDB. * Configure SurrealDB to accept tokens issued by Auth0. * Define user-level authorization using SurrealDB [record users](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) or [scopes](https://surrealdb.com/docs/surrealql/statements/define/scope) if you are on `v1.x`. * Authenticate users with Auth0 in a single-page application. * Retrieve and update information from SurrealDB using the authenticated user. This guide will cover the most general case, in which SurrealDB is the only backend for your application. You can still follow this guide even if you have additional backends, but in that case you may have other options available to request and validate tokens issued by Auth0. Likewise, even if your application is not strictly a Single-Page Application (SPA), you may still follow and benefit from this guide. Prerequities[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#prerequities) ------------------------------------------------------------------------------------------------------------- This guide assumes the following: * You have a [fresh instance of SurrealDB running.](https://surrealdb.com/docs/surrealdb/introduction/start) * You can [use a local Docker container](https://surrealdb.com/docs/surrealdb/installation/running/docker) without volumes for the purposes of this guide. docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:latest \ start --user root --pass secret To run the SurrealQL statements mentioned in this guide, you will also need an interactive shell. surreal sql -u root -p root --pretty You will also need to [create an Auth0 account](https://auth0.com/signup) , which can be on the free plan. Configuring Auth0[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#configuring-auth0) ----------------------------------------------------------------------------------------------------------------------- ### Creating a simple SPA, an Auth0 application and an Auth0 API[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#creating-a-simple-spa-an-auth0-application-and-an-auth0-api) First of all, you will need to complete the regular setup for creating a Single-Page Application and an API resource within Auth0. You can do this by following the official Auth0 [documentation](https://auth0.com/docs/quickstart/spa) for your SPA. If you are using plain JavaScript, follow [the vanilla guide](https://auth0.com/docs/quickstart/spa/vanillajs/01-login) to create an application in Auth0 and [the API guide](https://auth0.com/docs/quickstart/spa/vanillajs/02-calling-an-api) to create an API in Auth0. > Note > > You will not need to create an actual backend API (e.g. using backend languages like NodeJS or Go) as the documentation suggests when using SurrealDB. A simple file server (e.g. `python3 -m http.server 8080`, for local testing or any static web server for production) that can serve the static content of your website will suffice. However, creating an API resource in Auth0 is necessary, as it will generate an “audience” string which will be required for Auth0 to add claims to its access tokens. At the end of those tutorials, you should have both an application and an API created in your Auth0 account and a simple client-side web application that authenticates with Auth0 using those two resources. This website will capture the access token issued by Auth0, which is the token that we are using with SurrealDB. If you were not able to create a working website, you can also use this [minimal example created by SurrealDB](https://github.com/surrealdb/examples/tree/main/auth0) . When completing the actions above, make sure to keep the following information handy: * Auth0 Client ID, generated when creating the application. * Auth0 Domain, generated when creating the application. * Auth0 Audience, generated when creating the API. ### Creating a custom Auth0 action to add claims for SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#creating-a-custom-auth0-action-to-add-claims-for-surrealdb) Now, Auth0 is ready to perform authentication and issue tokens for your application. However, SurrealDB expects these tokens to contain some specific claims. Auth0 allows adding custom claims through its “actions” and “flows” features. Since Auth0 [requires claims for an API audience to be namespaced](https://auth0.com/docs/troubleshoot/product-lifecycle/past-migrations/custom-claims-migration#restricted-token-audience) , these claims will need to have the `https://surrealdb.com/` prefix. To add custom claims, you must [create an Auth0 action](https://auth0.com/docs/customize/actions/write-your-first-action#create-an-action) with the “Login / Post Login” trigger. For this example, you can use the following code: * [Using DEFINE ACCESS](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#tab-panel-270) * [Using Scope and Token](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#tab-panel-271) exports.onExecutePostLogin = async (event, api) => { if (event.authorization) { // The claims in this block are expected by SurrealDB. // These values should match your SurrealDB installation. api.accessToken.setCustomClaim(`https://surrealdb.com/ns`, "test"); api.accessToken.setCustomClaim(`https://surrealdb.com/db`, "test"); // This value corresponds to the name of the JWT access method // which will be created in SurrealDB during the next section. api.accessToken.setCustomClaim(`https://surrealdb.com/ac`, "auth0"); // In this block, we will add additional claims which are not required by SurrealDB. // These claims can be used from SurrealQL to implement application logic. // In this example, we will add the data that we will store for each user. // We will also use some of this data to perform authorization. api.accessToken.setCustomClaim(`https://surrealdb.com/email`, event.user.email); api.accessToken.setCustomClaim(`https://surrealdb.com/email_verified`, event.user.email_verified); api.accessToken.setCustomClaim(`https://surrealdb.com/name`, event.user.name); api.accessToken.setCustomClaim(`https://surrealdb.com/nickname`, event.user.nickname); api.accessToken.setCustomClaim(`https://surrealdb.com/picture`, event.user.picture); } }; exports.onExecutePostLogin = async (event, api) => { if (event.authorization) { // The claims in this block are expected by SurrealDB. // These values should match your SurrealDB installation. api.accessToken.setCustomClaim(`https://surrealdb.com/ns`, "test"); api.accessToken.setCustomClaim(`https://surrealdb.com/db`, "test"); // These values correspond to the names of the SCOPE and TOKEN resources // which will be created in SurrealDB during the next section. api.accessToken.setCustomClaim(`https://surrealdb.com/sc`, "user"); api.accessToken.setCustomClaim(`https://surrealdb.com/tk`, "auth0"); // In this block, we will add additional claims which are not required by SurrealDB. // These claims can be used from SurrealQL to implement application logic. // In this example, we will add the data that we will store for each user. // We will also use some of this data to perform authorization. api.accessToken.setCustomClaim(`https://surrealdb.com/email`, event.user.email); api.accessToken.setCustomClaim(`https://surrealdb.com/email_verified`, event.user.email_verified); api.accessToken.setCustomClaim(`https://surrealdb.com/name`, event.user.name); api.accessToken.setCustomClaim(`https://surrealdb.com/nickname`, event.user.nickname); api.accessToken.setCustomClaim(`https://surrealdb.com/picture`, event.user.picture); } }; This action should be saved and added to the “Login” flow in the “Actions > Flows” section. Configuring SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#configuring-surrealdb) ------------------------------------------------------------------------------------------------------------------------------- * [Using DEFINE ACCESS](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#tab-panel-274) * [Using Scope and Token](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#tab-panel-275) #### Defining permissions and fields in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#defining-permissions-and-fields-in-surrealdb) For this simple example, we will create a single table named “user”, where any user that authenticates through Auth0 using your application will be granted complete permissions over their data. For this to work as intended, we will need to ensure that the email address is unique between users and that users are granted permissions to access their own record as long as they authenticated with the access method that we will define. DEFINE TABLE user SCHEMAFULL -- Authorized users can select, update, delete and create user records. -- Records that do not match the permissions will not be modified nor returned. PERMISSIONS FOR select, update, delete, create WHERE -- The access method must match the method that we will define. $access = "auth0" -- The record identifier must match that of the authenticated user. AND id = $auth ; -- In this example, we will use the email as the primary identifier for a user. DEFINE INDEX email ON user FIELDS email UNIQUE; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); -- We define some other information present in the token that we want to store. DEFINE FIELD name ON user TYPE string; DEFINE FIELD nickname ON user TYPE string; DEFINE FIELD picture ON user TYPE string; #### Defining a token verification method in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#defining-a-token-verification-method-in-surrealdb) Next, we should configure SurrealDB so that it can verify tokens sent to it through the [HTTP REST API](https://surrealdb.com/docs/surrealdb/integration/http) via the “Authorization” header or through any of the [SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) via the “Authenticate” methods. To do that, we will leverage the JWKS support in SurrealDB in order to define a token verification mechanism pointing to a JWKS object served by Auth0. This JWKS object can be found in a [dedicated endpoint for your Auth0 domain](https://auth0.com/docs/secure/tokens/json-web-tokens/locate-json-web-key-sets) . Pointing to a JWKS file ensures that token verification will work seamlessly even after [rotating the signing keys](https://auth0.com/docs/get-started/tenant-settings/signing-keys/rotate-signing-keys) and that tokens signed with revoked keys will no longer be accepted by SurrealDB. To understand how revocation is handled by SurrealDB, read the [JSON Web Key Set documentation](https://surrealdb.com/docs/surrealql/statements/define/access/jwt#json-web-key-set-jwks) under `DEFINE ACCESS ... TYPE JWT`. We will also use the [`AUTHENTICATE`](https://surrealdb.com/docs/surrealql/statements/define/access/record#with-authenticate-clause) clause in order to check that any necessary token claims have the expected values before returning the user matching the email address provided by Auth0. This is required because Auth0 has no knowledge of the record identifiers that are used in SurrealDB, so we need to use an identifier that can actually be provided by Auth0 in order to retrieve the corresponding record user. The following queries will create the required resources to authenticate a token for a record user: -- Specify the namespace and database that will be used. -- These values should match the custom claims that we configured before. USE NS test DB test; -- Define the public key to verify tokens issued by Auth0 for our application. -- The name of the token should match the custom claim that we configured before. DEFINE ACCESS auth0 ON DATABASE TYPE RECORD -- We verify the token using the public keys hosted by Auth0. WITH JWT URL "https:///.well-known/jwks.json" -- We check the token claims and map the email address to a record user. AUTHENTICATE { IF ( -- The JWT specification allows the audience claim to be an array or a string. -- In this example, we ensure that it is provided as an array by Auth0. $token.aud.is_array() -- The audience claim must contain the audience of you application. -- This is the value that you defined when creating the API in Auth0. AND $token.aud CONTAINS "" -- The audience claim must contain your Auth0 user information endpoint. -- It contains the domain generated when when creating the application in Auth0. AND $token.aud CONTAINS "https:///userinfo" -- The email address in the token must be verified as belonging to the user. AND $token['https://surrealdb.com/email_verified'] = true ) { -- We return the only user that matches the email address claim found in the token. RETURN SELECT * FROM user WHERE email = $token['https://surrealdb.com/email'] } } ; In the example above, replace the placeholder with the domain value defined for your Auth0 application. It is important to not that [validating the audience of the token is a requirement of Auth0](https://auth0.com/docs/secure/tokens/access-tokens/validate-access-tokens) , other providers may require validating additional claims (e.g. `iss`, `sub`) to ensure that the token is being used as intended. With Auth0, you can also make use of [OpenID Connect scopes](https://auth0.com/docs/get-started/apis/scopes/openid-connect-scopes) , which can be accessed through the `scopes` claim via `$token.scopes` and contain the OIDC scopes requested by the application and granted by the user, which we will not do for this example. > Important > > In order to allow SurrealDB to establish a connection with Auth0 to download the JWKS object, you will require running it with the network [capability](https://surrealdb.com/docs/surrealdb/security/capabilities) > . For the strongest security, provide your specific Auth0 domain when starting SurrealDB with `—allow-net`. For example: `—allow-net example.eu.auth0.com`. #### Defining a token verification method in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#defining-a-token-verification-method-in-surrealdb-1) Next, we should configure SurrealDB so that it can verify tokens sent to it through the [HTTP REST API](https://surrealdb.com/docs/surrealdb/integration/http) via the “Authorization” header or through any of the [SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) via the “Authenticate” methods. To do that, we will leverage the JWKS support in SurrealDB in order to define a token verification mechanism pointing to a JWKS object served by Auth0. This JWKS object can be found in a [dedicated endpoint for your Auth0 domain](https://auth0.com/docs/secure/tokens/json-web-tokens/locate-json-web-key-sets) . Pointing to a JWKS file ensures that token verification will work seamlessly even after [rotating the signing keys](https://auth0.com/docs/get-started/tenant-settings/signing-keys/rotate-signing-keys) and that tokens signed with revoked keys will no longer be accepted by SurrealDB. To understand how revocation is handled by SurrealDB, read the [JSON Web Key Set documentation](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) under `DEFINE TOKEN`. The following queries will create the required resources to authenticate a token for a scope: -- Specify the namespace and database that will be used. -- These values should match the custom claims that we configured before. USE NS test DB test; -- Define the scope where the token will be used. -- The name of the scope should match the custom claim that we configured before. DEFINE SCOPE user; -- Define the public key to verify tokens issued by Auth0 for our application. -- The name of the token should match the custom claim that we configured before. DEFINE TOKEN auth0 ON SCOPE user TYPE JWKS VALUE "https:///.well-known/jwks.json"; In the example above, replace the placeholder with the domain value defined for your Auth0 application. > Important > > In order to allow SurrealDB to establish a connection with Auth0 to download the JWKS object, you will require running it with the network [capability](https://surrealdb.com/docs/surrealdb/security/capabilities) > . For the strongest security, provide your specific Auth0 domain when starting SurrealDB with `—allow-net`. For example: `—allow-net example.eu.auth0.com`. #### Defining authorization criteria in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#defining-authorization-criteria-in-surrealdb) For this simple example, we will create a single table named “user”, where any user that authenticates through Auth0 using your application with a verified email address will be able to register, view and update their data. For this to work as intended, we will need to verify some information in the token claims. DEFINE TABLE user SCHEMAFULL -- Authorized users can select, update, delete and create user records. -- Records that do not match the permissions will not be modified nor returned. PERMISSIONS FOR select, update, delete, create WHERE -- The token scope must match the scope that we defined. -- The name of the scope should match the scope that we defined before. $scope = "user" -- The audience claim must contain the audience of you application. -- This is the value that you defined when creating the API in Auth0. AND $token.aud CONTAINS "" -- The audience claim must contain your Auth0 user information endpoint. -- It contains the domain generated when when creating the application in Auth0. AND $token.aud CONTAINS "https:///userinfo" -- The email claim must match the email of the user being queried. AND email = $token['https://surrealdb.com/email'] -- The email must be verified as belonging to the user. AND $token['https://surrealdb.com/email_verified'] = true ; -- In this example, we will use the email as the primary identifier for a user. DEFINE INDEX email ON user FIELDS email UNIQUE; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); -- We define some other information present in the token that we want to store. DEFINE FIELD name ON user TYPE string; DEFINE FIELD nickname ON user TYPE string; DEFINE FIELD picture ON user TYPE string; It is important to know that [validating the audience of the token is a requirement of Auth0](https://auth0.com/docs/secure/tokens/access-tokens/validate-access-tokens) , other providers may require validating additional claims (e.g. iss, sub) to ensure that the token is being used as intended. With Auth0, you can also make use of [OpenID Connect scopes](https://auth0.com/docs/get-started/apis/scopes/openid-connect-scopes) , which can be accessed through the `scopes` claim via `$token.scopes` and contain the OIDC scopes requested by the application and granted by the user, which we will not do for this example. It is also important to note that the `$auth` variable accessible from SurrealQL will not contain any values in this case, as it requires the `id` claim to be added to the JWT, containing the value of the identifier of a SurrealDB record. For the current example, the `$auth` variable will not be necessary. Configuring the Application[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#configuring-the-application) ------------------------------------------------------------------------------------------------------------------------------------------- Now that we have everything ready for Auth0 to generate tokens and for SurrealDB to receive and verify them, we will modify our simple web application to use the token issued by Auth0 to register a new user or to update its information if the user already exists. Because of the diversity of SDKs that both SurrealDB and Auth0 support, it is difficult to provide examples that will work for everyone. In this guide, we will provide code examples for the most unspecific use case of using the [SurrealDB HTTP REST API](https://surrealdb.com/docs/surrealdb/integration/http) and [Auth0 SPA JS](https://github.com/auth0/auth0-spa-js) . This code will run entirely on the client and can be added to the static website that you created while following the Auth0 quick start documentation linked at the beginning of this guide. With the aforementioned interfaces, we can use the following functions to authenticate users: // Returns users in that the token is authorized to select. // Should return only the user matching the email in the token. const getUser = async () => { // We fetch an access token from Auth0 with the ID token. const auth0Token = await auth0Client.getTokenSilently(); const response = await fetch(surrealDbConfig.endpoint + "/key/user", { method: "GET", headers: { "Accept": "application/json", "Authorization": "Bearer " + auth0Token } }); return response.json(); }; // Creates a user matching the information in the token. // If the user already exists, updates the existing user with the new data. const createUpdateUser = async () => { // We collect the user data from the Auth0 ID token. const auth0User = await auth0Client.getUser(); // We fetch an access token from Auth0 with the ID token. const auth0Token = await auth0Client.getTokenSilently(); // We define the general query to create or update a user. // We leave the method to be defined later. let query = { body: JSON.stringify({ email: auth0User.email, name: auth0User.name, nickname: auth0User.nickname, picture: auth0User.picture }), headers: { "Accept": "application/json", "Authorization": "Bearer " + auth0Token } }; // We get the user that the token is authorized to access. const surrealDbUser = await getUser(); if (surrealDbUser[0].result.length == 0) { // If a user for the token does not exist, we create the record. console.log("Token user does not exist in database. Creating record."); query.method = "POST"; } else { // If a user for the token already exists, we update the record. console.log("Token user already exists in database. Updating record."); query.method = "PUT"; } // We perform the query and return the created/updated record. let response = await fetch(surrealDbConfig.endpoint + "/key/user", query); return response.json(); }; If you want to see how this code would fit inside the web application that you built, you can view this [minimal example created by SurrealDB](https://github.com/surrealdb/examples/tree/main/auth0) . To learn about other potential uses for the SurrealDB token functionality, you can read the \[`DEFINE ACCESS ... TYPE JWT`\] documentation page\](/docs/surrealql/statements/define/access/jwt). Annex[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#annex) ----------------------------------------------------------------------------------------------- In this section, we will provide a few examples of how to configure the application to work with Auth0. Example Single Page Application[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#example-single-page-application) --------------------------------------------------------------------------------------------------------------------------------------------------- You can view and download a minimal example of an SPA using Auth0 and SurrealDB [in this repository](https://github.com/surrealdb/examples/tree/main/auth0) . Alternative: Using HMAC[](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#alternative-using-hmac) ---------------------------------------------------------------------------------------------------------------------------------- Using public key cryptography algorithms for signing tokens prevents you from having to store any secrets at all in SurrealDB. However, some SurrealDB administrators may prefer to use HMAC algorithms, which use the same secret to both sign and verify the signature of the JWT. This secret will be stored in SurrealDB when provided as value for the `DEFINE ACCESS` statement and its access should be restricted, as it can be used to issue arbitrary tokens which will be trusted by SurrealDB. However, **we do not recommend this method** over using public cryptography, as the later significantly reduces the burden of creating strong secrets, keeping them secret and managing their lifecycle. According to the OAuth 2.0 specification (part of OpenID Connect), only confidential (as opposed to public) applications should be allowed to use HMAC algorithms, as it requires being able to keep the secret secure. For this reason, Auth0 will not allow using HMAC algorithms with applications of the SPA (Single-Page Application) type, as they generally would have to store the secret in the client, which is publicly accessible. However, because of the particular case of SurrealDB, the secret will not need to be exposed to the client and will instead be stored in SurrealDB. Auth0 can support scenarios like these through the option to [disable OIDC-conformant authentication](https://auth0.com/docs/authenticate/login/oidc-conformant-authentication) . Once disabled, Auth0 will allow selecting HMAC algorithms for SPA. Keep in mind that other options may become available in Auth0 after this change which, if modified without proper care, may compromise the security of your application. If the choice of using HMAC is made, the rest of the guide can be followed as is, with the exception of specifying the proper HMAC algorithm and placing the secret as the value when defining a token. In Auth0, the secret used to sign using HMAC algorithm corresponds to the [Auth0 Client Secret](https://auth0.com/docs/secure/application-credentials#client-secret-authentication) string. The example below shows how a token using the HS256 algorithm can be defined: * [Using DEFINE ACCESS](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#tab-panel-272) * [Using Scope and Token](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#tab-panel-273) -- Define the secret to verify tokens issued by Auth0 for our application. -- The name of the access method should match the custom claim that we configured before. DEFINE ACCESS auth0 ON DATABASE TYPE RECORD WITH JWT ALGORITHM HS256 KEY "" ; -- Define the secret to verify tokens issued by Auth0 for our application. -- The name of the token should match the custom claim that we configured before. DEFINE TOKEN auth0 ON SCOPE user TYPE HS256 VALUE ""; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-tutorials/integrate-auth0-as-authentication-provider.mdx) Copy page as Markdown [Tutorials](https://surrealdb.com/docs/tutorials) [Integrate AWS Cognito as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider) ### On this page * [Prerequities](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#prerequities) * [Configuring Auth0](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#configuring-auth0) * [Creating a simple SPA, an Auth0 application and an Auth0 API](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#creating-a-simple-spa-an-auth0-application-and-an-auth0-api) * [Creating a custom Auth0 action to add claims for SurrealDB](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#creating-a-custom-auth0-action-to-add-claims-for-surrealdb) * [Configuring SurrealDB](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#configuring-surrealdb) * [Configuring the Application](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#configuring-the-application) * [Annex](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#annex) * [Example Single Page Application](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#example-single-page-application) * [Alternative: Using HMAC](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider#alternative-using-hmac) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-tutorials/integrate-auth0-as-authentication-provider.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Integrate AWS Cognito as an Authentication Provider | Tutorials [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Tutorials](https://surrealdb.com/docs/tutorials) [Integrate Auth0 as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider) [Integrate AWS Cognito as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider) [Use SurrealDB in GitHub Actions](https://surrealdb.com/docs/tutorials/using-github-actions) [Connect to SurrealDB via Ngrok tunnel](https://surrealdb.com/docs/tutorials/connect-to-surrealdb-via-ngrok-tunnel) [Working with SurrealDB over HTTP via Postman](https://surrealdb.com/docs/tutorials/working-with-surrealdb-over-http-via-postman) [Define a Schema in SurrealDB](https://surrealdb.com/docs/tutorials/define-a-schema) [Build a realtime presence web application](https://surrealdb.com/docs/tutorials/build-a-realtime-presence-web-application-using-surrealdb-live-queries) [surrealdb/examples](https://github.com/surrealdb/examples) [](https://surrealdb.com/docs/tutorials) [Integrate AWS Cognito as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider) Integrate AWS Cognito as an Authentication Provider[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#integrate-aws-cognito-as-an-authentication-provider) ================================================================================================================================================================================================= This guide will cover using [AWS Cognito](https://aws.amazon.com/cognito/) as the authentication provider for client-side web applications using SurrealDB as the only backend. Depending on the version of SurrealDB that you are on: `v1.x` or `v2.x`, you may have different options available with respect to using [Scope](https://surrealdb.com/docs/surrealql/statements/define/scope) and [Token](https://surrealdb.com/docs/surrealql/statements/define/token) or [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) methods to you for integrating Auth0 as an authentication provider. In this guide you will learn how to: * Configure AWS Cognito to issue tokens that can be used with SurrealDB. * Configure SurrealDB to accept tokens issued by AWS Cognito. * Define user-level authorization using SurrealDB [record users](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) . * Authenticate users with AWS Cognito in a client-side web application. * Retrieve and update information from SurrealDB using the authenticated user. This guide will cover the most general case, in which SurrealDB is the only backend for your application. You can still follow this guide even if you have additional backends, but in that case you may have other options available to request and validate tokens issued by AWS Cognito. Prerequities[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#prerequities) ------------------------------------------------------------------------------------------------------------------- This guide assumes the following: * You have a [fresh instance of SurrealDB running](https://surrealdb.com/docs/surrealdb/introduction/start) with version `1.2.0` or later. * You can [use a local Docker container](https://surrealdb.com/docs/surrealdb/installation/running/docker) without volumes for the purposes of this guide. docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:latest \ start --user root --pass secret To run the SurrealQL statements mentioned in this guide, you will also need an interactive shell. surreal sql -u root -p root --pretty You will also need to [have an Amazon Web Services account](https://aws.amazon.com/resources/create-account/) . By following this guide you will be subject to **at least** the [AWS Cognito](https://aws.amazon.com/cognito/pricing/) and [AWS Lambda](https://aws.amazon.com/lambda/pricing/) pricing plans. Although the resources used in this guide should be well within the free tier for both, we cannot guarantee that this will be the case in your particular situation. Configuring AWS Cognito[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#configuring-aws-cognito) ----------------------------------------------------------------------------------------------------------------------------------------- In this section, we will create a user pool and a client that will be used to authenticate users with AWS Cognito. ### Creating a User Pool and a Client[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-user-pool-and-a-client) Cognito offers [user pools](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html) as a user directory for mobile and web applications. This directory can hold users defined directly within Cognito, but can also integrate with third-party identity providers like Google and Facebook. Additionally, it provides the ability to handle user registrations directly in the [Cognito Hosted UI](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-app-integration.html) , which can also take care of requiring specific information, verifying user email addresses and phone numbers or even enforcing multi-factor authentication. Within a user pool, [creating a client](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html#cognito-user-pools-app-idp-settings-console-create) establishes a method (in our case a client-side web application) to authenticate with the user pool. You will need to [create a user pool](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pool-as-user-directory.html) that meets your requirements. In order to be able to follow along with this guide, the following configuration options are strongly recommended: * In the “Configure sign-in experience” step: * Select at least “Email” in “Cognito user pool sign-in options”. * In the “Configure sign-up experience” step: * Select at least “email” in “Required attributes”. * Select “Allow Cognito to automatically send messages to verify and confirm”. * In the “Configure message delivery” step: * Select “Send email with Cognito” in “Email”. * In the “Integrate your app” step: * Select “Use the Cognito Hosted UI” in “Hosted authentication pages”. * Select “Use a Cognito domain” in “Domain” and set a name for it. * Select “Public client” in “Initial app client”. * Select “Don’t generate a client secret” in “Initial app client”. * Under “Allowed callback URLs” in “Initial app client”, set the URL of your web application. * Under “Advanced app client settings” in “Initial app client”, ensure “Authorization code grant” is selected. * Under “Attribute read and write permissions” in “Initial app client”, ensure “email” is selected for “Read”. * Under “Attribute read and write permissions” in “Initial app client”, ensure “email\_verified” is selected for “Read”. > Important > > These suggested configuration options may not provide the strongest security or match your specific requirements, but will ensure that you are able to follow with this guide to understand the simplest example of an integration between SurrealDB and AWS Cognito, which you should later update to meet your requirements. Changes in these options may require changes in the other steps outlined in this guide. Other configuration options can be left with their default values or may be changed to fit your requirements. Once you user pool has been created, open it in the AWS Console and take note of the following values: * “User pool ID” from the “User pool overview” section in the main user pool view. * “Cognito domain” from the “Configuration for all app clients” section under the “App integration” tab. * “Client ID” from the only client in the “App client list” under the “App integration” tab. ### Creating a Pre-Token Generation Lambda[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-pre-token-generation-lambda) Cognito is now ready to perform authentication and issue tokens for your application. However, SurrealDB expects these tokens to contain some specific claims. Cognito allows modifying token claims through [pre-token generation lambda](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html#cognito-user-pools-lambda-trigger-syntax-pre-token-generation) triggers. Specifically, we will be [adding custom claims](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html#aws-lambda-triggers-pre-token-generation-example-2) to the token. To create the trigger, just [create an AWS Lambda function](https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html#getting-started-create-function) with the following code: * [Using DEFINE ACCESS](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#tab-panel-276) * [Using Scope and Token](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#tab-panel-277) const handler = async (event) => { event.response = { claimsOverrideDetails: { claimsToAddOrOverride: { ac: "cognito", // The access method that has been defined using DEFINE ACCESS. ns: "test", // The namespace selected when calling DEFINE ACCESS. db: "test", // The database selected when calling DEFINE ACCESS. }, }, }; return event; }; export { handler }; const handler = async (event) => { event.response = { claimsOverrideDetails: { claimsToAddOrOverride: { tk: "cognito", // The name of the token given when defining it with DEFINE TOKEN. sc: "user", // The scope that the token has been defined for with DEFINE TOKEN. ns: "test", // The namespace selected when calling DEFINE TOKEN. db: "test", // The database selected when calling DEFINE TOKEN. }, }, }; return event; }; export { handler }; Note that, in order to use the suggested code, the function must be configured as “Node.js 20.x” or equivalent. After the Lambda trigger has been created, we will need to [associate it with our client integration](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html) . Visit your user pool in Cognito and, under the “User pool properties” tab, in the “Lambda triggers” section, click on “Add Lambda trigger”. For this guide, the trigger type will be “Authentication”, specifically a “Pre token generation trigger”. Select the function that you created. ### Creating a Test User[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-test-user) Depending on how you configured your user pool, users will be able to register by different means. To ensure this guide can be followed easily, we will create a test user that to authenticate in our web application. Open your user pool and click on the “Create user” button under the “Users” section in the main user pool view. Provide an email address for that user and check “Mark email address as verified” to avoid having to do so manaually. Set a password that matches the requirements that you configured. Configuring SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#configuring-surrealdb) ------------------------------------------------------------------------------------------------------------------------------------- * [Using DEFINE ACCESS](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#tab-panel-278) * [Using Scope and Token](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#tab-panel-279) #### Defining permissions and fields in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#defining-permissions-and-fields-in-surrealdb) For this simple example, we will create a single table named “user”, where any user that authenticates through AWS Cognito using your application will be granted complete permissions over their data. For this to work as intended, we will need to ensure that the email address is unique between users and that users are granted permissions to access their own record as long as they authenticated with the access method that we will define. DEFINE TABLE user SCHEMAFULL -- Authorized users can select, update, delete and create user records. -- Records that do not match the permissions will not be modified nor returned. PERMISSIONS FOR select, update, delete, create WHERE -- The access method must match the method that we will define. $access = "cognito" -- The record identifier must match that of the authenticated user. AND id = $auth ; -- In this example, we will use the email as the primary identifier for a user. DEFINE INDEX email ON user FIELDS email UNIQUE; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); -- We define some other information present in the token that we want to store. DEFINE FIELD cognito_username ON user TYPE string; #### Defining a token verification method in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#defining-a-token-verification-method-in-surrealdb) Next, we should configure SurrealDB so that it can verify tokens sent to it through the [HTTP REST API](https://surrealdb.com/docs/surrealdb/integration/http) via the “Authorization” header or through any of the [SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) via the “Authenticate” methods. To do that, we will leverage the [JWKS support in SurrealDB](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) in order to define a token verification mechanism pointing to a JWKS object served by AWS for your Cognito user pool. This JWKS object can be found in an endpoint [build from your AWS region and user pool](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html) in the format `https://cognito-idp..amazonaws.com//.well-known/jwks.json`. Pointing to a remote JWKS object ensures that token verification will work seamlessly even in the case that AWS rotates their encryption keys and that tokens signed with revoked keys will no longer be accepted by SurrealDB. To understand how revocation is handled by SurrealDB, read the [JSON Web Key Set documentation](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) under `DEFINE ACCESS ... TYPE JWT`. We will also use the [`AUTHENTICATE`](https://surrealdb.com/docs/surrealql/statements/define/access/record#with-authenticate-clause) clause in order to check that any necessary token claims have the expected values before returning the user matching the email address provided by AWS Cognito. This is required because AWS Cognito has no knowledge of the record identifiers that are used in SurrealDB, so we need to use an identifier that can actually be provided by AWS Cognito in order to retrieve the corresponding record user. The following queries will create the required resources to authenticate a token for a record using JWKS: -- Specify the namespace and database that will be used. -- These values should match the custom claims that we configured before. USE NS test DB test; -- Define the public key to verify tokens issued by your AWS Cognito user pool. -- The name of the access method should match the custom claim that we configured before. DEFINE ACCESS cognito ON DATABASE TYPE RECORD -- We verify the token using the public keys hosted by AWS. WITH JWT URL "https://cognito-idp..amazonaws.com//.well-known/jwks.json" -- We check the token claims and map the email address to a record user. AUTHENTICATE { IF ( -- The issuer claim must match the URL of your AWS Cognito user pool. $token.iss = "https://cognito-idp..amazonaws.com/" -- The audience claim must match you AWS Cognito Client ID. AND $token.aud = "" -- The email address in the token must be verified as belonging to the user. AND $token.email_verified = true ) { -- We return the only user that matches the email address claim found in the token. RETURN SELECT * FROM user WHERE email = $token.email } } ; In the example above, replace the placeholders with values applicable to your Cognito user pool. It is important to know that [validating the issuer and audience of the token is a requirement of AWS Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html) , other providers may require validating additional claims to ensure that the token is being used as intended. In order to allow SurrealDB to establish a connection with AWS Cognito to download the JWKS object, you will require running it with the network [capability](https://surrealdb.com/docs/surrealdb/security/capabilities) . For the strongest security, provide your specific Cognito user pool domain when starting SurrealDB with `--allow-net`. For example: `--allow-net cognito-idp.eu-west-1.amazonaws.com`. #### Defining a token verification method in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#defining-a-token-verification-method-in-surrealdb-1) Next, we should configure SurrealDB so that it can verify tokens sent to it through the [HTTP REST API](https://surrealdb.com/docs/surrealdb/integration/http) via the “Authorization” header or through any of the [SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) via the “Authenticate” methods. To do that, we will leverage the [JWKS support in SurrealDB](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) in order to define a token verification mechanism pointing to a JWKS object served by AWS for your Cognito user pool. This JWKS object can be found in an endpoint [build from your AWS region and user pool](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html) in the format `https://cognito-idp..amazonaws.com//.well-known/jwks.json`. Pointing to a remote JWKS object ensures that token verification will work seamlessly even in the case that AWS rotates their encryption keys and that tokens signed with revoked keys will no longer be accepted by SurrealDB. To understand how revocation is handled by SurrealDB, read the [JSON Web Key Set documentation](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) under `DEFINE TOKEN`. The following queries will create the required resources to authenticate a token for a scope using JWKS: -- Specify the namespace and database that will be used. -- These values should match the custom claims that we configured before. USE NS test DB test; -- Define the scope where the token will be used. -- The name of the scope should match the custom claim that we configured before. DEFINE SCOPE user; -- Define the public key to verify tokens issued by your AWS Cognito user pool. -- The name of the token should match the custom claim that we configured before. DEFINE TOKEN cognito ON SCOPE user TYPE JWKS VALUE "https://cognito-idp..amazonaws.com//.well-known/jwks.json"; ; In the example above, replace the placeholders with values applicable to your Cognito user pool. In order to allow SurrealDB to establish a connection with AWS Cognito to download the JWKS object, you will require running it with the network [capability](https://surrealdb.com/docs/surrealdb/security/capabilities) . For the strongest security, provide your specific Cognito user pool domain when starting SurrealDB with `--allow-net`. For example: `--allow-net cognito-idp.eu-west-1.amazonaws.com`. #### Defining authorization criteria in SurrealDB[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#defining-authorization-criteria-in-surrealdb) For this example, we will create a single table named `user`, where any user that authenticates through AWS Cognito using your application with a verified email address will be able to register, view and update their data. For this to work as intended, we will need to verify some information in the token claims. DEFINE TABLE user SCHEMAFULL -- Authorized users can select, update, delete and create user records. -- Records that do not match the permissions will not be modified nor returned. PERMISSIONS FOR select, update, delete, create WHERE -- The token scope must match the scope that we defined. -- The name of the scope should match the scope that we defined before. $scope = "user" -- The issuer claim must match the URL of your AWS Cognito user pool. AND $token.iss = "https://cognito-idp..amazonaws.com/" -- The audience claim must match you AWS Cognito Client ID. AND $token.aud = "" -- The email claim must match the email of the user being queried. AND email = $token.email -- The email must be verified as belonging to the user. AND $token.email_verified = true ; -- In this example, we will use the email as the primary identifier for a user. DEFINE INDEX email ON user FIELDS email UNIQUE; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); -- We define some other information present in the token that we want to store. DEFINE FIELD cognito_username ON user TYPE string; It is important to know that [validating the issuer and audience of the token is a requirement of AWS Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html) , other providers may require validating additional claims to ensure that the token is being used as intended. It is also important to note that the `$auth` variable accessible from SurrealQL will not contain any values in this case, as it requires the `id` claim to be added to the JWT, containing the value of the identifier of a SurrealDB record. For the current example, the `$auth` variable will not be necessary. Creating a Simple Web Application[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-simple-web-application) ------------------------------------------------------------------------------------------------------------------------------------------------------------- For this guide, we will create a simple client-side web application that will direct the user to log in using the Cognito Hosted UI, redirect the user back your our application with a query parameter containing an authorization code and exchange that code to Cognito for the user identity token which we will use to authenticate with SurrealDB. The application will create or update a SurrealDB user using data from the token claims. This user will later be able visit our web application and retrieve their information from SurrealDB. > Note > > As most other authentication providers using OpenID Connect (OIDC), AWS Cognito issues both identity and access tokens. In this example, we will be using the identity token as it can include custom claims (which are required by SurrealDB) by default. It is important to note that identity tokens should only be used to assert identity claims as opposed to access claims. In this case, we trust the identity token to provide information about the indentity of the user. If we wanted the token to contain authorization claims (e.g. OAuth scopes) we should instead rely on the access token. Customizing access token claims has been [recently supported by AWS](https://aws.amazon.com/about-aws/whats-new/2023/12/amazon-cognito-user-pools-customize-access-tokens/) > and requires enabling [advanced security features](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pool-settings-advanced-security.html#cognito-user-pool-settings-advanced-security.title) > . This guide makes the deliberate decision of using the identity token to simplify the process. We have developed an [example application](https://github.com/surrealdb/examples/tree/main/aws-cognito) that uses plain JavaScript to authenticate with Cognito using basic HTTP requests against the [login endpoint](https://docs.aws.amazon.com/cognito/latest/developerguide/login-endpoint.html) of the Cognito Hosted UI and the Cognito [token endpoint](https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html) . For the purposes of following this guide, we recommend using our example code. However, keep in mind that this code aims to be as simple as possible and is not suitable for production applications. Alternatively, you can develop this application yourself using the [Cognito SDK](https://docs.aws.amazon.com/cognito/latest/developerguide/service_code_examples_cognito-identity-provider.html) or the new [Amplify SDK](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-integrate-apps.html#cognito-integrate-apps-amplify) . When using our example code, you will only need to update the `config.json` file with the values that you saved after creating your user pool and run the web application using the `start.sh` script or any equivalent web server. Once in the web application, clicking the “Log in” button should take you to the Cognito Hosted UI to log in with your test user, after which you should be redirected back to your web application. For this to work, the URL of your web application (without a trailing slash) should be present in the “Allowed callback URLs” list that you defined when configuring the user pool client. After redirection, the web application will show some information about the authenticated user and attempt to create it in SurrealDB via the configured endpoint. After the user is created, subsequent logins will retrieve its information from SurrealDB and display it in the web application. If that is not the case, use the developer console of your browser together with the SurrealDB logs (running with `--log trace` during the debugging for maximum verbosity) to understand why. Once the example web application is working, you can inspect the simple code under `app.js` to understand how. Annex[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#annex) ----------------------------------------------------------------------------------------------------- In this section, we will provide a few examples of how to configure the application to work with AWS Cognito. Example Single Page Application[](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#example-single-page-application) --------------------------------------------------------------------------------------------------------------------------------------------------------- You can view and download a minimal example of an web application using AWS Cognito and SurrealDB [in the AWS cognito example project](https://github.com/surrealdb/examples/tree/main/aws-cognito) . [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-tutorials/integrate-aws-cognito-as-authentication-provider.mdx) Copy page as Markdown [Integrate Auth0 as an Authentication Provider](https://surrealdb.com/docs/tutorials/integrate-auth0-as-authentication-provider) [Use SurrealDB in GitHub Actions](https://surrealdb.com/docs/tutorials/using-github-actions) ### On this page * [Prerequities](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#prerequities) * [Configuring AWS Cognito](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#configuring-aws-cognito) * [Creating a User Pool and a Client](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-user-pool-and-a-client) * [Creating a Pre-Token Generation Lambda](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-pre-token-generation-lambda) * [Creating a Test User](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-test-user) * [Configuring SurrealDB](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#configuring-surrealdb) * [Creating a Simple Web Application](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#creating-a-simple-web-application) * [Annex](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#annex) * [Example Single Page Application](https://surrealdb.com/docs/tutorials/integrate-aws-cognito-as-authentication-provider#example-single-page-application) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-tutorials/integrate-aws-cognito-as-authentication-provider.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Golang | SDK | Quick start [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/golang) [Installation](https://surrealdb.com/docs/sdk/golang/installation) [Quick start](https://surrealdb.com/docs/sdk/golang/start) [Data Types](https://surrealdb.com/docs/sdk/golang/data-types) [Connection Engines](https://surrealdb.com/docs/sdk/golang/connection-engines) Methods [Introduction](https://surrealdb.com/docs/sdk/golang/methods) [Authenticate](https://surrealdb.com/docs/sdk/golang/methods/authenticate) [Create](https://surrealdb.com/docs/sdk/golang/methods/create) [Delete](https://surrealdb.com/docs/sdk/golang/methods/delete) [Info](https://surrealdb.com/docs/sdk/golang/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/golang/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/golang/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/golang/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/golang/methods/kill) [LiveNotification](https://surrealdb.com/docs/sdk/golang/methods/live-notification) [LiveQuery](https://surrealdb.com/docs/sdk/golang/methods/live) [Merge](https://surrealdb.com/docs/sdk/golang/methods/merge) [Patch](https://surrealdb.com/docs/sdk/golang/methods/patch) [Query](https://surrealdb.com/docs/sdk/golang/methods/query) [QueryRaw](https://surrealdb.com/docs/sdk/golang/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/golang/methods/relate) [Select](https://surrealdb.com/docs/sdk/golang/methods/select) [SignIn](https://surrealdb.com/docs/sdk/golang/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/golang/methods/signup) [Unset](https://surrealdb.com/docs/sdk/golang/methods/unset) [Update](https://surrealdb.com/docs/sdk/golang/methods/update) [Upsert](https://surrealdb.com/docs/sdk/golang/methods/upsert) [Version](https://surrealdb.com/docs/sdk/golang/methods/version) [New](https://surrealdb.com/docs/sdk/golang/methods/new) [Use](https://surrealdb.com/docs/sdk/golang/methods/use) [Let](https://surrealdb.com/docs/sdk/golang/methods/let) Core [Introduction](https://surrealdb.com/docs/sdk/golang/core) [Handle authentication](https://surrealdb.com/docs/sdk/golang/core/handling-authentication) [surrealdb/surrealdb.go](https://github.com/surrealdb/surrealdb.go) [](https://surrealdb.com/docs/sdk/golang) [Quick start](https://surrealdb.com/docs/sdk/golang/start) Quick start[](https://surrealdb.com/docs/sdk/golang/start#quick-start) ======================================================================= Now that you have a more comprehensive understanding of the SurrealDB SDK for Golang, let’s create a simple project to get you started. This guide will walk you through connecting and authenticating to a SurrealDB server, set the namespace and database, and make several data manipulation requests. Prerequisites[](https://surrealdb.com/docs/sdk/golang/start#prerequisites) --------------------------------------------------------------------------- This guide assumes the following: * You have a basic understanding of the Go ecosystem and the Go language. * You have an install version of SurrealDB `>1.4.2` installed on your machine. Setup your project[](https://surrealdb.com/docs/sdk/golang/start#setup-your-project) ------------------------------------------------------------------------------------- Create a new `main.go` file and add the following code to try out some basic operations using the SurrealDB SDK. package main import ( "encoding/json" "fmt" surrealdb "github.com/surrealdb/surrealdb.go" "github.com/surrealdb/surrealdb.go/pkg/models" ) type Person struct { ID *models.RecordID `json:"id,omitempty"` Name string `json:"name"` Surname string `json:"surname"` Location models.GeometryPoint `json:"location"` } func main() { // Connect to SurrealDB db, err := surrealdb.New("ws://localhost:8000") if err != nil { panic(err) } // Set the namespace and database if err = db.Use("testNS", "testDB"); err != nil { panic(err) } // Sign in to authentication `db` authData := &surrealdb.Auth{ Username: "root", // use your setup username Password: "secret", // use your setup password } token, err := db.SignIn(authData) if err != nil { panic(err) } // Check token validity. This is not necessary if you called `SignIn` before. This authenticates the `db` instance too if sign in was // not previously called if err := db.Authenticate(token); err != nil { panic(err) } // And we can later on invalidate the token if desired defer func(token string) { if err := db.Invalidate(); err != nil { panic(err) } }(token) // Create an entry person1, err := surrealdb.Create[Person](db, models.Table("persons"), map[interface{}]interface{}{ "Name": "John", "Surname": "Doe", "Location": models.NewGeometryPoint(-0.11, 22.00), }) if err != nil { panic(err) } fmt.Printf("Created person with a map: %+v\n", person1) // Or use structs person2, err := surrealdb.Create[Person](db, models.Table("persons"), Person{ Name: "John", Surname: "Doe", Location: models.NewGeometryPoint(-0.11, 22.00), }) if err != nil { panic(err) } fmt.Printf("Created person with a struct: %+v\n", person2) // Get entry by Record ID person, err := surrealdb.Select[Person, models.RecordID](db, *person1.ID) if err != nil { panic(err) } fmt.Printf("Selected a person by record id: %+v\n", person) // Or retrieve the entire table persons, err := surrealdb.Select[[]Person, models.Table](db, models.Table("persons")) if err != nil { panic(err) } fmt.Printf("Selected all in persons table: %+v\n", persons) // Delete an entry by ID if _, err = surrealdb.Delete[Person](db, *person2.ID); err != nil { panic(err) } // Delete all entries if _, err = surrealdb.Delete[[]Person](db, models.Table("persons")); err != nil { panic(err) } // Confirm empty table persons, err = surrealdb.Select[[]Person](db, models.Table("persons")) if err != nil { panic(err) } fmt.Printf("No Selected person: %+v\n", persons) } ### Run the example[](https://surrealdb.com/docs/sdk/golang/start#run-the-example) * Run `go mod init github.com//` to initialise a go.mod file * Run `go mod tidy` to download the `surrealdb.go` dependency * Run `go run main.go` to run the example. Next steps[](https://surrealdb.com/docs/sdk/golang/start#next-steps) --------------------------------------------------------------------- Now that you have learned the basics of the Golang SDK for SurrealDB, you can learn more about the SDK and its methods [in the methods section](https://surrealdb.com/docs/sdk/golang/methods) and [data types section](https://surrealdb.com/docs/sdk/golang/data-types) . [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-golang/start.mdx) Copy page as Markdown [Installation](https://surrealdb.com/docs/sdk/golang/installation) [Data Types](https://surrealdb.com/docs/sdk/golang/data-types) ### On this page * [Prerequisites](https://surrealdb.com/docs/sdk/golang/start#prerequisites) * [Setup your project](https://surrealdb.com/docs/sdk/golang/start#setup-your-project) * [Run the example](https://surrealdb.com/docs/sdk/golang/start#run-the-example) * [Next steps](https://surrealdb.com/docs/sdk/golang/start#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-golang/start.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Surrealist | Settings and Customisation [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Introduction](https://surrealdb.com/docs/surrealist) [Installation](https://surrealdb.com/docs/surrealist/installation) [Getting Started](https://surrealdb.com/docs/surrealist/getting-started) Concepts [Introduction](https://surrealdb.com/docs/surrealist/concepts) [Sending queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) [Exploring database records](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) [Designing the database schema](https://surrealdb.com/docs/surrealist/concepts/designing-the-database-schema) [Manage database access](https://surrealdb.com/docs/surrealist/concepts/manage-database-access) [Sending queries with GraphQL](https://surrealdb.com/docs/surrealist/concepts/sending-queries-with-graphql) [Local database serving](https://surrealdb.com/docs/surrealist/concepts/local-database-serving) [Writing stored procedures](https://surrealdb.com/docs/surrealist/concepts/writing-stored-procedures) [Specialized API docs](https://surrealdb.com/docs/surrealist/concepts/specialized-api-docs) [Deploying your database](https://surrealdb.com/docs/surrealist/concepts/surreal-cloud) Advanced Topics [Introduction](https://surrealdb.com/docs/surrealist/advanced-topics) [Search and Shortcuts](https://surrealdb.com/docs/surrealist/advanced-topics/search-and-shortcuts) [Embedding Surrealist](https://surrealdb.com/docs/surrealist/advanced-topics/embedding-surrealist) [Connection Templates](https://surrealdb.com/docs/surrealist/advanced-topics/connection-templates) [Settings and Customisation](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation) [SurrealQL Editors](https://surrealdb.com/docs/surrealist/advanced-topics/surrealql-editors) [URL Intents](https://surrealdb.com/docs/surrealist/advanced-topics/intents) FAQs [Introduction](https://surrealdb.com/docs/surrealist/faqs) [FAQs](https://surrealdb.com/docs/surrealist/faqs/info) [Known Issues](https://surrealdb.com/docs/surrealist/faqs/known-issues) [Troubleshooting](https://surrealdb.com/docs/surrealist/troubleshooting) [surrealdb/surrealist](https://github.com/surrealdb/surrealist) [](https://surrealdb.com/docs/surrealist) [Advanced topics](https://surrealdb.com/docs/surrealist/advanced-topics) [Settings and Customisation](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation) Settings and customisation[](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#settings-and-customisation) ========================================================================================================================================== In the bottom left of the interface you can find a settings button (⚙) to open the settings dialog. These settings allow you to further customise the behaviour and appearance of Surrealist, such as switching between light and dark mode, increasing or decreasing the editor font sizes, and configure default values for new connections. Behaviour[](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#behaviour) -------------------------------------------------------------------------------------------------------- In this section you can configure the behaviour of the interface, such as connection and editor related settings. Appearance[](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#appearance) ---------------------------------------------------------------------------------------------------------- The appearance section allows you to scale the fonts in the editor and also select your default query output view and set the behaviour for your Query and designer views. Templates[](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#templates) -------------------------------------------------------------------------------------------------------- You can configure [connection templates](https://surrealdb.com/docs/surrealist/advanced-topics/connection-templates) to reuse connection details when creating new connections. This is especially useful when you find yourself connecting to similar databases frequently. Database Serving[](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#database-serving) ---------------------------------------------------------------------------------------------------------------------- This section allows you to alter the behaviour of the database serving functionality found in Surrealist Desktop, such as altering the port and specifying the path to the database executable. Feature flags[](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#feature-flags) ---------------------------------------------------------------------------------------------------------------- Feature flags allow you to enable or disable stable and experimental features in Surrealist to suit your needs best. Some of these features need to be stable and may not work as expected or be removed in future versions. To access the feature flags, press `Ctrl + K` or `Cmd + K` on your keyboard to open the command palette, then type in “Manage Feature Flags” and hit enter. You’ll be taken to the feature flags settings. After enabling a feature flag, you can access it by clicking on the settings button in the bottom left of the interface and selecting the feature flags tab. Here, you can enable or disable features. Here is a list of the current feature flags: | Options | Description | | --- | --- | | `feature_flags` | Toggle feature flags. | | `models_view` | Toggle the view for SurrealML models. | | `apidocs_view` | Toggle the view for API documentation. | | `themes` | Toggle between light and dark mode. | | `newsfeed` | Toggle the newsfeed view. | | `database_version_check` | Toggle the database version check in the Surrealist CLI | | `highlight_tool` | Toggle the highlight tool. | | `surreal_compat` | Toggle SurrealDB version between `1.x` and `2.x`. | | `changelog` | Toggle the changelog view to see the latest changes, read all or unread all. | [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/advanced-topics/settings-and-customisation.mdx) Copy page as Markdown [Connection Templates](https://surrealdb.com/docs/surrealist/advanced-topics/connection-templates) [SurrealQL Editors](https://surrealdb.com/docs/surrealist/advanced-topics/surrealql-editors) ### On this page * [Settings and customisation](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#settings-and-customisation) * [Behaviour](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#behaviour) * [Appearance](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#appearance) * [Templates](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#templates) * [Database Serving](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#database-serving) * [Feature flags](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation#feature-flags) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/advanced-topics/settings-and-customisation.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Install | Data migration [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/integrations) Data Management [Introduction](https://surrealdb.com/docs/integrations/data-management) [Airbyte](https://surrealdb.com/docs/integrations/data-management/airbyte) [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) Embeddings [Introduction](https://surrealdb.com/docs/integrations/embeddings) [Quick start with Python](https://surrealdb.com/docs/integrations/embeddings/python) [Quick start with Rust](https://surrealdb.com/docs/integrations/embeddings/rust) [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) Frameworks [Introduction](https://surrealdb.com/docs/integrations/frameworks) [Agno](https://surrealdb.com/docs/integrations/frameworks/agno) [Camel](https://surrealdb.com/docs/integrations/frameworks/camel) [CrewAI](https://surrealdb.com/docs/integrations/frameworks/crewai) [Dagster](https://surrealdb.com/docs/integrations/frameworks/dagster) [DeepEval](https://surrealdb.com/docs/integrations/frameworks/deepeval) [Dynamiq](https://surrealdb.com/docs/integrations/frameworks/dynamiq) [Feast](https://surrealdb.com/docs/integrations/frameworks/feast) [Google Agent](https://surrealdb.com/docs/integrations/frameworks/googleagent) [Langchain](https://surrealdb.com/docs/integrations/frameworks/langchain) [Llama Index](https://surrealdb.com/docs/integrations/frameworks/llama-index) [Smolagents](https://surrealdb.com/docs/integrations/frameworks/smolagents) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/integrations) [Data Management](https://surrealdb.com/docs/integrations/data-management) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) Install[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install#install) ================================================================================================ The Surreal Sync data migration tool can be installed using the following commands. # Linux (x86_64) curl -L https://github.com/surrealdb/surreal-sync/releases/latest/download/surreal-sync-x86_64-unknown-linux-gnu.tar.gz | tar xz sudo mv surreal-sync /usr/local/bin/ # macOS (Apple Silicon) curl -L https://github.com/surrealdb/surreal-sync/releases/latest/download/surreal-sync-aarch64-apple-darwin.tar.gz | tar xz sudo mv surreal-sync /usr/local/bin/ # Linux (ARM64) curl -L https://github.com/surrealdb/surreal-sync/releases/latest/download/surreal-sync-aarch64-unknown-linux-gnu.tar.gz | tar xz sudo mv surreal-sync /usr/local/bin/ For more ways to install or build from source, see the project’s [readme page](https://github.com/surrealdb/surreal-sync?tab=readme-ov-file#install-pre-built-binaries) . [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/install.mdx) Copy page as Markdown [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) ### On this page * [Install](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install#install) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/install.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Neo4J | Data migration [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/integrations) Data Management [Introduction](https://surrealdb.com/docs/integrations/data-management) [Airbyte](https://surrealdb.com/docs/integrations/data-management/airbyte) [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) Embeddings [Introduction](https://surrealdb.com/docs/integrations/embeddings) [Quick start with Python](https://surrealdb.com/docs/integrations/embeddings/python) [Quick start with Rust](https://surrealdb.com/docs/integrations/embeddings/rust) [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) Frameworks [Introduction](https://surrealdb.com/docs/integrations/frameworks) [Agno](https://surrealdb.com/docs/integrations/frameworks/agno) [Camel](https://surrealdb.com/docs/integrations/frameworks/camel) [CrewAI](https://surrealdb.com/docs/integrations/frameworks/crewai) [Dagster](https://surrealdb.com/docs/integrations/frameworks/dagster) [DeepEval](https://surrealdb.com/docs/integrations/frameworks/deepeval) [Dynamiq](https://surrealdb.com/docs/integrations/frameworks/dynamiq) [Feast](https://surrealdb.com/docs/integrations/frameworks/feast) [Google Agent](https://surrealdb.com/docs/integrations/frameworks/googleagent) [Langchain](https://surrealdb.com/docs/integrations/frameworks/langchain) [Llama Index](https://surrealdb.com/docs/integrations/frameworks/llama-index) [Smolagents](https://surrealdb.com/docs/integrations/frameworks/smolagents) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/integrations) [Data Management](https://surrealdb.com/docs/integrations/data-management) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) Neo4J[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#neo4j) ========================================================================================== Supported data types[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#supported-data-types) ------------------------------------------------------------------------------------------------------------------------ Neo4J data types are either fully supported via direct conversion, or partially supported via an object that maintains the information in the original type. For more on these data types, see [the project repository](https://github.com/surrealdb/surreal-sync/blob/main/docs/neo4j-data-types.md) . Import example[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#import-example) ------------------------------------------------------------------------------------------------------------ Log in to Neo4J, start server with user `neo4j` and password `password`, then run the following queries: Sample Neo4J dataCREATE (a:Person {name: 'Alice', age: 29}) CREATE (b:Person {name: 'Bob', age: 35}) CREATE (c:Person {name: 'Charlie', age: 40}) CREATE (a)-[:FRIENDS_WITH]->(b) CREATE (b)-[:FRIENDS_WITH]->(c) Next, start a surrealdb server with the command `surreal start --user root --pass root`. Then run the following command to import the data to SurrealDB: surreal-sync sync neo4j \ --source-uri "bolt://localhost:7687" \ --source-username "neo4j" \ --source-password "password" \ --to-namespace "test" \ --to-database "test" Log in to SurrealDB under namespace `test` and database `test`, use `SELECT *, friends_with->person AS friends FROM person` statement to see imported data: Result[\ {\ age: 29,\ friends: NONE,\ id: person:0,\ labels: [\ 'Person'\ ],\ name: 'Alice',\ neo4j_id: 0\ },\ {\ age: 35,\ friends: NONE,\ id: person:1,\ labels: [\ 'Person'\ ],\ name: 'Bob',\ neo4j_id: 1\ },\ {\ age: 40,\ friends: NONE,\ id: person:2,\ labels: [\ 'Person'\ ],\ name: 'Charlie',\ neo4j_id: 2\ }\ ] After migration[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#after-migration) -------------------------------------------------------------------------------------------------------------- After a migration is done, you may want to add a schema. As SurrealDB is schemaless by default, tables will be declared with the `DEFINE TABLE TYPE ANY SCHEMALESS PERMISSIONS NONE`. For example, if [this sample movie database](https://github.com/neo4j-graph-examples/movies/blob/main/scripts/movies.cypher) is imported, the Surrealist Designer view will simply show nine table names with no specified relation. ![alt text](https://surrealdb.com/docs/_astro/loose_schema.B45u86VM_1q91Ey.webp) Note that the graph relations have already been set up; they just have not been strictly defined in the schema. SELECT name, ->wrote->movie.title AS wrote FROM person ORDER BY name; -- Output [\ {\ name: 'Aaron Sorkin',\ wrote: [\ 'A Few Good Men'\ ]\ },\ {\ name: 'Al Pacino',\ wrote: []\ },\ {\ name: 'Alice',\ wrote: []\ },\ // and so on\ ] To begin setting a schema, you can use this sort of query to see exactly which relations exist. -- Get every type and destination of links from `person`, flatten into -- a single array, remove duplicates (SELECT VALUE ->?.{ val: id.tb() + '->' + out.tb() } FROM person).flatten().map(|$v| $v.val).distinct(); [\ 'friends_with->person',\ 'acted_in->movie',\ 'directed->movie',\ 'produced->movie',\ 'wrote->movie',\ 'follows->person',\ 'reviewed->movie'\ ] This information can now be used to overwrite the existing loose definitions into concrete `RELATION` tables with a specified `in` and `out`. DEFINE TABLE OVERWRITE friends_with TYPE RELATION IN person OUT person; DEFINE TABLE OVERWRITE follows TYPE RELATION IN person OUT person; DEFINE TABLE OVERWRITE acted_in TYPE RELATION IN person OUT movie; DEFINE TABLE OVERWRITE directed TYPE RELATION IN person OUT movie; DEFINE TABLE OVERWRITE wrote TYPE RELATION IN person OUT movie; DEFINE TABLE OVERWRITE follows TYPE RELATION IN person OUT person; DEFINE TABLE OVERWRITE reviewed TYPE RELATION IN person OUT movie; DEFINE TABLE OVERWRITE produced TYPE RELATION IN person OUT movie; ![alt text](https://surrealdb.com/docs/_astro/concrete_schema.pa1C9wPn_ZA12Oh.webp) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/neo4j.mdx) Copy page as Markdown [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) ### On this page * [Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#neo4j) * [Supported data types](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#supported-data-types) * [Import example](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#import-example) * [After migration](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j#after-migration) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/neo4j.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Configure an Instance [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [What is Surreal Cloud?](https://surrealdb.com/docs/cloud) [Surreal Cloud Architecture](https://surrealdb.com/docs/cloud/architecture) Get started [Introduction](https://surrealdb.com/docs/cloud/getting-started) [Create an Account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) [Create an Instance](https://surrealdb.com/docs/cloud/getting-started/create-an-instance) [Create an Organisation](https://surrealdb.com/docs/cloud/getting-started/create-an-organisation) Connect to an Instance [Introduction](https://surrealdb.com/docs/cloud/connect) [Querying in Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) [Connect via CLI](https://surrealdb.com/docs/cloud/connect/cli) [Connect via SDK](https://surrealdb.com/docs/cloud/connect/sdk) [Connect via HTTP using cURL](https://surrealdb.com/docs/cloud/connect/http) Advanced topics [Introduction](https://surrealdb.com/docs/cloud/advanced-topics) [Backup and restore](https://surrealdb.com/docs/cloud/advanced-topics/data-export-and-backup) [Configure an Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance) [Manage Organisation permissions](https://surrealdb.com/docs/cloud/advanced-topics/manage-organisation-permissions) [Migrating data](https://surrealdb.com/docs/cloud/advanced-topics/migrating-data) [Monitoring](https://surrealdb.com/docs/cloud/advanced-topics/monitoring) [Logs](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/logs) [Metrics](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/metrics) [Configure network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access) [Search and shortcuts](https://surrealdb.com/docs/cloud/advanced-topics/search-and-shortcuts) [SurrealQL editors](https://surrealdb.com/docs/cloud/advanced-topics/surrealql-editors) Billing and support [Introduction](https://surrealdb.com/docs/cloud/billing-and-support) [Billing](https://surrealdb.com/docs/cloud/billing-and-support/billing) [Support](https://surrealdb.com/docs/cloud/billing-and-support/support) [FAQs](https://surrealdb.com/docs/cloud/faqs) [Forum](https://surrealdb.com/community) [](https://surrealdb.com/docs/cloud) [Advanced topics](https://surrealdb.com/docs/cloud/advanced-topics) [Configure an Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance) Configure an Instance[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#configure-an-instance) ====================================================================================================================== After you have created an Instance, you can configure it to your needs. This includes changing the Instance type, compute nodes, and capabilities. To do this, click on the **Configure Instance** button on the Instance card; this will open up a configuration drawer. Instance Configuration[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-configuration) ------------------------------------------------------------------------------------------------------------------------ In the Instance configuration drawer, you can change the following settings: * **Capabilities**: the capabilities of the Instance, this controls the functionality available to users such as scripting, guest access, enabled endpoints for RPC and HTTP endpoints. You can also opt into beta features and restrict access to specific resources. * **Version**: the version of SurrealDB that will be used for the Instance. If you a new version is available, you will be prompted to upgrade and also see the release notes. ### Instance capabilities[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-capabilities) The capabilities you choose will determine the features available to your Instance. This includes scripting, guest access, enabled endpoints for RPC and HTTP and more. For RPC methods, HTTP endpoints and preview features, you can leave pick one of the following options: * **Allow all by default**: All endpoints are allowed by default and you can then select which endpoints to deny. * **Deny all by default**: All endpoints are denied by default and you can then select which endpoints to allow. On the other hand network access and functions provide a more granular control over the endpoints. > Important > > Learn more about configuring [network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access) > in the advanced topics section. ![Configure an Instance](https://surrealdb.com/docs/_astro/configure-dark.iKcgUmwj_Z2rPOIY.webp) Upgrade Instance[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#upgrade-instance) ------------------------------------------------------------------------------------------------------------ To upgrade an Instance, you can click the **Upgrade now** button on the Instance card. This will open a modal to select the tier you want to upgrade to as shown below. Depending on the tier you choose, and the resources you have allocated, you will be billed accordingly. When you click this button, you will have the following options: * **Instance type**: the type of the Instance. * **Storage capacity**: the size of the storage disk ### Instance type[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-type) While the Instance type you choose will determine the compute and memory available to your Instance. Before you hit the limit of your current plan, you can upgrade your plan to a higher tier using this option. First, select the **Instance type** option which will open a modal to select the tier you want to upgrade to as shown below. Depending on the tier you choose, and the resources you have allocated, you will be billed accordingly. ![Instance Type](https://surrealdb.com/docs/_astro/change-instance-type-1.D3r9mmSS_ZvUU2J.webp) ### Storage capacity[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#storage-capacity) The storage capacity is the amount of storage available to your Instance. This is used to store the database files and can be increased at any time. For paid plans, you can increase the storage capacity up to the maximum limit of your plan. > Important > > Storage expansion is unavailable for free instances. Upgrade your instance to unlock the ability to increase your storage capacity. ![Instance Usage](https://surrealdb.com/docs/_astro/view-instance-usage.CBQ6i6b9_qeiEQ.webp) Pause or Delete an Instance[](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#pause-or-delete-an-instance) ---------------------------------------------------------------------------------------------------------------------------------- From the overview page, you can delete an Instance by clicking the option button on the Instance card and selecting **Delete**. This will bring up a confirmation modal. Once confirmed, the Instance will be deleted. To pause an Instance, click the option button on the Instance card and select **Pause**. This will bring up a confirmation modal. Once confirmed, the Instance will be paused. ![Delete an Instance](https://surrealdb.com/docs/_astro/delete-instance.Z9h-dzaM_Z1QdL1.webp) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/advanced-topics/configure-an-instance.mdx) Copy page as Markdown [Backup and restore](https://surrealdb.com/docs/cloud/advanced-topics/data-export-and-backup) [Manage Organisation permissions](https://surrealdb.com/docs/cloud/advanced-topics/manage-organisation-permissions) ### On this page * [Instance Configuration](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-configuration) * [Instance capabilities](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-capabilities) * [Upgrade Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#upgrade-instance) * [Instance type](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-type) * [Storage capacity](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#storage-capacity) * [Pause or Delete an Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#pause-or-delete-an-instance) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/advanced-topics/configure-an-instance.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Get Started with Surreal Cloud [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [What is Surreal Cloud?](https://surrealdb.com/docs/cloud) [Surreal Cloud Architecture](https://surrealdb.com/docs/cloud/architecture) Get started [Introduction](https://surrealdb.com/docs/cloud/getting-started) [Create an Account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) [Create an Instance](https://surrealdb.com/docs/cloud/getting-started/create-an-instance) [Create an Organisation](https://surrealdb.com/docs/cloud/getting-started/create-an-organisation) Connect to an Instance [Introduction](https://surrealdb.com/docs/cloud/connect) [Querying in Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) [Connect via CLI](https://surrealdb.com/docs/cloud/connect/cli) [Connect via SDK](https://surrealdb.com/docs/cloud/connect/sdk) [Connect via HTTP using cURL](https://surrealdb.com/docs/cloud/connect/http) Advanced topics [Introduction](https://surrealdb.com/docs/cloud/advanced-topics) [Backup and restore](https://surrealdb.com/docs/cloud/advanced-topics/data-export-and-backup) [Configure an Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance) [Manage Organisation permissions](https://surrealdb.com/docs/cloud/advanced-topics/manage-organisation-permissions) [Migrating data](https://surrealdb.com/docs/cloud/advanced-topics/migrating-data) [Monitoring](https://surrealdb.com/docs/cloud/advanced-topics/monitoring) [Logs](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/logs) [Metrics](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/metrics) [Configure network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access) [Search and shortcuts](https://surrealdb.com/docs/cloud/advanced-topics/search-and-shortcuts) [SurrealQL editors](https://surrealdb.com/docs/cloud/advanced-topics/surrealql-editors) Billing and support [Introduction](https://surrealdb.com/docs/cloud/billing-and-support) [Billing](https://surrealdb.com/docs/cloud/billing-and-support/billing) [Support](https://surrealdb.com/docs/cloud/billing-and-support/support) [FAQs](https://surrealdb.com/docs/cloud/faqs) [Forum](https://surrealdb.com/community) [](https://surrealdb.com/docs/cloud) [Get Started](https://surrealdb.com/docs/cloud/getting-started) Get started with Surreal Cloud[](https://surrealdb.com/docs/cloud/getting-started#get-started-with-surreal-cloud) ================================================================================================================== Surreal Cloud provides services to help you build, manage, and deploy your database. Whether you are a beginner getting started with Surreal Cloud or an experienced user looking for specific information, this document provides a comprehensive understanding of Surreal Cloud. To get started with Surreal Cloud, you need to sign up for an account and create an instance. Once you have created a project, you can start building your database, managing your data, and deploying your application. Surreal Cloud offers several options for connecting to your created database instance, [including Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) , [a command-line interface](https://surrealdb.com/docs/cloud/connect/cli) , and [any of the supported SDKs](https://surrealdb.com/docs/cloud/connect/sdk) . [Sign up for Surreal Cloud](https://app.surrealdb.com/cloud) Next Steps[](https://surrealdb.com/docs/cloud/getting-started#next-steps) -------------------------------------------------------------------------- * [Create a Surreal Cloud account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) * [Create a Surreal Cloud Instance](https://surrealdb.com/docs/cloud/getting-started/create-an-instance) * [Connect to your Instance](https://surrealdb.com/docs/cloud/connect) * [Getting Started with Surreal Cloud](https://youtu.be/upm1lwaHmwU?si=8Xcmz-wFt6OMDc6R) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/getting-started/index.mdx) Copy page as Markdown [Surreal Cloud Architecture](https://surrealdb.com/docs/cloud/architecture) [Create an Account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) ### On this page * [Get started with Surreal Cloud](https://surrealdb.com/docs/cloud/getting-started#get-started-with-surreal-cloud) * [Next Steps](https://surrealdb.com/docs/cloud/getting-started#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/getting-started/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Connect to Instance | Surreal Cloud [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [What is Surreal Cloud?](https://surrealdb.com/docs/cloud) [Surreal Cloud Architecture](https://surrealdb.com/docs/cloud/architecture) Get started [Introduction](https://surrealdb.com/docs/cloud/getting-started) [Create an Account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) [Create an Instance](https://surrealdb.com/docs/cloud/getting-started/create-an-instance) [Create an Organisation](https://surrealdb.com/docs/cloud/getting-started/create-an-organisation) Connect to an Instance [Introduction](https://surrealdb.com/docs/cloud/connect) [Querying in Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) [Connect via CLI](https://surrealdb.com/docs/cloud/connect/cli) [Connect via SDK](https://surrealdb.com/docs/cloud/connect/sdk) [Connect via HTTP using cURL](https://surrealdb.com/docs/cloud/connect/http) Advanced topics [Introduction](https://surrealdb.com/docs/cloud/advanced-topics) [Backup and restore](https://surrealdb.com/docs/cloud/advanced-topics/data-export-and-backup) [Configure an Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance) [Manage Organisation permissions](https://surrealdb.com/docs/cloud/advanced-topics/manage-organisation-permissions) [Migrating data](https://surrealdb.com/docs/cloud/advanced-topics/migrating-data) [Monitoring](https://surrealdb.com/docs/cloud/advanced-topics/monitoring) [Logs](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/logs) [Metrics](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/metrics) [Configure network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access) [Search and shortcuts](https://surrealdb.com/docs/cloud/advanced-topics/search-and-shortcuts) [SurrealQL editors](https://surrealdb.com/docs/cloud/advanced-topics/surrealql-editors) Billing and support [Introduction](https://surrealdb.com/docs/cloud/billing-and-support) [Billing](https://surrealdb.com/docs/cloud/billing-and-support/billing) [Support](https://surrealdb.com/docs/cloud/billing-and-support/support) [FAQs](https://surrealdb.com/docs/cloud/faqs) [Forum](https://surrealdb.com/community) [](https://surrealdb.com/docs/cloud) [Connect to Instance](https://surrealdb.com/docs/cloud/connect) Connect to your Surreal Cloud Instance[](https://surrealdb.com/docs/cloud/connect#connect-to-your-surreal-cloud-instance) ========================================================================================================================== You can connect to your Surreal Cloud instances in three main ways. In this section you will explore the following connection options: * [querying in Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) : Surrealist is a query builder and IDE for SurrealDB. It allows you to connect to your Surreal Cloud Instance and run queries in a graphical interface. * [connect via CLI](https://surrealdb.com/docs/cloud/connect/cli) : using the command-line interface for SurrealDB, you can connect to your Surreal Cloud Instance and run queries in a command-line interface. * [connect via SDK](https://surrealdb.com/docs/cloud/connect/sdk) : Surreal Cloud supports a variety of SDKs for connecting to your Instance. You can use the SDKs to connect to your Instance and run queries in your preferred programming language. * [connect via HTTP](https://surrealdb.com/docs/cloud/connect/http) : Surreal Cloud supports a REST API for connecting to your Instance. You can use the API to connect to your Instance and run queries using CURL or any HTTP client. Before you begin[](https://surrealdb.com/docs/cloud/connect#before-you-begin) ------------------------------------------------------------------------------ To connect to an Instance, you must have the following: * [a Surreal Cloud account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) . * [an Instance to connect to](https://surrealdb.com/docs/cloud/getting-started/create-an-instance) . Next steps[](https://surrealdb.com/docs/cloud/connect#next-steps) ------------------------------------------------------------------ Explore the connection options for Surreal Cloud instances in detail. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/connect/index.mdx) Copy page as Markdown [Create an Organisation](https://surrealdb.com/docs/cloud/getting-started/create-an-organisation) [Querying in Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) ### On this page * [Connect to your Surreal Cloud Instance](https://surrealdb.com/docs/cloud/connect#connect-to-your-surreal-cloud-instance) * [Before you begin](https://surrealdb.com/docs/cloud/connect#before-you-begin) * [Next steps](https://surrealdb.com/docs/cloud/connect#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/connect/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # JSON lines | Data migration [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/integrations) Data Management [Introduction](https://surrealdb.com/docs/integrations/data-management) [Airbyte](https://surrealdb.com/docs/integrations/data-management/airbyte) [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) Embeddings [Introduction](https://surrealdb.com/docs/integrations/embeddings) [Quick start with Python](https://surrealdb.com/docs/integrations/embeddings/python) [Quick start with Rust](https://surrealdb.com/docs/integrations/embeddings/rust) [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) Frameworks [Introduction](https://surrealdb.com/docs/integrations/frameworks) [Agno](https://surrealdb.com/docs/integrations/frameworks/agno) [Camel](https://surrealdb.com/docs/integrations/frameworks/camel) [CrewAI](https://surrealdb.com/docs/integrations/frameworks/crewai) [Dagster](https://surrealdb.com/docs/integrations/frameworks/dagster) [DeepEval](https://surrealdb.com/docs/integrations/frameworks/deepeval) [Dynamiq](https://surrealdb.com/docs/integrations/frameworks/dynamiq) [Feast](https://surrealdb.com/docs/integrations/frameworks/feast) [Google Agent](https://surrealdb.com/docs/integrations/frameworks/googleagent) [Langchain](https://surrealdb.com/docs/integrations/frameworks/langchain) [Llama Index](https://surrealdb.com/docs/integrations/frameworks/llama-index) [Smolagents](https://surrealdb.com/docs/integrations/frameworks/smolagents) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/integrations) [Data Management](https://surrealdb.com/docs/integrations/data-management) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) JSONL Source Usage Guide[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#jsonl-source-usage-guide) ===================================================================================================================================== This page is a slimmed down version of [its counterpart](https://github.com/surrealdb/surreal-sync/blob/main/docs/jsonl.md) at the README documentation for the surreal-sync tool. Overview[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#overview) ----------------------------------------------------------------------------------------------------- The JSONL source in surreal-sync allows you to import JSON Lines (JSONL) files into SurrealDB. Each JSONL file becomes a table in SurrealDB, and each line in the file becomes a document in that table. JSONL source is particularly useful for: * Importing data from APIs that export in JSON (`jq` can be used to convert JSON to JSONL) * Migrating from document-based systems like Notion * Bulk loading structured JSON data, like configuration files or logs (even SurrealDB terminal output [when set to JSON mode](https://surrealdb.com/blog/two-new-ways-to-keep-an-eye-on-your-surrealdb-database#saving-logging-output-as-a-file) ) * Converting references between documents into SurrealDB’s record links (RecordIds) Basic Usage[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#basic-usage) ----------------------------------------------------------------------------------------------------------- surreal-sync sync jsonl \ --source-uri /path/to/jsonl/directory \ --to-namespace myns \ --to-database mydb Prerequisites[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#prerequisites) --------------------------------------------------------------------------------------------------------------- Before using JSONL source, ensure you have: 1. SurrealDB running locally or accessible via network 2. surreal-sync built and available in your PATH To start SurrealDB locally: surreal start --user root --pass root Example: Importing Notion-like Data[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#example-importing-notion-like-data) ---------------------------------------------------------------------------------------------------------------------------------------------------------- Let’s walk through an example using sample Notion-like data with pages, blocks, and databases. ### Sample JSONL Files[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#sample-jsonl-files) Create a directory with the following JSONL files. Note: each one of these has an `id` field, which is required in SurrealDB. To set another field as the id field, pass in the flag `--id-field "field_name"`. **databases.jsonl**: {"id": "db1", "name": "Documentation", "description": "Main documentation database", "created_at": "2023-12-01T10:00:00Z", "properties": {"status": "active", "version": 1.0}} {"id": "db2", "name": "API Docs", "description": "API reference documentation", "created_at": "2023-12-15T10:00:00Z", "properties": {"status": "beta", "version": 0.5}} **pages.jsonl**: {"id": "page1", "title": "Getting Started", "content": "Welcome to our documentation", "parent": {"type": "database_id", "database_id": "db1"}, "created_at": "2024-01-01T10:00:00Z"} {"id": "page2", "title": "Advanced Topics", "content": "Deep dive into advanced features", "parent": {"type": "page_id", "page_id": "page1"}, "created_at": "2024-01-02T10:00:00Z"} {"id": "page3", "title": "API Reference", "content": "Complete API documentation", "parent": {"type": "database_id", "database_id": "db2"}, "created_at": "2024-01-03T10:00:00Z"} **blocks.jsonl**: {"id": "block1", "type": "paragraph", "text": "This is a paragraph block", "parent": {"type": "page_id", "page_id": "page1"}, "order": 1} {"id": "block2", "type": "heading", "text": "Introduction", "level": 1, "parent": {"type": "page_id", "page_id": "page1"}, "order": 2} {"id": "block3", "type": "code", "text": "console.log('Hello World');", "language": "javascript", "parent": {"type": "page_id", "page_id": "page2"}, "order": 1} {"id": "block4", "type": "list", "items": ["Item 1", "Item 2", "Item 3"], "parent": {"type": "block_id", "block_id": "block2"}, "order": 3} ### Running the Import[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#running-the-import) Use the following command to import the data with conversion rules for parent references: surreal-sync sync jsonl \ --source-uri /workspace/tests/test_data/jsonl \ --to-namespace notion \ --to-database docs \ --surreal-endpoint http://surrealdb:8000 \ --surreal-username root \ --surreal-password root \ --rule 'type="database_id",database_id databases:database_id' \ --rule 'type="page_id",page_id pages:page_id' \ --rule 'type="block_id",block_id blocks:block_id' > Note > > Replace `http://surrealdb:8000` with `http://localhost:8000` if running SurrealDB locally on your machine. If successful, the command will complete without output. You can verify the import worked by checking the database as shown in the next section. ### Understanding Conversion Rules[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#understanding-conversion-rules) The `--rule` flag defines how to convert JSON objects into SurrealDB record links. The format is: --rule 'type="TYPE_VALUE",ID_FIELD TARGET_TABLE:ID_FIELD' For example: * `type="page_id",page_id pages:page_id` means: * When a JSON object has `"type": "page_id"` * Take the value from the `page_id` field * Convert it to a record link like `pages:page1` if the `page_id` is `page1`. Custom ID Fields[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#custom-id-fields) --------------------------------------------------------------------------------------------------------------------- By default, surreal-sync looks for an `id` field in each JSON object. You can specify a different field name: surreal-sync sync jsonl \ --source-uri /path/to/jsonl \ --to-namespace myns \ --to-database mydb \ --id-field "item_id" Example JSONL with custom ID field: {"item_id": "prod1", "name": "Widget", "price": 19.99} {"item_id": "prod2", "name": "Gadget", "price": 29.99} Advanced Options[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#advanced-options) --------------------------------------------------------------------------------------------------------------------- ### Batch Size[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#batch-size) Control how many records are processed at once: --batch-size 500 ### Dry Run[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#dry-run) Test the import without actually writing data: --dry-run ### Environment Variables[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#environment-variables) You can also use environment variables for configuration: export SURREAL_ENDPOINT=http://localhost:8000 export SURREAL_USERNAME=root export SURREAL_PASSWORD=root export SOURCE_URI=/path/to/jsonl surreal-sync sync jsonl \ --to-namespace myns \ --to-database mydb Tips and Best Practices[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#tips-and-best-practices) ----------------------------------------------------------------------------------------------------------------------------------- 1. **File Naming**: Name your JSONL files exactly as you want your SurrealDB tables to be named. 2. **ID Values**: Ensure all documents have unique ID values within each file. 3. **Data Types**: JSONL source preserves JSON data types: * Numbers remain as integers or floats * Strings remain as strings * Arrays and objects are preserved * Booleans remain as booleans * Null values are preserved 4. **References**: Use conversion rules to maintain relationships between documents across different tables. 5. **Performance**: For large datasets, adjust the batch size based on your system’s memory and SurrealDB’s capacity. Troubleshooting[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#troubleshooting) ------------------------------------------------------------------------------------------------------------------- 1. **Missing ID Field**: If you see “Missing ID field” errors, ensure your JSON objects have the ID field (default: “id”) or specify the correct field with `--id-field`. 2. **Invalid Rule Format**: Conversion rules must follow the exact format. Check for proper quoting and spacing. 3. **File Not Found**: Ensure the source URI points to a directory containing `.jsonl` files, not individual files. 4. **Connection Issues**: Verify SurrealDB is running and accessible at the specified endpoint. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/json-lines.mdx) Copy page as Markdown [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) ### On this page * [JSONL Source Usage Guide](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#jsonl-source-usage-guide) * [Overview](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#overview) * [Basic Usage](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#basic-usage) * [Prerequisites](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#prerequisites) * [Example: Importing Notion-like Data](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#example-importing-notion-like-data) * [Sample JSONL Files](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#sample-jsonl-files) * [Running the Import](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#running-the-import) * [Understanding Conversion Rules](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#understanding-conversion-rules) * [Custom ID Fields](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#custom-id-fields) * [Advanced Options](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#advanced-options) * [Batch Size](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#batch-size) * [Dry Run](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#dry-run) * [Environment Variables](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#environment-variables) * [Tips and Best Practices](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#tips-and-best-practices) * [Troubleshooting](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines#troubleshooting) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/json-lines.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # MongoDB | Data migration [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/integrations) Data Management [Introduction](https://surrealdb.com/docs/integrations/data-management) [Airbyte](https://surrealdb.com/docs/integrations/data-management/airbyte) [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) Embeddings [Introduction](https://surrealdb.com/docs/integrations/embeddings) [Quick start with Python](https://surrealdb.com/docs/integrations/embeddings/python) [Quick start with Rust](https://surrealdb.com/docs/integrations/embeddings/rust) [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) Frameworks [Introduction](https://surrealdb.com/docs/integrations/frameworks) [Agno](https://surrealdb.com/docs/integrations/frameworks/agno) [Camel](https://surrealdb.com/docs/integrations/frameworks/camel) [CrewAI](https://surrealdb.com/docs/integrations/frameworks/crewai) [Dagster](https://surrealdb.com/docs/integrations/frameworks/dagster) [DeepEval](https://surrealdb.com/docs/integrations/frameworks/deepeval) [Dynamiq](https://surrealdb.com/docs/integrations/frameworks/dynamiq) [Feast](https://surrealdb.com/docs/integrations/frameworks/feast) [Google Agent](https://surrealdb.com/docs/integrations/frameworks/googleagent) [Langchain](https://surrealdb.com/docs/integrations/frameworks/langchain) [Llama Index](https://surrealdb.com/docs/integrations/frameworks/llama-index) [Smolagents](https://surrealdb.com/docs/integrations/frameworks/smolagents) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/integrations) [Data Management](https://surrealdb.com/docs/integrations/data-management) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) MongoDB[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb#mongodb) ================================================================================================ Supported data types[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb#supported-data-types) -------------------------------------------------------------------------------------------------------------------------- MongoDB data types are either fully supported via direct conversion, or partially supported via an object that maintains the information in the original type. For more on these data types, see [the project repository](https://github.com/surrealdb/surreal-sync/blob/main/docs/neo4j-data-types.md) . Import example[](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb#import-example) -------------------------------------------------------------------------------------------------------------- Create file users.json containing data to be stored in MongoDB: [\ {\ "_id": { "$oid": "64a7a21fc25e3d2c7a3b55f1" },\ "name": "Alice Johnson",\ "email": "alice@example.com",\ "age": 29,\ "roles": ["admin", "editor"]\ },\ {\ "_id": { "$oid": "64a7a21fc25e3d2c7a3b55f2" },\ "name": "Bob Smith",\ "email": "bob@example.com",\ "age": 35,\ "roles": ["user"]\ }\ ] Next, import the data to MongoDB via the following command. mongoimport --db testdb --collection users --file users.json --jsonArray With the data imported, start the server at the path of your current directory. mongod --dbpath /your/current/directory With the MongoDB server running, start a SurrealDB server with `surreal start --user root --pass root`. The `surreal-sync` command can then be used to import the data from the running MongoDB instance. surreal-sync sync mongo-db \ --source-database "testdb" \ --to-namespace "production" \ --to-database "migrated_data" \ --source-uri "mongodb://localhost:27017" To confirm the data, log in to the SurrealDB database under namespace `production` and database `migrated_data`, and use the query `SELECT * FROM users` to see the data. Result[\ {\ age: 29,\ email: 'alice@example.com',\ id: users:64a7a21fc25e3d2c7a3b55f1,\ name: 'Alice Johnson',\ roles: [\ 'admin',\ 'editor'\ ]\ },\ {\ age: 35,\ email: 'bob@example.com',\ id: users:64a7a21fc25e3d2c7a3b55f2,\ name: 'Bob Smith',\ roles: [\ 'user'\ ]\ }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/mongodb.mdx) Copy page as Markdown [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) ### On this page * [MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb#mongodb) * [Supported data types](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb#supported-data-types) * [Import example](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb#import-example) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/surreal-sync/mongodb.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Surrealist | Getting Started [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Introduction](https://surrealdb.com/docs/surrealist) [Installation](https://surrealdb.com/docs/surrealist/installation) [Getting Started](https://surrealdb.com/docs/surrealist/getting-started) Concepts [Introduction](https://surrealdb.com/docs/surrealist/concepts) [Sending queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) [Exploring database records](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) [Designing the database schema](https://surrealdb.com/docs/surrealist/concepts/designing-the-database-schema) [Manage database access](https://surrealdb.com/docs/surrealist/concepts/manage-database-access) [Sending queries with GraphQL](https://surrealdb.com/docs/surrealist/concepts/sending-queries-with-graphql) [Local database serving](https://surrealdb.com/docs/surrealist/concepts/local-database-serving) [Writing stored procedures](https://surrealdb.com/docs/surrealist/concepts/writing-stored-procedures) [Specialized API docs](https://surrealdb.com/docs/surrealist/concepts/specialized-api-docs) [Deploying your database](https://surrealdb.com/docs/surrealist/concepts/surreal-cloud) Advanced Topics [Introduction](https://surrealdb.com/docs/surrealist/advanced-topics) [Search and Shortcuts](https://surrealdb.com/docs/surrealist/advanced-topics/search-and-shortcuts) [Embedding Surrealist](https://surrealdb.com/docs/surrealist/advanced-topics/embedding-surrealist) [Connection Templates](https://surrealdb.com/docs/surrealist/advanced-topics/connection-templates) [Settings and Customisation](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation) [SurrealQL Editors](https://surrealdb.com/docs/surrealist/advanced-topics/surrealql-editors) [URL Intents](https://surrealdb.com/docs/surrealist/advanced-topics/intents) FAQs [Introduction](https://surrealdb.com/docs/surrealist/faqs) [FAQs](https://surrealdb.com/docs/surrealist/faqs/info) [Known Issues](https://surrealdb.com/docs/surrealist/faqs/known-issues) [Troubleshooting](https://surrealdb.com/docs/surrealist/troubleshooting) [surrealdb/surrealist](https://github.com/surrealdb/surrealist) [](https://surrealdb.com/docs/surrealist) [Getting Started](https://surrealdb.com/docs/surrealist/getting-started) Getting Started[](https://surrealdb.com/docs/surrealist/getting-started#getting-started) ========================================================================================= This page will guide you through the process of setting up and using Surrealist. At this point we assume you have already installed Surrealist on your system. If you have not done so yet, please refer to the [installation guide](https://surrealdb.com/docs/surrealist/installation) . Start screen[](https://surrealdb.com/docs/surrealist/getting-started#start-screen) ----------------------------------------------------------------------------------- When launching Surrealist for the first time you will be greeted by the start screen. This screen allows you to easy jump into a sandbox environment or create a new connection to your SurrealDB instance. If you are new to SurrealDB or looking to experiment with SurrealQL, we recommend starting with the sandbox environment. This environment allows you to test and experiment without installing the SurrealDB database or storing data persistently. If you are looking to connect to a local or remote hosted SurrealDB instance, you can create a new connection by clicking the **Create new** button. This will open the connection dialog, then select the **Connection** option where you can enter your database details. Alternatively, you can select the **Cloud instance** option where you can create a new connection to a Surreal Cloud instance. Learn more about Surreal Cloud in the [Surreal Cloud documentation](https://surrealdb.com/docs/cloud) . ![Start screen](https://surrealdb.com/docs/surrealist/getting-started) Connections[](https://surrealdb.com/docs/surrealist/getting-started#connections) --------------------------------------------------------------------------------- In order to interact with a SurrealDB database you must first create a connection. Connections store the details required to connect to a database, such as the endpoint, namespace, database, and authentication details. When you select an active connection in Surrealist, you will connect to the database and be able to interact with it using the available interface views. After opening a connection, you can switch to another connection at any time by pressing the connection name in the top left of the interface. This will open the connection list allowing you to switch to another connection, or create a new one. Within the connection list you will also find a special connection called Sandbox, which is always available and allows you to test and experiment without storing data persistently. This connection is useful for learning SurrealQL, testing queries, and more. As the Sandbox connection runs inside a single thread within the browser’s WebAssembly engine, it should not be used to estimate or evaluate performance. Creating a connection[](https://surrealdb.com/docs/surrealist/getting-started#creating-a-connection) ----------------------------------------------------------------------------------------------------- When creating a new connection you will be prompted with a dialog to enter your database details. These details are required to connect to your SurrealDB instance. * **Protocol dropdown**: The protocol determines how Surrealist connects to the database. If you are unsure, select `HTTP`. * **Endpoint**: The address of your database. This can be localhost, an ip address, or a domain name. * **Authentication method**: The auth method determines how you want to authenticate with your database. > Important > > In the new Surrealist redesign, the namespace and database fields are now configured per connection before you can [send queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) > . Depending on the selected authentication method, you will be prompted to enter additional details such as a username and password, or a token. ![Http connection](https://surrealdb.com/docs/surrealist/getting-started) After filling in all fields, press **Create** to continue. If the connection was established successfully, you should see a green indicator next to the session name in the top left of the interface. If the connection failed and you see a red indicator, make sure all your details are correct and update your connection accordingly. You can edit connection details again by expanding the connection list, and choosing **Edit** on your connection. Using the Sandbox[](https://surrealdb.com/docs/surrealist/getting-started#using-the-sandbox) --------------------------------------------------------------------------------------------- As mentioned earlier, the sandbox environment is a special connection that allows you to test and learn SurrealQL. When active, the toolbar will provide some additional buttons useful for working with the sandbox environment. * **Reset sandbox**: This button will clear any records and schema you have configured in the sandbox. Use this button to start fresh or clear your sandbox environment. * **Load demo dataset**: This button will load an official demo dataset into the sandbox environment. This dataset contains a set of records and relations that you can explore and query. Creating a Surreal Cloud Connection[](https://surrealdb.com/docs/surrealist/getting-started#creating-a-surreal-cloud-connection) --------------------------------------------------------------------------------------------------------------------------------- A Surreal Cloud connection allows you to connect to a SurrealDB Instance. This connection type is useful for working with production databases, and for using SurrealDB’s cloud-native features. To make a Surreal Cloud connection, you will need to create a Surreal Cloud account. Learn more about Surreal Cloud in the [Surreal Cloud documentation](https://surrealdb.com/docs/cloud) . Navigation[](https://surrealdb.com/docs/surrealist/getting-started#navigation) ------------------------------------------------------------------------------- On the left side of the interface you will find the navigation sidebar. This sidebar allows you to switch between different views in Surrealist. Each view is focused on a specific workflow for working with the database. If you are just getting started, we recommend one of the following: * Choose the **Query** view if you want to start writing your SurrealQL queries and execute them. * Choose the **Explorer** view if you want to explore existing records and the relations between them. Additional views are available for managing your schema, writing stored procedures (functions), exploring API documentation, and more. In addition to the different views, you can also find a button to open [Surrealist Search](https://surrealdb.com/docs/surrealist/advanced-topics/search-and-shortcuts) , and a button to open the [settings](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation) . Within the settings dialog you can also find an option to change the appearance of the navigation sidebar. Video guide[](https://surrealdb.com/docs/surrealist/getting-started#video-guide) --------------------------------------------------------------------------------- After reading this guide, you can watch the following video which covers some features of Surrealist that can help you get started. This includes organising connections and connection groups, [recordID](https://surrealdb.com/docs/surrealql/datamodel/ids) inspector, [editor shortcuts](https://surrealdb.com/docs/surrealist/advanced-topics/search-and-shortcuts) , surrealist search and useful settings. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/getting-started.mdx) Copy page as Markdown [Installation](https://surrealdb.com/docs/surrealist/installation) [Concepts](https://surrealdb.com/docs/surrealist/concepts) ### On this page * [Start screen](https://surrealdb.com/docs/surrealist/getting-started#start-screen) * [Connections](https://surrealdb.com/docs/surrealist/getting-started#connections) * [Creating a connection](https://surrealdb.com/docs/surrealist/getting-started#creating-a-connection) * [Using the Sandbox](https://surrealdb.com/docs/surrealist/getting-started#using-the-sandbox) * [Creating a Surreal Cloud Connection](https://surrealdb.com/docs/surrealist/getting-started#creating-a-surreal-cloud-connection) * [Navigation](https://surrealdb.com/docs/surrealist/getting-started#navigation) * [Video guide](https://surrealdb.com/docs/surrealist/getting-started#video-guide) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/getting-started.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # n8n | Data Management [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/integrations) Data Management [Introduction](https://surrealdb.com/docs/integrations/data-management) [Airbyte](https://surrealdb.com/docs/integrations/data-management/airbyte) [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) [Installing the migration tool](https://surrealdb.com/docs/integrations/data-management/surreal-sync/install) [Migrate from JSON lines](https://surrealdb.com/docs/integrations/data-management/surreal-sync/json-lines) [Migrate from MongoDB](https://surrealdb.com/docs/integrations/data-management/surreal-sync/mongodb) [Migrate from Neo4J](https://surrealdb.com/docs/integrations/data-management/surreal-sync/neo4j) [Unstructured](https://surrealdb.com/docs/integrations/data-management/unstructured) Embeddings [Introduction](https://surrealdb.com/docs/integrations/embeddings) [Quick start with Python](https://surrealdb.com/docs/integrations/embeddings/python) [Quick start with Rust](https://surrealdb.com/docs/integrations/embeddings/rust) [Mistral](https://surrealdb.com/docs/integrations/embeddings/mistral) [Fastembed](https://surrealdb.com/docs/integrations/embeddings/fastembed) [OpenAI](https://surrealdb.com/docs/integrations/embeddings/openai) Frameworks [Introduction](https://surrealdb.com/docs/integrations/frameworks) [Agno](https://surrealdb.com/docs/integrations/frameworks/agno) [Camel](https://surrealdb.com/docs/integrations/frameworks/camel) [CrewAI](https://surrealdb.com/docs/integrations/frameworks/crewai) [Dagster](https://surrealdb.com/docs/integrations/frameworks/dagster) [DeepEval](https://surrealdb.com/docs/integrations/frameworks/deepeval) [Dynamiq](https://surrealdb.com/docs/integrations/frameworks/dynamiq) [Feast](https://surrealdb.com/docs/integrations/frameworks/feast) [Google Agent](https://surrealdb.com/docs/integrations/frameworks/googleagent) [Langchain](https://surrealdb.com/docs/integrations/frameworks/langchain) [Llama Index](https://surrealdb.com/docs/integrations/frameworks/llama-index) [Smolagents](https://surrealdb.com/docs/integrations/frameworks/smolagents) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/integrations) [Data Management](https://surrealdb.com/docs/integrations/data-management) [n8n](https://surrealdb.com/docs/integrations/data-management/n8n) n8n[](https://surrealdb.com/docs/integrations/data-management/n8n#n8n) ======================================================================= This guide shows how to integrate SurrealDB with [n8n](https://n8n.io/) , a [fair-code licensed](https://docs.n8n.io/reference/license/) workflow automation platform. The official n8n node for SurrealDB. It provides both action and tool nodes to interact with a SurrealDB database, allowing you to create, read, update, and delete records, as well as execute custom SurrealQL queries. It is available in the [n8n Community Nodes](https://docs.n8n.io/integrations/community-nodes/) repository. > Important > > As with all community nodes, this node works only with self-hosted n8n instances, not with n8n Cloud. This node has been tested with SurrealDB `v2.x` Features[](https://surrealdb.com/docs/integrations/data-management/n8n#features) --------------------------------------------------------------------------------- * **Dual Node Types**: Functions as both an action node and a tool node for AI workflows * **Complete CRUD Operations**: Create, read, update, and delete SurrealDB records * **Custom Queries**: Execute any SurrealQL query with full parameter support * **Enhanced Query Builder**: Visual interface for building `SELECT` queries with `WHERE`, `ORDER BY`, `GROUP BY`, and other clauses * **Table Operations**: List fields and explore table structure * **Relationship Support**: Query and manage record relationships * **Native Data Format**: Works with SurrealDB’s native data formats * **Connection Pooling**: Configurable connection pooling for improved performance and resource management * **Enhanced Error Handling**: Comprehensive error classification, automatic retry logic, and connection recovery * **Intelligent Recovery**: Different error handling strategies for different operation types * **Detailed Error Reporting**: Rich error context with categorization and severity levels * **Pool Monitoring**: Built-in pool statistics and performance monitoring Prerequisites[](https://surrealdb.com/docs/integrations/data-management/n8n#prerequisites) ------------------------------------------------------------------------------------------- 1. You need a self-hosted n8n instance (`v0.214.0` or later recommended). 2. You need access to a SurrealDB instance (`2.0.0` or later recommended). Installation Steps[](https://surrealdb.com/docs/integrations/data-management/n8n#installation-steps) ----------------------------------------------------------------------------------------------------- 1. Open your n8n instance 2. Go to **Settings** > **Community Nodes** 3. Click **Install** 4. Enter `n8n-nodes-surrealdb` and click **Install** 5. Restart your n8n instance if prompted > Note > > To use this node as a tool in AI workflows, you must set the environment variable `N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true`. Configuration[](https://surrealdb.com/docs/integrations/data-management/n8n#configuration) ------------------------------------------------------------------------------------------- In order to use SurrealDB in n8n, you need to configure the SurrealDB node. ### Credentials[](https://surrealdb.com/docs/integrations/data-management/n8n#credentials) To use the SurrealDB node, you need to create credentials with the following properties: | Property | Description | | --- | --- | | **Connection String** | The connection string to your SurrealDB instance (must start with `http://` or `https://`). WebSocket connections (`ws://` or `wss://`) are not supported. | | **Authentication** | Choose the authentication scope: | | **Root** | Full access to all namespaces and databases | | **Namespace** | Access limited to a specific namespace | | **Database** | Access limited to a specific database within a namespace | | **Username** | Username for authentication | | **Password** | Password for authentication | | **Namespace** | Target namespace (required for Namespace and Database authentication) | | **Database** | Target database (required for Database authentication) | The authentication type you choose affects how namespace and database information is handled. Depending on the authentication type, you will need to provide different information. See the table below for more details. | Authentication Type | Access Scope | Required Fields | Optional Fields | Override Capability | | --- | --- | --- | --- | --- | | **Root Authentication** | All namespaces and databases | None | Namespace, Database | Can override namespace/database at node level | | **Namespace Authentication** | All databases within a specific namespace | Namespace | Database | Can override database at node level | | **Database Authentication** | Specific database within a specific namespace | Namespace, Database | None | Can override both at node level for specific operations | ### Node-Level Namespace and Database Overrides[](https://surrealdb.com/docs/integrations/data-management/n8n#node-level-namespace-and-database-overrides) For most operations, you can override the namespace and database settings from your credentials: 1. In the node configuration, expand the **Options** section 2. Enter values in the **Namespace** and/or **Database** fields 3. These values will take precedence over the credential settings for that specific operation 4. You will be required to provide a namespace when using Namespace authentication 5. You will be required to provide both a namespace and database when using Database authentication Operations[](https://surrealdb.com/docs/integrations/data-management/n8n#operations) ------------------------------------------------------------------------------------- The SurrealDB node provides a comprehensive set of operations organized by resource type. For anything not covered, you can use the **Execute Query** operation. | Category | Operation | Description | | --- | --- | --- | | **Record Operations** | **Create Record** | Create a single record in a table | | **Get Record** | Retrieve a specific record by ID | | **Update Record** | Update a specific record by ID | | **Upsert Record** | Create or update a record (insert if not exists, update if exists) | | **Delete Record** | Delete a specific record by ID | | **Table Operations** | **Get All Records** | Retrieve all records from a table | | **Create Many** | Create multiple records in a table | | **Get Many** | Retrieve multiple records by IDs | | **Update All Records** | Update all records in a table | | **Delete All Records** | Delete all records from a table | | **Merge All Records** | Merge the same data into all records in a table | | **Create Table** | Define a new table with optional schema | | **Delete Table** | Remove a table from the database | | **Get Table** | Retrieve information about a table | | **Field Operations** | **List Fields** | List all fields defined on a table | | **Create Field** | Create a new field on a table | | **Delete Field** | Delete a field from a table | | **Index Operations** | **Create Index** | Create a new index on a table | | **Delete Index** | Delete an index from a table | | **Relationship Operations** | **Create Relationship** | Create a relationship between two records | | **Delete Relationship** | Delete a relationship between records | | **Query Relationships** | Query relationships between records | | **Query Operations** | **Execute Query** | Execute a raw SurrealQL query with parameters | | **Build Select Query** | Build SELECT queries using a visual interface with WHERE, ORDER BY, GROUP BY, and other clauses | | **System Operations** | **Health Check** | Check if the database instance is responsive | | **Version** | Get the version of the SurrealDB instance | | **Get Pool Statistics** | Monitor connection pool performance and statistics | Understanding SurrealDB and n8n Integration[](https://surrealdb.com/docs/integrations/data-management/n8n#understanding-surrealdb-and-n8n-integration) ------------------------------------------------------------------------------------------------------------------------------------------------------- ### Connection Protocol[](https://surrealdb.com/docs/integrations/data-management/n8n#connection-protocol) > Important > > Due to n8n’s architecture, this node only supports HTTP/HTTPS connections to SurrealDB. WebSocket connections (WS/WSS) are not supported. Your connection string must start with `http://` or `https://` (not `ws://` or `wss://`). This means that when configuring your SurrealDB instance, ensure it’s accessible via `HTTP/HTTPS`. If you’re using SurrealDB Cloud or another instance that only offers WebSocket connections, you’ll need to set up a [self-hosted SurrealDB instance](https://surrealdb.com/pricing) with HTTP enabled. This limitation is due to how n8n handles connections and executes node operations. You can read more about this in the [n8n documentation](https://docs.n8n.io/integrations/community-nodes/n8n-nodes-surrealdb/connection-protocol/) . This node uses the HTTP/HTTPS protocol exclusively, which means that each operation creates a new connection to SurrealDB, the connection is closed after the operation completes, and no persistent connection is maintained between operations. ### Connection Pooling[](https://surrealdb.com/docs/integrations/data-management/n8n#connection-pooling) The SurrealDB node includes comprehensive connection pooling to improve performance and resource management. Connection pooling allows the node to reuse database connections across multiple operations, reducing connection overhead and improving response times. #### Pool Configuration Options[](https://surrealdb.com/docs/integrations/data-management/n8n#pool-configuration-options) You can configure the connection pool through the “Connection Pooling” options in any node operation: * **Max Connections** (default: 10): Maximum number of connections in the pool * **Min Connections** (default: 2): Minimum number of connections to keep in the pool * **Acquire Timeout** (default: 30000ms): Maximum time to wait for a connection from the pool * **Health Check Interval** (default: 60000ms): Interval between health checks for pool connections * **Max Idle Time** (default: 300000ms): Maximum time a connection can remain idle before being closed * **Retry Attempts** (default: 3): Number of retry attempts for failed connection acquisitions * **Retry Delay** (default: 1000ms): Delay between retry attempts #### Pool Monitoring[](https://surrealdb.com/docs/integrations/data-management/n8n#pool-monitoring) Use the **System > Get Pool Statistics** operation to monitor pool performance: { "poolStatistics": { "totalConnections": 5, "activeConnections": 2, "idleConnections": 3, "waitingRequests": 0, "totalRequests": 150, "failedRequests": 2, "averageResponseTime": 45, "successRate": 99 }, "performance": { "averageResponseTimeMs": 45, "requestsPerSecond": 2, "errorRate": 1 }, "poolHealth": { "utilizationRate": 40, "availableConnections": 3, "waitingRequests": 0 } } ### SurrealDB Result Handling[](https://surrealdb.com/docs/integrations/data-management/n8n#surrealdb-result-handling) SurrealDB operations often return empty results rather than errors when no matching data is found. This behavior differs from many other databases and can be important to understand when building workflows: * **Empty Results vs. Errors**: A query for a non-existent record returns an empty result, not an error * **Always Output Data**: The “Always Output Data” option is particularly useful with SurrealDB to ensure your workflow continues even when no results are found ### Working with SurrealDB Data Types[](https://surrealdb.com/docs/integrations/data-management/n8n#working-with-surrealdb-data-types) SurrealDB supports rich data types that map well to n8n’s JSON handling: * **Records and IDs**: SurrealDB record IDs use the format `table:id` * **Relationships**: Relationships are first-class citizens in SurrealDB * **Arrays and Objects**: Nested data structures are fully supported Error Handling[](https://surrealdb.com/docs/integrations/data-management/n8n#error-handling) --------------------------------------------------------------------------------------------- The SurrealDB node includes a comprehensive error handling and recovery system that automatically manages common database issues: ### Automatic Error Classification[](https://surrealdb.com/docs/integrations/data-management/n8n#automatic-error-classification) The system automatically categorizes errors into different types: | Error Type | Description | | --- | --- | | **Connection Errors** | Network issues, timeouts, connection refused | | **Authentication Errors** | Invalid credentials, unauthorized access | | **Query Errors** | Syntax errors, malformed queries | | **Validation Errors** | Invalid data, missing required fields | | **System Errors** | Database server issues, internal errors | ### Intelligent Retry Logic[](https://surrealdb.com/docs/integrations/data-management/n8n#intelligent-retry-logic) * **Exponential Backoff**: Automatic retry with increasing delays * **Operation-Specific Retries**: Different retry strategies for read vs write operations * **Configurable Limits**: Adjustable retry counts and delays * **Smart Error Filtering**: Only retry on recoverable errors ### Connection Recovery[](https://surrealdb.com/docs/integrations/data-management/n8n#connection-recovery) * **Automatic Reconnection**: Reconnects to SurrealDB on connection failures * **Re-authentication**: Automatically re-authenticates after reconnection * **Connection Validation**: Verifies connection health before retrying operations ### Enhanced Error Reporting[](https://surrealdb.com/docs/integrations/data-management/n8n#enhanced-error-reporting) When `Continue on Fail` is enabled, errors include detailed information: { "error": { "message": "Connection timeout", "category": "TIMEOUT_ERROR", "severity": "MEDIUM", "retryable": true, "context": { "operation": "executeQuery", "itemIndex": 0, "timestamp": "2024-01-15T10:30:00Z", "recoveryStrategy": "CONNECTION_RECOVERY" } } } ### Error Handling Strategies[](https://surrealdb.com/docs/integrations/data-management/n8n#error-handling-strategies) Different operation types use different error handling strategies: * **Read Operations**: Faster retries, continue on low/medium errors * **Write Operations**: More retries, stop on medium+ errors * **Critical Operations**: Minimal retries, stop on any error * **Bulk Operations**: Moderate retries, handle rate limiting For detailed information about the error handling system, see [Error Handling Documentation](https://github.com/surrealdb/n8n-nodes-surrealdb/blob/main/docs/ERROR_HANDLING.md) . Resources[](https://surrealdb.com/docs/integrations/data-management/n8n#resources) ----------------------------------------------------------------------------------- * [n8n Community Nodes Documentation](https://docs.n8n.io/integrations/community-nodes/) * [SurrealDB Documentation](https://surrealdb.com/docs) * [SurrealQL Reference](https://surrealdb.com/docs/surrealql) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/n8n.mdx) Copy page as Markdown [Fivetran](https://surrealdb.com/docs/integrations/data-management/fivetran) [Surreal Sync](https://surrealdb.com/docs/integrations/data-management/surreal-sync) ### On this page * [Features](https://surrealdb.com/docs/integrations/data-management/n8n#features) * [Prerequisites](https://surrealdb.com/docs/integrations/data-management/n8n#prerequisites) * [Installation Steps](https://surrealdb.com/docs/integrations/data-management/n8n#installation-steps) * [Configuration](https://surrealdb.com/docs/integrations/data-management/n8n#configuration) * [Credentials](https://surrealdb.com/docs/integrations/data-management/n8n#credentials) * [Node-Level Namespace and Database Overrides](https://surrealdb.com/docs/integrations/data-management/n8n#node-level-namespace-and-database-overrides) * [Operations](https://surrealdb.com/docs/integrations/data-management/n8n#operations) * [Understanding SurrealDB and n8n Integration](https://surrealdb.com/docs/integrations/data-management/n8n#understanding-surrealdb-and-n8n-integration) * [Connection Protocol](https://surrealdb.com/docs/integrations/data-management/n8n#connection-protocol) * [Connection Pooling](https://surrealdb.com/docs/integrations/data-management/n8n#connection-pooling) * [SurrealDB Result Handling](https://surrealdb.com/docs/integrations/data-management/n8n#surrealdb-result-handling) * [Working with SurrealDB Data Types](https://surrealdb.com/docs/integrations/data-management/n8n#working-with-surrealdb-data-types) * [Error Handling](https://surrealdb.com/docs/integrations/data-management/n8n#error-handling) * [Automatic Error Classification](https://surrealdb.com/docs/integrations/data-management/n8n#automatic-error-classification) * [Intelligent Retry Logic](https://surrealdb.com/docs/integrations/data-management/n8n#intelligent-retry-logic) * [Connection Recovery](https://surrealdb.com/docs/integrations/data-management/n8n#connection-recovery) * [Enhanced Error Reporting](https://surrealdb.com/docs/integrations/data-management/n8n#enhanced-error-reporting) * [Error Handling Strategies](https://surrealdb.com/docs/integrations/data-management/n8n#error-handling-strategies) * [Resources](https://surrealdb.com/docs/integrations/data-management/n8n#resources) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-integrations/data-management/n8n.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Surrealist | Exploring database records [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Introduction](https://surrealdb.com/docs/surrealist) [Installation](https://surrealdb.com/docs/surrealist/installation) [Getting Started](https://surrealdb.com/docs/surrealist/getting-started) Concepts [Introduction](https://surrealdb.com/docs/surrealist/concepts) [Sending queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) [Exploring database records](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) [Designing the database schema](https://surrealdb.com/docs/surrealist/concepts/designing-the-database-schema) [Manage database access](https://surrealdb.com/docs/surrealist/concepts/manage-database-access) [Sending queries with GraphQL](https://surrealdb.com/docs/surrealist/concepts/sending-queries-with-graphql) [Local database serving](https://surrealdb.com/docs/surrealist/concepts/local-database-serving) [Writing stored procedures](https://surrealdb.com/docs/surrealist/concepts/writing-stored-procedures) [Specialized API docs](https://surrealdb.com/docs/surrealist/concepts/specialized-api-docs) [Deploying your database](https://surrealdb.com/docs/surrealist/concepts/surreal-cloud) Advanced Topics [Introduction](https://surrealdb.com/docs/surrealist/advanced-topics) [Search and Shortcuts](https://surrealdb.com/docs/surrealist/advanced-topics/search-and-shortcuts) [Embedding Surrealist](https://surrealdb.com/docs/surrealist/advanced-topics/embedding-surrealist) [Connection Templates](https://surrealdb.com/docs/surrealist/advanced-topics/connection-templates) [Settings and Customisation](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation) [SurrealQL Editors](https://surrealdb.com/docs/surrealist/advanced-topics/surrealql-editors) [URL Intents](https://surrealdb.com/docs/surrealist/advanced-topics/intents) FAQs [Introduction](https://surrealdb.com/docs/surrealist/faqs) [FAQs](https://surrealdb.com/docs/surrealist/faqs/info) [Known Issues](https://surrealdb.com/docs/surrealist/faqs/known-issues) [Troubleshooting](https://surrealdb.com/docs/surrealist/troubleshooting) [surrealdb/surrealist](https://github.com/surrealdb/surrealist) [](https://surrealdb.com/docs/surrealist) [Concepts](https://surrealdb.com/docs/surrealist/concepts) [Exploring database records](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) Exploring database records[](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#exploring-database-records) ================================================================================================================================= The second view you can find in Surrealist is the Explorer view, which lists out tables and allows you to browse through all records. This view is especially useful when you want to effortlessly explore your data, compare records, or edit their contents. ![Surrealist explorer view](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) ### Tables panel[](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#tables-panel) As the name implies, the tables panel will list out all tables in the database. The icon to the left indicates if a table is a normal table or an edge table. The + button in the top right of this panel allows you to quickly define new tables without switching to the Designer view. > Note > > In the [Designer view](https://surrealdb.com/docs/surrealist/concepts/designing-the-database-schema) > section we will explain more in-depth about what constitutes tables as normal or edge tables. ### Record explorer panel[](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#record-explorer-panel) After selecting a table in the tables panel, the records in this table are listed out here, grouped into navigable pages. You can press any record to open it in the inspector panel where you can view and edit record contents and view its relationships. Pressing the ▼ button in the top right of this panel expands an input in which you can enter a [WHERE](https://surrealdb.com/docs/surrealql/statements/select#filter-queries-using-the-where-clause) clause to filter down the displayed records on certain conditions. Additionally, the + button can be used to manually create new records in the current table. ### Inspector panel[](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#inspector-panel) The inspector panel, although available globally, is mostly used in the Explorer view to view and edit the contents of a record. You can also use the inspector to view and follow the relationships of a record, as well as delete records. Import Files[](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#import-files) ----------------------------------------------------------------------------------------------------- Surrealist allows you to import files into your database in the Explorer view. This can be useful for importing data from other sources, or for importing data from one database to another. ### Importing a CSV file[](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#importing-a-csv-file) To import CSV data into SurrealDB using Surrealist, navigate to the `Import database` button at the bottom of the Explorer view. This will open your default file explorer, where you can select the CSV file you want to import. Once you have selected the file, you will be prompted to enter a table name. This is the name of the table that will be created in your database. Surrealist will automatically convert the rows from your CSV file into records in the new table. The importer also handles SurrealQL compatible data like objects, arrays, and dates. ![Surrealist import view](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) Here’s a more detailed breakdown: 1. Locate the Importer: Open Surrealist and navigate to the Explorer view, then find the “Import database” option. 2. Select CSV File: Browse and select the CSV file you want to import. 3. Specify Table Name: Enter the name you want to give the new table that will be created from the CSV data. 4. Automatic Conversion: Surrealist will automatically convert the rows from your CSV file into records in the new table. * If your table includes relationships, Surrealist will automatically create the necessary edges if you toggle the insert as a relationship option. 5. SurrealQL Compatibility: The importer is also designed to handle data types that are compatible with SurrealQL, such as objects, arrays, and dates. 6. Additional Features: Surrealist also supports exporting data in SurrealQL format and importing SurrealQL files, as well as models from various machine learning libraries. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/concepts/explore-database-records.mdx) Copy page as Markdown [Sending queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) [Designing the database schema](https://surrealdb.com/docs/surrealist/concepts/designing-the-database-schema) ### On this page * [Tables panel](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#tables-panel) * [Record explorer panel](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#record-explorer-panel) * [Inspector panel](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#inspector-panel) * [Import Files](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#import-files) * [Importing a CSV file](https://surrealdb.com/docs/surrealist/concepts/explore-database-records#importing-a-csv-file) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/concepts/explore-database-records.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Configure network access [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [What is Surreal Cloud?](https://surrealdb.com/docs/cloud) [Surreal Cloud Architecture](https://surrealdb.com/docs/cloud/architecture) Get started [Introduction](https://surrealdb.com/docs/cloud/getting-started) [Create an Account](https://surrealdb.com/docs/cloud/getting-started/create-an-account) [Create an Instance](https://surrealdb.com/docs/cloud/getting-started/create-an-instance) [Create an Organisation](https://surrealdb.com/docs/cloud/getting-started/create-an-organisation) Connect to an Instance [Introduction](https://surrealdb.com/docs/cloud/connect) [Querying in Surrealist](https://surrealdb.com/docs/cloud/connect/surrealist) [Connect via CLI](https://surrealdb.com/docs/cloud/connect/cli) [Connect via SDK](https://surrealdb.com/docs/cloud/connect/sdk) [Connect via HTTP using cURL](https://surrealdb.com/docs/cloud/connect/http) Advanced topics [Introduction](https://surrealdb.com/docs/cloud/advanced-topics) [Backup and restore](https://surrealdb.com/docs/cloud/advanced-topics/data-export-and-backup) [Configure an Instance](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance) [Manage Organisation permissions](https://surrealdb.com/docs/cloud/advanced-topics/manage-organisation-permissions) [Migrating data](https://surrealdb.com/docs/cloud/advanced-topics/migrating-data) [Monitoring](https://surrealdb.com/docs/cloud/advanced-topics/monitoring) [Logs](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/logs) [Metrics](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/metrics) [Configure network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access) [Search and shortcuts](https://surrealdb.com/docs/cloud/advanced-topics/search-and-shortcuts) [SurrealQL editors](https://surrealdb.com/docs/cloud/advanced-topics/surrealql-editors) Billing and support [Introduction](https://surrealdb.com/docs/cloud/billing-and-support) [Billing](https://surrealdb.com/docs/cloud/billing-and-support/billing) [Support](https://surrealdb.com/docs/cloud/billing-and-support/support) [FAQs](https://surrealdb.com/docs/cloud/faqs) [Forum](https://surrealdb.com/community) [](https://surrealdb.com/docs/cloud) [Advanced topics](https://surrealdb.com/docs/cloud/advanced-topics) [Configure network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access) Network access[](https://surrealdb.com/docs/cloud/advanced-topics/network-access#network-access) ------------------------------------------------------------------------------------------------- Network access is a type of [Instance capability](https://surrealdb.com/docs/cloud/advanced-topics/configure-an-instance#instance-capabilities) that allows you to control and manage how your Surreal Cloud Instance connects to external services and networks through [HTTP functions and requests](https://surrealdb.com/docs/surrealql/functions/database/http) . This capability enables you to make HTTP calls to external APIs and services directly from your database queries while maintaining control over which endpoints can be accessed. With this feature, you can define precise rules that determine which network destinations your database can access. This helps protect your data, prevent unauthorized connections, and ensure compliance with your organization’s security policies. In Surreal Cloud, network access lets you: * Allow or deny outbound connections to specific domains, IP addresses, or ranges. * Create custom policies that fit your infrastructure and security needs. By leveraging network access, you gain full control over your database’s connectivity, reducing the risk of accidental data leaks or malicious activity. > Note > > Supported versions: `>=2.1.8, <2.2.0` or `>=2.2.6, <2.3.0` or `>=2.3.6`. If you are using a different version, you can still use network access, but you will need to manually upgrade to access the network access rules. Using network access in Surreal Cloud[](https://surrealdb.com/docs/cloud/advanced-topics/network-access#using-network-access-in-surreal-cloud) ----------------------------------------------------------------------------------------------------------------------------------------------- In your Surreal Cloud dashboard, you can easily configure and preview network access rules. ### Configure network access in Surreal Cloud[](https://surrealdb.com/docs/cloud/advanced-topics/network-access#configure-network-access-in-surreal-cloud) To allow outgoing HTTP requests from your instance, you’ll need to enable network access capability and configure allowed network patterns. Here’s how to do it: 1. Open the Network Access Section:In the Surreal Cloud dashboard, click on the **configure instance** button to open the instance configuration drawer. Here, you’ll find the **network access** option (set to Deny by default) to specify allowed and denied network patterns. 2. Set Allowed and Denied Patterns: * Enter the domains, IP addresses, you wish to allow in the **Allowed by default** field. * Add any domains or IPs you want to block in the **Denied by default** field. 3. Click on **Apply capabilities** to save your changes. This will change your instance configuration. ![Configure an Instance](https://surrealdb.com/docs/_astro/network-access.BYR1iJFa_ZH8C5D.webp) Additional resources[](https://surrealdb.com/docs/cloud/advanced-topics/network-access#additional-resources) ------------------------------------------------------------------------------------------------------------- Learn more about network access in the video tutorial below: [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/advanced-topics/network-access.mdx) Copy page as Markdown [Metrics](https://surrealdb.com/docs/cloud/advanced-topics/monitoring/metrics) [Search and shortcuts](https://surrealdb.com/docs/cloud/advanced-topics/search-and-shortcuts) ### On this page * [Network access](https://surrealdb.com/docs/cloud/advanced-topics/network-access#network-access) * [Using network access in Surreal Cloud](https://surrealdb.com/docs/cloud/advanced-topics/network-access#using-network-access-in-surreal-cloud) * [Configure network access in Surreal Cloud](https://surrealdb.com/docs/cloud/advanced-topics/network-access#configure-network-access-in-surreal-cloud) * [Additional resources](https://surrealdb.com/docs/cloud/advanced-topics/network-access#additional-resources) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-cloud/advanced-topics/network-access.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Surrealist | Sending queries to the database [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Introduction](https://surrealdb.com/docs/surrealist) [Installation](https://surrealdb.com/docs/surrealist/installation) [Getting Started](https://surrealdb.com/docs/surrealist/getting-started) Concepts [Introduction](https://surrealdb.com/docs/surrealist/concepts) [Sending queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) [Exploring database records](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) [Designing the database schema](https://surrealdb.com/docs/surrealist/concepts/designing-the-database-schema) [Manage database access](https://surrealdb.com/docs/surrealist/concepts/manage-database-access) [Sending queries with GraphQL](https://surrealdb.com/docs/surrealist/concepts/sending-queries-with-graphql) [Local database serving](https://surrealdb.com/docs/surrealist/concepts/local-database-serving) [Writing stored procedures](https://surrealdb.com/docs/surrealist/concepts/writing-stored-procedures) [Specialized API docs](https://surrealdb.com/docs/surrealist/concepts/specialized-api-docs) [Deploying your database](https://surrealdb.com/docs/surrealist/concepts/surreal-cloud) Advanced Topics [Introduction](https://surrealdb.com/docs/surrealist/advanced-topics) [Search and Shortcuts](https://surrealdb.com/docs/surrealist/advanced-topics/search-and-shortcuts) [Embedding Surrealist](https://surrealdb.com/docs/surrealist/advanced-topics/embedding-surrealist) [Connection Templates](https://surrealdb.com/docs/surrealist/advanced-topics/connection-templates) [Settings and Customisation](https://surrealdb.com/docs/surrealist/advanced-topics/settings-and-customisation) [SurrealQL Editors](https://surrealdb.com/docs/surrealist/advanced-topics/surrealql-editors) [URL Intents](https://surrealdb.com/docs/surrealist/advanced-topics/intents) FAQs [Introduction](https://surrealdb.com/docs/surrealist/faqs) [FAQs](https://surrealdb.com/docs/surrealist/faqs/info) [Known Issues](https://surrealdb.com/docs/surrealist/faqs/known-issues) [Troubleshooting](https://surrealdb.com/docs/surrealist/troubleshooting) [surrealdb/surrealist](https://github.com/surrealdb/surrealist) [](https://surrealdb.com/docs/surrealist) [Concepts](https://surrealdb.com/docs/surrealist/concepts) [Sending queries to the database](https://surrealdb.com/docs/surrealist/concepts/sending-queries) Sending queries to the database[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#sending-queries-to-the-database) ================================================================================================================================== The first view you encounter when starting Surrealist is the Query view. At its core the Query view allows you to execute SurrealQL queries and view their responses, however additional features include configuring variables to use within your queries, saving queries for later use, and viewing incoming live select messages. ![Surrealist query view](https://surrealdb.com/docs/surrealist/concepts/sending-queries) > Note > > The example above shows writing a query to a sandbox connection where no credentials are required. When connecting to a Surreal Cloud instance or a local connection, you will need to enter your namespace and database credentials in the connection dialog before you can send queries. Queries panel[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#queries-panel) ---------------------------------------------------------------------------------------------- The panel on the left of the Query view lists out your active query tabs. Each tab has its own query editor, so you can work with multiple queries at once and keep your workspace organised. You can double-click a tab to rename it, and right-click a tab to reveal additional actions. Additionally, near the bottom of the queries panel, you can find buttons to open the query history and view your saved queries. Query history[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#query-history) ---------------------------------------------------------------------------------------------- The query history drawer will show a list of recently executed queries for the current connection. Right click any query and choose “Open in new tab” or “Open in current tab” to bring it back into your query editor. Saved Queries[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#saved-queries) ---------------------------------------------------------------------------------------------- The saved queries drawer lists out any queries you have saved, which you can do by pressing the saved query button underneath the query editor. This is useful for queries you intend on executing frequently or would like to use in multiple connections. ### Saving a new query[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#saving-a-new-query) To save a query, press the save query button at the top right the query editor. You will be prompted to enter a name for the query, after which it will be saved and listed in the saved queries panel. Variables panel[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#variables-panel) -------------------------------------------------------------------------------------------------- You can reveal the variables panel by pressing “Show variables”. Any variables you define in here are made available to your query. Use the convenient “Infer from query” button to analyse your query for variables and automatically define them. ### Results panel[](https://surrealdb.com/docs/surrealist/concepts/sending-queries#results-panel) After executing a query the result of the query will be displayed in the results panel underneath the query editor. This panel lets you visualise the response in multiple ways, so pick the most convenient one for your use case. * **Combined mode**: will list all query responses in a single list. This is especially useful for requests with only a handful of statements or when you would like to view all responses at a glance. * **Individual mode**: will display each response separately and provide navigation buttons to switch between them. This is mostly useful when you are sending many queries at once or would like to focus on each response individually. * **Graph mode**: will display the response as a graph if they are compatible with the response i.e is a list of records with relations. This is useful for visualising the relationships between records. * **Table mode**: presents your responses in an interactive table similar to the Explorer view. You can press any record id to view and edit the record in the inspector panel. * **Live mode**: will display incoming messages received by any [LIVE SELECT](https://surrealdb.com/docs/surrealql/statements/live) queries sent in your request. You can also terminate active live queries here as well. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/concepts/sending-queries.mdx) Copy page as Markdown [Concepts](https://surrealdb.com/docs/surrealist/concepts) [Exploring database records](https://surrealdb.com/docs/surrealist/concepts/explore-database-records) ### On this page * [Queries panel](https://surrealdb.com/docs/surrealist/concepts/sending-queries#queries-panel) * [Query history](https://surrealdb.com/docs/surrealist/concepts/sending-queries#query-history) * [Saved Queries](https://surrealdb.com/docs/surrealist/concepts/sending-queries#saved-queries) * [Saving a new query](https://surrealdb.com/docs/surrealist/concepts/sending-queries#saving-a-new-query) * [Variables panel](https://surrealdb.com/docs/surrealist/concepts/sending-queries#variables-panel) * [Results panel](https://surrealdb.com/docs/surrealist/concepts/sending-queries#results-panel) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealist/concepts/sending-queries.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Dependency Injection | .NET | SDK | Concepts [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK Concepts](https://surrealdb.com/docs/sdk/dotnet/core) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) Dependency Injection[](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#dependency-injection) ============================================================================================================= The .NET SDK also support Dependency Injection to ease the use of the `SurrealDbClient` in your application. Create a new project[](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#create-a-new-project) ------------------------------------------------------------------------------------------------------------- Let’s start by creating a new ASP.NET Core web app. dotnet new webapp -o SurrealDbWeatherApi cd SurrealDbWeatherApi dotnet add package SurrealDb.Net Define a Connection String[](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#define-a-connection-string) ------------------------------------------------------------------------------------------------------------------------- Open `appsettings.Development.json` and replace everything in there with the following code. We have added a new Connection String called `SurrealDB` with the default configuration. { "AllowedHosts": "*", "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "ConnectionStrings": { "SurrealDB": "Server=http://127.0.0.1:8000;Namespace=test;Database=test;Username=root;Password=root" } } Register services[](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#register-services) ------------------------------------------------------------------------------------------------------- Open `Program.cs` and replace everything in there with the following code. This code is using the `AddSurreal()` extension method to inject services automatically. Notice that all we have to do is one line of code to configure the SurrealDB client with the previously set Connection String. > Note > > By default, this function will register both `ISurrealDbClient` and `SurrealDbClient` using the `Singleton` service lifetime. var builder = WebApplication.CreateBuilder(args); var services = builder.Services; var configuration = builder.Configuration; services.AddControllers(); services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); services.AddSurreal(configuration.GetConnectionString("SurrealDB")); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); > Note > > In this example, we use a [Connection String](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) > to configure services. This is the most convenient way to initialize the `SurrealDbClient` in your application. You can always choose to construct a Connection String manually via a `SurrealDbOptionsBuilder` and pass it to the `AddSurreal()` method. Consume the SurrealDB client[](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#consume-the-surrealdb-client) ----------------------------------------------------------------------------------------------------------------------------- Open `WeatherForecastController.cs` and replace everything in there with the following code. Finally, we can inject the `ISurrealDbClient` inside our Controller. using Microsoft.AspNetCore.Mvc; namespace SurrealDbWeatherApi.Controllers; [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private const string Table = "weatherForecast"; private readonly ISurrealDbClient _surrealDbClient; public WeatherForecastController(ISurrealDbClient surrealDbClient) { _surrealDbClient = surrealDbClient; } [HttpGet] [Route("/")] public Task> GetAll(CancellationToken cancellationToken) { return _surrealDbClient.Select(Table, cancellationToken); } [HttpPost] [Route("/")] public Task Create(CreateWeatherForecast data, CancellationToken cancellationToken) { var weatherForecast = new WeatherForecast { Date = data.Date, Country = data.Country, TemperatureC = data.TemperatureC, Summary = data.Summary }; return _surrealDbClient.Create(Table, weatherForecast, cancellationToken); } // ... // Other methods omitted for brevity } public class CreateWeatherForecast { public DateTime Date { get; set; } public string? Country { get; set; } public int TemperatureC { get; set; } public string? Summary { get; set; } } Then make sure your SurrealDB server is running on `127.0.0.1:8000` and run your app from the command line with: dotnet run [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/dependency-injection.mdx) Copy page as Markdown [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Overview](https://surrealdb.com/docs/sdk/dotnet) ### On this page * [Create a new project](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#create-a-new-project) * [Define a Connection String](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#define-a-connection-string) * [Register services](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#register-services) * [Consume the SurrealDB client](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection#consume-the-surrealdb-client) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/dependency-injection.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # .NET | SDK | Quick start [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) Quick start[](https://surrealdb.com/docs/sdk/dotnet/start#quick-start) ======================================================================= Now that you have a more comprehensive understanding of the SurrealDB SDK for .NET, let’s create a simple project to get you started. This guide will walk you through creating a simple console application that uses the SurrealDB SDK for .NET to interact with a SurrealDB instance. > Important > > If you want to see the final project for this guide, you can find it [here](https://github.com/surrealdb/surrealdb.net/tree/main/SurrealDb.Examples.Console) > and follow the instructions in the `README.md` file to get the project running. Prerequisites[](https://surrealdb.com/docs/sdk/dotnet/start#prerequisites) --------------------------------------------------------------------------- This guide assumes the following: * You have a basic understanding of the .NET ecosystem and the C# language. * You have an install version of SurrealDB `>1.4.2` installed on your machine. Setup your project[](https://surrealdb.com/docs/sdk/dotnet/start#setup-your-project) ------------------------------------------------------------------------------------- Let’s start by creating a new console app. # Create a new console app dotnet new console -o SurrealDbExample # Navigate into the generated directory cd SurrealDbExample # Add the SurrealDB SDK to your project via the dotnet CLI dotnet add package SurrealDb.Net Open `Program.cs` and replace everything in there with the following code to try out some basic operations using the SurrealDB SDK. using SurrealDb.Net; using SurrealDb.Net.Models; using SurrealDb.Net.Models.Auth; using System.Text.Json; const string TABLE = "person"; using var db = new SurrealDbClient("ws://127.0.0.1:8000/rpc"); await db.SignIn(new RootAuth { Username = "root", Password = "secret" }); await db.Use("test", "test"); var person = new Person { Title = "Founder & CEO", Name = new() { FirstName = "Tobie", LastName = "Morgan Hitchcock" }, Marketing = true }; var created = await db.Create(TABLE, person); Console.WriteLine(ToJsonString(created)); var updated = await db.Merge( new() { Id = (TABLE, "jaime"), Marketing = true } ); Console.WriteLine(ToJsonString(updated)); var people = await db.Select(TABLE); Console.WriteLine(ToJsonString(people)); var queryResponse = await db.Query( $"SELECT Marketing, count() AS Count FROM type::table({TABLE}) GROUP BY Marketing" ); var groups = queryResponse.GetValue>(0); Console.WriteLine(ToJsonString(groups)); static string ToJsonString(object? o) { return JsonSerializer.Serialize(o, new JsonSerializerOptions { WriteIndented = true, }); } public class Person : Record { public string? Title { get; set; } public Name? Name { get; set; } public bool Marketing { get; set; } } public class Name { public string? FirstName { get; set; } public string? LastName { get; set; } } public class ResponsibilityMerge : Record { public bool Marketing { get; set; } } public class Group { public bool Marketing { get; set; } public int Count { get; set; } } Then make sure your SurrealDB server is running on `127.0.0.1:8000` and run your app from the command line with: dotnet run Next steps[](https://surrealdb.com/docs/sdk/dotnet/start#next-steps) --------------------------------------------------------------------- Now that you have learned the basics of the .NET SDK for SurrealDB, you can learn more about the SDK and its methods [in the methods section](https://surrealdb.com/docs/sdk/dotnet/methods) and [data types section](https://surrealdb.com/docs/sdk/dotnet/data-types) . [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/start.mdx) Copy page as Markdown [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) ### On this page * [Prerequisites](https://surrealdb.com/docs/sdk/dotnet/start#prerequisites) * [Setup your project](https://surrealdb.com/docs/sdk/dotnet/start#setup-your-project) * [Next steps](https://surrealdb.com/docs/sdk/dotnet/start#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/start.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # .NET | SDK | Concepts [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK Concepts](https://surrealdb.com/docs/sdk/dotnet/core) Concepts[](https://surrealdb.com/docs/sdk/dotnet/core#concepts) ================================================================ In this section, we will go over the core concepts of the SurrealDB SDK for .NET. You will learn how to connect to a SurrealDB instance, manage authentication, and interact with the database. * [Create a new Connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) * [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) * [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) * [Data manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) * [Realtime Streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) * [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) * [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) * [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/index.mdx) Copy page as Markdown [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) ### On this page * [Concepts](https://surrealdb.com/docs/sdk/dotnet/core#concepts) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Data Manipulation | .NET | SDK | Concepts [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK Concepts](https://surrealdb.com/docs/sdk/dotnet/core) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) Data Manipulation[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#data-manipulation) ==================================================================================================== SurrealDB supports a number of methods for interacting with the database and performing CRUD operations. | Method | Description | | --- | --- | | [`db.Select()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#select) | Selects all records in a table, or a specific record, from the database | | [`db.Create()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#create) | Creates a record in the database | | [`db.Insert()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#insert) | Inserts one or multiple records in the database | | [`db.InsertRelation()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#insert_relation) | Inserts one or multiple relations in the database | | [`db.Update()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#update) | Updates all records in a table, or a specific record, in the database | | [`db.Upsert()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#upsert) | Creates or updates a set of records in a table, or a specific record, in the database | | [`db.Merge()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#merge) | Modifies all records in a table, or a specific record, in the database | | [`db.Patch()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#patch) | Applies JSON Patch changes to all records, or a specific record, in the database | | [`db.Delete()`](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#delete) | Deletes all records in a table, or a specific record, from the database | `.Select()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#select) -------------------------------------------------------------------------------------- Selects all records in a table, or a specific record, from the database. Method Syntaxawait db.Select(resource) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or a [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to select. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage) // Select all records from a table var people = await db.Select("person"); // Select a specific record from a table var person = await db.Select(("person", "h5wxrf2ewk8xjxosxtyc")); var person = await db.Select(new StringRecordId("person:h5wxrf2ewk8xjxosxtyc")); // Select a specific record from a table, given a non-string id var person = await db.Select(("person", new Guid("8424486b-85b3-4448-ac8d-5d51083391c7"))); `.Create()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#create) -------------------------------------------------------------------------------------- Creates a record in the database. Method Syntaxawait db.Create(resource, data) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-1) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or a [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to create. | | | `data` optional | | The document / record data to insert. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-1) // Create a record with a random ID var person = await db.Create("person"); // Create a record with a random ID & specific fields var person = await db.Create("person", new Person { Name = "Tobie" }); // Create a record with a specific ID var personToCreate = new Person { Id = ("person", "tobie"), Name = "Tobie", Settings = new Settings { Active = true, Marketing = true, }, }; var result = await db.Create(personToCreate); `.Insert()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#insert) -------------------------------------------------------------------------------------- Inserts one or multiple records in the database. Method Syntaxawait db.Insert(table, data) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-2) | Arguments | | Description | | | --- | --- | | `table` optional | | Optionally pass along a table to insert into. | | | `data` optional | | Either a single document/record or an array of documents/records to insert | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-2) var posts = new List { new Post { Id = ("post", "First"), Title = "An article", Content = "This is the first article" }, new Post { Id = ("post", "Second"), Title = "An article", Content = "This is the second article" } }; await db.Insert("post", posts); `.InsertRelation()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#insert_relation) ------------------------------------------------------------------------------------------------------- Inserts one or multiple relations in the database. Method Syntaxawait db.InsertRelation(table, data) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-3) | Arguments | | Description | | | --- | --- | | `table` optional | | Optionally pass along a table to insert into. | | | `data` optional | | Either a single document/record or an array of documents/records to insert | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-3) await db.InsertRelation( new WroteRelation { In = ("user", "u1"), Out = ("post", "p1"), CreatedAt = now, NumberOfPages = 144 } ); `.Update()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#update) -------------------------------------------------------------------------------------- Updates all records in a table, or a specific record, in the database. Method Syntaxawait db.Update(thing, data) > Note > > This function replaces the current document / record data with the specified data. ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-4) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or the specific [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to update. | | | `data` optional | | The document / record data to update. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-4) var post = new Post { Id = ("post", "another"), Title = "A new article", Content = "This is a new article created using the .NET SDK" }; // Updates a single record await db.Update(post); var data = new Person { Name = "Tobie", Settings = new Settings { Active = true, Marketing = true, }, }; // Updates all records inside the "person" table await db.Update("person", data); `.Upsert()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#upsert) -------------------------------------------------------------------------------------- Creates or updates a specific record. Method Syntaxawait db.Upsert(data) > Note > > This function creates a new document / record or replaces the current one with the specified data. ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-5) | Arguments | | Description | | | --- | --- | | `data` required | | The document / record data to insert. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-5) var person = new Person { Id = ("person", "tobie"), // Id is mandatory to apply create or update Name = "Tobie", Settings = new Settings { Active = true, Marketing = true, }, }; // Create a new record when it doesn't exist var created = await db.Upsert(person); // Update an existing record when it does exist var updated = await db.Upsert(person); `.Merge()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#merge) ------------------------------------------------------------------------------------ Modifies all records in a table, or a specific record. Method Syntaxawait db.Merge(resource, data) > Note > > This function merges the current document / record data with the specified data. ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-6) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or the specific [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to merge. | | | `data` optional | | The data with which to modify the records. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-6) Merging data within a single record// Only changes the fields specified in the merge object var merge = new PersonMerge { Id = ("person", "tobie"), Settings = new Settings { Active = true, Marketing = false, }, }; var result = await db.Merge(merge); // Only changes the fields specified in the Dictionary var data = new Dictionary { { "tags", new List { "developer", "engineer" } } }; var result = await db.Merge(("person", "tobie"), data); Merging data for every record in a table// Only changes the fields specified in the merge object var merge = new PersonMerge { Settings = new Settings { Active = true, Marketing = false, }, }; var result = await db.Merge("person", merge); // Only changes the fields specified in the Dictionary var data = new Dictionary { { "tags", new List { "developer", "engineer" } } }; var result = await db.Merge("person", data); `.Patch()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#patch) ------------------------------------------------------------------------------------ Applies JSON Patch changes to all records, or a specific record, in the database. Method Syntaxawait db.Patch(resource, data) > Note > > This function patches document / record data with the specified [JSON Patch](https://jsonpatch.com/) > data. ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-7) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or the specific [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to patch. | | | `data` optional | | The JSON Patch data with which to patch the records. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-7) // Update a record with a specific ID var result = await db.Patch(("person", "tobie"), patches); // Update all records in a table var result = await db.Patch("person", patches); `.Delete()`[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#delete) ----------------------------------------------------------------------------------- Deletes all records in a table, or a specific record, from the database. Method Syntaxawait db.Delete(resource) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-8) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or a [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to delete. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-8) // Delete all records from a table await db.Delete("person"); // Delete a specific record from a table await db.Delete(("person", "h5wxrf2ewk8xjxosxtyc")); [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/data-manipulation.mdx) Copy page as Markdown [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) ### On this page * [.Select()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#select) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage) * [.Create()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#create) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-1) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-1) * [.Insert()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#insert) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-2) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-2) * [.InsertRelation()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#insert_relation) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-3) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-3) * [.Update()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#update) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-4) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-4) * [.Upsert()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#upsert) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-5) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-5) * [.Merge()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#merge) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-6) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-6) * [.Patch()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#patch) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-7) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-7) * [.Delete()](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#delete) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#arguments-8) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation#example-usage-8) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/data-manipulation.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # PHP | SDKs | Integration | SurrealDB Docs [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/php) [Data Types](https://surrealdb.com/docs/sdk/php/data-types) [Getting Started](https://surrealdb.com/docs/sdk/php/setup) Methods [Introduction](https://surrealdb.com/docs/sdk/php/methods) [authenticate](https://surrealdb.com/docs/sdk/php/methods/authenticate) [close](https://surrealdb.com/docs/sdk/php/methods/close) [connect](https://surrealdb.com/docs/sdk/php/methods/connect) [create](https://surrealdb.com/docs/sdk/php/methods/create) [delete](https://surrealdb.com/docs/sdk/php/methods/delete) [export](https://surrealdb.com/docs/sdk/php/methods/export) [health](https://surrealdb.com/docs/sdk/php/methods/health) [import](https://surrealdb.com/docs/sdk/php/methods/import) [info](https://surrealdb.com/docs/sdk/php/methods/info) [insert](https://surrealdb.com/docs/sdk/php/methods/insert) [insertRelation](https://surrealdb.com/docs/sdk/php/methods/insertrelation) [invalidate](https://surrealdb.com/docs/sdk/php/methods/invalidate) [let](https://surrealdb.com/docs/sdk/php/methods/let) [merge](https://surrealdb.com/docs/sdk/php/methods/merge) [patch](https://surrealdb.com/docs/sdk/php/methods/patch) [query](https://surrealdb.com/docs/sdk/php/methods/query) [queryRaw](https://surrealdb.com/docs/sdk/php/methods/queryraw) [run](https://surrealdb.com/docs/sdk/php/methods/run) [select](https://surrealdb.com/docs/sdk/php/methods/select) [signin](https://surrealdb.com/docs/sdk/php/methods/signin) [signup](https://surrealdb.com/docs/sdk/php/methods/signup) [status](https://surrealdb.com/docs/sdk/php/methods/status) [unset](https://surrealdb.com/docs/sdk/php/methods/unset) [update](https://surrealdb.com/docs/sdk/php/methods/update) [upsert](https://surrealdb.com/docs/sdk/php/methods/upsert) [use](https://surrealdb.com/docs/sdk/php/methods/use) [version](https://surrealdb.com/docs/sdk/php/methods/version) Language Starter [Introduction](https://surrealdb.com/docs/sdk/php/core) [Initialization](https://surrealdb.com/docs/sdk/php/core/initialization) [Authentication](https://surrealdb.com/docs/sdk/php/core/authentication) [Data Querying](https://surrealdb.com/docs/sdk/php/core/data-querying) [surrealdb/surrealdb.php](https://github.com/surrealdb/surrealdb.php) [](https://surrealdb.com/docs/sdk/php) [Getting Started](https://surrealdb.com/docs/sdk/php/setup) Getting Started[](https://surrealdb.com/docs/sdk/php/setup#getting-started) ============================================================================ Before you can use this SDK in PHP, you need to install the SDK via the [Composer package](https://getcomposer.org/download/) manager. This guide will walk you through the process of installing the SDK into your project. ### Install the SDK[](https://surrealdb.com/docs/sdk/php/setup#install-the-sdk) Within your Compose project, run the following command to install the SDK: composer require surrealdb/surrealdb.php ### Using the SDK[](https://surrealdb.com/docs/sdk/php/setup#using-the-sdk) Once you have installed the SDK, make sure to include the [Composer autoloader](https://getcomposer.org/doc/01-basic-usage.md#autoloading) in your PHP script: require __DIR__ . '/vendor/autoload.php'; You can now use the SDK in your PHP script by importing it: use Surreal\Surreal; Now that you have the SDK installed and imported, you can start using the SDK to interact with SurrealDB. Check out the [initialization guide](https://surrealdb.com/docs/sdk/php/core/initialization) to learn more. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-php/setup.mdx) Copy page as Markdown [Data Types](https://surrealdb.com/docs/sdk/php/data-types) [Methods](https://surrealdb.com/docs/sdk/php/methods) ### On this page * [Install the SDK](https://surrealdb.com/docs/sdk/php/setup#install-the-sdk) * [Using the SDK](https://surrealdb.com/docs/sdk/php/setup#using-the-sdk) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-php/setup.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Run SurrealQL queries | .NET | SDK | Concepts [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK Concepts](https://surrealdb.com/docs/sdk/dotnet/core) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) Run SurrealQL queries[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#run-surrealql-queries) ============================================================================================================ The methods below are used to interact with the database and perform CRUD operations. You can also use the `query` method to run [SurrealQL statements](https://surrealdb.com/docs/surrealql/statements) against the database. | Method | Description | | --- | --- | | [`db.Query()`](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#query) | Runs a set of SurrealQL statements against the database | | [`db.RawQuery()`](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#raw_query) | Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query | `.Query()`[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#query) --------------------------------------------------------------------------------- Runs a set of SurrealQL statements against the database. Method Syntaxawait db.Query(sql) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#arguments) | Arguments | | Description | | | --- | --- | | `sql` required | | Specifies the SurrealQL statements. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#example-usage) // Execute query with params const string table = "person"; var result = await db.Query($"CREATE person; SELECT * FROM type::table({table});"); // Get the first result from the first query var created = result.GetValue(0); // Get all of the results from the second query var people = result.GetValue>(1); `.RawQuery()`[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#raw_query) ---------------------------------------------------------------------------------------- Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query. Method Syntaxawait db.RawQuery(sql, params) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#arguments-1) | Arguments | | Description | | | --- | --- | | `sql` required | | Specifies the SurrealQL statements. | | | `params` optional | | Assigns variables which can be used in the query. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#example-usage-1) // Assign the variable on the connection var @params = new Dictionary { { "table", "person" } }; var result = await db.RawQuery("CREATE person; SELECT * FROM type::table($table);", @params); // Get the first result from the first query var created = result.GetValue(0); // Get all of the results from the second query var people = result.GetValue>(1); [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/writing-surrealql.mdx) Copy page as Markdown [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) ### On this page * [.Query()](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#query) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#arguments) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#example-usage) * [.RawQuery()](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#raw_query) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#arguments-1) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql#example-usage-1) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/writing-surrealql.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Set parameters | .NET | SDK | Concepts [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK Concepts](https://surrealdb.com/docs/sdk/dotnet/core) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) Set parameters[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#set-parameters) ======================================================================================= Within your application, you can define parameters that can be used to store and retrieve data from SurrealDB. Parameters are used to store data in a structured format, and can be used to store data in a key-value pair format. > Important > > Parameters allow you to define global (database-wide) parameters that are available to every client. | Method | Description | | --- | --- | | [`db.Set(key, value)`](https://surrealdb.com/docs/sdk/dotnet/core/parameters#set) | Assigns a value as a parameter for this connection | | [`db.Unset(key)`](https://surrealdb.com/docs/sdk/dotnet/core/parameters#unset) | Removes a parameter for this connection | `.Set()`[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#set) ---------------------------------------------------------------------- Assigns a value as a parameter for this connection. Method Syntaxawait db.Set(key, val) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#arguments) | Arguments | | Description | | | --- | --- | | `key` required | | Specifies the name of the variable. | | | `value` required | | Assigns the value to the variable name. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#example-usage) // Assign the variable on the connection await db.Set("name", new Name { FirstName = "Tobie", LastName = "Morgan Hitchcock" }); // Use the variable in a subsequent query await db.Query($"CREATE person SET name = $name"); // Use the variable in a subsequent query await db.Query($"SELECT * FROM person WHERE name.first_name = $name.first_name"); `.Unset()`[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#unset) -------------------------------------------------------------------------- Removes a parameter for this connection. Method Syntaxawait db.Unset(key) ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#arguments-1) | Arguments | | Description | | | --- | --- | | `key` required | | Specifies the SurrealQL statements. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/core/parameters#example-usage-1) await db.Unset("name"); [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/parameters.mdx) Copy page as Markdown [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) ### On this page * [.Set()](https://surrealdb.com/docs/sdk/dotnet/core/parameters#set) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/parameters#arguments) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/parameters#example-usage) * [.Unset()](https://surrealdb.com/docs/sdk/dotnet/core/parameters#unset) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/core/parameters#arguments-1) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/core/parameters#example-usage-1) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/core/parameters.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # .NET | SDK | Methods | Version [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK methods](https://surrealdb.com/docs/sdk/dotnet/methods) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) `.Version()`[](https://surrealdb.com/docs/sdk/dotnet/methods/version#version) ============================================================================== Retrieves the version of the SurrealDB instance. Method Syntaxawait db.Version() ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/methods/version#arguments) | Arguments | | Description | | | --- | --- | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/methods/version#example-usage) string version = await db.Version(); // Will return "surrealdb-1.1.1" [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/methods/version.mdx) Copy page as Markdown [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Concepts](https://surrealdb.com/docs/sdk/dotnet/core) ### On this page * [.Version()](https://surrealdb.com/docs/sdk/dotnet/methods/version#version) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/methods/version#arguments) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/methods/version#example-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/methods/version.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # .NET | SDK | Methods | Patch [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/sdk/dotnet) [Installation](https://surrealdb.com/docs/sdk/dotnet/installation) [Quick start](https://surrealdb.com/docs/sdk/dotnet/start) [Data Types](https://surrealdb.com/docs/sdk/dotnet/data-types) Methods [Introduction](https://surrealdb.com/docs/sdk/dotnet/methods) [Authenticate](https://surrealdb.com/docs/sdk/dotnet/methods/authenticate) [Connect](https://surrealdb.com/docs/sdk/dotnet/methods/connect) [Create](https://surrealdb.com/docs/sdk/dotnet/methods/create) [Delete](https://surrealdb.com/docs/sdk/dotnet/methods/delete) [Export](https://surrealdb.com/docs/sdk/dotnet/methods/export) [Health](https://surrealdb.com/docs/sdk/dotnet/methods/health) [Import](https://surrealdb.com/docs/sdk/dotnet/methods/import) [Info](https://surrealdb.com/docs/sdk/dotnet/methods/info) [InsertRelation](https://surrealdb.com/docs/sdk/dotnet/methods/insert-relation) [Insert](https://surrealdb.com/docs/sdk/dotnet/methods/insert) [Invalidate](https://surrealdb.com/docs/sdk/dotnet/methods/invalidate) [Kill](https://surrealdb.com/docs/sdk/dotnet/methods/kill) [ListenLive](https://surrealdb.com/docs/sdk/dotnet/methods/listen_live) [LiveQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-query) [LiveRawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/live-raw-query) [LiveTable](https://surrealdb.com/docs/sdk/dotnet/methods/live-table) [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) [RawQuery](https://surrealdb.com/docs/sdk/dotnet/methods/raw-query) [Relate](https://surrealdb.com/docs/sdk/dotnet/methods/relate) [Run](https://surrealdb.com/docs/sdk/dotnet/methods/run) [Select](https://surrealdb.com/docs/sdk/dotnet/methods/select) [Set](https://surrealdb.com/docs/sdk/dotnet/methods/set) [SignIn](https://surrealdb.com/docs/sdk/dotnet/methods/signin) [SignUp](https://surrealdb.com/docs/sdk/dotnet/methods/signup) [Unset](https://surrealdb.com/docs/sdk/dotnet/methods/unset) [Update](https://surrealdb.com/docs/sdk/dotnet/methods/update) [Upsert](https://surrealdb.com/docs/sdk/dotnet/methods/upsert) [Use](https://surrealdb.com/docs/sdk/dotnet/methods/use) [Version](https://surrealdb.com/docs/sdk/dotnet/methods/version) Concepts [Introduction](https://surrealdb.com/docs/sdk/dotnet/core) [Create a new connection](https://surrealdb.com/docs/sdk/dotnet/core/create-a-new-connection) [Handle authentication](https://surrealdb.com/docs/sdk/dotnet/core/authentication) [Set parameters](https://surrealdb.com/docs/sdk/dotnet/core/parameters) [Data Manipulation](https://surrealdb.com/docs/sdk/dotnet/core/data-manipulation) [Realtime data streaming](https://surrealdb.com/docs/sdk/dotnet/core/streaming) [Run SurrealQL queries](https://surrealdb.com/docs/sdk/dotnet/core/writing-surrealql) [Connection Strings](https://surrealdb.com/docs/sdk/dotnet/core/connection-strings) [Dependency Injection](https://surrealdb.com/docs/sdk/dotnet/core/dependency-injection) [surrealdb/surrealdb.net](https://github.com/surrealdb/surrealdb.net) [](https://surrealdb.com/docs/sdk/dotnet) [SDK methods](https://surrealdb.com/docs/sdk/dotnet/methods) [Patch](https://surrealdb.com/docs/sdk/dotnet/methods/patch) `.Patch()`[](https://surrealdb.com/docs/sdk/dotnet/methods/patch#patch) =========================================================================== Applies JSON Patch changes to all records, or a specific record, in the database. Method Syntaxawait db.Patch(resource, data) > Note > > This function patches document / record data with the specified [JSON Patch](https://jsonpatch.com/) > data. ### Arguments[](https://surrealdb.com/docs/sdk/dotnet/methods/patch#arguments) | Arguments | | Description | | | --- | --- | | `thing` required | | The table name or the specific [`RecordId`](https://surrealdb.com/docs/sdk/dotnet/data-types#recordid)
to patch. | | | `data` optional | | The JSON Patch data with which to patch the records. | | | `cancellationToken` optional | | The cancellationToken enables graceful cancellation of asynchronous operations. | | ### Example usage[](https://surrealdb.com/docs/sdk/dotnet/methods/patch#example-usage) // Update a record with a specific ID var result = await db.Patch(("person", "tobie"), patches); // Update all records in a table var result = await db.Patch("person", patches); [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/methods/patch.mdx) Copy page as Markdown [Merge](https://surrealdb.com/docs/sdk/dotnet/methods/merge) [Query](https://surrealdb.com/docs/sdk/dotnet/methods/query) ### On this page * [.Patch()](https://surrealdb.com/docs/sdk/dotnet/methods/patch#patch) * [Arguments](https://surrealdb.com/docs/sdk/dotnet/methods/patch#arguments) * [Example usage](https://surrealdb.com/docs/sdk/dotnet/methods/patch#example-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-sdk-dotnet/methods/patch.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Arguments | Embedded scripting functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Scripting functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) Arguments[](https://surrealdb.com/docs/surrealql/functions/script/arguments#arguments) ======================================================================================= Additional arguments can be passed in to the function from SurrealDB, and these are accessible as an array using the `arguments` object within the JavaScript function. -- Create a new parameter LET $val = "SurrealDB"; -- Create a new parameter LET $words = ["awesome", "advanced", "cool"]; -- Pass the parameter values into the function CREATE article SET summary = function($val, $words) { const [val, words] = arguments; return `${val} is ${words.join(', ')}`; }; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/arguments.mdx) Copy page as Markdown [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/arguments.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Sequence functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) Sequence functions[](https://surrealdb.com/docs/surrealql/functions/database/sequence#sequence-functions) ========================================================================================================== Available since: v3.0.0-alpha.2 These functions can be used to work with [sequences](https://surrealdb.com/docs/surrealql/statements/define/sequence) . | Function | Description | | --- | --- | | [`sequence::next()`](https://surrealdb.com/docs/surrealql/functions/database/sequence#sequencenext) | Returns the next value in a sequence. | `sequence::next`[](https://surrealdb.com/docs/surrealql/functions/database/sequence#sequencenext) -------------------------------------------------------------------------------------------------- The `sequence::next` function returns the next value in a sequence. API DEFINITIONsequence::next(sequence_name) -> int DEFINE SEQUENCE mySeq2 BATCH 1000 START 100 TIMEOUT 5s; sequence::nextval('mySeq2'); -- 100 [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/sequence.mdx) Copy page as Markdown [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) ### On this page * [sequence::next](https://surrealdb.com/docs/surrealql/functions/database/sequence#sequencenext) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/sequence.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Value functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) Value functions[](https://surrealdb.com/docs/surrealql/functions/database/value#value-functions) ================================================================================================= This module contains several miscellaneous functions that can be used with values of any type. | Function | Description | | --- | --- | | [`.chain()`](https://surrealdb.com/docs/surrealql/functions/database/value#chain) | Allows an anonymous function to be called on a value | | [`value::diff()`](https://surrealdb.com/docs/surrealql/functions/database/value#valuediff) | Returns the operation required for one value to equal another | | [`value::patch()`](https://surrealdb.com/docs/surrealql/functions/database/value#valuepatch) | Applies JSON Patch operations to a value | `.chain()`[](https://surrealdb.com/docs/surrealql/functions/database/value#chain) ---------------------------------------------------------------------------------- Available since: v2.0.0 The `.chain()` method takes the output of an expression upon which the user can call an [anonymous function](https://surrealdb.com/docs/surrealql/datamodel/closures) (closure). API DEFINITIONvalue.chain(|$val_name| @closure_body) -> value; Any value can be followed with the `.chain()` syntax, after which an anonymous function (closure) can be written to perform an operation on it. 'SurrealDB'.chain(|$n| $n + ' 2.0'); -- 'SurrealDB 2.0' The function is only called using the `.` operator (method syntax) and, as the name implies, works well within a chain of methods. { company: 'SurrealDB', latest_version: '2.0' } .chain(|$name| $name) .replace('SurrealDB', 'SURREALDB!!!!!'); Response"{ company: 'SURREALDB!!!!!', latest_version: '2.0' }" For a similar function that allows using a closure on each item in an array instead of a value as a whole, see [array::map](https://surrealdb.com/docs/surrealql/functions/database/array#arraymap) . `value::diff`[](https://surrealdb.com/docs/surrealql/functions/database/value#valuediff) ----------------------------------------------------------------------------------------- Available since: v2.0.0 The `value::diff` function returns an object that shows the [JSON Patch](https://jsonpatch.com/) operation(s) required for the first value to equal the second one. API DEFINITIONvalue::diff(value, value) -> array The following is an example of the `value::diff` function used to display the changes required to change one string into another. Note that the JSON Patch spec requires an array of objects, and thus an array will be returned even if only one patch is needed between two values. RETURN 'tobie'.diff('tobias'); Output[\ {\ op: 'change',\ path: '/',\ value: '@@ -1,5 +1,6 @@\ tobi\ -e\ +as\ '\ }\ ] An example of the output when the diff output includes more than one operation: { company: 'SurrealDB' }.diff({ company: 'SurrealDB!!', latest_version: '2.0', location: city:london }); Response[\ {\ op: 'change',\ path: '/company',\ value: '@@ -2,8 +2,10 @@\ urrealDB\ +!!\ '\ },\ {\ op: 'add',\ path: '/latest_version',\ value: '2.0'\ },\ {\ op: 'add',\ path: '/location',\ value: city:london\ }\ ] `value::patch`[](https://surrealdb.com/docs/surrealql/functions/database/value#valuepatch) ------------------------------------------------------------------------------------------- Available since: v2.0.0 The `value::patch` function applies an array of JSON Patch operations to a value. API DEFINITIONvalue::patch(value, patch: array) -> value LET $company = { company: 'SurrealDB', latest_version: '1.5.4' }; $company.patch([{\ 'op': 'replace',\ 'path': 'latest_version',\ 'value': '2.0'\ }]); Response{ company: 'SurrealDB', version: '2.0' } [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/value.mdx) Copy page as Markdown [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) ### On this page * [.chain()](https://surrealdb.com/docs/surrealql/functions/database/value#chain) * [value::diff](https://surrealdb.com/docs/surrealql/functions/database/value#valuediff) * [value::patch](https://surrealdb.com/docs/surrealql/functions/database/value#valuepatch) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/value.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Built-in functions | Embedded scripting functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Scripting functions](https://surrealdb.com/docs/surrealql/functions/script) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) Built-in functions[](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#built-in-functions) ================================================================================================================== Besides basic JavaScript utilities and [classes for SurrealQL types](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) , there are a handful of utilities built into the embedded scripting functions. | Function | Description | | --- | --- | | [`async fetch(resource, options)`](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#fetch) | Full fledged fetch implementation closely matching the [official specification](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
. | | [`async query(surql)`](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#query) | Run SurrealQL subqueries from within the embedded scripting functions. | | [`async value(variable)`](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#value) | Retrieve values for SurrealQL variables from within the embedded scripting functions. | `async fetch(resource, options)`[](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#fetch) ------------------------------------------------------------------------------------------------------------------- Full fledged fetch implementation closely matching the [official specification](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) . > Note > > For complete documentation, please refer to the MDN documentation. | Arguments | | Description | | | --- | --- | | `resource` required | | Accepts either a url in a string, or a URL or Request object. | | | `options` | | Accepts various options related to the request. Refer to MDN docs for a full reference. | | RETURN function() { // List all posts const posts = fetch('https://jsonplaceholder.typicode.com/posts'); // Update post with ID 1 const updated = fetch('https://jsonplaceholder.typicode.com/posts/1', { method: 'PUT', body: JSON.stringify({ id: 1, title: 'foo', body: 'bar', userId: 1, }), headers: { 'Content-type': 'application/json; charset=UTF-8', }, }); return { posts, updated }; } `async query(surql)`[](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#query) ------------------------------------------------------------------------------------------------------- Run SurrealQL subqueries from within the embedded scripting functions. > Note > > Only subqueries can be executed with the query() function. This means that only a single query can currently be executed, and that only CRUD operations are allowed. | Arguments | | Description | | | --- | --- | | `surql` required | | Accepts a single SurrealQL query, which is limited to a CRUD operation. | | CREATE user:john, user:mary; RETURN function() { // Select all users const users = await surrealdb.query("SELECT * FROM user"); // Prepared query const query = new surrealdb.Query("SELECT * FROM $id", { id: new Record('user', 'mary') }); // Execute prepared query const mary = (await surrealdb.query(query))[0]; // Assign variables later to prepared query query.bind('id', new Record('user', 'john')); // Execute prepared query const john = (await surrealdb.query(query))[0]; return { john, mary }; } `async value(variable)`[](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#value) ---------------------------------------------------------------------------------------------------------- Retrieve values for SurrealQL variables from within the embedded scripting functions. | Arguments | | Description | | | --- | --- | | `variable` required | | Accepts the path to a variable | | LET $something = 123; LET $obj = { nested: 456 }; LET $arr = [\ { value: 1 },\ { value: 2 },\ { value: 3 },\ { value: 4 },\ { value: 5 },\ { value: 6 },\ ]; RETURN function() { // Get the value for a variable const something = await surrealdb.value("$something"); // Get the value for a nested property const nested = await surrealdb.value("$obj.nested"); // Filter properties from an array const fromArray = await surrealdb.value("$arr[WHERE value > 3].value"); return { something, nested, fromArray }; } [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/built-in-functions.mdx) Copy page as Markdown [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) ### On this page * [async fetch(resource, options)](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#fetch) * [async query(surql)](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#query) * [async value(variable)](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions#value) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/built-in-functions.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Function context | Embedded scripting functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Scripting functions](https://surrealdb.com/docs/surrealql/functions/script) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) Function context[](https://surrealdb.com/docs/surrealql/functions/script/context#function-context) =================================================================================================== Embedded scripting functions inherit the context in which they are ran in. The this context of every embedded function is automatically set to the current document on every invocation. This allows the function to access the properties and fields of the current record being accessed / modified. CREATE film SET ratings = [\ { rating: 6, user: user:bt8e39uh1ouhfm8ko8s0 },\ { rating: 8, user: user:bsilfhu88j04rgs0ga70 },\ ], featured = function() { return this.ratings.filter( ({ rating }) => rating >= 7 ).map(({ rating, ...data }) => { return { ...data, rating: rating * 10 }; }); } ; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/context.mdx) Copy page as Markdown [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/context.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # IF ELSE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) `IF ELSE` statement[](https://surrealdb.com/docs/surrealql/statements/ifelse#if-else-statement) ================================================================================================ The `IF ELSE` statement can be used as a main statement, or within a parent statement, to return a value depending on whether a condition, or a series of conditions match. The statement allows for multiple `ELSE IF` expressions, and a final `ELSE` expression, with no limit to the number of `ELSE IF` conditional expressions. > Note > > As [THROW](https://surrealdb.com/docs/surrealql/statements/throw) > , [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) > , and [BREAK](https://surrealdb.com/docs/surrealql/statements/break) > do not return an expression, they must be inside a separate code block inside an `IF ELSE` statement. The original `IF ELSE` syntax involves one or more conditions, `THEN`, an expression if the condition evaluates to true, and the `END` keyword to finalize it. A separate code block can also be used, in which case multiple statements can be run. SurrealQL legacy syntaxIF @condition THEN @expression | { @expression; .. } [ ELSE IF @condition THEN @expression | { @expression; .. } ] ... [ ELSE @expression | { @expression; .. } ] END; The modern `IF ELSE` syntax does not require `THEN` or `END`, instead using `{}` to open up a code block on each condition check which will be run when it evaluates to true. Modern syntaxIF @condition { @expression; .. } [ ELSE IF @condition { @expression; .. } ] ... [ ELSE { @expression; .. } ] Example usage[](https://surrealdb.com/docs/surrealql/statements/ifelse#example-usage) -------------------------------------------------------------------------------------- ### Basic usage[](https://surrealdb.com/docs/surrealql/statements/ifelse#basic-usage) The following queries show example usage of this statement. The smallest possible `IF THEN` statement simply does something when a condition is true, and nothing otherwise. // Original syntax IF 9 = 9 THEN RETURN "Nine is indeed nine" END; // New scope syntax IF 9 = 9 { RETURN 'Nine is indeed nine' }; As the last line of a scope is its return value, the `RETURN` keyword can also be placed before the entire `IF THEN` statement. This is particularly convenient in long `IF ELSE` chains to avoid using the `RETURN` keyword at the end of every check for a condition. LET $num = 100; RETURN IF $num < 0 { "Negative" } ELSE IF $num = 0 { "Zero" } ELSE IF $num = 13 { "Thirteen" } ELSE { "Positive uninteresting number" }; The `THROW` keyword inside `{}` braces can be used to break out of an `IF LET` statement early. LET $badly_formatted_datetime = "2024-04TT08:08:08Z"; // Original syntax IF !type::is::datetime($badly_formatted_datetime) THEN { THROW "Whoops, that isn't a real datetime" } END; // New scope syntax IF !type::is::datetime($badly_formatted_datetime) { THROW "Whoops, that isn't a real datetime" }; Response"An error occurred: Whoops, that isn't a real datetime" `ELSE IF` branches and a final `ELSE` can be added into an `IF ELSE` statement: // Original syntax IF $access = "admin" THEN SELECT * FROM account ELSE IF $access = "user" THEN SELECT * FROM $auth.account ELSE { THROW "Access method hasn't been defined!" } END; // New scope syntax RETURN IF $access = "admin" { (SELECT * FROM account) } ELSE IF $access = "user" { (SELECT * FROM $auth.account) } ELSE { THROW "Access method hasn't been defined!" }; ### Advanced usage[](https://surrealdb.com/docs/surrealql/statements/ifelse#advanced-usage) The output of an `IF ELSE` statement can be assigned to a parameter: LET $num = 9; // Original syntax LET $odd_even = IF $num % 2 = 0 THEN "even" ELSE "odd" END; // New scope syntax LET $odd_even = IF $num % 2 = 0 { "even" } ELSE { "odd" }; If-else statements can also be used as subqueries within other statements. UPSERT person SET railcard = (IF age <= 10 THEN) 'junior' ELSE IF age <= 21 THEN 'student' ELSE IF age >= 65 THEN 'senior' ELSE NULL END ; Alternatively: UPSERT person SET railcard = (IF age <= 10 { 'junior' }) ELSE IF age <= 21 { 'student' } ELSE IF age >= 65 { 'senior' } ELSE { NULL }; You can also have nested conditions: IF $access = "admin" THEN SELECT * FROM admin_data WHERE access_level = 'full' ELSE IF $access = "user" THEN IF $auth.role = "premium" THEN IF $auth.subscription_status = "active" THEN SELECT * FROM premium_user_data WHERE active = 1 ELSE IF $auth.subscription_status = "trial" THEN SELECT * FROM trial_user_data ELSE SELECT * FROM basic_user_data END ELSE IF $auth.role = "standard" THEN SELECT * FROM standard_user_data WHERE region = 'US' ELSE IF $auth.role = "standard" AND $auth.subscription_status = "active" THEN SELECT * FROM standard_user_data WHERE region = 'EU' ELSE SELECT * FROM unauthorized_user_data END ELSE SELECT * FROM unknown_access_data END; The new scope syntax makes it easy to carry out multiple statements even inside nested conditions. IF $access = 'admin' { CREATE admin_user_event SET time = time::now(), info = "Admin user activity registered"; SELECT * FROM admin_data WHERE access_level = 'full'; } ELSE IF $access = 'user' { IF $auth.role = 'premium' { CREATE premium_user_event SET time = time::now(), info = "Premium user activity registered"; IF $auth.subscription_status = 'active' { SELECT * FROM premium_user_data WHERE active = 1 } ELSE IF $auth.subscription_status = 'trial' { SELECT * FROM trial_user_data } ELSE { SELECT * FROM basic_user_data } } ELSE IF $auth.role = 'standard' { SELECT * FROM standard_user_data WHERE region = 'US' } ELSE IF $auth.role = 'standard' AND $auth.subscription_status = 'active' { SELECT * FROM standard_user_data WHERE region = 'EU' } ELSE { SELECT * FROM unauthorized_user_data } } ELSE { SELECT * FROM unknown_access_data } [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/ifelse.mdx) Copy page as Markdown [FOR](https://surrealdb.com/docs/surrealql/statements/for) [INFO](https://surrealdb.com/docs/surrealql/statements/info) ### On this page * [IF ELSE statement](https://surrealdb.com/docs/surrealql/statements/ifelse#if-else-statement) * [Example usage](https://surrealdb.com/docs/surrealql/statements/ifelse#example-usage) * [Basic usage](https://surrealdb.com/docs/surrealql/statements/ifelse#basic-usage) * [Advanced usage](https://surrealdb.com/docs/surrealql/statements/ifelse#advanced-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/ifelse.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Parameters | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Parameters](https://surrealdb.com/docs/surrealql/parameters) Parameters[](https://surrealdb.com/docs/surrealql/parameters#parameters) ========================================================================= Parameters can be used like variables to store a value which can then be used in subsequent queries. To define a parameter in SurrealQL, use the [`LET`](https://surrealdb.com/docs/surrealql/statements/let) statement. The name of the parameter should begin with a `$` character. Defining parameters within SurrealQL[](https://surrealdb.com/docs/surrealql/parameters#defining-parameters-within-surrealql) ----------------------------------------------------------------------------------------------------------------------------- -- Define the parameter LET $suffix = "Morgan Hitchcock"; -- Use the parameter CREATE person SET name = "Tobie " + $suffix; -- (Another way to do the same) CREATE person SET name = string::join(" ", "Jaime", $suffix); Response[\ {\ "id": "person:3vs17lb9eso9m7gd8mml",\ "name": "Tobie Morgan Hitchcock"\ }\ ] [\ {\ "id": "person:xh4zbns5mgmywe6bo1pi",\ "name": "Jaime Morgan Hitchcock"\ }\ ] A parameter can store any value, including the result of a query. -- Assuming the CREATE statements from the previous example LET $founders = (SELECT * FROM person); RETURN $founders.name; Response[\ "Tobie Morgan Hitchcock",\ "Jaime Morgan Hitchcock"\ ] Parameters persist across the current connection, and thus can be reused between different namespaces and databases. In the example below, a created `person` record assigned to a parameter is reused in a query in a completely different namespace and database. LET $billy = CREATE ONLY person:billy SET name = "Billy"; -- Fails as `person:billy` already exists CREATE person CONTENT $billy; USE NAMESPACE other_namespace; USE DATABASE other_database; -- Succeeds as `person:billy` does not yet exist in this namespace and database CREATE person CONTENT $billy; Parameters can be defined using SurrealQL as shown above, or can be passed in using the client libraries as request variables. Redefining and shadowing parameters[](https://surrealdb.com/docs/surrealql/parameters#redefining-and-shadowing-parameters) --------------------------------------------------------------------------------------------------------------------------- Parameters in SurrealQL are immutable. The same parameter can be redefined using a `LET` statement. LET $my_name = "Alucard"; LET $my_name = "Sypha"; RETURN $my_name; Output'Sypha' Before SurrealDB 3.0, the `=` on its own was used as syntactic sugar for a `LET` statement. This has since been deprecated in order to make it clearer that parameters can be redeclared, but not modified. * [Before 3.X](https://surrealdb.com/docs/surrealql/parameters#tab-panel-246) * [Since 3.X](https://surrealdb.com/docs/surrealql/parameters#tab-panel-247) LET $my_name = "Alucard"; $my_name = "Sypha"; RETURN $my_name; Output'Sypha' LET $my_name = "Alucard"; $my_name = "Sypha"; RETURN $my_name; Output'There was a problem with the database: Parse error: Variable declaration without `let` is deprecated --> [4:1] | 4 | $my_name = "Sypha"; | ^^^^^^^^^^^^^^^^^^^ replace with `let $my_name = ..` ' If the parameter is redefined inside another scope, the original value will be shadowed. Shadowing refers to when a value is temporarily obstructed by a new value of the same name until the new scope has completed. LET $nums = [\ [1,2],\ [3,4]\ ]; { LET $nums = $nums.flatten(); -- Flattened into a single array, -- so $nums is shadowed as [1,2,3,4] RETURN $nums; }; -- Returns original unflattened $nums: -- [[1,2], [3,4]] RETURN $nums; Even a parameter defined using a [`DEFINE PARAM`](https://surrealdb.com/docs/surrealql/statements/define/param) statement can be shadowed. DEFINE PARAM $USERNAME VALUE "user@user.com"; $USERNAME = "some other email"; However, the parameter `$USERNAME` in this case is still defined as its original value, as can be seen via an [`INFO FOR DB`](https://surrealdb.com/docs/surrealql/statements/info) statement. { accesses: {}, analyzers: {}, apis: {}, configs: {}, functions: {}, models: {}, params: { USERNAME: "DEFINE PARAM $USERNAME VALUE 'user@user.com' PERMISSIONS FULL" }, tables: {}, users: {} } As the shadowed `$USERNAME` parameter will persist over the length of the connection, the parameter `$USERNAME` will once again show up as its original defined value if the connection is discontinued and restarted. Defining parameters within client libraries[](https://surrealdb.com/docs/surrealql/parameters#defining-parameters-within-client-libraries) ------------------------------------------------------------------------------------------------------------------------------------------- SurrealDB’s client libraries allow parameters to be passed in as JSON values, which are then converted to SurrealDB data types when the query is run. The following example show a variable being used within a SurrealQL query from the JavaScript library. let people = await surreal.query("SELECT * FROM article WHERE status INSIDE $status", { status: ["live", "draft"], }); Reserved variable names[](https://surrealdb.com/docs/surrealql/parameters#reserved-variable-names) --------------------------------------------------------------------------------------------------- SurrealDB automatically predefines certain variables depending on the type of operation being performed. For example, `$this` and `$parent` are automatically predefined for subqueries so that the fields of one can be compared to another if necessary. In addition, the predefined variables `$access`, `$auth`, `$token`, and `$session` are protected variables used to give access to parts of the current database configuration and can never be overwritten. LET $access = true; LET $auth = 10; LET $token = "Mytoken"; LET $session = rand::int(0, 100); Output-------- Query 1 -------- "'access' is a protected variable and cannot be set" -------- Query 2 -------- "'auth' is a protected variable and cannot be set" -------- Query 3 -------- "'token' is a protected variable and cannot be set" -------- Query 4 -------- "'session' is a protected variable and cannot be set" Other predefined variables listed below are not specifically protected, but should not be used in order to avoid unexpected behaviour. ### $before, $after[](https://surrealdb.com/docs/surrealql/parameters#before-after) Represent the values before and after a mutation on a field. CREATE cat SET name = "Mr. Meow", nicknames = ["Mr. Cuddlebun"]; UPDATE cat SET nicknames += "Snuggles" WHERE name = "Mr. Meow" RETURN $before, $after; Response[\ {\ "after": {\ "id": "cat:6p71csv2zqianixf0dkz",\ "name": "Mr. Meow",\ "nicknames": [\ "Mr. Cuddlebun",\ "Snuggles"\ ]\ },\ "before": {\ "id": "cat:6p71csv2zqianixf0dkz",\ "name": "Mr. Meow",\ "nicknames": [\ "Mr. Cuddlebun"\ ]\ }\ }\ ] ### $auth[](https://surrealdb.com/docs/surrealql/parameters#auth) Represents the currently authenticated record user. DEFINE TABLE user SCHEMAFULL PERMISSIONS FOR select, update, delete WHERE id = $auth.id; ### $event[](https://surrealdb.com/docs/surrealql/parameters#event) Represents the type of table event triggered on an event. This parameter will be one of either `"CREATE"`, `"UPDATE"`, or `"DELETE"`. DEFINE EVENT user_created ON TABLE user WHEN $event = "CREATE" THEN ( CREATE log SET table = "user", event = $event, created_at = time::now() ); ### $input[](https://surrealdb.com/docs/surrealql/parameters#input) Represents the initially inputted value in a field definition, as the value clause could have modified the $value variable. CREATE city:london SET population = 8900000, year = 2019, historical_data = []; INSERT INTO city [\ { id: "london", population: 9600000, year: 2023 }\ ] ON DUPLICATE KEY UPDATE -- Stick old data into historical_data historical_data += { year: year, population: population }, -- Then update current record with the new input using $input population = $input.population, year = $input.year; [\ {\ "historical_data": [\ {\ "population": 8900000,\ "year": 2019\ }\ ],\ "id": "city:london",\ "population": 9600000,\ "year": 2023\ }\ ] ### $parent, $this[](https://surrealdb.com/docs/surrealql/parameters#parent-this) `$this` represents the current record in a subquery, and `$parent` its parent. CREATE user SET name = "User1", member_of = "group1"; CREATE user SET name = "User2", member_of = "group1"; CREATE user SET name = "User3", member_of = "group1"; SELECT name, (SELECT VALUE name FROM user WHERE member_of = $parent.member_of) AS group_members FROM user WHERE name = "User1"; Response[\ {\ "group_members": [\ "User1",\ "User3",\ "User2"\ ],\ "name": "User1"\ }\ ] INSERT INTO person (name) VALUES ("John Doe"), ("John Doe"), ("Jane Doe"); SELECT *, (SELECT VALUE id FROM person WHERE $this.name = $parent.name) AS people_with_same_name FROM person; Response[\ {\ "id": "person:hwffcckiv61ylwiw43yf",\ "name": "John Doe",\ "people_with_same_name": [\ "person:hwffcckiv61ylwiw43yf",\ "person:tmscoy7bjj20xki0fld5"\ ]\ },\ {\ "id": "person:tmscoy7bjj20xki0fld5",\ "name": "John Doe",\ "people_with_same_name": [\ "person:hwffcckiv61ylwiw43yf",\ "person:tmscoy7bjj20xki0fld5"\ ]\ },\ {\ "id": "person:y7mdf3912rf5gynvxc7q",\ "name": "Jane Doe",\ "people_with_same_name": [\ "person:y7mdf3912rf5gynvxc7q"\ ]\ }\ ] ### $access[](https://surrealdb.com/docs/surrealql/parameters#access) Represents the name of the access method used to authenticate the current session. IF $access = "admin" THEN ( SELECT * FROM account ) ELSE IF $access = "user" THEN ( SELECT * FROM $auth.account ) ELSE [] END ### $session[](https://surrealdb.com/docs/surrealql/parameters#session) Represents values from the session functions as an object. You can learn more about those values from the [security parameters](https://surrealdb.com/docs/surrealdb/security/authentication#session) section. CREATE user SET name = "Some User", on_database = $session.db; Response[\ {\ "id": "user:wa3ajflozlqoyurc4i4v",\ "name": "Some User",\ "on_database": "database"\ }\ ] ### $token[](https://surrealdb.com/docs/surrealql/parameters#token) Represents values held inside the JWT token used for the current session. You can learn more about those values from the [security parameters](https://surrealdb.com/docs/surrealdb/security/authentication#token) section. DEFINE TABLE user SCHEMAFULL PERMISSIONS FOR select, update, delete, create WHERE $access = "users" AND email = $token.email; ### $value[](https://surrealdb.com/docs/surrealql/parameters#value) Represents the value after a mutation on a field (identical to $after in the case of an event). DEFINE EVENT email ON TABLE user WHEN $before.email != $after.email THEN ( CREATE event SET user = $value.id, time = time::now(), value = $after.email, action = 'email_changed' ); ### $request[](https://surrealdb.com/docs/surrealql/parameters#request) Available since: v2.0.0 This parameter represents the value of a request to a custom API defined using the [`DEFINE API`](https://surrealdb.com/docs/surrealql/statements/define/api) statement. DEFINE API OVERWRITE "/test" FOR get, post MIDDLEWARE api::req::raw_body(false) THEN { RETURN { status: 404, body: $request.body, headers: { 'bla': '123' } }; }; The `$request` parameter may contain values at the following fields: `body`, `headers`, `method`, `query`, and `params`. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/parameters.mdx) Copy page as Markdown [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Transactions](https://surrealdb.com/docs/surrealql/transactions) ### On this page * [Defining parameters within SurrealQL](https://surrealdb.com/docs/surrealql/parameters#defining-parameters-within-surrealql) * [Redefining and shadowing parameters](https://surrealdb.com/docs/surrealql/parameters#redefining-and-shadowing-parameters) * [Defining parameters within client libraries](https://surrealdb.com/docs/surrealql/parameters#defining-parameters-within-client-libraries) * [Reserved variable names](https://surrealdb.com/docs/surrealql/parameters#reserved-variable-names) * [$before, $after](https://surrealdb.com/docs/surrealql/parameters#before-after) * [$auth](https://surrealdb.com/docs/surrealql/parameters#auth) * [$event](https://surrealdb.com/docs/surrealql/parameters#event) * [$input](https://surrealdb.com/docs/surrealql/parameters#input) * [$parent, $this](https://surrealdb.com/docs/surrealql/parameters#parent-this) * [$access](https://surrealdb.com/docs/surrealql/parameters#access) * [$session](https://surrealdb.com/docs/surrealql/parameters#session) * [$token](https://surrealdb.com/docs/surrealql/parameters#token) * [$value](https://surrealdb.com/docs/surrealql/parameters#value) * [$request](https://surrealdb.com/docs/surrealql/parameters#request) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/parameters.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Machine learning functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [ML functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) Machine Learning functions[](https://surrealdb.com/docs/surrealql/functions/ml/functions#machine-learning-functions) ===================================================================================================================== These functions can be used when calculating outputs from a trained machine learning model that has been uploaded to the database. | Function | Description | | --- | --- | | [` ml::name-of-model() `](https://surrealdb.com/docs/surrealql/functions/ml/functions#mlname-of-modelversion) | Computes a value from a trained machine learning model | `ml::name-of-model()`[](https://surrealdb.com/docs/surrealql/functions/ml/functions#mlname-of-modelversion) --------------------------------------------------------------------------------------------------------------------- Once a model has been uploaded to the database, the model can be called with inputs resulting in a calculation from the trained ml model. We can do a basic raw computation with the following call: API DEFINITIONml::house-price-prediction<0.0.1>(500.0, 1.0); In the above example, the model we are calling is called `house-price-prediction` with the version `0.0.1`. We then pass in a raw vector of `[ [500.0, 1.0] ]` Depending on the model, the name and version of the model will vary as well as the inputs. The name and version of the model will be defined in the `.surml` file which will defined when uploading the model to the database. We can also perform a “buffered compute” with the code below: API DEFINITIONml::house-price-prediction<0.0.1>({squarefoot: 500.0, num_floors: 1.0}); Here, we are using the key mappings in the header of the `.surml` file uploaded to the database to map the fields defined in the object passed into the `ml::` function in the correct order. If there are any normalisation parameters in the header of the `.surml` file, these will also be applied. The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN ml::house-price-prediction<0.0.1>({squarefoot: 500.0, num_floors: 1.0}); 250000 Seeing as the ML is integrated into our surql, we can infer entire columns using the ml function. We can demonstrate this with a simple example of house prices. We can define some basic table with the following surql: CREATE house_listing SET squarefoot_col = 500.0, num_floors_col = 1.0; CREATE house_listing SET squarefoot_col = 1000.0, num_floors_col = 2.0; CREATE house_listing SET squarefoot_col = 1500.0, num_floors_col = 3.0; We can then get all the rows with the imputed price prediction with the surql below: SELECT *, ml::house-price-prediction<0.0.1>({ squarefoot: squarefoot_col, num_floors: num_floors_col }) AS price_prediction FROM house_listing; This would statement gives us the following result: [\ {\ "id": "house_listing:7bo0f35tl4hpx5bymq5d",\ "num_floors_col": 3,\ "price_prediction": 406534.75,\ "squarefoot_col": 1500\ },\ {\ "id": "house_listing:8k2ttvhp2vh8v7skwyie",\ "num_floors_col": 2,\ "price_prediction": 291870.5,\ "squarefoot_col": 1000\ },\ {\ "id": "house_listing:vnlv3nzr21oi5o23kydw",\ "num_floors_col": 1,\ "price_prediction": 177206.21875,\ "squarefoot_col": 500\ }\ ] We can see that our price prediction is calculated in the query. We can build on the previous surql to filter based on the computed price prediction with the surql below: SELECT * FROM ( SELECT *, ml::house-price-prediction<0.0.1>({ squarefoot: squarefoot_col, num_floors: num_floors_col }) AS price_prediction FROM house_listing ) WHERE price_prediction > 177206.21875; This gives us the following result: [\ {\ "id": "house_listing:7bo0f35tl4hpx5bymq5d",\ "num_floors_col": 3,\ "price_prediction": 406534.75,\ "squarefoot_col": 1500\ },\ {\ "id": "house_listing:8k2ttvhp2vh8v7skwyie",\ "num_floors_col": 2,\ "price_prediction": 291870.5,\ "squarefoot_col": 1000\ }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/ml/functions.mdx) Copy page as Markdown [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Parameters](https://surrealdb.com/docs/surrealql/parameters) ### On this page * [Machine Learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions#machine-learning-functions) * [ml::name-of-model()](https://surrealdb.com/docs/surrealql/functions/ml/functions#mlname-of-modelversion) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/ml/functions.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # FOR statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [FOR](https://surrealdb.com/docs/surrealql/statements/for) `FOR` statement[](https://surrealdb.com/docs/surrealql/statements/for#for-statement) ===================================================================================== The `FOR` statement can be used to iterate over the values of an array, and to perform certain actions with those values. > Note > > A `FOR` loop currently cannot modify items outside its own scope, such as variables declared before the loop. SurrealQL SyntaxFOR @item IN @iterable { @block }; Example usage[](https://surrealdb.com/docs/surrealql/statements/for#example-usage) ----------------------------------------------------------------------------------- The following query shows example usage of this statement. -- Create a person for everyone in the array FOR $name IN ['Tobie', 'Jaime'] { CREATE type::thing('person', $name) CONTENT { name: $name }; }; The following query shows the `FOR` statement being used update a property on every user matching certain criteria. -- Set can_vote to true for every person over 18 years old. FOR $person IN (SELECT VALUE id FROM person WHERE age >= 18) { UPDATE $person SET can_vote = true; }; Ranges in FOR loops[](https://surrealdb.com/docs/surrealql/statements/for#ranges-in-for-loops) ----------------------------------------------------------------------------------------------- Available since: v2.0.0 A `FOR` loop can also be made out of a [range UUID](https://surrealdb.com/docs/surrealql/datamodel/ranges) of integers. FOR $year IN 0..=2024 { CREATE historical_events SET for_year = $year, events = "To be added"; } Limitations of FOR loops[](https://surrealdb.com/docs/surrealql/statements/for#limitations-of-for-loops) --------------------------------------------------------------------------------------------------------- Parameters declared outside of a `FOR` loop can be used inside the loop. LET $table1 = "person"; LET $table2 = "cat"; FOR $i in 0..4 { CREATE type::thing($table1, $i); CREATE type::thing($table2, $i); }; However, they currently cannot be modified inside a loop, making an operation like the following impossible. LET $init = []; FOR $num IN 1..=3 { $init += $num; }; RETURN $init; In this case, the [`array::fold`](https://surrealdb.com/docs/surrealql/functions/database/array#arrayfold) and [`array::reduce`](https://surrealdb.com/docs/surrealql/functions/database/array#arrayreduce) functions can often be used to accomplish the intended behaviour. (1..=3).reduce(|$one, $two| $one + $two); Output6 [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/for.mdx) Copy page as Markdown [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) ### On this page * [FOR statement](https://surrealdb.com/docs/surrealql/statements/for#for-statement) * [Example usage](https://surrealdb.com/docs/surrealql/statements/for#example-usage) * [Ranges in FOR loops](https://surrealdb.com/docs/surrealql/statements/for#ranges-in-for-loops) * [Limitations of FOR loops](https://surrealdb.com/docs/surrealql/statements/for#limitations-of-for-loops) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/for.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Sleep function | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) Sleep function[](https://surrealdb.com/docs/surrealql/functions/database/sleep#sleep-function) =============================================================================================== This function can be used to introduce a delay or pause in the execution of a query or a batch of queries for a specific amount of time. | Function | Description | | --- | --- | | [`sleep()`](https://surrealdb.com/docs/surrealql/functions/database/sleep#sleep) | Delays or pauses in the execution of a query or a batch of queries. | `sleep`[](https://surrealdb.com/docs/surrealql/functions/database/sleep#sleep) ------------------------------------------------------------------------------- The `sleep` function delays or pauses the execution of a query or a set of statements. API DEFINITIONsleep(duration) -> none The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN sleep(1s); RETURN sleep(500ms); SurrealDB also has a [SLEEP statement](https://surrealdb.com/docs/surrealql/statements/sleep) statement that accepts a datetime; however, the `sleep` function can be used in more dynamic ways such as the following example that simulates a 100ms delay between each record in a query. -- Create 3 `person` records CREATE |person:3|; LET $now = time::now(); SELECT *, sleep(100ms) AS _, time::now() - $now AS elapsed FROM person; Response[\ {\ _: NONE,\ elapsed: 101ms457µs,\ id: person:fkgvriz1kl2tcgv6yqfq\ },\ {\ _: NONE,\ elapsed: 203ms599µs,\ id: person:lgibwdgtvx4v8ck60guk\ },\ {\ _: NONE,\ elapsed: 305ms728µs,\ id: person:pr0uby896y1az2p44wtw\ }\ ] SLEEP during parallel operations[](https://surrealdb.com/docs/surrealql/functions/database/sleep#sleep-during-parallel-operations) ----------------------------------------------------------------------------------------------------------------------------------- The `sleep()` function does not interfere with operations that are underway in the background, such as a [`DEFINE INDEX`](https://surrealdb.com/docs/surrealql/statements/define/indexes) statement using the `CONCURRENTLY` clause. CREATE |user:50000| SET name = id.id() RETURN NONE; DEFINE INDEX unique_name ON TABLE user FIELDS name UNIQUE CONCURRENTLY; INFO FOR INDEX unique_name ON TABLE user;√ RETURN sleep(50ms); INFO FOR INDEX unique_name ON TABLE user; RETURN sleep(50ms); INFO FOR INDEX unique_name ON TABLE user; RETURN sleep(50ms); INFO FOR INDEX unique_name ON TABLE user; Possible output-------- Query 1 -------- { building: { initial: 0, pending: 0, status: 'indexing', updated: 0 } } -------- Query 2 -------- { building: { initial: 100, pending: 20, status: 'indexing', updated: 0 } } -------- Query 3 -------- { building: { initial: 100, pending: 4, status: 'indexing', updated: 16 } } -------- Query 4 -------- { building: { status: 'ready' } } Use cases[](https://surrealdb.com/docs/surrealql/functions/database/sleep#use-cases) ------------------------------------------------------------------------------------- Putting a database to sleep can be useful in a small number of situations, such as: * Testing and debugging: can be used to understand how concurrent transactions interact, test how systems handle timeouts and delays, simulate behaviour in more distant regions with longer latency * Throttling: can be used to throttle the execution of operations to prevent the database from being overwhelmed by too many requests at once * Security measures: can be used to slow down the response rate of login attempts to mitigate the risk of brute force attacks [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/sleep.mdx) Copy page as Markdown [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) ### On this page * [sleep](https://surrealdb.com/docs/surrealql/functions/database/sleep#sleep) * [SLEEP during parallel operations](https://surrealdb.com/docs/surrealql/functions/database/sleep#sleep-during-parallel-operations) * [Use cases](https://surrealdb.com/docs/surrealql/functions/database/sleep#use-cases) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/sleep.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Type conversion | Embedded scripting functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Scripting functions](https://surrealdb.com/docs/surrealql/functions/script) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) Type conversion[](https://surrealdb.com/docs/surrealql/functions/script/type-conversion#type-conversion) ========================================================================================================= Any value from SurrealDB is converted into a JavaScript type automatically, and the return value from the JavaScript function is converted to a SurrealQL value. Boolean values, Integers, Floats, Strings, Arrays, Objects, and Date objects are all converted automatically to and from SurrealQL values. CREATE user:test SET created_at = function() { return new Date(); }; In addition, a number of special classes are included within the JavaScript functions for the additional types which are not built into JavaScript. These enable the creation of [`duration`](https://surrealdb.com/docs/surrealql/datamodel/datetimes#durations-and-datetimes) values, [`record`](https://surrealdb.com/docs/surrealql/datamodel/ids) ids, and [`UUID`](https://surrealdb.com/docs/surrealql/datamodel/strings#uuid) values from within JavaScript. Any values of these types passed into embedded scripting functions are also represented with these special classes. CREATE user:test SET session_timeout = function() { return new Duration('1w'); }, best_friend = function() { return new Record('user', 'joanna'); }, identifier = function() { return new Uuid('03412258-988f-47cd-82db-549902cdaffe'); } ; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/type-conversion.mdx) Copy page as Markdown [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/type-conversion.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Machine Learning functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [ML functions](https://surrealdb.com/docs/surrealql/functions/ml) Machine Learning functions[](https://surrealdb.com/docs/surrealql/functions/ml#machine-learning-functions) =========================================================================================================== SurrealDB offers machine learning functions to help you build and deploy machine learning models. Currently, SurrealDB supports the following machine learning functions: * [Trained models](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/ml/index.mdx) Copy page as Markdown [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) ### On this page * [Machine Learning functions](https://surrealdb.com/docs/surrealql/functions/ml#machine-learning-functions) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/ml/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Comments | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Comments](https://surrealdb.com/docs/surrealql/comments) Comments[](https://surrealdb.com/docs/surrealql/comments#comments) =================================================================== In SurrealQL, comments can be written in a number of different ways. /* In SurrealQL, comments can be written as single-line or multi-line comments, and comments can be used and interspersed within statements. */ SELECT * FROM /* get all users */ user; # There are a number of ways to use single-line comments SELECT * FROM user; // Alternatively using two forward-slash characters SELECT * FROM user; -- Another way is to use two dash characters SELECT * FROM user; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/comments.mdx) Copy page as Markdown [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Overview](https://surrealdb.com/docs/surrealql) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/comments.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Vector functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) Vector functions[](https://surrealdb.com/docs/surrealql/functions/database/vector#vector-functions) ==================================================================================================== A collection of essential vector operations that provide foundational functionality for numerical computation, machine learning, and data analysis. These operations include distance measurements, similarity coefficients, and other basic and complex operations related to vectors. Through understanding and implementing these functions, we can perform a wide variety of tasks ranging from data processing to advanced statistical analyses. | Function | Description | | --- | --- | | [`vector::add()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectoradd) | Performs element-wise addition of two vectors | | [`vector::angle()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorangle) | Computes the angle between two vectors | | [`vector::cross()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorcross) | Computes the cross product of two vectors | | [`vector::divide()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordivide) | Performs element-wise division between two vectors | | [`vector::dot()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordot) | Computes the dot product of two vectors | | [`vector::magnitude()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectormagnitude) | Computes the magnitude (or length) of a vector | | [`vector::multiply()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectormultiply) | Performs element-wise multiplication of two vectors | | [`vector::normalize()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectornormalize) | Computes the normalization of a vector | | [`vector::project()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorproject) | Computes the projection of one vector onto another | | [`vector::scale()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorscale) | Multiplies each item in a vector | | [`vector::subtract()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsubtract) | Performs element-wise subtraction between two vectors | | [`vector::distance::chebyshev()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancechebyshev) | Computes the Chebyshev distance | | [`vector::distance::euclidean()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceeuclidean) | Computes the Euclidean distance between two vectors | | [`vector::distance::hamming()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancehamming) | Computes the Hamming distance between two vectors | | [`vector::distance::knn()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceknn) | Returns the distance computed during the query | | [`vector::distance::manhattan()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancemanhattan) | Computes the Manhattan distance between two vectors | | [`vector::distance::minkowski()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceminkowski) | Computes the Minkowski distance between two vectors | | [`vector::similarity::cosine()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilaritycosine) | Computes the Cosine similarity between two vectors | | [`vector::similarity::jaccard()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilarityjaccard) | Computes the Jaccard similarity between two vectors | | [`vector::similarity::pearson()`](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilaritypearson) | Computes the Pearson correlation coefficient between two vectors | `vector::add`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectoradd) ------------------------------------------------------------------------------------------ The `vector::add` function performs element-wise addition of two vectors, where each element in the first vector is added to the corresponding element in the second vector. API DEFINITIONvector::add(array, array) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::add([1, 2, 3], [1, 2, 3]); [2, 4, 6] `vector::angle`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorangle) ---------------------------------------------------------------------------------------------- The `vector::angle` function computes the angle between two vectors, providing a measure of the orientation difference between them. API DEFINITIONvector::angle(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::angle([5, 10, 15], [10, 5, 20]); 0.36774908225917935f `vector::cross`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorcross) ---------------------------------------------------------------------------------------------- The `vector::cross` function computes the cross product of two vectors, which results in a vector that is orthogonal (perpendicular) to the plane containing the original vectors. API DEFINITIONvector::cross(array, array) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::cross([1, 2, 3], [4, 5, 6]); [-3, 6, -3] `vector::divide`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordivide) ------------------------------------------------------------------------------------------------ The `vector::divide` function performs element-wise division between two vectors, where each element in the first vector is divided by the corresponding element in the second vector. API DEFINITIONvector::divide(array, array) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::divide([10, -20, 30, 0], [0, -1, 2, -3]); [NaN, 20, 15, 0] `vector::dot`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordot) ------------------------------------------------------------------------------------------ The `vector::dot` function computes the dot product of two vectors, which is the sum of the products of the corresponding entries of the two sequences of numbers. API DEFINITIONvector::dot(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::dot([1, 2, 3], [1, 2, 3]); 14 `vector::magnitude`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectormagnitude) ------------------------------------------------------------------------------------------------------ The `vector::magnitude` function computes the magnitude (or length) of a vector, providing a measure of the size of the vector in multi-dimensional space. API DEFINITIONvector::magnitude(array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::magnitude([ 1, 2, 3, 3, 3, 4, 5 ]); 8.54400374531753f `vector::multiply`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectormultiply) ---------------------------------------------------------------------------------------------------- The `vector::multiply` function performs element-wise multiplication of two vectors, where each element in the first vector is multiplied by the corresponding element in the second vector. API DEFINITIONvector::multiply(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::multiply([1, 2, 3], [1, 2, 3]); [1, 4, 9] `vector::normalize`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectornormalize) ------------------------------------------------------------------------------------------------------ The `vector::normalize` function computes the normalization of a vector, transforming it to a unit vector (a vector of length 1) that maintains the original direction. API DEFINITIONvector::normalize(array) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::normalize([ 4, 3 ]); [0.8f, 0.6f] `vector::project`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorproject) -------------------------------------------------------------------------------------------------- The `vector::project` function computes the projection of one vector onto another, providing a measure of the shadow of one vector on the other. The projection is obtained by multiplying the magnitude of the given vectors with the cosecant of the angle between the two vectors. API DEFINITIONvector::project(array, array) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::project([1, 2, 3], [4, 5, 6]); [1.6623376623376624f, 2.077922077922078f, 2.4935064935064934f] `vector::scale`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorscale) ---------------------------------------------------------------------------------------------- Available since: v2.0.0 The `vector::scale` function multiplies each item in a vector by a number. API DEFINITIONvector::scale(array, number) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::scale([3, 1, 5, -3, 7, 2], 5); [ 15, 5, 25, -15, 35, 10 ] `vector::subtract`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsubtract) ---------------------------------------------------------------------------------------------------- The `vector::subtract` function performs element-wise subtraction between two vectors, where each element in the second vector is subtracted from the corresponding element in the first vector. API DEFINITIONvector::subtract(array, array) -> array The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::subtract([4, 5, 6], [3, 2, 1]); [1, 3, 5] `vector::distance::chebyshev`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancechebyshev) ------------------------------------------------------------------------------------------------------------------------ The `vector::distance::chebyshev` function computes the Chebyshev distance (also known as maximum value distance) between two vectors, which is the greatest of their differences along any coordinate dimension. API DEFINITIONvector::distance::chebyshev(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::distance::chebyshev([2, 4, 5, 3, 8, 2], [3, 1, 5, -3, 7, 2]); 6f `vector::distance::euclidean`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceeuclidean) ------------------------------------------------------------------------------------------------------------------------ The `vector::distance::euclidean` function computes the Euclidean distance between two vectors, providing a measure of the straight-line distance between two points in a multi-dimensional space. API DEFINITIONvector::distance::euclidean(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::distance::euclidean([10, 50, 200], [400, 100, 20]); 432.43496620879307f `vector::distance::hamming`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancehamming) -------------------------------------------------------------------------------------------------------------------- The `vector::distance::hamming` function computes the Hamming distance between two vectors, measuring the minimum number of substitutions required to change one vector into the other, useful for comparing strings or codes. API DEFINITIONvector::distance::hamming(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::distance::hamming([1, 2, 2], [1, 2, 3]); 1 `vector::distance::knn`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceknn) ------------------------------------------------------------------------------------------------------------ The `vector::distance::knn` function returns the distance computed during the query by the Knn operator (avoiding recomputation). API DEFINITIONvector::distance::knn() -> number The following example shows this function, and its output, when used in a [`SELECT`](https://surrealdb.com/docs/surrealql/statements/select) statement: CREATE pts:1 SET point = [1,2,3,4]; CREATE pts:2 SET point = [4,5,6,7]; CREATE pts:3 SET point = [8,9,10,11]; SELECT id, vector::distance::knn() AS dist FROM pts WHERE point <|2,EUCLIDEAN|> [2,3,4,5]; [\ {\ id: pts:1,\ dist: 2f\ },\ {\ id: pts:2,\ dist: 4f\ }\ ] `vector::distance::manhattan`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancemanhattan) ------------------------------------------------------------------------------------------------------------------------ The `vector::distance::manhattan` function computes the Manhattan distance (also known as the L1 norm or Taxicab geometry) between two vectors, which is the sum of the absolute differences of their corresponding elements. API DEFINITIONvector::distance::manhattan(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::distance::manhattan([10, 20, 15, 10, 5], [12, 24, 18, 8, 7]); 13 `vector::distance::minkowski`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceminkowski) ------------------------------------------------------------------------------------------------------------------------ The `vector::distance::minkowski` function computes the Minkowski distance between two vectors, a generalization of other distance metrics such as Euclidean and Manhattan when parameterized with different values of p. API DEFINITIONvector::distance::minkowski(array, array, number) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::distance::minkowski([10, 20, 15, 10, 5], [12, 24, 18, 8, 7], 3); 4.862944131094279f `vector::similarity::cosine`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilaritycosine) ---------------------------------------------------------------------------------------------------------------------- The `vector::similarity::cosine` function computes the Cosine similarity between two vectors, indicating the cosine of the angle between them, which is a measure of how closely two vectors are oriented to each other. API DEFINITIONvector::similarity::cosine(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::similarity::cosine([10, 50, 200], [400, 100, 20]); 0.15258215962441316f `vector::similarity::jaccard`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilarityjaccard) ------------------------------------------------------------------------------------------------------------------------ The `vector::similarity::jaccard` function computes the Jaccard similarity between two vectors, measuring the intersection divided by the union of the datasets represented by the vectors. API DEFINITIONvector::similarity::jaccard(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::similarity::jaccard([0,1,2,5,6], [0,2,3,4,5,7,9]); 0.3333333333333333f `vector::similarity::pearson`[](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilaritypearson) ------------------------------------------------------------------------------------------------------------------------ The `vector::similarity::pearson` function Computes the Pearson correlation coefficient between two vectors, reflecting the degree of linear relationship between them. API DEFINITIONvector::similarity::pearson(array, array) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN vector::similarity::pearson([1,2,3], [1,5,7]); 0.9819805060619659f [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/vector.mdx) Copy page as Markdown [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) ### On this page * [vector::add](https://surrealdb.com/docs/surrealql/functions/database/vector#vectoradd) * [vector::angle](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorangle) * [vector::cross](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorcross) * [vector::divide](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordivide) * [vector::dot](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordot) * [vector::magnitude](https://surrealdb.com/docs/surrealql/functions/database/vector#vectormagnitude) * [vector::multiply](https://surrealdb.com/docs/surrealql/functions/database/vector#vectormultiply) * [vector::normalize](https://surrealdb.com/docs/surrealql/functions/database/vector#vectornormalize) * [vector::project](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorproject) * [vector::scale](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorscale) * [vector::subtract](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsubtract) * [vector::distance::chebyshev](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancechebyshev) * [vector::distance::euclidean](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceeuclidean) * [vector::distance::hamming](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancehamming) * [vector::distance::knn](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceknn) * [vector::distance::manhattan](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistancemanhattan) * [vector::distance::minkowski](https://surrealdb.com/docs/surrealql/functions/database/vector#vectordistanceminkowski) * [vector::similarity::cosine](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilaritycosine) * [vector::similarity::jaccard](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilarityjaccard) * [vector::similarity::pearson](https://surrealdb.com/docs/surrealql/functions/database/vector#vectorsimilaritypearson) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/vector.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # SurrealQL functions | Embedded scripting functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Scripting functions](https://surrealdb.com/docs/surrealql/functions/script) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) SurrealQL functions[](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions#surrealql-functions) ===================================================================================================================== Embedded scripting functions have access to native SurrealQL functions, allowing for complex and performant operations otherwise not possible. SurrealQL functions are published under the `surrealdb.functions` variable. Custom functions are not available within the embedded JavaScript function at the moment. RETURN function() { // Using the rand::uuid::v4() function const uuid = surrealdb.functions.rand.uuid.v4(); }; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/surrealql-functions.mdx) Copy page as Markdown [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/script/surrealql-functions.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE SCOPE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) > Warning > > This statement is deprecated in favor of `DEFINE ACCESS ... TYPE RECORD`. Learn more in the [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) > . `DEFINE SCOPE` statement[](https://surrealdb.com/docs/surrealql/statements/define/scope#define-scope-statement) ================================================================================================================ Setting scope access allows SurrealDB to operate as a web database. With scopes you can set authentication and access rules which enable fine-grained access to tables and fields. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/scope#requirements) ------------------------------------------------------------------------------------------ * You must be authenticated as a root or namespace user before you can use the `DEFINE SCOPE` statement. * [You must select your namespace and database](https://surrealdb.com/docs/surrealql/statements/use) before you can use the `DEFINE SCOPE` statement. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/scope#statement-syntax) -------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE SCOPE [ OVERWRITE | IF NOT EXISTS ] @name SESSION @duration SIGNUP @expression SIGNIN @expression [ COMMENT @string ] Example usage[](https://surrealdb.com/docs/surrealql/statements/define/scope#example-usage) -------------------------------------------------------------------------------------------- Below shows how you can create a scope using the `DEFINE SCOPE` statement. -- Enable scope authentication directly in SurrealDB DEFINE SCOPE account SESSION 24h SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) ) SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) ; Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/scope#using-if-not-exists-clause) ------------------------------------------------------------------------------------------------------------------------ Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define a scope only if it does not already exist. If the scope already exists, the `DEFINE SCOPE` statement will return an error. -- Create a SCOPE if it does not already exist DEFINE SCOPE IF NOT EXISTS example; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/scope.mdx) Copy page as Markdown [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) ### On this page * [DEFINE SCOPE statement](https://surrealdb.com/docs/surrealql/statements/define/scope#define-scope-statement) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/scope#requirements) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/scope#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/scope#example-usage) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/scope#using-if-not-exists-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/scope.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE NAMESPACE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) `DEFINE NAMESPACE` statement[](https://surrealdb.com/docs/surrealql/statements/define/namespace#define-namespace-statement) ============================================================================================================================ SurrealDB has a multi-tenancy model which allows you to scope databases to a namespace. There is no limit to the number of databases that can be in a namespace, nor is there a limit to the number of namespaces allowed. Only users with root access are authorized to create namespaces. Let’s say that you’re using SurrealDB to create a multi-tenant SaaS application. You can guarantee that the data of each tenant will be kept separate from other tenants if you put each tenant’s databases into separate namespaces. In other words, this will ensure that information will remain siloed so user will only have access the information in the namespace they are a member of. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/namespace#requirements) ---------------------------------------------------------------------------------------------- * You must be authenticated as a root owner or editor before you can use the `DEFINE NAMESPACE` statement. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/namespace#statement-syntax) ------------------------------------------------------------------------------------------------------ SurrealQL SyntaxDEFINE NAMESPACE [ OVERWRITE | IF NOT EXISTS ] @name [ COMMENT @string ] Example usage[](https://surrealdb.com/docs/surrealql/statements/define/namespace#example-usage) ------------------------------------------------------------------------------------------------ Below shows how you can create a namespace using the `DEFINE NAMESPACE` statement. -- Namespace for Abcum Ltd. DEFINE NAMESPACE abcum; Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/namespace#using-if-not-exists-clause) ---------------------------------------------------------------------------------------------------------------------------- Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define a namespace only if it does not already exist. You should use the `IF NOT EXISTS` clause when defining a namespace in SurrealDB if you want to ensure that the namespace is only created if it does not already exist. If the namespace already exists, the `DEFINE NAMESPACE` statement will return an error. It’s particularly useful when you want to safely attempt to define a namespace without manually checking its existence first. On the other hand, you should not use the `IF NOT EXISTS` clause when you want to ensure that the namespace definition is updated regardless of whether it already exists. In such cases, you might prefer using the `OVERWRITE` clause, which allows you to define a namespace and overwrite an existing one if it already exists, ensuring that the latest version of the namespace definition is always in use -- Create a NAMESPACE if it does not already exist DEFINE NAMESPACE IF NOT EXISTS example; Using `OVERWRITE` clause[](https://surrealdb.com/docs/surrealql/statements/define/namespace#using-overwrite-clause) -------------------------------------------------------------------------------------------------------------------- Available since: v2.0.0 The `OVERWRITE` clause can be used to define a namespace and overwrite an existing one if it already exists. You should use the `OVERWRITE` clause when you want to modify an existing namespace definition. If the namespace already exists, the `DEFINE NAMESPACE` statement will overwrite the existing namespace definition with the new one. -- Create an NAMESPACE and overwrite if it already exists DEFINE NAMESPACE OVERWRITE example; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/namespace.mdx) Copy page as Markdown [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) ### On this page * [DEFINE NAMESPACE statement](https://surrealdb.com/docs/surrealql/statements/define/namespace#define-namespace-statement) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/namespace#requirements) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/namespace#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/namespace#example-usage) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/namespace#using-if-not-exists-clause) * [Using OVERWRITE clause](https://surrealdb.com/docs/surrealql/statements/define/namespace#using-overwrite-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/namespace.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # INSERT statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) `INSERT` statement[](https://surrealdb.com/docs/surrealql/statements/insert#insert-statement) ============================================================================================== The `INSERT` statement can be used to insert or update data into the database, using the same statement syntax as the traditional SQL Insert statement. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/insert#statement-syntax) SurrealQL SyntaxINSERT [ RELATION ] [ IGNORE ] INTO @what [ @value\ | (@fields) VALUES (@values)\ [ ON DUPLICATE KEY UPDATE @field = @value ... ]\ ] [ RETURN NONE | RETURN BEFORE | RETURN AFTER | RETURN DIFF | RETURN @statement_param, ... | RETURN VALUE @statement_param ] ; Example usage[](https://surrealdb.com/docs/surrealql/statements/insert#example-usage) -------------------------------------------------------------------------------------- The following query shows example usage of this statement. INSERT INTO company { name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] }; 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 INSERT INTO company (name, founded) VALUES ('SurrealDB', '2021-09-10'); -- Insert multiple records INSERT INTO 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. INSERT INTO product (name, url) VALUES ('Salesforce', 'salesforce.com') ON DUPLICATE KEY UPDATE tags += 'crm'; 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`](https://surrealdb.com/docs/surrealql/parameters#input) : INSERT INTO city (id, population, at_year) VALUES ("Calgary", 1665000, 2024) ON DUPLICATE KEY UPDATE population = $input.population, at_year = $input.at_year; An example of `ON DUPLICATE KEY UPDATE` when a unique key is encountered shows the same behaviour as that with a duplicate record: DEFINE FIELD for ON user_data TYPE record; DEFINE INDEX one_user ON user_data FIELDS for UNIQUE; INSERT INTO user_data { for: user:one, some: "data" } ON DUPLICATE KEY UPDATE times_updated += 1, last_edited = time::now(); INSERT INTO user_data { for: user:one, some_more: "data" } ON DUPLICATE KEY UPDATE times_updated += 1, last_edited = time::now(); Output-------- Query -------- [\ {\ for: user:one,\ id: user_data:kp78dubsxmp4f04x0de3,\ some: 'data'\ }\ ] -------- Query -------- [\ {\ for: user:one,\ id: user_data:kp78dubsxmp4f04x0de3,\ last_edited: d'2025-07-14T05:15:52.146Z',\ some: 'data',\ times_updated: 1\ }\ ] 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. INSERT INTO recordings_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. INSERT INTO person [\ { id: "jaime", name: "Jaime", surname: "Morgan Hitchcock" },\ { id: "tobie", name: "Tobie", surname: "Morgan Hitchcock" },\ ] ### Ignoring duplicates[](https://surrealdb.com/docs/surrealql/statements/insert#ignoring-duplicates) 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. INSERT IGNORE INTO person [\ { id: "jaime", name: "Jaime", surname: "Morgan Hitchcock" },\ { id: "tobie", name: "Tobie", surname: "Morgan Hitchcock" },\ \ { id: "jaime", name: "Jaime", surname: "Morgan Hitchcock" }, -- will not throw an error\ ] ### Return Values[](https://surrealdb.com/docs/surrealql/statements/insert#return-values) Available since: v2.0.0 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 INSERT INTO company { name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] } RETURN NONE; `RETURN DIFF` returns the changeset diff: -- Insert a record and return the diff INSERT INTO company { name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] } RETURN DIFF; Response-------- Query 1 (500µs) -------- [\ [\ {\ op: 'replace',\ path: '/',\ value: {\ founded: '2021-09-10',\ founders: [\ person:tobie,\ person:jaime\ ],\ id: company:hu5o1wqbo29t10engbeo,\ name: 'SurrealDB',\ tags: [\ 'big data',\ 'database'\ ]\ }\ }\ ]\ ] `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 INSERT INTO company { name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] } RETURN BEFORE; -- Return the record after creation INSERT INTO company { name: 'SurrealDB', founded: "2021-09-10", founders: [person:tobie, person:jaime], tags: ['big data', 'database'] } RETURN AFTER; 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. INSERT INTO person { age: 46, username : "john-smith", interests : ['skiing', 'music'] } RETURN age, interests, age + 1 AS age_next_year; INSERT INTO planet [\ {\ name: 'Venus',\ surface_temp: 462,\ temp_55_km_up: 27\ },\ {\ name: 'Earth',\ surface_temp: 15,\ temp_55_km_up: -55\ }\ ] RETURN VALUE temp_55_km_up; Response-------- Query -------- [\ {\ age: 46,\ age_next_year: 47,\ interests: [\ 'skiing',\ 'music'\ ]\ }\ ] -------- Query -------- [\ 27,\ -55\ ] Bulk insert[](https://surrealdb.com/docs/surrealql/statements/insert#bulk-insert) ---------------------------------------------------------------------------------- Available since: v2.0.0 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. INSERT INTO person [\ { id: "jaime", name: "Jaime", surname: "Morgan Hitchcock" },\ { id: "tobie", name: "Tobie", surname: "Morgan Hitchcock" },\ -- ... 1000 more records\ ] Insert relation tables[](https://surrealdb.com/docs/surrealql/statements/insert#insert-relation-tables) -------------------------------------------------------------------------------------------------------- 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](https://surrealdb.com/docs/surrealql/statements/relate) statement. For example: -- Insert records into the person table INSERT INTO person [\ { id: 1 },\ { id: 2 },\ { id: 3 },\ ]; -- Insert a single relation INSERT RELATION INTO likes { in: person:1, id: 'object', out: person:2, }; -- Insert multiple relations INSERT RELATION INTO likes [\ {\ in: person:1,\ id: 'array',\ out: person:2,\ },\ {\ in: person:2,\ id: 'array_two',\ out: person:3,\ }\ ]; -- Insert a relation and return the value of the likes field INSERT RELATION INTO likes (in, id, out) VALUES (person:1, 'values', person:2); -- Select the value of the likes field SELECT VALUE ->likes FROM person; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/insert.mdx) Copy page as Markdown [INFO](https://surrealdb.com/docs/surrealql/statements/info) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) ### On this page * [INSERT statement](https://surrealdb.com/docs/surrealql/statements/insert#insert-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/insert#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/insert#example-usage) * [Ignoring duplicates](https://surrealdb.com/docs/surrealql/statements/insert#ignoring-duplicates) * [Return Values](https://surrealdb.com/docs/surrealql/statements/insert#return-values) * [Bulk insert](https://surrealdb.com/docs/surrealql/statements/insert#bulk-insert) * [Insert relation tables](https://surrealdb.com/docs/surrealql/statements/insert#insert-relation-tables) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/insert.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DELETE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) `DELETE` statement[](https://surrealdb.com/docs/surrealql/statements/delete#delete-statement) ============================================================================================== The `DELETE` statement can be used to delete records from the database. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/delete#statement-syntax) SurrealQL SyntaxDELETE [ FROM | ONLY ] @targets [ WHERE @condition ] [ RETURN NONE | RETURN BEFORE | RETURN AFTER | RETURN DIFF | RETURN @statement_param, ... ] [ TIMEOUT @duration ] [ EXPLAIN [ FULL ]] ; Example usage[](https://surrealdb.com/docs/surrealql/statements/delete#example-usage) -------------------------------------------------------------------------------------- ### Basic usage[](https://surrealdb.com/docs/surrealql/statements/delete#basic-usage) The following queries shows basic usage of the DELETE statement, which is used to delete records from a table or a graph edge. Deleting records can be done in multiple ways. Specifying only the table name will delete all the records from a table. Note that a `DELETE` statement returns nothing (i.e. an empty array) by default. -- Delete all records from a table DELETE person; Output[] A `DELETE` statement on a specific ID will delete a single record. -- Delete a record with a specific numeric id DELETE person:100; -- Delete a record with a specific string id DELETE person:tobie; The `ONLY` keyword can be followed by a `RETURN BEFORE` clause to return just the object for the record in question before it was deleted. DELETE ONLY person:tobie RETURN BEFORE; Note that as a `DELETE` statement returns an empty array by default, and the `ONLY` keyword assumes that a single object will be returned, it will return an error if `RETURN BEFORE` is not included. DELETE ONLY person:tobie; Output'Expected a single result output when using the ONLY keyword' ### Deleting records based on conditions[](https://surrealdb.com/docs/surrealql/statements/delete#deleting-records-based-on-conditions) The delete statement supports conditional matching of records using a `WHERE` clause. If the expression in the `WHERE` clause evaluates to true, then the respective record will be deleted. -- Update all records which match the condition DELETE city WHERE name = 'London'; By default, the delete statement does not return any data, returning only an empty array if the statement succeeds completely. Specify a `RETURN` clause to change the value which is returned for each document that is deleted. -- Don't return any result (the default) DELETE user WHERE age < 18 RETURN NONE; -- Return the changeset diff DELETE user WHERE interests CONTAINS 'reading' RETURN DIFF; -- Return the record before changes were applied DELETE user WHERE interests CONTAINS 'reading' RETURN BEFORE; -- Return the record after changes were applied DELETE user WHERE interests CONTAINS 'reading' RETURN AFTER; An important point to know when using a `WHERE` clause is that it performs a check on the [truthiness](https://surrealdb.com/docs/surrealql/datamodel/values#values-and-truthiness) of a value, namely whether a value exists and is not a default value like 0, an empty string, empty array, and so on. As such, the `DELETE` query below that only specifies `WHERE age` essentially evaluates to “WHERE age exists” and will delete every cat in the database with an `age`. CREATE cat:one SET age = 4; CREATE cat:two; DELETE cat WHERE age; SELECT * FROM cat; Output[\ {\ id: cat:two\ }\ ] This pattern is particularly useful when using SurrealDB’s [literal types](https://surrealdb.com/docs/surrealql/datamodel/literals) which were added in version 2.0. A literal type containing objects that contain a single top-level field can easily be matched on through the field name. DEFINE FIELD error_info ON TABLE info TYPE { continue: { message: "Continue" } } | { retry_with_id: { error: string } } | { deprecated: { message: string } }; CREATE info SET error_info = { continue: { message: "Continue" }}; CREATE info SET error_info = { continue: { message: "Continue" }}; CREATE info SET error_info = { deprecated: { message: "We don't use this anymore" }}; DELETE info WHERE error_info.continue; SELECT * FROM info; Output[\ {\ error_info: {\ deprecated: {\ message: "We don't use this anymore"\ }\ },\ id: info:o0pmm7zos98iv03xliav\ }\ ] ### Using TIMEOUT duration records based on conditions[](https://surrealdb.com/docs/surrealql/statements/delete#using-timeout-duration-records-based-on-conditions) When processing a large result set with many interconnected records, it is possible to use the `TIMEOUT` keywords to specify a timeout duration for the statement. If the statement continues beyond this duration, then the transaction will fail, no records will be deleted from the database, and the statement will return an error. DELETE person WHERE ->knows->person->(knows WHERE influencer = false) TIMEOUT 5s; Deleting graph edges[](https://surrealdb.com/docs/surrealql/statements/delete#deleting-graph-edges) ---------------------------------------------------------------------------------------------------- You can also delete graph edges between two records in the database by using the DELETE statement. For example the graph edge below: RELATE person:tobie->bought->product:iphone; [\ {\ "id": bought:ctwsll49k37a7rmqz9rr,\ "in": person:tobie,\ "out": product:iphone\ }\ ] Can be deleted by: DELETE person:tobie->bought WHERE out=product:iphone; Soft deletions[](https://surrealdb.com/docs/surrealql/statements/delete#soft-deletions) ---------------------------------------------------------------------------------------- While soft deletions do not exist natively in SurrealDB, they can be simulated by [defining an event](https://surrealdb.com/docs/surrealql/statements/define/event) that reacts whenever a deletion occurs. The following example archives the data of a deleted record in another table. This can be combined with [fewer permissions for the new table](https://surrealdb.com/docs/surrealql/statements/define/table#defining-permissions) so that it can be accessed only by [system users](https://surrealdb.com/docs/surrealql/statements/define/user) and not [record users](https://surrealdb.com/docs/surrealql/statements/define/access/record) . DEFINE EVENT archive_person ON TABLE person WHEN $event = "DELETE" THEN { CREATE deleted_person SET data = $before, deleted_at = time::now() }; CREATE |person:1..3|; DELETE person:1; -- Only two `person` records left SELECT * FROM person; -- But the data of `person:1` is still here SELECT * FROM deleted_person; Output-------- Query -------- [\ {\ id: person:2\ },\ {\ id: person:3\ }\ ] -------- Query -------- [\ {\ data: {\ id: person:1\ },\ deleted_at: d'2024-09-12T00:46:59.176Z',\ id: deleted_person:p3fpzhpxuu9jvjn8juyf\ }\ ] The `EXPLAIN` clause[](https://surrealdb.com/docs/surrealql/statements/delete#the-explain-clause) -------------------------------------------------------------------------------------------------- When `EXPLAIN` is used: 1. The `DELETE` statement returns an explanation, essentially revealing the execution plan to provide transparency and understanding of the query performance. 2. The records are not deleted. `EXPLAIN` can be followed by `FULL` to see the number of executed rows. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/delete.mdx) Copy page as Markdown [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [FOR](https://surrealdb.com/docs/surrealql/statements/for) ### On this page * [DELETE statement](https://surrealdb.com/docs/surrealql/statements/delete#delete-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/delete#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/delete#example-usage) * [Basic usage](https://surrealdb.com/docs/surrealql/statements/delete#basic-usage) * [Deleting records based on conditions](https://surrealdb.com/docs/surrealql/statements/delete#deleting-records-based-on-conditions) * [Using TIMEOUT duration records based on conditions](https://surrealdb.com/docs/surrealql/statements/delete#using-timeout-duration-records-based-on-conditions) * [Deleting graph edges](https://surrealdb.com/docs/surrealql/statements/delete#deleting-graph-edges) * [Soft deletions](https://surrealdb.com/docs/surrealql/statements/delete#soft-deletions) * [The EXPLAIN clause](https://surrealdb.com/docs/surrealql/statements/delete#the-explain-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/delete.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE SEQUENCE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) `DEFINE SEQUENCE` statement[](https://surrealdb.com/docs/surrealql/statements/define/sequence#define-sequence-statement) ========================================================================================================================= Available since: v3.0.0-alpha.2 A sequence is used to generate reliable, monotonically increasing numeric sequences in both single-node and clustered SurrealDB deployments (multiple compute nodes backed by TiKV). It uses a batch-allocation strategy to minimise coordination while guaranteeing global uniqueness. The key features of a sequence are as follows: * Batch allocation: Nodes request ranges of sequence values at once, reducing network chatter and coordination overhead. * Node ownership tagging: Every batch is tagged with the requesting node’s UUID to prevent overlap between nodes. * Durable Persistence: Sequence metadata is stored in the underlying key-value store to survive restarts and network partitions. * Concurrent, thread-safe access: A DashMap caches active sequences, allowing lock-free reads on the hot path. * Exponential back-off with full jitter: When a batch-allocation attempt fails, the node retries with an exponential delay that includes full jitter to avoid thundering-herd effects across the cluster. * Automatic cleanup: Listens for namespace and database-removal events and purges the corresponding sequence state. The sequence implementation avoids contention by having each node reserve a range of sequence values, allowing it to serve multiple requests locally without requiring distributed coordination for every request. When a node exhausts its allocated range, it acquires a new batch from the distributed store. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/sequence#statement-syntax) ----------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE SEQUENCE [ OVERWRITE | IF NOT EXISTS ] @name [ BATCH @batch ] [ START @start ] [ TIMEOUT @duration ] Examples[](https://surrealdb.com/docs/surrealql/statements/define/sequence#examples) ------------------------------------------------------------------------------------- A sequence can be created with nothing more than a name. DEFINE SEQUENCE mySeq; The `BATCH`, `START`, and ‘TIMEOUT’ clauses can be included to configure the sequence. DEFINE SEQUENCE mySeq2 BATCH 1000 START 100 TIMEOUT 5s; sequence::nextval('mySeq2'); -- Output: 100 DEFINE SEQUENCE mySeq3 BATCH 1000 START 100 TIMEOUT 0s; sequence::nextval('mySeq2'); -- Output: 'The query was not executed because it exceeded the timeout' See also[](https://surrealdb.com/docs/surrealql/statements/define/sequence#see-also) ------------------------------------------------------------------------------------- * [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/sequence.mdx) Copy page as Markdown [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) ### On this page * [DEFINE SEQUENCE statement](https://surrealdb.com/docs/surrealql/statements/define/sequence#define-sequence-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/sequence#statement-syntax) * [Examples](https://surrealdb.com/docs/surrealql/statements/define/sequence#examples) * [See also](https://surrealdb.com/docs/surrealql/statements/define/sequence#see-also) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/sequence.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Session functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) Session functions[](https://surrealdb.com/docs/surrealql/functions/database/session#session-functions) ======================================================================================================= These functions return information about the current SurrealDB session. | Function | Description | | --- | --- | | [`session::ac()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessionac) | Returns the current user’s access method | | [`session::db()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessiondb) | Returns the currently selected database | | [`session::id()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessionid) | Returns the current user’s session ID | | [`session::ip()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessionip) | Returns the current user’s session IP address | | [`session::ns()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessionns) | Returns the currently selected namespace | | [`session::origin()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessionorigin) | Returns the current user’s HTTP origin | | [`session::rd()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessionrd) | Returns the current user’s record authentication data | | [`session::token()`](https://surrealdb.com/docs/surrealql/functions/database/session#sessiontoken) | Returns the current user’s authentication token | `session::ac`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessionac) ------------------------------------------------------------------------------------------- Available since: v2.0.0 > Note > > This function was known as `session::sc` in versions of SurrrealDB before 2.0. The behaviour has not changed. The `session::ac` function returns the current user’s access method. API DEFINITIONsession::ac() -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN session::ac(); "user" `session::db`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessiondb) ------------------------------------------------------------------------------------------- The `session::db` function returns the currently selected database. API DEFINITIONsession::db() -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN session::db(); "my_db" `session::id`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessionid) ------------------------------------------------------------------------------------------- The `session::id` function returns the current user’s session ID. API DEFINITIONsession::id() -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN session::id(); "I895rKuixHwCNIduyBIYH2M0Pga7oUmWnng5exEE4a7EB942GVElGrnRhE5scF5d" `session::ip`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessionip) ------------------------------------------------------------------------------------------- The `session::ip` function returns the current user’s session IP address. API DEFINITIONsession::ip() -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN session::ip(); "2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF" `session::ns`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessionns) ------------------------------------------------------------------------------------------- The `session::ns` function returns the currently selected namespace. API DEFINITIONsession::ns() -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN session::ns(); "my_ns" `session::origin`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessionorigin) --------------------------------------------------------------------------------------------------- The `session::origin` function returns the current user’s HTTP origin. API DEFINITIONsession::origin() -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN session::origin(); "http://localhost:3000" `session::rd`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessionrd) ------------------------------------------------------------------------------------------- Available since: v2.0.0 The `session::rd` function returns the current user’s record authentication. API DEFINITIONsession::rd() -> string `session::token`[](https://surrealdb.com/docs/surrealql/functions/database/session#sessiontoken) ------------------------------------------------------------------------------------------------- The `session::token` function returns the current authentication token. API DEFINITIONsession::token() -> string [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/session.mdx) Copy page as Markdown [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) ### On this page * [session::ac](https://surrealdb.com/docs/surrealql/functions/database/session#sessionac) * [session::db](https://surrealdb.com/docs/surrealql/functions/database/session#sessiondb) * [session::id](https://surrealdb.com/docs/surrealql/functions/database/session#sessionid) * [session::ip](https://surrealdb.com/docs/surrealql/functions/database/session#sessionip) * [session::ns](https://surrealdb.com/docs/surrealql/functions/database/session#sessionns) * [session::origin](https://surrealdb.com/docs/surrealql/functions/database/session#sessionorigin) * [session::rd](https://surrealdb.com/docs/surrealql/functions/database/session#sessionrd) * [session::token](https://surrealdb.com/docs/surrealql/functions/database/session#sessiontoken) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/session.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # USE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [USE](https://surrealdb.com/docs/surrealql/statements/use) `USE` statement[](https://surrealdb.com/docs/surrealql/statements/use#use-statement) ===================================================================================== The `USE` statement specifies a namespace and / or a database to use for the subsequent SurrealQL statements when switching between namespaces and databases. If you have a single namespace and database, you can define them in the [sql command](https://surrealdb.com/docs/surrealdb/cli/sql#example-usage) . Ensure that your database and namespace exist and you have [started your database](https://surrealdb.com/docs/surrealdb/cli/start) before using the Sql command option. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/use#statement-syntax) SurrealQL SyntaxUSE [ NS @ns ] [ DB @db ]; Example usage[](https://surrealdb.com/docs/surrealql/statements/use#example-usage) ----------------------------------------------------------------------------------- The following query shows example usage of this statement if you have multiple namespaces and databases. USE NS test; -- Switch to the 'test' Namespace USE DB test; -- Switch to the 'test' Database USE NS test DB test; -- Switch to the 'test' Namespace and 'test' Database You can also use the [INFO Statement](https://surrealdb.com/docs/surrealql/statements/info) to check the current namespace and database. INFO FOR NS; -- Check the current Namespace INFO FOR DB; -- Check the current Database [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/use.mdx) Copy page as Markdown [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [Functions](https://surrealdb.com/docs/surrealql/functions) ### On this page * [USE statement](https://surrealdb.com/docs/surrealql/statements/use#use-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/use#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/use#example-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/use.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE PARAM statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) `DEFINE PARAM` statement[](https://surrealdb.com/docs/surrealql/statements/define/param#define-param-statement) ================================================================================================================ The `DEFINE PARAM` statement allows you to define global (database-wide) parameters that are available to every client. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/param#requirements) ------------------------------------------------------------------------------------------ * You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the `DEFINE PARAM` statement. * [You must select your namespace and database](https://surrealdb.com/docs/surrealql/statements/use) before you can use the `DEFINE PARAM` statement. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/param#statement-syntax) -------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE PARAM [ OVERWRITE | IF NOT EXISTS ] $@name VALUE @value [ COMMENT @string ] [ PERMISSIONS [ NONE | FULL | WHERE @condition ] ] Example usage[](https://surrealdb.com/docs/surrealql/statements/define/param#example-usage) -------------------------------------------------------------------------------------------- Below shows how you can create a parameter using the `DEFINE PARAM` statement. DEFINE PARAM $endpointBase VALUE "https://dummyjson.com"; Then, simply use the global parameter like you would with any variable. RETURN http::get($endpointBase + "/products"); Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/param#using-if-not-exists-clause) ------------------------------------------------------------------------------------------------------------------------ Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define a param only if it does not already exist. You should use the `IF NOT EXISTS` clause when defining a param in SurrealDB if you want to ensure that the param is only created if it does not already exist. If the param already exists, the `DEFINE PARAM` statement will return an error. It’s particularly useful when you want to safely attempt to define a param without manually checking its existence first. On the other hand, you should not use the `IF NOT EXISTS` clause when you want to ensure that the param definition is updated regardless of whether it already exists. In such cases, you might prefer using the `OVERWRITE` clause, which allows you to define a param and overwrite an existing one if it already exists, ensuring that the latest version of the param definition is always in use -- Create a PARAM if it does not already exist DEFINE PARAM IF NOT EXISTS $example VALUE 123; Using `OVERWRITE` clause[](https://surrealdb.com/docs/surrealql/statements/define/param#using-overwrite-clause) ---------------------------------------------------------------------------------------------------------------- Available since: v2.0.0 The `OVERWRITE` clause can be used to define a param and overwrite an existing one if it already exists. You should use the `OVERWRITE` clause when you want to modify an existing param definition. If the param already exists, the `DEFINE PARAM` statement will overwrite the existing param definition with the new one. -- Create an PARAM and overwrite if it already exists DEFINE PARAM OVERWRITE $example VALUE 123; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/param.mdx) Copy page as Markdown [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) ### On this page * [DEFINE PARAM statement](https://surrealdb.com/docs/surrealql/statements/define/param#define-param-statement) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/param#requirements) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/param#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/param#example-usage) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/param#using-if-not-exists-clause) * [Using OVERWRITE clause](https://surrealdb.com/docs/surrealql/statements/define/param#using-overwrite-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/param.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # RETURN statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) `RETURN` statement[](https://surrealdb.com/docs/surrealql/statements/return#return-statement) ============================================================================================== The `RETURN` statement can be used to return an implicit value or the result of a query, and to set the return value for a transaction, block, or function. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/return#statement-syntax) SurrealQL SyntaxRETURN @value Example usage[](https://surrealdb.com/docs/surrealql/statements/return#example-usage) -------------------------------------------------------------------------------------- ### Basic usage[](https://surrealdb.com/docs/surrealql/statements/return#basic-usage) `RETURN` is always followed by a value. As every data type in SurrealDB is a type of [value](https://surrealdb.com/docs/surrealql/datamodel/values) , the `RETURN` statement can return anything from simple values to the result of queries. -- Return a simple value RETURN 123; RETURN "I am a string!"; RETURN { prop: "value" }; -- Return the result of a query RETURN SELECT * FROM person; RETURN (CREATE person).id; Values on their own are treated as if they have an implicit `RETURN` in front. As such, the following queries return the same output as in the previous example. 123; "I am a string!"; { prop: "value" }; SELECT * FROM person; (CREATE person).id; Transaction return value[](https://surrealdb.com/docs/surrealql/statements/return#transaction-return-value) ------------------------------------------------------------------------------------------------------------ `RETURN` statements can set the result of any transaction. This includes transactions, blocks and functions. Transaction return valueBEGIN TRANSACTION; -- We are executing quite a few queries here LET $firstname = "John"; LET $lastname = "Doe"; LET $person = CREATE person CONTENT { firstname: $firstname, lastname: $lastname, }; -- But because we end with a RETURN query, only the person's ID will be returned -- The results of the other queries will be omitted. RETURN $person.id; -- One issue with this approach is that query errors are generic. -- To get around that, use a block, which is executed as a transaction by itself. COMMIT TRANSACTION; Return breaks execution[](https://surrealdb.com/docs/surrealql/statements/return#return-breaks-execution) ---------------------------------------------------------------------------------------------------------- Available since: v2.0.0 Opposite to `RETURN` in SurrealDB `1.x`, `RETURN` now breaks execution of statements, functions and transactions. Function return valueDEFINE FUNCTION fn::person::create($firstname: string, $lastname: string) { LET $person = CREATE person CONTENT { firstname: $firstname, lastname: $lastname, }; -- The RETURN statement will set the return value of the custom function, and further queries will not be executed. RETURN $person.id; -- This query will never be executed CREATE person SET firstname = "Stephen", lastname = "Strange"; }; fn::person::create("Thanos", "Johnson"); SELECT * FROM person; FunctionsDEFINE FUNCTION fn::round::up($num: number) { IF $num % 2 == 0 { RETURN $num; -- Breaks execution for the function }; -- This is only executed if the RETURN inside the IF statement did not break execution RETURN $num + 1; }; TransactionsBEGIN; RETURN 1; -- I am executed CREATE a; -- I am not executed RETURN 2; -- I am not executed COMMIT; Lastly, if not executed inside a transaction or function, `RETURN` will break execution until the most top-level statement it is executed in. RETURN will **not** prevent top level statements from being executed, nor will it adjust their output. StatementsLET $id = 123; LET $id = { IF $id { RETURN type::thing('table', $id); }; RETURN table:rand(); }; -- This still executes. The `RETURN` statement only broke until the block in the variable assignment. CREATE $id; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/return.mdx) Copy page as Markdown [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) ### On this page * [RETURN statement](https://surrealdb.com/docs/surrealql/statements/return#return-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/return#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/return#example-usage) * [Basic usage](https://surrealdb.com/docs/surrealql/statements/return#basic-usage) * [Transaction return value](https://surrealdb.com/docs/surrealql/statements/return#transaction-return-value) * [Return breaks execution](https://surrealdb.com/docs/surrealql/statements/return#return-breaks-execution) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/return.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # INFO statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [INFO](https://surrealdb.com/docs/surrealql/statements/info) `INFO` statement[](https://surrealdb.com/docs/surrealql/statements/info#info-statement) ======================================================================================== The `INFO` command outputs information about the setup of the SurrealDB system. There are a number of different `INFO` commands for retrieving the configuration at the different levels of the database. SurrealQL SyntaxINFO FOR [\ ROOT\ | NS | NAMESPACE\ | DB | DATABASE\ | TABLE @table\ | USER @user [ON @level]\ | INDEX @index ON @table\ ]; The information returned from an `INFO` command is an object containing items that almost always correspond to a matching [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) statement. For example, the `INFO FOR NS` command returns the information on the access methods, databases and users of a namespace, which are defined with `DEFINE ACCESS`, `DEFINE DATABASE` and `DEFINE USER` statements. > Note > > Before SurrealDB v3.0.0-alpha.2, the output of an `INFO FOR` was only able to be used as a standalone statement and not in a dynamic context, such as inside other queries or as the value of a parameter. Example usage[](https://surrealdb.com/docs/surrealql/statements/info#example-usage) ------------------------------------------------------------------------------------ There are a number of different `INFO` commands for retrieving the configuration at the different levels of the database. System information[](https://surrealdb.com/docs/surrealql/statements/info#system--information) ----------------------------------------------------------------------------------------------- ### Root information[](https://surrealdb.com/docs/surrealql/statements/info#root-information) The top-level ROOT command returns information regarding: * The users and namespaces which exists within the SurrealDB system. * The memory allocated by SurrealDB itself. Note that this may not match what the operating system reports, as it also includes memory consumed by third-party libraries or pre-allocated memory. * The level of parallelism: This number indicates the number of available hardware threads. > Note > > You must be authenticated as a top-level root user to execute this command. #### Examples[](https://surrealdb.com/docs/surrealql/statements/info#examples) INFO FOR ROOT; Sample output{ accesses: {}, namespaces: { ns: 'DEFINE NAMESPACE ns' }, nodes: { "2d3b720d-f152-4c0d-8a16-26d1474ed3cd": 'NODE 2d3b720d-f152-4c0d-8a16-26d1474ed3cd SEEN 1745463977888 ACTIVE' }, system: { available_parallelism: 14, cpu_usage: 0.3816290497779846f, load_average: [\ 1.2734375f,\ 1.68310546875f,\ 1.9189453125f\ ], memory_allocated: 13900485, memory_usage: 136314880, physical_cores: 14, threads: 32 }, users: { root: "DEFINE USER root ON ROOT PASSHASH '$argon2id$v=19$m=19456,t=2,p=1$0RoO7PtdHuGLOz9Pomoucg$T6FYVogdEF8sFse/Su11nKaZb8FjEp3Bb3rD35mI1b8' ROLES OWNER DURATION FOR TOKEN 1h, FOR SESSION NONE" } } ### Namespace information[](https://surrealdb.com/docs/surrealql/statements/info#namespace-information) The `NS` or `NAMESPACE` command returns information regarding the users, databases and access methods under the namespace in use. > Note > > You must be authenticated as a top-level root user, or a namespace user to execute this command. > Note > > You must have a NAMESPACE selected before running this command. #### Examples[](https://surrealdb.com/docs/surrealql/statements/info#examples-1) INFO FOR NS; Sample output{ accesses: {}, databases: { db: 'DEFINE DATABASE db' }, users: { n: "DEFINE USER n ON NAMESPACE PASSHASH '' ROLES VIEWER DURATION FOR TOKEN 1h, FOR SESSION NONE", username: "DEFINE USER username ON NAMESPACE PASSHASH '$argon2id$v=19$m=19456,t=2,p=1$K9DIBCuzH2IA6w7t3ZVGkQ$KkRODt0cqgUap9OwZCxLJC4ESo6wEToUk55oumhmgA0' ROLES EDITOR DURATION FOR TOKEN 1m, FOR SESSION 12h" } } ### Database information[](https://surrealdb.com/docs/surrealql/statements/info#database-information) The `DB` or `DATABASE` command returns information regarding the users, tables, params, models, functions, analyzers and access methods under the database in use. > Note > > You must be authenticated as a top-level root user, a namespace user, or a database user to execute this command. > Note > > You must have a NAMESPACE and a DATABASE selected before running this command. #### Examples[](https://surrealdb.com/docs/surrealql/statements/info#examples-2) INFO FOR DB; Sample output{ accesses: {}, analyzers: {}, apis: {}, buckets: {}, configs: {}, functions: {}, models: {}, params: {}, tables: { person: 'DEFINE TABLE person TYPE ANY SCHEMALESS PERMISSIONS NONE' }, users: { db_user: "DEFINE USER db_user ON DATABASE PASSHASH '$argon2id$v=19$m=19456,t=2,p=1$S8WJ88AnJwWah2VjqnmnoA$OJcQs9SHC5R5q3kOimiKsV5fIUpwZiax3RUcW8VQupk' ROLES OWNER DURATION FOR TOKEN 1h, FOR SESSION NONE" } } ### Table information[](https://surrealdb.com/docs/surrealql/statements/info#table-information) The `TABLE` command returns information regarding the events, fields, tables, and live statement configurations on a specific table. > Note > > You must be authenticated as a top-level root user, a namespace user, or a database user to execute this command. > Note > > You must have a NAMESPACE and a DATABASE selected before running this command. #### Examples[](https://surrealdb.com/docs/surrealql/statements/info#examples-3) INFO FOR TABLE user; Sample output{ events: {}, fields: { name: 'DEFINE FIELD name ON user TYPE string PERMISSIONS FULL' }, indexes: {}, lives: {}, tables: {} } ### User information[](https://surrealdb.com/docs/surrealql/statements/info#user-information) The `USER` command returns information for a user [defined](https://surrealdb.com/docs/surrealql/statements/define/user) on either the root, namespace, or database level. > Note > > You must be authenticated as a user equal to or greater than the level of the user you are attempting to obtain information for to execute this command. #### Examples[](https://surrealdb.com/docs/surrealql/statements/info#examples-4) INFO FOR USER root ON ROOT; INFO FOR USER ns_user ON NAMESPACE; INFO FOR USER db_user ON DATABASE; If a level after `ON` is not specified, the `INFO` command will default to the database level. Thus, the following two commands are equivalent. INFO FOR USER db_user ON DATABASE; INFO FOR USER db_user; Sample output"DEFINE USER db_user ON DATABASE PASSHASH '$argon2id$v=19$m=19456,t=2,p=1$S8WJ88AnJwWah2VjqnmnoA$OJcQs9SHC5R5q3kOimiKsV5fIUpwZiax3RUcW8VQupk' ROLES OWNER DURATION FOR TOKEN 1h, FOR SESSION NONE" ### Index information[](https://surrealdb.com/docs/surrealql/statements/info#index-information) `INFO FOR INDEX` returns the status for an index: started, initial indexing, update indexing, built, or error. This command only applies when the [`CONCURRENTLY`](https://surrealdb.com/docs/surrealql/statements/define/indexes#using-concurrently-clause) clause is used in a `DEFINE INDEX` command. Without this clause, the following statement will not be executed until the index is fully created, or fails. In this case, the `INFO FOR INDEX` statement will return an empty object: `{}`. CREATE |user:50000| SET name = id.id() RETURN NONE; DEFINE INDEX unique_name ON TABLE user FIELDS name UNIQUE; INFO FOR INDEX unique_name ON TABLE user; However, when the `CONCURRENTLY` clause is used, the index will build in the background while other statements are permitted to run. In this case, the `INFO FOR INDEX` statement will provide the current status on the index. The following code sample shows such an example in which an index is defined on a large number of records. A [`SLEEP`](https://surrealdb.com/docs/surrealql/statements/sleep) statement is run in between each `INFO FOR INDEX` command to show the progress after each 50 millisecond interval. CREATE |user:50000| SET name = id.id() RETURN NONE; DEFINE INDEX unique_name ON TABLE user FIELDS name UNIQUE CONCURRENTLY; INFO FOR INDEX unique_name ON user; SLEEP 50ms; INFO FOR INDEX unique_name ON user; SLEEP 50ms; INFO FOR INDEX unique_name ON user; SLEEP 50ms; INFO FOR INDEX unique_name ON user; Possible output-------- Query 1 -------- { building: { initial: 0, pending: 0, status: 'indexing', updated: 0 } } -------- Query 2 -------- { building: { initial: 100, pending: 20, status: 'indexing', updated: 0 } } -------- Query 3 -------- { building: { initial: 100, pending: 4, status: 'indexing', updated: 16 } } -------- Query 4 -------- { building: { status: 'ready' } } ### The `STRUCTURE` clause[](https://surrealdb.com/docs/surrealql/statements/info#the-structure-clause) > Note > > This clause was originally created for internal use and is subject to change without notice. Adding the `STRUCTURE` clause changes the structure of the statement from an object that contains objects into an object with fields that each contain an array and often extra info. DEFINE TABLE user SCHEMAFULL; DEFINE FIELD name ON user TYPE STRING; INFO FOR TABLE user; INFO FOR TABLE user STRUCTURE; Output-------- Query -------- { events: {}, fields: { name: 'DEFINE FIELD name ON user TYPE string PERMISSIONS FULL' }, indexes: {}, lives: {}, tables: {} } -------- Query -------- { events: [], fields: [\ {\ flex: false,\ kind: 'string',\ name: 'name',\ permissions: {\ create: true,\ delete: true,\ select: true,\ update: true\ },\ readonly: false,\ what: 'user'\ }\ ], indexes: [], lives: [], tables: [] } ### Using the output of `INFO`[](https://surrealdb.com/docs/surrealql/statements/info#using-the-output-of-info) Available since: v3.0.0-alpha.2 The output of an `INFO` statement, both with and without the `STRUCTURE` clause, can be used in other operations. As the output of the statement is always a single object, the SurrealQL [object functions](https://surrealdb.com/docs/surrealql/functions/database/object) can also be used on the output for such tasks as schema change tracking. LET $cat = CREATE ONLY cat RETURN VALUE id; LET $first_schema = { revision: rand::uuid(), schema: INFO FOR DB }; $first_schema; CREATE person SET feeds = [$cat]; LET $second_schema = { revision: rand::uuid(), schema: INFO FOR DB }; $second_schema; $first_schema.diff($second_schema); Output-------- First schema -------- { revision: u'019665cc-f730-75f0-8251-894e11fee7d8', schema: { accesses: {}, analyzers: {}, apis: {}, buckets: {}, configs: {}, functions: {}, models: {}, params: {}, tables: { cat: 'DEFINE TABLE cat TYPE ANY SCHEMALESS PERMISSIONS NONE' }, users: {} } } -------- Second schema -------- { revision: u'019665cc-f73b-7313-807f-dd22ad1a0685', schema: { accesses: {}, analyzers: {}, apis: {}, buckets: {}, configs: {}, functions: {}, models: {}, params: {}, tables: { cat: 'DEFINE TABLE cat TYPE ANY SCHEMALESS PERMISSIONS NONE', person: 'DEFINE TABLE person TYPE ANY SCHEMALESS PERMISSIONS NONE' }, users: {} } } -------- Diff -------- [\ {\ op: 'replace',\ path: '/revision',\ value: u'019665cc-f73b-7313-807f-dd22ad1a0685'\ },\ {\ op: 'add',\ path: '/schema/tables/person',\ value: 'DEFINE TABLE person TYPE ANY SCHEMALESS PERMISSIONS NONE'\ }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/info.mdx) Copy page as Markdown [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) ### On this page * [INFO statement](https://surrealdb.com/docs/surrealql/statements/info#info-statement) * [Example usage](https://surrealdb.com/docs/surrealql/statements/info#example-usage) * [System information](https://surrealdb.com/docs/surrealql/statements/info#system--information) * [Root information](https://surrealdb.com/docs/surrealql/statements/info#root-information) * [Namespace information](https://surrealdb.com/docs/surrealql/statements/info#namespace-information) * [Database information](https://surrealdb.com/docs/surrealql/statements/info#database-information) * [Table information](https://surrealdb.com/docs/surrealql/statements/info#table-information) * [User information](https://surrealdb.com/docs/surrealql/statements/info#user-information) * [Index information](https://surrealdb.com/docs/surrealql/statements/info#index-information) * [The STRUCTURE clause](https://surrealdb.com/docs/surrealql/statements/info#the-structure-clause) * [Using the output of INFO](https://surrealdb.com/docs/surrealql/statements/info#using-the-output-of-info) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/info.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # WITH clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) `WITH` clause[](https://surrealdb.com/docs/surrealql/clauses/with#with-clause) =============================================================================== When retrieving data from a table, the query planner can replace the standard table iterator with one or several index iterators based on the structure and requirements of the query. This is particularly useful when querying large datasets, as it can significantly reduce the time it takes to retrieve the data. However, there may be situations where manual control over these potential optimizations is desired or required. The `WITH` clause is used to replace the default table iterator with an index iterator. In cases where the cardinality of an index can be high, potentially even equal to the number of records in the table, the sum of the records iterated by several indexes may end up being larger than the number of records obtained by iterating over the table. In such cases, if there are different index possibilities, the most probable optimal choice would be to use the index known with the lowest cardinality. The query planner can replace the standard table iterator with one or several index iterators based on the structure and requirements of the query. > Note > > If you are using a `SELECT` statement, the `WITH` clause is used to specify the index to use for the query. You can define an index using the [`DEFINE INDEX`](https://surrealdb.com/docs/surrealql/statements/define/indexes) > statement. Also see the [`DEFINE ANALYZER`](https://surrealdb.com/docs/surrealql/statements/define/analyzer) > statement for more information on optimizing query performance with full-text search. Syntax[](https://surrealdb.com/docs/surrealql/clauses/with#syntax) ------------------------------------------------------------------- Clause Syntax[ WITH [ NOINDEX | INDEX @indexes ... ]] This clause can be used in the following ways: * `WITH NOINDEX`: forces the query planner to use the table iterator. (Default) * `WITH INDEX @indexes`: restricts the query planner to using only the specified index(es) -- forces the query planner to use the specified index(es): SELECT * FROM person WITH INDEX ft_email WHERE email = 'tobie@surrealdb.com' AND company = 'SurrealDB'; -- forces the usage of the table iterator SELECT name FROM person WITH NOINDEX WHERE job = 'engineer' AND gender = 'm'; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/with.mdx) Copy page as Markdown [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [Statements](https://surrealdb.com/docs/surrealql/statements) ### On this page * [WITH clause](https://surrealdb.com/docs/surrealql/clauses/with#with-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/with#syntax) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/with.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # BREAK statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) `BREAK` statement[](https://surrealdb.com/docs/surrealql/statements/break#break-statement) =========================================================================================== The BREAK statement can be used to break out of a loop, such as inside one created by the [FOR statement](https://surrealdb.com/docs/surrealql/statements/for) . ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/break#statement-syntax) SurrealQL SyntaxBREAK Example usage[](https://surrealdb.com/docs/surrealql/statements/break#example-usage) ------------------------------------------------------------------------------------- The following queries shows example usage of this statement. Creating a person for everyone in the array where the number is less than or equal to 5: LET $numbers = [1,2,3,4,5,6,7,8,9]; FOR $num IN $numbers { IF $num > 5 { BREAK; } ELSE IF $num < 5 { CREATE type::thing( 'person', $num ) CONTENT { name: "Person number " + $num }; }; }; Breaking out of a loop once unwanted data is encountered: -- Data retrieved from somewhere which contains many NONE values LET $weather = [\ {\ city: 'London',\ temperature: 22.2,\ timestamp: 1722565566389\ },\ NONE,\ {\ city: 'London',\ temperature: 20.1,\ timestamp: 1722652002699\ },\ {\ city: 'Phoenix',\ temperature: 45.1,\ timestamp: 1722565642160\ },\ NONE,\ NONE,\ {\ city: 'Phoenix',\ temperature: 45.1,\ timestamp: 1722652070372\ },\ ]; -- Sort the data to move the NONE values to the end -- and break once the first NONE is reached FOR $data IN array::sort::desc($weather) { IF $data IS NONE { BREAK; } ELSE { CREATE weather CONTENT $data; }; }; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/break.mdx) Copy page as Markdown [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) ### On this page * [BREAK statement](https://surrealdb.com/docs/surrealql/statements/break#break-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/break#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/break#example-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/break.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # CANCEL statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) `CANCEL` statement[](https://surrealdb.com/docs/surrealql/statements/cancel#cancel-statement) ============================================================================================== Each statement within SurrealDB is run within its own transaction. If a set of changes need to be made together, then groups of statements can be run together as a single transaction, either succeeding as a whole, or failing without leaving any residual data modifications. While a transaction will fail if any of its statements encounters an error, the `CANCEL` statement can also be used to cancel a transaction manually. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/cancel#statement-syntax) SurrealQL SyntaxCANCEL [ TRANSACTION ]; Example usage[](https://surrealdb.com/docs/surrealql/statements/cancel#example-usage) -------------------------------------------------------------------------------------- The following query shows example usage of this statement. BEGIN TRANSACTION; -- Setup accounts CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 91031.31; -- Move money UPDATE account:one SET balance += 300.00; UPDATE account:two SET balance -= 300.00; -- Rollback all changes CANCEL TRANSACTION; `CANCEL` is not used to automatically cancel a transaction based on a condition such as inside an [IF..ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) block. Instead, a [THROW](https://surrealdb.com/docs/surrealql/statements/throw) statement is used. THROW can be followed by any value, usually a string containing context behind the error. BEGIN TRANSACTION; -- Setup accounts CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 200.31; -- Move money UPDATE account:one SET balance += 300.00; UPDATE account:two SET balance -= 300.00; IF account:two.balance < 0 { THROW "Not enough funds"; }; COMMIT TRANSACTION; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/cancel.mdx) Copy page as Markdown [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) ### On this page * [CANCEL statement](https://surrealdb.com/docs/surrealql/statements/cancel#cancel-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/cancel#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/cancel#example-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/cancel.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Geo functions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Functions](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) Geo functions[](https://surrealdb.com/docs/surrealql/functions/database/geo#geo-functions) =========================================================================================== These functions can be used when working with and analysing geospatial data. | Function | Description | | --- | --- | | [`geo::area()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geoarea) | Calculates the area of a geometry | | [`geo::bearing()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geobearing) | Calculates the bearing between two geolocation points | | [`geo::centroid()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geocentroid) | Calculates the centroid of a geometry | | [`geo::distance()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geodistance) | Calculates the distance between two geolocation points | | [`geo::​hash::decode()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geohashdecode) | Decodes a geohash into a geometry point | | [`geo::​hash::encode()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geohashencode) | Encodes a geometry point into a geohash | | [`geo::is::valid()`](https://surrealdb.com/docs/surrealql/functions/database/geo#geoisvalid) | Determines if a geometry type is a geography type | Point and geometry[](https://surrealdb.com/docs/surrealql/functions/database/geo#point-and-geometry) ----------------------------------------------------------------------------------------------------- * A `point` is composed of two floats that represent the longitude (east/west) and latitude (north/south) of a location. * A `geometry` is a type of object defined in the [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) spec, of which Polygon is the most common. They can be passed in to the geo functions as objects that contain a “type” (such as “Polygon”) and “coordinates” (an array of points). `geo::area`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geoarea) ----------------------------------------------------------------------------------- The `geo::area` function calculates the area of a geometry in square metres. API DEFINITIONgeo::area(geometry) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement for four approximate points found on a map for the US state of Wyoming which has an area of 253,340 km2 and a mostly rectangular shape. Note: the doubled square brackets are because the function takes an array of an array to allow for more complex types such as MultiPolygon. ![A map of Wyoming in the United States with four approximate points on each corner used to approximate its total surface area in SurrealDB's geo area function.](https://surrealdb.com/docs/_astro/geo-wyoming.Bqu2fAUr_Z1iBI4O.webp) RETURN geo::area({ type: "Polygon", coordinates: [[\ [-111.0690, 45.0032],\ [-104.0838, 44.9893],\ [-104.0910, 40.9974],\ [-111.0672, 40.9862]\ ]] }); Response253317731850.3478f If the argument is not a geometry type, then an [`EMPTY`](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) value will be returned: RETURN geo::area(12345); null `geo::bearing`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geobearing) ----------------------------------------------------------------------------------------- The `geo::bearing` function calculates the bearing between two geolocation points. Bearing begins at 0 degrees to indicate north, increasing clockwise into positive values and decreasing counterclockwise into negative values that converge at 180 degrees. ![A circle showing how bearing is defined from 0 degrees to 360 degrees.](https://surrealdb.com/docs/_astro/geo-bearing.Cp7zER9U_O1K4X.webp) API DEFINITIONgeo::bearing(point, point) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: -- LET used here for readability LET $paris = (2.358058597411099, 48.861109346459536); LET $le_puy_en_velay = (3.883428431947686, 45.04383588468415); RETURN geo::bearing($paris, $le_puy_en_velay); Response164.18154786094604 ![A map showing the path from Paris, the capital of France, to a French town called Le Puy En Velay. The bearing is south southeast.](https://surrealdb.com/docs/_astro/geo-paris.BTLEtjqd_Z13lDJ2.webp) If either argument is not a geolocation point, then an [`EMPTY`](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) value will be returned: RETURN geo::bearing(12345, true); null `geo::centroid`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geocentroid) ------------------------------------------------------------------------------------------- The `geo::centroid` function calculates the centroid between multiple geolocation points. API DEFINITIONgeo::centroid(geometry) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement. Note: the doubled square brackets are because the function takes an array of an array to allow for more complex types such as MultiPolygon. RETURN geo::centroid({ type: "Polygon", coordinates: [[\ [-0.03921743611083, 51.88106875736589], -- London\ [30.48112752349519, 50.68377089794912], -- Kyiv\ [23.66174524001544, 42.94500782833793], -- Sofia\ [ 1.92481534361859, 41.69698118125476] -- Barcelona\ ]] }); The return value is a mountainous region somewhere in Austria: Response(13.483896437936192, 47.07117241195589) ![A map showing the centroid between four points in Europe: London, Kyiv, Sofia, and Barcelona. The centroid itself is located in Austria.](https://surrealdb.com/docs/_astro/geo-centroid.B-ZAkdfK_Z1AzYt5.webp) If either argument is not a geometry type, then an [`EMPTY`](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) value will be returned: RETURN geo::centroid(12345); null `geo::distance`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geodistance) ------------------------------------------------------------------------------------------- The `geo::distance` function calculates the haversine distance, in metres, between two geolocation points. API DEFINITIONgeo::distance(point, point) -> number The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: let $london = (-0.04592553673505285, 51.555282574465764); let $harare = (30.463880214538577, -17.865161568822085); RETURN geo::distance($london, $harare); Response8268604.251890702f ![A map showing the distance in a straight line from London, the capital of the United Kingdom, to Harare, the capital of Zimbabwe](https://surrealdb.com/docs/_astro/geo-london.CCp7-_NN_KSB4o.webp) If either argument is not a geolocation point, then an [`EMPTY`](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) value will be returned: RETURN geo::distance(12345, true); null `geo::hash::decode`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geohashdecode) ------------------------------------------------------------------------------------------------- The `geo::hash::decode` function converts a geohash into a geolocation point. API DEFINITIONgeo::hash::decode(point) -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN geo::hash::decode("mpuxk4s24f51"); Response(51.50986494496465, -0.11809204705059528) If the argument is not a geolocation point, then an [`EMPTY`](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) value will be returned: RETURN geo::hash::decode(12345); null `geo::hash::encode`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geohashencode) ------------------------------------------------------------------------------------------------- The `geo::hash::encode` function converts a geolocation point into a geohash. API DEFINITIONgeo::hash::encode(point) -> string The function accepts a second argument, which determines the accuracy and granularity of the geohash. API DEFINITIONgeo::hash::encode(point, number) -> string The following example shows this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: RETURN geo::hash::encode( (51.509865, -0.118092) ); "mpuxk4s24f51" The following example shows this function with two arguments, and its output, when used in a select statement: RETURN geo::hash::encode( (51.509865, -0.118092), 5 ); "mpuxk" If the first argument is not a geolocation point, then an [`EMPTY`](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) value will be returned: RETURN geo::hash::encode(12345); null `geo::is::valid`[](https://surrealdb.com/docs/surrealql/functions/database/geo#geoisvalid) ------------------------------------------------------------------------------------------- The `geo::is::valid` function determines if a geometry type is a geography type. Geography types are used to store geolocation data in a [Geographic Coordinate System (GCS)](https://en.wikipedia.org/wiki/Geographic_coordinate_system) , whereas geometry types can store geolocation data in any coordinate system, including GCS, mathematical planes, board game layouts, etc… A geography type add the following constraint: each `Point` coordinates are in the range of -180° to 180° for longitude and -90° to 90° for latitude. API DEFINITIONgeo::is::valid(geometry) -> bool The following examples show this function, and its output, when used in a [`RETURN`](https://surrealdb.com/docs/surrealql/statements/return) statement: A valid geography pointRETURN geo::is::valid( (51.509865, -0.118092) ); true Out of range geometry pointRETURN geo::is::valid( (-181.0, -0.118092) ); false [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/geo.mdx) Copy page as Markdown [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) ### On this page * [Point and geometry](https://surrealdb.com/docs/surrealql/functions/database/geo#point-and-geometry) * [geo::area](https://surrealdb.com/docs/surrealql/functions/database/geo#geoarea) * [geo::bearing](https://surrealdb.com/docs/surrealql/functions/database/geo#geobearing) * [geo::centroid](https://surrealdb.com/docs/surrealql/functions/database/geo#geocentroid) * [geo::distance](https://surrealdb.com/docs/surrealql/functions/database/geo#geodistance) * [geo::hash::decode](https://surrealdb.com/docs/surrealql/functions/database/geo#geohashdecode) * [geo::hash::encode](https://surrealdb.com/docs/surrealql/functions/database/geo#geohashencode) * [geo::is::valid](https://surrealdb.com/docs/surrealql/functions/database/geo#geoisvalid) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/functions/database/geo.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # FETCH clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) `FETCH` clause[](https://surrealdb.com/docs/surrealql/clauses/fetch#fetch-clause) ================================================================================== The `FETCH` clause is used to retrieve related records or data from other tables in a single query. This is particularly useful when you want to gather data that is linked through relationships ([record links](https://surrealdb.com/docs/surrealql/datamodel/records) or [graph edges](https://surrealdb.com/docs/surrealql/statements/relate) ) without having to perform multiple separate queries. Benefits of Using the `FETCH` Clause[](https://surrealdb.com/docs/surrealql/clauses/fetch#benefits-of-using-the-fetch-clause) ------------------------------------------------------------------------------------------------------------------------------ * Efficiency: By using the FETCH clause, you can reduce the number of queries needed to gather related data. This can lead to performance improvements, especially when dealing with complex data models with multiple relationships. * Simplified Queries: It simplifies your queries by allowing you to specify related data to be fetched directly within the same query. This makes your code cleaner and easier to understand. * Reduced Network Overhead: Fetching related data in a single query reduces the number of round trips to the database, which can decrease network latency and improve the overall speed of your application. * Consistency: By fetching related data in one go, you ensure that the data is consistent and up-to-date at the time of retrieval, reducing the risk of discrepancies that might occur if data is fetched in separate queries. Example Usage[](https://surrealdb.com/docs/surrealql/clauses/fetch#example-usage) ---------------------------------------------------------------------------------- Suppose you have a person table and a post table, where each post is related to a person. You can use the FETCH clause to retrieve a person along with their posts in a single query: SELECT * FROM person FETCH posts; In this example, `posts` would be a related field in the `person` table that links to the `post` table. The `FETCH` clause allows you to retrieve all posts associated with each person in the result set. Overall, the `FETCH` clause in SurrealQL is a powerful tool for optimizing data retrieval and simplifying query logic when working with related data. In addition to fetching related records, the `FETCH` clause can also be used to replace record ids with their actual record values. Consider the following example: Without the `FETCH` clause[](https://surrealdb.com/docs/surrealql/clauses/fetch#without-the-fetch-clause) ---------------------------------------------------------------------------------------------------------- [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/fetch.mdx) Copy page as Markdown [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) ### On this page * [FETCH clause](https://surrealdb.com/docs/surrealql/clauses/fetch#fetch-clause) * [Benefits of Using the FETCH Clause](https://surrealdb.com/docs/surrealql/clauses/fetch#benefits-of-using-the-fetch-clause) * [Example Usage](https://surrealdb.com/docs/surrealql/clauses/fetch#example-usage) * [Without the FETCH clause](https://surrealdb.com/docs/surrealql/clauses/fetch#without-the-fetch-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/fetch.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Clauses | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) Clauses[](https://surrealdb.com/docs/surrealql/clauses#clauses) ================================================================ In SurrealQL, clauses can be used to alter the way a query is executed. They are used in the following ways: * [`EXPLAIN`](https://surrealdb.com/docs/surrealql/clauses/explain) : Explain the query plan. * [`FETCH`](https://surrealdb.com/docs/surrealql/clauses/fetch) : Fetch all the fields of related records. * [`FROM`](https://surrealdb.com/docs/surrealql/clauses/from) : Specify the table(s) or other target(s) to query from. * [`GROUP BY`](https://surrealdb.com/docs/surrealql/clauses/group-by) : Group the results by a set of fields. * [`LIMIT`](https://surrealdb.com/docs/surrealql/clauses/limit) : Limit the number of results. * [`OMIT`](https://surrealdb.com/docs/surrealql/clauses/omit) : Omit related records. * [`ORDER BY`](https://surrealdb.com/docs/surrealql/clauses/order-by) : Specify the sort order of the results. * [`SPLIT`](https://surrealdb.com/docs/surrealql/clauses/split) : Split the results into a set of subqueries. * [`WHERE`](https://surrealdb.com/docs/surrealql/clauses/where) : Specify a condition that acts as a filter. * [`WITH`](https://surrealdb.com/docs/surrealql/clauses/with) : Replace the default table iterator with an index iterator. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/index.mdx) Copy page as Markdown [Values](https://surrealdb.com/docs/surrealql/datamodel/values) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Statements | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) Statements[](https://surrealdb.com/docs/surrealql/statements#statements) ========================================================================= SurrealDB has a variety of statements that let you configure and query a database. In this section, we’ll look at the different types of statements that are available. Statement parameters[](https://surrealdb.com/docs/surrealql/statements#statement-parameters) --------------------------------------------------------------------------------------------- A number of parameters prefixed with `$` are automatically available within a statement that provide access to relevant context inside the statement. These are known as reserved variable names. For example: * [$before](https://surrealdb.com/docs/surrealql/parameters#before-after) and [$after](https://surrealdb.com/docs/surrealql/parameters#before-after) can be accessed in statements that mutate values to see the values before and after an update, * [$session](https://surrealdb.com/docs/surrealql/parameters#session) provides context on the current session, * [$parent](https://surrealdb.com/docs/surrealql/parameters#parent-this) provides access to the value in a primary query while inside a subquery. For a full list of these automatically generated parameters, see the [parameters](https://surrealdb.com/docs/surrealql/parameters#reserved-variable-names) page. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/index.mdx) Copy page as Markdown [WITH](https://surrealdb.com/docs/surrealql/clauses/with) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) ### On this page * [Statements](https://surrealdb.com/docs/surrealql/statements#statements) * [Statement parameters](https://surrealdb.com/docs/surrealql/statements#statement-parameters) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # ORDER BY clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) `ORDER BY` clause[](https://surrealdb.com/docs/surrealql/clauses/order-by#order-by-clause) =========================================================================================== To sort records, SurrealDB allows ordering on multiple fields and nested fields. Use the `ORDER BY` clause to specify a comma-separated list of field names that should be used to order the resulting records. The `ASC` and `DESC` keywords can be used to specify whether results should be sorted in an ascending or descending manner. The `COLLATE` keyword can be used to use Unicode collation when ordering text in string values, ensuring that different cases, and different languages are sorted in a consistent manner. Finally, the `NUMERIC` can be used to correctly sort text which contains numeric values. It is also worth noting that `COLLATE` ignores unicode order. e.g. ‘á’ comes after ‘z’ by default (Unicode sorting) but with `COLLATE` ‘á’ comes before ‘z’. Syntax[](https://surrealdb.com/docs/surrealql/clauses/order-by#syntax) ----------------------------------------------------------------------- Clause Syntax[ ORDER [ BY ] \ @field [ COLLATE ] [ NUMERIC ] [ ASC | DESC ], ...\ | RAND() ] ] Examples[](https://surrealdb.com/docs/surrealql/clauses/order-by#examples) --------------------------------------------------------------------------- SELECT * FROM ORDER BY ASC; -- Order records randomly SELECT * FROM
ORDER BY rand(); -- Order records descending by a single field SELECT * FROM
ORDER BY DESC; -- Order records by multiple fields independently SELECT * FROM
ORDER BY ASC, DESC; -- Order text fields with lexical collation instead of Unicode order SELECT * FROM
ORDER BY COLLATE ASC; -- Order text fields with which include numeric values SELECT * FROM
ORDER BY NUMERIC ASC; -- COLLATE and NUMERIC can be used together SELECT * FROM
ORDER BY COLLATE NUMERIC ASC; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/order-by.mdx) Copy page as Markdown [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) ### On this page * [ORDER BY clause](https://surrealdb.com/docs/surrealql/clauses/order-by#order-by-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/order-by#syntax) * [Examples](https://surrealdb.com/docs/surrealql/clauses/order-by#examples) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/order-by.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # COMMIT statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) `COMMIT` statement[](https://surrealdb.com/docs/surrealql/statements/commit#commit-statement) ============================================================================================== Each statement within SurrealDB is run within its own transaction by default. If a set of changes need to be made together, then groups of statements can be run together as a single transaction, either succeeding as a whole, or failing without leaving any residual data modifications. A `COMMIT` statement is used at the end of such a transaction to make the data modifications a permanent part of the database. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/commit#statement-syntax) SurrealQL SyntaxCOMMIT [ TRANSACTION ]; Example usage[](https://surrealdb.com/docs/surrealql/statements/commit#example-usage) -------------------------------------------------------------------------------------- The following query shows example usage of this statement. BEGIN TRANSACTION; -- Setup accounts CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 91031.31; -- Move money UPDATE account:one SET balance += 300.00; UPDATE account:two SET balance -= 300.00; -- Finalise all changes COMMIT TRANSACTION; The following two options can be used at any point if a transaction must be cancelled without commiting the changes: * [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) to manually cancel the transaction. * [THROW](https://surrealdb.com/docs/surrealql/statements/throw) to cancel a transaction with an optional error message. THROW is the only way to cancel a transaction based on a condition, such as inside an [IF..ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) block. In addition, a `RETURN` statement can be used to return early from a successful transaction. This is often used in order to return a customized output. BEGIN; CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 91031.31, wants_to_send_money = true; IF !account:two.wants_to_send_money { THROW "Customer doesn't want to send any money!"; }; LET $first = UPDATE ONLY account:one SET balance += 300.00; LET $second = UPDATE ONLY account:two SET balance -= 300.00; RETURN "Money sent! Status:\n" + $first + '\n' + $second; COMMIT; Output'Money sent! Status: { balance: 135905.16f, id: account:one } { balance: 90731.31f, id: account:two, wants_to_send_money: true }' [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/commit.mdx) Copy page as Markdown [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) ### On this page * [COMMIT statement](https://surrealdb.com/docs/surrealql/statements/commit#commit-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/commit#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/commit#example-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/commit.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # BEGIN statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) `BEGIN` statement[](https://surrealdb.com/docs/surrealql/statements/begin#begin-statement) =========================================================================================== Each statement within SurrealDB is run within its own transaction by default. The `BEGIN` statement can be used to modify this behaviour by running a group of statements inside a single transaction, either succeeding as a whole, or failing. Once all of the statements within a transaction succeed, then all of the data modifications can be made permanent by finalizing the transaction with a [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) statement at the end. If any statement within a transaction encounters an error or the transaction is manually cancelled ([CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) ), then any data modification made within the transaction is rolled back, and will not become a permanent part of the database. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/begin#statement-syntax) SurrealQL SyntaxBEGIN [ TRANSACTION ]; Example usage[](https://surrealdb.com/docs/surrealql/statements/begin#example-usage) ------------------------------------------------------------------------------------- The following query shows example usage of this statement. -- Start a new database transaction. Transactions are a way to ensure multiple operations -- either all succeed or all fail, maintaining data integrity. BEGIN TRANSACTION; -- Create a new account with the ID 'one' and set its initial balance to 135605.16 CREATE account:one SET balance = 135605.16; -- Create another new account with the ID 'two' and set its initial balance to 91031.31 CREATE account:two SET balance = 91031.31; -- Update the balance of account 'one' by adding 300.00 to the current balance. -- This could represent a deposit or other form of credit on the balance property. UPDATE account:one SET balance += 300.00; -- Update the balance of account 'two' by subtracting 300.00 from the current balance. -- This could represent a withdrawal or other form of debit on the balance property. UPDATE account:two SET balance -= 300.00; -- Finalize the transaction. This will apply the changes to the database. If there was an error -- during any of the previous steps within the transaction, all changes would be rolled back and -- the database would remain in its initial state. COMMIT TRANSACTION; Returning early from a transaction[](https://surrealdb.com/docs/surrealql/statements/begin#returning-early-from-a-transaction) ------------------------------------------------------------------------------------------------------------------------------- While all transactions require a final `COMMIT` or `CANCEL` statement in order to run, an early return can take place via the following: * An error inside one of the statements inside the transaction, * A `THROW` statement to return early with an error, * A `RETURN` statement to return early. This is often used to customize the output of a transaction. An example of the above: BEGIN; CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 91031.31, wants_to_send_money = true; IF !account:two.wants_to_send_money { THROW "Customer doesn't want to send any money!"; }; LET $first = UPDATE ONLY account:one SET balance += 300.00; LET $second = UPDATE ONLY account:two SET balance -= 300.00; RETURN "Money sent! Status:\n" + $first + '\n' + $second; COMMIT; Output'Money sent! Status: { balance: 135905.16f, id: account:one } { balance: 90731.31f, id: account:two, wants_to_send_money: true }' [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/begin.mdx) Copy page as Markdown [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) ### On this page * [BEGIN statement](https://surrealdb.com/docs/surrealql/statements/begin#begin-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/begin#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/begin#example-usage) * [Returning early from a transaction](https://surrealdb.com/docs/surrealql/statements/begin#returning-early-from-a-transaction) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/begin.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # LIVE SELECT statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) `LIVE SELECT` statement[](https://surrealdb.com/docs/surrealql/statements/live#live-select-statement) ====================================================================================================== The `LIVE SELECT` statement can be used to initiate a real-time selection from a table, including the option to apply filters. In practical terms, when you execute a `LIVE SELECT` query, it triggers an ongoing session that captures any subsequent changes to the data in real-time. These changes are then immediately transmitted to the client, ensuring that the client is consistently updated with the latest data modifications. > Important > > Currently, `LIVE SELECT` is only supported in single-node deployments, with multi-node support being actively developed. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/live#statement-syntax) SurrealQL SyntaxLIVE SELECT [\ [ VALUE ] @fields [ AS @alias ]\ | DIFF\ ] FROM @targets [ WHERE @conditions ] [ FETCH @fields ... ] ; Example usage[](https://surrealdb.com/docs/surrealql/statements/live#example-usage) ------------------------------------------------------------------------------------ ### Basic usage[](https://surrealdb.com/docs/surrealql/statements/live#basic-usage) By default, SurrealDB will push the entire record over the websocket when created or updated, and just the record’s ID when deleted. LIVE SELECT * FROM person; ['b1f1d115-ad0f-460d-8cbf-dbc7ce48851c'] The result of the above query will be a UUID. This UUID is the Live Query Unique ID, and is used to differentate between different Live Queries. You will want to keep track of this ID, so that you can differentiate between different notifications being received after this query. You can also use this UUID to [KILL](https://surrealdb.com/docs/surrealql/statements/kill) (stop) the Live Query. The protocol will then send messages that are of a Notification format. You can find an example of such a message in the [Live Query WebSocket protocol](https://surrealdb.com/docs/surrealdb/integration/rpc#live-websocket-only) description. ### Diff[](https://surrealdb.com/docs/surrealql/statements/live#diff) When using the `DIFF` mode, updates will be sent in the form of an array with [JSON Patch](https://jsonpatch.com/) messages. LIVE SELECT DIFF FROM person; ['b87cbb0d-ca15-4f0a-8f86-caa680672aa5'] ### Filter the live query[](https://surrealdb.com/docs/surrealql/statements/live#filter-the-live-query) You can optionally apply filters with the `WHERE` clause. LIVE SELECT * FROM person WHERE age > 18; Consistency Guarantees[](https://surrealdb.com/docs/surrealql/statements/live#consistency-guarantees) ------------------------------------------------------------------------------------------------------ When using Live Queries, it is important to understand the ordering of messages and events when many clients and transactions are running in paralllel. Notifications on live queries are only published for committed transactions. While a best effort is made to assure ordering is correct, a strict correctness is not yet in place for a full guarantee. As such that some messages may be received out of order from their commit order. However, transactions that are committed from the same client will always be in order. Security enforcement is always evaluated per notification and will reflect the value of authorisation at the time of publishing the notification. This means that if a transaction is committed, after which the authorisation immediately changes for the live query receiver, the receiver will get the notification under the new rules. Fetching inside live queries[](https://surrealdb.com/docs/surrealql/statements/live#fetching-inside-live-queries) ------------------------------------------------------------------------------------------------------------------ Available since: v2.2.0 The `FETCH` clause can be used inside live queries as well. LIVE SELECT * FROM person WHERE age > 18 FETCH friends; Other notes[](https://surrealdb.com/docs/surrealql/statements/live#other-notes) -------------------------------------------------------------------------------- Currently, it is not possible to use [parameters inside of Live Queries](https://github.com/surrealdb/surrealdb/issues/4026) . LIVE SELECT * FROM person WHERE $field > $value It is possible to have parameters for the table reference. LIVE SELECT * FROM $table WHERE field > 50 [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/live.mdx) Copy page as Markdown [LET](https://surrealdb.com/docs/surrealql/statements/let) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) ### On this page * [LIVE SELECT statement](https://surrealdb.com/docs/surrealql/statements/live#live-select-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/live#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/live#example-usage) * [Basic usage](https://surrealdb.com/docs/surrealql/statements/live#basic-usage) * [Diff](https://surrealdb.com/docs/surrealql/statements/live#diff) * [Filter the live query](https://surrealdb.com/docs/surrealql/statements/live#filter-the-live-query) * [Consistency Guarantees](https://surrealdb.com/docs/surrealql/statements/live#consistency-guarantees) * [Fetching inside live queries](https://surrealdb.com/docs/surrealql/statements/live#fetching-inside-live-queries) * [Other notes](https://surrealdb.com/docs/surrealql/statements/live#other-notes) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/live.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # GROUP BY clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) `GROUP BY` clause[](https://surrealdb.com/docs/surrealql/clauses/group-by#group-by-clause) =========================================================================================== The `GROUP BY` clause is used in SurrealQL to aggregate data based on one or more columns. It is particularly useful when you want to perform calculations on groups of data, such as counting the number of records, calculating averages, or finding sums for each group. This is often used in reporting and data analysis to summarize data in a meaningful way. More specifically, it is used to: * Aggregating Data: When you need to calculate aggregate values like SUM, COUNT, AVG, MIN, or MAX for each group of data. * Data Summarization: When you want to summarize data into categories or groups. * Reporting: When generating reports that require grouped data, such as sales reports by region or department. Syntax[](https://surrealdb.com/docs/surrealql/clauses/group-by#syntax) ----------------------------------------------------------------------- Clause SyntaxGROUP BY @fields Data representation[](https://surrealdb.com/docs/surrealql/clauses/group-by#data-representation) ------------------------------------------------------------------------------------------------- SELECT product_id, region, math::sum(amount) AS total_sales FROM sales GROUP BY product_id, region; Explanation: * `SELECT product_id, region, math::sum(amount) AS total_sales`: This selects the product\_id and region columns and calculates the total sales amount for each group. The `AS` clause is used to rename the calculated column to `total_sales`. * `FROM sales`: This specifies the table from which to retrieve the data. Using the `FROM` clause, we specify the table `sales` to retrieve the data from. * `GROUP BY product_id, region`: This groups the results by product\_id and region, so the SUM function calculates the total sales for each unique combination of product\_id and region. This query will return a result set where each row represents a unique combination of `product_id` and `region`, along with the total sales amount for that combination. This is useful for understanding how different products are performing in different regions. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/group-by.mdx) Copy page as Markdown [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) ### On this page * [GROUP BY clause](https://surrealdb.com/docs/surrealql/clauses/group-by#group-by-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/group-by#syntax) * [Data representation](https://surrealdb.com/docs/surrealql/clauses/group-by#data-representation) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/group-by.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE TABLE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) `DEFINE TABLE` statement[](https://surrealdb.com/docs/surrealql/statements/define/table#define-table-statement) ================================================================================================================ The `DEFINE TABLE` statement allows you to declare your table by name, enabling you to apply strict controls to a table’s schema by making it `SCHEMAFULL`, create a foreign table view, and set permissions specifying what operations can be performed on the table. > Note > > The fields of a table are not defined using `DEFINE TABLE`, but via individual [`DEFINE FIELD`](https://surrealdb.com/docs/surrealql/statements/define/field) > statements. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/table#requirements) ------------------------------------------------------------------------------------------ * You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the `DEFINE TABLE` statement. * [You must select your namespace and database](https://surrealdb.com/docs/surrealql/statements/use) before you can use the `DEFINE TABLE` statement. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/table#statement-syntax) -------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE TABLE [ OVERWRITE | IF NOT EXISTS ] @name [ DROP ] [ SCHEMAFULL | SCHEMALESS ] [ TYPE [ ANY | NORMAL | RELATION [ IN | FROM ] @table [ OUT | TO ] @table [ ENFORCED ]]] [ AS SELECT @projections\ FROM @tables\ [ WHERE @condition ]\ [ GROUP [ BY ] @groups ]\ ] [ CHANGEFEED @duration [ INCLUDE ORIGINAL ] ] [ PERMISSIONS [ NONE | FULL\ | FOR select @expression\ | FOR create @expression\ | FOR update @expression\ | FOR delete @expression\ ] ] [ COMMENT @string ] Example usage[](https://surrealdb.com/docs/surrealql/statements/define/table#example-usage) -------------------------------------------------------------------------------------------- Below shows how you can create a table using the `DEFINE TABLE` statement. -- Declare the name of a table. DEFINE TABLE reading; The following example uses the `DROP` portion of the `DEFINE TABLE` statement. Marking a table as `DROP` disallows creating or updating records. `DROP` tables are useful in combination with events or foreign (view) tables, as you can compute a record and essentially drop the input. -- By marking a table as DROP, you disallow any records to be created or updated. -- Records that currently exist in the table will not automatically be deleted, you can still remove them manually. DEFINE TABLE reading DROP; The following expression shows how you can define a `CHANGEFEED` for a table. After creating, updating, and deleting records in the table as usual, using `SHOW CHANGES FOR` will show the changes that have taken place during this time. -- Define the change feed and its duration -- Optionally, append INCLUDE ORIGINAL to include info -- on the current record before a change took place DEFINE TABLE reading CHANGEFEED 3d; -- Create some records in the reading table CREATE reading SET story = "Once upon a time"; CREATE reading SET story = "there was a database"; -- Replay changes to the reading table since a certain date -- Must be after the timestamp at which the changefeed began SHOW CHANGES FOR TABLE reading SINCE d"2023-09-07T01:23:52Z" LIMIT 10; -- Alternatively, show the changes for the table since a version number SHOW CHANGES FOR TABLE reading SINCE 0 LIMIT 10; Response without INCLUDE ORIGINAL[\ {\ "changes": [\ {\ "define_table": {\ "name": "reading"\ }\ }\ ],\ "versionstamp": 29\ },\ {\ "changes": [\ {\ "update": {\ "id": "reading:h1gcbc7ykbpslellh2g2",\ "story": "Once upon a time"\ }\ }\ ],\ "versionstamp": 30\ },\ {\ "changes": [\ {\ "update": {\ "id": "reading:l9qfcncklhnlklby1avf",\ "story": "there was a database"\ }\ }\ ],\ "versionstamp": 31\ }\ ] Response with INCLUDE ORIGINAL[\ {\ "changes": [\ {\ "define_table": {\ "name": "reading"\ }\ }\ ],\ "versionstamp": 29\ },\ {\ "changes": [\ {\ "current": {\ "id": "reading:2j3rc2yw1jzspcuvfe9v",\ "story": "Once upon a time"\ },\ "update": [\ {\ "op": "replace",\ "path": "/",\ "value": null\ }\ ]\ }\ ],\ "versionstamp": 30\ },\ {\ "changes": [\ {\ "current": {\ "id": "reading:iuiurhi0y2ka0by0skqi",\ "story": "there was a database"\ },\ "update": [\ {\ "op": "replace",\ "path": "/",\ "value": null\ }\ ]\ }\ ],\ "versionstamp": 31\ }\ ] Schemafull tables[](https://surrealdb.com/docs/surrealql/statements/define/table#schemafull-tables) ---------------------------------------------------------------------------------------------------- The following example demonstrates the `SCHEMAFULL` portion of the `DEFINE TABLE` statement. When a table is defined as schemafull, the database strictly enforces any schema definitions that are specified using the `DEFINE TABLE` statement. New fields can not be added to a `SCHEMAFULL` table unless they are defined via the [`DEFINE FIELD`](https://surrealdb.com/docs/surrealql/statements/define/field) statement. > Note > > Since `v2.0.0`, schemafull tables are implicitly type [`NORMAL`](https://surrealdb.com/docs/surrealql/statements/define/table#table-with-specialized-type-clause) > tables by default. -- Create schemafull user table. DEFINE TABLE user SCHEMAFULL; -- Define some fields. DEFINE FIELD firstName ON TABLE user TYPE string; DEFINE FIELD lastName ON TABLE user TYPE string; DEFINE FIELD email ON TABLE user TYPE string ASSERT string::is::email($value); DEFINE INDEX userEmailIndex ON TABLE user COLUMNS email UNIQUE; -- SEE IT IN ACTION -- 1: Add a user with all required fields and an undefined one, 'photoURI'. CREATE user CONTENT { firstName: 'Tobie', lastName: 'Hitchcock', email: 'Tobie.Hitchcock@surrealdb.com', photoURI: 'photo/yxCFi22Jw2.webp' }; -- 2: Statement will not fail but photoURI will be ignored as it is not a -- defined field. -- 3: Query the data SELECT * FROM user Schemaless tables[](https://surrealdb.com/docs/surrealql/statements/define/table#schemaless-tables) ---------------------------------------------------------------------------------------------------- The following example demonstrates the `SCHEMALESS` portion of the `DEFINE TABLE` statement. This allows you to explicitly state that the specified table has no schema. -- Create schemaless user table. DEFINE TABLE user SCHEMALESS; -- Define some fields. DEFINE FIELD firstName ON TABLE user TYPE string; DEFINE FIELD lastName ON TABLE user TYPE string; DEFINE FIELD email ON TABLE user TYPE string ASSERT string::is::email($value); DEFINE INDEX userEmailIndex ON TABLE user COLUMNS email UNIQUE; -- SEE IT IN ACTION - Example 1 -- 1: Add a user with all required fields and an undefined one. CREATE user SET firstName = 'Tobie', lastName = 'Hitchcock', email = 'Tobie.Hitchcock@surrealdb.com', photoURI = 'photo/yxCFi22Jw2.webp'; -- 2: Statement will succeed because user is a SCHEMALESS table. -- SEE IT IN ACTION - Example 2 -- 1: Add a user with an invalid email address and include a new field that was never defined. CREATE user SET firstName = 'Jamie', lastName = 'Hitchcock', email = 'Jamie.Hitchcock', photoURI = 'photo/yxCFi22Jw2.webp'; -- 2: Statement will fail because the value for email was not valid. Interaction between fields[](https://surrealdb.com/docs/surrealql/statements/define/table#interaction-between-fields) ---------------------------------------------------------------------------------------------------------------------- While a `DEFINE TABLE` statement represents a template for any subsequent records to be created, a `DEFINE FIELD` statement pertains to concrete field data of a record. As such, a `DEFINE FIELD` statement gives access to the record’s other fields through their names, as well as the current field through the [`$value`](https://surrealdb.com/docs/surrealql/parameters#value) parameter. DEFINE TABLE person SCHEMAFULL; DEFINE FIELD first_name ON TABLE person TYPE string ASSERT string::len($value) < 20; DEFINE FIELD last_name ON TABLE person TYPE string ASSERT string::len($value) < 20; DEFINE FIELD name ON TABLE person VALUE first_name + ' ' + last_name; // Creates a `person` with the name "Bob Bobson" CREATE person SET first_name = "Bob", last_name = "Bobson"; Pre-computed table views[](https://surrealdb.com/docs/surrealql/statements/define/table#pre-computed-table-views) ------------------------------------------------------------------------------------------------------------------ In SurrealDB, like in other databases, you can create views. The way you create views is using the `DEFINE TABLE` statement like you would for any other table, then adding the `AS` clause at the end with your `SELECT` query. -- Define a table as a view which aggregates data from the review table DEFINE TABLE avg_product_review TYPE NORMAL AS SELECT count() AS number_of_reviews, math::mean( rating) AS avg_review, ->product.id AS product_id, ->product.name AS product_name FROM review GROUP BY product_id, product_name; -- Query the projection SELECT * FROM avg_product_review; There are a few important things which make our views far more powerful than a typical relational database view and a few limitations to keep in mind. Starting with what makes them powerful. Our pre-computed table views are most similar to event-based, incrementally updating, materialised views. Let’s explain what that means. * Event-based, meaning that when you run add or remove data from the underlying table, in our example, the `review` table, it triggers a matching event on the `avg_product_review` table view. * Materialised view, meaning that the first time we run the table view query, it will run the query like a normal `SELECT` statement, but then materialise the result. Instead of normal views which behave like bookmarked `SELECT` queries, that just look like tables to the user. * Incrementally updating, meaning that for any subsequent run, it will listen for the event trigger and perform the most efficient operation possible to always keep the result up to date, instead of just running the `SELECT` statement again. While this functionality can be replicated in many other databases, it is usually only done by expert users as it can be very complicated to set up and maintain. Therefore, the true power of our pre-computed table views is making this advanced functionality accessible to everyone. As mentioned though, there are a few limitations to keep in mind. * First, while subsequent runs are very efficient, the initial run of large analytical queries can be slow and use a lot of resources, because its just a normal `SELECT` statement. Therefore indexing and query optimisation are still very important. * Second, while both graph relations and record links are supported, the table view update event, only gets triggered based on the table we have in our `FROM` clause. In our case, just the `review` table, not the `product` we are also using in the query. Meaning that if you delete a `review` the `avg_product_review` will reflect that in near real-time. However if you delete a `product`, it will still show up in `avg_product_review`. Also note that table views are not triggered when importing data. Defining permissions[](https://surrealdb.com/docs/surrealql/statements/define/table#defining-permissions) ---------------------------------------------------------------------------------------------------------- By default, the permissions on a table will be set to NONE unless otherwise specified. CREATE some_table; DEFINE TABLE some_other_table; INFO FOR DB; Response{ analyzers: {}, functions: {}, models: {}, params: {}, scopes: {}, tables: { some_other_table: 'DEFINE TABLE some_other_table TYPE ANY SCHEMALESS PERMISSIONS NONE', some_table: 'DEFINE TABLE some_table TYPE ANY SCHEMALESS PERMISSIONS NONE' }, tokens: {}, users: {} } The following shows how to set table level `PERMISSIONS` using the `DEFINE TABLE` statement. This allows you to set independent permissions for selecting, creating, updating, and deleting data. -- Specify access permissions for the 'post' table DEFINE TABLE post SCHEMALESS PERMISSIONS FOR select -- Published posts can be selected WHERE published = true -- A user can select all their own posts OR user = $auth.id FOR create, update -- A user can create or update their own posts WHERE user = $auth.id FOR delete -- A user can delete their own posts WHERE user = $auth.id -- Or an admin can delete any posts OR $auth.admin = true ; Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/table#using-if-not-exists-clause) ------------------------------------------------------------------------------------------------------------------------ Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define a table only if it does not already exist. You should use the `IF NOT EXISTS` clause when defining a table in SurrealDB if you want to ensure that the table is only created if it does not already exist. If the table already exists, the `DEFINE TABLE` statement will return an error. It’s particularly useful when you want to safely attempt to define a table without manually checking its existence first. On the other hand, you should not use the `IF NOT EXISTS` clause when you want to ensure that the table definition is updated regardless of whether it already exists. In such cases, you might prefer using the `OVERWRITE` clause, which allows you to define a table and overwrite an existing one if it already exists, ensuring that the latest version of the table definition is always in use -- Create a TABLE if it does not already exist DEFINE TABLE IF NOT EXISTS reading Using `OVERWRITE` clause[](https://surrealdb.com/docs/surrealql/statements/define/table#using-overwrite-clause) ---------------------------------------------------------------------------------------------------------------- Available since: v2.0.0 The `OVERWRITE` clause can be used to define a table and overwrite an existing one if it already exists. You should use the `OVERWRITE` clause when you want to modify an existing table definition. If the table already exists, the `DEFINE TABLE` statement will overwrite the existing table definition with the new one. -- Create an TABLE and overwrite if it already exists DEFINE TABLE OVERWRITE example; Table with specialized `TYPE`\-clause[](https://surrealdb.com/docs/surrealql/statements/define/table#table-with-specialized-type-clause) ----------------------------------------------------------------------------------------------------------------------------------------- Available since: v1.4.0 When defining a table in SurrealDB, you can specify the type of data that can be stored in the table. This can be done using the `TYPE` clause, followed by either `ANY`, `NORMAL`, or `RELATION`. With `TYPE ANY`, you can specify a table to store any type of data, whether it’s a normal record or a relational record. With `TYPE NORMAL`, you can specify a table to only store “normal” records, and not relations. When a table is defined as `TYPE NORMAL`, it will not be able to store relations this can be useful when you want to restrict the type of data that can be stored in a table in schemafull mode. Finally, with `TYPE RELATION`, you can specify a table to only store relational type content. This can be useful when you want to restrict the type of data that can be stored in a table. DEFINE TABLE person TYPE ANY; DEFINE TABLE person; With `TYPE NORMAL`, you can specify a table to only store “normal” records, and not relations. -- Since it's default, we can also omit the TYPE clause DEFINE TABLE person TYPE NORMAL; With `TYPE RELATION`, you can specify a table to only store relational type content, and restrict what kind of relations can be stored. -- Just a RELATION table, no constraints on the type of table DEFINE TABLE likes TYPE RELATION; -- Define a relation table, and constrain the type of relation which can be stored DEFINE TABLE likes TYPE RELATION FROM user TO post; -- OR use IN and OUT alternatively to FROM and TO DEFINE TABLE likes TYPE RELATION IN user OUT post; -- To allow a link to one of a possible set of record types, use the | operator DEFINE TABLE likes TYPE RELATION FROM user TO post|video; DEFINE TABLE likes TYPE RELATION IN user OUT post|video; -- Define a relation table, and constrain the type of relation which can be stored DEFINE TABLE assigned_to SCHEMAFULL TYPE RELATION IN tag OUT sticky PERMISSIONS FOR create, select, update, delete WHERE in.owner == $auth.id AND out.author == $auth.id; Using ENFORCED to ensure that related records exist[](https://surrealdb.com/docs/surrealql/statements/define/table#using-enforced-to-ensure-that-related-records-exist) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Available since: v2.0.0 As relations are represented by standalone tables, they can be constructed before any linked records exist. RELATE city:one->road_to->city:two SET distance = 12.4, slope = 5.4; Output[\ {\ distance: 12.4f,\ id: road_to:pacwucj25a056hhs2s5h,\ in: city:one,\ out: city:two,\ slope: 5.4f\ }\ ] As such, a query on the relation will return nothing until the records it has been defined upon are created. SELECT ->road_to->city FROM city; CREATE city:one, city:two; SELECT ->road_to->city FROM city; Output-------- Query -------- [] -------- Query -------- [\ {\ "->road_to": {\ "->city": [\ city:two\ ]\ }\ },\ {\ "->road_to": {\ "->city": []\ }\ }\ ] If this behaviour is not desirable, the `ENFORCED` clause can be used on a table of `TYPE RELATION` to disallow a `RELATE` statement from working unless it points to existing data. DEFINE TABLE road_to TYPE RELATION IN city OUT city ENFORCED; RELATE city:one->road_to->city:three SET distance = 5.5, slope = 30.0; Output"The record 'city:three' does not exist" [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/table.mdx) Copy page as Markdown [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) ### On this page * [DEFINE TABLE statement](https://surrealdb.com/docs/surrealql/statements/define/table#define-table-statement) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/table#requirements) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/table#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/table#example-usage) * [Schemafull tables](https://surrealdb.com/docs/surrealql/statements/define/table#schemafull-tables) * [Schemaless tables](https://surrealdb.com/docs/surrealql/statements/define/table#schemaless-tables) * [Interaction between fields](https://surrealdb.com/docs/surrealql/statements/define/table#interaction-between-fields) * [Pre-computed table views](https://surrealdb.com/docs/surrealql/statements/define/table#pre-computed-table-views) * [Defining permissions](https://surrealdb.com/docs/surrealql/statements/define/table#defining-permissions) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/table#using-if-not-exists-clause) * [Using OVERWRITE clause](https://surrealdb.com/docs/surrealql/statements/define/table#using-overwrite-clause) * [Table with specialized TYPE -clause](https://surrealdb.com/docs/surrealql/statements/define/table#table-with-specialized-type-clause) * [Using ENFORCED to ensure that related records exist](https://surrealdb.com/docs/surrealql/statements/define/table#using-enforced-to-ensure-that-related-records-exist) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/table.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Transactions | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Transactions](https://surrealdb.com/docs/surrealql/transactions) Transactions[](https://surrealdb.com/docs/surrealql/transactions#transactions) =============================================================================== Each statement within SurrealDB is run within its own transaction by default. If a set of changes need to be made together, then groups of statements can be run together as a single transaction. If all of the statements within a transaction succeed, and the transaction is successful, then all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be cancelled or rolled back, then any data modification made within the transaction is rolled back, and will not become a permanent part of the database. Starting a transaction[](https://surrealdb.com/docs/surrealql/transactions#starting-a-transaction) --------------------------------------------------------------------------------------------------- The `BEGIN` or `BEGIN TRANSACTION` statement starts a transaction in which multiple statements can be run together. Starting a transactionBEGIN [ TRANSACTION ]; The following query shows example usage of this statement. Example usage of BEGIN TRANSACTION-- Start a new database transaction. Transactions are a way to ensure multiple operations -- either all succeed or all fail, maintaining data integrity. BEGIN TRANSACTION; -- Create a new account with the ID 'one' and set its initial balance to 135605.16 CREATE account:one SET balance = 135605.16; -- Create another new account with the ID 'two' and set its initial balance to 91031.31 CREATE account:two SET balance = 91031.31; -- Update the balance of account 'one' by adding 300.00 to the current balance. -- This could represent a deposit or other form of credit on the balance property. UPDATE account:one SET balance += 300.00; -- Update the balance of account 'two' by subtracting 300.00 from the current balance. -- This could represent a withdrawal or other form of debit on the balance property. UPDATE account:two SET balance -= 300.00; -- Finalize the transaction. This will apply the changes to the database. If there was an error -- during any of the previous steps within the transaction, all changes would be rolled back and -- the database would remain in its initial state. COMMIT TRANSACTION; Committing a transaction[](https://surrealdb.com/docs/surrealql/transactions#committing-a-transaction) ------------------------------------------------------------------------------------------------------- The [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) statement is used to commit a set of statements within a transaction, ensuring that all data modifications become a permanent part of the database. Committing a transactionCOMMIT [ TRANSACTION ]; The following query shows example usage of this statement. Example usage of COMMIT TRANSACTIONBEGIN TRANSACTION; -- Setup accounts CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 91031.31; -- Move money UPDATE account:one SET balance += 300.00; UPDATE account:two SET balance -= 300.00; -- Finalise all changes COMMIT TRANSACTION; Cancelling a transaction[](https://surrealdb.com/docs/surrealql/transactions#cancelling-a-transaction) ------------------------------------------------------------------------------------------------------- The [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) statement can be used to cancel a set of statements within a transaction, reverting or rolling back any data modification made within the transaction as a whole. Cancelling a transactionCANCEL [ TRANSACTION ]; The following query shows example usage of this statement. Example usage of CANCEL TRANSACTIONBEGIN TRANSACTION; -- Setup accounts CREATE account:one SET balance = 135605.16; CREATE account:two SET balance = 91031.31; -- Move money UPDATE account:one SET balance += 300.00; UPDATE account:two SET balance -= 300.00; -- Rollback all changes CANCEL TRANSACTION; THROW to conditionally cancel a transaction[](https://surrealdb.com/docs/surrealql/transactions#throw-to-conditionally-cancel-a-transaction) --------------------------------------------------------------------------------------------------------------------------------------------- While transactions are automatically rolled back if an error occurs in any of its statements, [THROW](https://surrealdb.com/docs/surrealql/statements/throw) can also be used to explicitly break out of a transaction at any point. `THROW` can be followed by any value which serves as the error message, usually a string. BEGIN TRANSACTION; CREATE account:one SET dollars = 100; CREATE account:two SET dollars = 100; LET $transfer_amount = 150; UPDATE account:one SET dollars -= $transfer_amount; UPDATE account:two SET dollars += $transfer_amount; IF account:one.dollars < 0 { THROW "Insufficient funds, would have $" + account:one.dollars + " after transfer" }; COMMIT TRANSACTION; SELECT * FROM account; Output when $transfer_amount set to 150'An error occurred: Insufficient funds, would have $-50 after transfer' Output when $transfer_amount set to 50[\ {\ dollars: 50,\ id: account:one\ },\ {\ dollars: 150,\ id: account:two\ }\ ] Using transactions to test code for errors[](https://surrealdb.com/docs/surrealql/transactions#using-transactions-to-test-code-for-errors) ------------------------------------------------------------------------------------------------------------------------------------------- As failed transactions automatically roll back any changes made, a transaction with a final `THROW` statement can be used as a confirmation that no errors have taken place inside a group of queries. Take the following example that creates a unique index and then inserts some records to make sure that the database logic is functioning as expected. However, as names are not necessarily unique, the index soon gives an error and cancels the transaction before `THROW` can be reached. BEGIN TRANSACTION; DEFINE INDEX unique_name ON TABLE person FIELDS name UNIQUE; INSERT INTO person [\ { name: 'Agatha Christie', born: d'1890-09-15' },\ { name: 'Billy Billerson', born: d'1979-09-11' },\ -- Pretend there are is 10,000 more objects here\ { name: 'Agatha Christie', born: d'1955-05-15' },\ ]; THROW "Reached the end"; COMMIT TRANSACTION; The output is not the expected ‘An error occurred: Reached the end’ message, showing that not all queries were successful. Output"Database index `unique_name` already contains 'Agatha Christie', with record `person:qs4bpvl96sf9x40b3567`" If the index is redefined to be less strict, the statetements will work and the expected output will be reached, confirming that no errors occurred during the test. BEGIN TRANSACTION; DEFINE INDEX OVERWRITE unique_person ON TABLE person FIELDS name, born UNIQUE; INSERT INTO person [\ { name: 'Agatha Christie', born: d'1890-09-15' },\ { name: 'Billy Billerson', born: d'1979-09-11' },\ { name: 'Agatha Christie', born: d'1955-05-15' },\ ]; THROW "Reached the end"; COMMIT TRANSACTION; Expected output'An error occurred: Reached the end' [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/transactions.mdx) Copy page as Markdown [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Comments](https://surrealdb.com/docs/surrealql/comments) ### On this page * [Starting a transaction](https://surrealdb.com/docs/surrealql/transactions#starting-a-transaction) * [Committing a transaction](https://surrealdb.com/docs/surrealql/transactions#committing-a-transaction) * [Cancelling a transaction](https://surrealdb.com/docs/surrealql/transactions#cancelling-a-transaction) * [THROW to conditionally cancel a transaction](https://surrealdb.com/docs/surrealql/transactions#throw-to-conditionally-cancel-a-transaction) * [Using transactions to test code for errors](https://surrealdb.com/docs/surrealql/transactions#using-transactions-to-test-code-for-errors) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/transactions.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # LIMIT clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) `LIMIT` clause[](https://surrealdb.com/docs/surrealql/clauses/limit#limit-clause) ================================================================================== The `LIMIT` clause is used to limit the number of records returned by a query. It is particularly useful when you want to retrieve a specific number of records from a table. When using the `LIMIT` clause, it is possible to paginate results by using the `START` clause to start from a specific record from the result set. It is important to note that the `START` count starts from 0. Syntax[](https://surrealdb.com/docs/surrealql/clauses/limit#syntax) -------------------------------------------------------------------- Clause SyntaxLIMIT @number [START @start 0] Examples[](https://surrealdb.com/docs/surrealql/clauses/limit#examples) ------------------------------------------------------------------------ -- Select the first 10 records SELECT * FROM person LIMIT 10; -- Start at record 50 and select the following 10 records SELECT * FROM person LIMIT 10 START 50; -- Select the first 5 records from the array SELECT * FROM [1,2,3,4,5,6,7,8,9,10] LIMIT 5 START 4; Result[\ 5,\ 6,\ 7,\ 8,\ 9\ ] The `LIMIT` clause followed by `1` is often used along with the `ONLY` clause to satisfy the requirement that only up to a single record can be returned. -- Record IDs are unique so guaranteed to be no more than 1 SELECT * FROM ONLY person:jamie; -- Error because no guarantee that this will return a single record SELECT * FROM ONLY person WHERE name = "Jaime"; -- Add `LIMIT 1` to ensure that only up to one record will be returned SELECT * FROM ONLY person WHERE name = "Jaime" LIMIT 1; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/limit.mdx) Copy page as Markdown [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) ### On this page * [LIMIT clause](https://surrealdb.com/docs/surrealql/clauses/limit#limit-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/limit#syntax) * [Examples](https://surrealdb.com/docs/surrealql/clauses/limit#examples) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/limit.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # OMIT clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) `OMIT` clause[](https://surrealdb.com/docs/surrealql/clauses/omit#omit-clause) =============================================================================== The `OMIT` clause is used to omit fields from the result set which can be particularly useful when querying large datasets. Syntax[](https://surrealdb.com/docs/surrealql/clauses/omit#syntax) ------------------------------------------------------------------- Clause SyntaxOMIT @fields FROM @table Examples[](https://surrealdb.com/docs/surrealql/clauses/omit#examples) ----------------------------------------------------------------------- CREATE person:tobie SET name = 'Tobie', password = '123456', opts.security = 'secure', opts.enabled = true; CREATE person:jaime SET name = 'Jaime', password = 'asdfgh', opts.security = 'secure', opts.enabled = false; SELECT * FROM person; -- Omit the password field and security field in the options object SELECT * OMIT password, opts.security FROM person; -- Using destructuring syntax (since 2.0.0) SELECT * OMIT password, opts.{ security, enabled } FROM person; Return fields-------- Query 3 (132.138µs) -------- [\ {\ id: person:jaime,\ name: 'Jaime',\ opts: {\ enabled: false,\ security: 'secure'\ },\ password: 'asdfgh'\ },\ {\ id: person:tobie,\ name: 'Tobie',\ opts: {\ enabled: true,\ security: 'secure'\ },\ password: '123456'\ }\ ] -------- Query 4 (61.876µs) -------- [\ {\ id: person:jaime,\ name: 'Jaime',\ opts: {\ enabled: false\ }\ },\ {\ id: person:tobie,\ name: 'Tobie',\ opts: {\ enabled: true\ }\ }\ ] -------- Query 5 (52.152µs) -------- [\ {\ id: person:jaime,\ name: 'Jaime',\ opts: {}\ },\ {\ id: person:tobie,\ name: 'Tobie',\ opts: {}\ }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/omit.mdx) Copy page as Markdown [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) ### On this page * [OMIT clause](https://surrealdb.com/docs/surrealql/clauses/omit#omit-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/omit#syntax) * [Examples](https://surrealdb.com/docs/surrealql/clauses/omit#examples) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/omit.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # EXPLAIN clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) `EXPLAIN` clause[](https://surrealdb.com/docs/surrealql/clauses/explain#explain-clause) ======================================================================================== The `EXPLAIN` clause is used to explain the query plan of a query. It is particularly useful when you want to understand how a query is executed and how it is optimized by the database. When `EXPLAIN` is used, the statement returns an explanation, essentially revealing the execution plan to provide transparency and understanding of the query performance. Syntax[](https://surrealdb.com/docs/surrealql/clauses/explain#syntax) ---------------------------------------------------------------------- Clause Syntax@query EXPLAIN [FULL] Using the `EXPLAIN` clause in addition to the `FULL` keyword is expeciallly useful when you want to understand the performance of a query and can provide more details when debugging. Examples[](https://surrealdb.com/docs/surrealql/clauses/explain#examples) -------------------------------------------------------------------------- For example, consider the performance of the following query when the field `email` is not indexed. We can see that the execution plan will iterate over the whole table. Index not usedCREATE person:tobie SET name = "Tobie", address = "1 Bagshot Row", email = "tobie@surrealdb.com"; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN FULL; Output-------- Query -------- [\ {\ detail: {\ table: 'person'\ },\ operation: 'Iterate Table'\ },\ {\ detail: {\ type: 'Memory'\ },\ operation: 'Collector'\ }\ ] -------- Query -------- [\ {\ detail: {\ table: 'person'\ },\ operation: 'Iterate Table'\ },\ {\ detail: {\ type: 'Memory'\ },\ operation: 'Collector'\ },\ {\ detail: {\ count: 1\ },\ operation: 'Fetch'\ }\ ] On the other hand, here is the result when the field `email` is indexed. We can see that the execution plan will use the index to retrieve the record. Index usedDEFINE INDEX fast_email ON TABLE person FIELDS email; CREATE person:tobie SET name = "Tobie", address = "1 Bagshot Row", email = "tobie@surrealdb.com"; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN; SELECT * FROM person WHERE email='tobie@surrealdb.com' EXPLAIN FULL; Output-------- Query -------- [\ {\ detail: {\ plan: {\ index: 'fast_email',\ operator: '=',\ value: 'tobie@surrealdb.com'\ },\ table: 'person'\ },\ operation: 'Iterate Index'\ },\ {\ detail: {\ type: 'Memory'\ },\ operation: 'Collector'\ }\ ] -------- Query -------- [\ {\ detail: {\ plan: {\ index: 'fast_email',\ operator: '=',\ value: 'tobie@surrealdb.com'\ },\ table: 'person'\ },\ operation: 'Iterate Index'\ },\ {\ detail: {\ type: 'Memory'\ },\ operation: 'Collector'\ },\ {\ detail: {\ count: 1\ },\ operation: 'Fetch'\ }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/explain.mdx) Copy page as Markdown [Clauses](https://surrealdb.com/docs/surrealql/clauses) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) ### On this page * [EXPLAIN clause](https://surrealdb.com/docs/surrealql/clauses/explain#explain-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/explain#syntax) * [Examples](https://surrealdb.com/docs/surrealql/clauses/explain#examples) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/explain.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Record links | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) Record links[](https://surrealdb.com/docs/surrealql/datamodel/records#record-links) ==================================================================================== One of the most powerful features of SurrealDB is the ability to traverse from record-to-record without the need for traditional SQL JOINs. Each record ID points directly to a specific record in the database, without needing to run a table scan query. Record IDs can be stored within other records, allowing them to be linked together. Creating a record[](https://surrealdb.com/docs/surrealql/datamodel/records#creating-a-record) ---------------------------------------------------------------------------------------------- When you create a record without specifying the id, then a randomly generated id is created and used for the record id. CREATE person SET name = 'Tobie'; person:aio58g22n3upq16hsani It’s also possible to specify a specific record id when creating or updating records. CREATE person:tester SET name = 'Tobie'; person:tester Select directly off of Record IDs[](https://surrealdb.com/docs/surrealql/datamodel/records#select-directly-off-of-record-ids) ------------------------------------------------------------------------------------------------------------------------------ Because Record IDs are their own datatype in SurrealQL, you are able to select directly off of them. CREATE person SET name = 'Tobie', email = 'tobie@surrealdb.com', opts.enabled = true; -- Select the whole record person:aio58g22n3upq16hsani.* -- Select specific fields (since 2.0.0) person:aio58g22n3upq16hsani.{ name, email } Storing record links within records[](https://surrealdb.com/docs/surrealql/datamodel/records#storing-record-links-within-records) ---------------------------------------------------------------------------------------------------------------------------------- Records ids can be stored directly within other records, either as top-level properties, or nested within objects or arrays. CREATE person:jaime SET name = 'Jaime', friends = [person:tobie, person:simon]; CREATE person:tobie SET name = 'Tobie', friends = [person:simon, person:marcus]; CREATE person:simon SET name = 'Simon', friends = [person:jaime, person:tobie]; CREATE person:marcus SET name = 'Marcus', friends = [person:tobie]; Fetching remote records from within records[](https://surrealdb.com/docs/surrealql/datamodel/records#fetching-remote-records-from-within-records) -------------------------------------------------------------------------------------------------------------------------------------------------- Nested field traversal can be used to fetch the properties from the remote records, as if the record was embedded within the record being queried. SELECT friends.name FROM person:tobie; [\ {\ friends: {\ name: ["Simon", "Marcus"]\ }\ }\ ] There is no limit to the number of remote traversals that can be performed in a query. Using `.` dot notation, SurrealDB does not differentiate between nested object properties, or remote records, and will fetch remote records asynchronously when needed for a query. SELECT friends.friends.friends.name FROM person:tobie; [\ {\ friends: {\ friends: {\ friends: {\ name: [\ [ ["Tobie", "Simon"], ["Simon", "Marcus"] ],\ [ ["Simon", "Marcus"] ]\ ]\ }\ }\ }\ }\ ] Drawbacks of embedding record links[](https://surrealdb.com/docs/surrealql/datamodel/records#drawbacks-of-embedding-record-links) ---------------------------------------------------------------------------------------------------------------------------------- In SurrealDB, embedding record links within records is a powerful feature allowing efficient querying and data retrieval. However, it also has a drawback: When an embedded record is deleted or changed, the parent record will not automatically reflect that change. This leads to stale references or “NONE” entries that can accumulate over time. For referential integrity between records, we recommend using the [`RELATE`](https://surrealdb.com/docs/surrealql/statements/relate) statement to create relationships that automatically update when the referenced record is deleted or changed or use [record references](https://surrealdb.com/docs/surrealql/datamodel/references) which allows you automatically track and query relationships between records in both directions. Next steps[](https://surrealdb.com/docs/surrealql/datamodel/records#next-steps) -------------------------------------------------------------------------------- You’ve now seen how to create records using randomly generated ids, or specific record ids. This is just the beginning! The power and flexibility which is possible with the remote record fetching functionality within queries opens up a whole new set of possibilities for storing and querying traditional data, and connected datasets. The next page follows up with a feature available in SurrealDB since version 2.2.0: the ability to use record links in a bidirectional manner thanks to reference tracking. Also check out this explainer video on using record links in SurrealDB: [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/records.mdx) Copy page as Markdown [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) ### On this page * [Creating a record](https://surrealdb.com/docs/surrealql/datamodel/records#creating-a-record) * [Select directly off of Record IDs](https://surrealdb.com/docs/surrealql/datamodel/records#select-directly-off-of-record-ids) * [Storing record links within records](https://surrealdb.com/docs/surrealql/datamodel/records#storing-record-links-within-records) * [Fetching remote records from within records](https://surrealdb.com/docs/surrealql/datamodel/records#fetching-remote-records-from-within-records) * [Drawbacks of embedding record links](https://surrealdb.com/docs/surrealql/datamodel/records#drawbacks-of-embedding-record-links) * [Next steps](https://surrealdb.com/docs/surrealql/datamodel/records#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/records.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Datetimes | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) Datetimes[](https://surrealdb.com/docs/surrealql/datamodel/datetimes#datetimes) ================================================================================ SurrealDB has native support for datetimes with nanosecond precision. SurrealDB automatically parses and understands datetimes which are written as strings in the SurrealQL language. Times must also be formatted in an [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) format. > Note > > As of `v2.0.0`, SurrealDB no longer eagerly converts a string into a datetime. An implicit `d` prefix or cast using `` is required instead. CREATE event SET time = d"2023-07-03T07:18:52Z"; SurrealDB handles all datetimes with nanosecond precision. CREATE event SET time = d"2023-07-03T07:18:52.841147Z"; SurrealDB handles all timezones, and automatically converts and stores datetimes as a UTC date. CREATE event SET time = d"2023-07-03T07:18:52.841147+02:00"; The above queries will all work even if the datetime format is incorrect, but will generate a `string` instead of a `datetime`. Casting with `` will force an error if the input is incorrect. With correct input: CREATE event SET time = "2023-07-03T07:18:52.841147Z"; Response[{ id: event:jwm8ncmfi30nrxdf24ws, time: d'2023-07-03T07:18:52.841147Z' }] With incorrect input (missing final Z): CREATE event SET time = "2023-07-03T07:18:52.841147"; Response"Expected a datetime but cannot convert '2023-07-03T07:18:52.841147' into a datetime" > Note > > As a convenience, a date without time will always parse correctly as a datetime. CREATE event SET time = "2024-04-03"; Response[{ id: event:4t50wjjlne9v8km2qcwq, time: d'2024-04-03T00:00:00Z' }] Datetime types in schemafull tables[](https://surrealdb.com/docs/surrealql/datamodel/datetimes#datetime-types-in-schemafull-tables) ------------------------------------------------------------------------------------------------------------------------------------ Defining a schemafull table will also ensure that datetimes are properly formatted and not passed on as simple strings. DEFINE TABLE event SCHEMAFULL; DEFINE FIELD time ON event TYPE datetime; CREATE event SET time = "2023-07-03T07:18:52.841147"; Response"Found '2023-07-03T07:18:52.841147' for field `time`, with record `event:l4q1s7hermg9yoemkqrp`, but expected a datetime" The above query will fail because the datetime is not cast as a datetime type. The correct query is: DEFINE TABLE event SCHEMAFULL; DEFINE FIELD time ON event TYPE datetime; CREATE event SET time = d"2023-07-03T07:18:52.84114Z"; Response[NONE, NONE, [{ id: event:a3g30bdxypvt21tf3jo0, time: d'2023-07-03T07:18:52.841140Z' }]] ### Datetime comparison[](https://surrealdb.com/docs/surrealql/datamodel/datetimes#datetime-comparison) A datetime can be compared with another using the advanced SurrealDB operators. SELECT * FROM d"2023-07-03T07:18:52Z" < d"2023-07-03T07:18:52.84114Z"; Response[[true]] Durations and datetimes[](https://surrealdb.com/docs/surrealql/datamodel/datetimes#durations-and-datetimes) ------------------------------------------------------------------------------------------------------------ Durations can be used to modify and alter datetimes. CREATE event SET time = d"2023-07-03T07:18:52Z" + 2w; Response[[{ id: event:⟨9ey7v8r0fd46xblf9dsf⟩, time: d'2023-07-17T07:18:52Z' }]] Multi-part durations can also be used to modify datetimes. CREATE event SET time = d"2023-07-03T07:18:52.841147Z" + 1h30m20s1350ms; Response[[{ id: event:⟨9ey7v8r0fd46xblf9dsf⟩, time: d'2023-07-03T08:49:14.191147Z' }]] ### Duration units[](https://surrealdb.com/docs/surrealql/datamodel/datetimes#duration-units) Durations can be specified in any of the following units: | Unit | Description | | --- | --- | | ns | Nanoseconds | | us | Microseconds, alternative: µs | | ms | Milliseconds | | s | Seconds | | m | Minutes | | h | Hours | | d | Days | | w | Weeks | | y | Years | Next steps[](https://surrealdb.com/docs/surrealql/datamodel/datetimes#next-steps) ---------------------------------------------------------------------------------- You’ve now seen how to store, modify, and handle dates and times in SurrealDB. For more advanced functionality, take a look at the [time](https://surrealdb.com/docs/surrealql/functions/database/time) functions, which enable extracting, altering, rounding, and grouping datetimes into specific time intervals. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/datetimes.mdx) Copy page as Markdown [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) ### On this page * [Datetime types in schemafull tables](https://surrealdb.com/docs/surrealql/datamodel/datetimes#datetime-types-in-schemafull-tables) * [Datetime comparison](https://surrealdb.com/docs/surrealql/datamodel/datetimes#datetime-comparison) * [Durations and datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes#durations-and-datetimes) * [Duration units](https://surrealdb.com/docs/surrealql/datamodel/datetimes#duration-units) * [Next steps](https://surrealdb.com/docs/surrealql/datamodel/datetimes#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/datetimes.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Sets | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) Sets[](https://surrealdb.com/docs/surrealql/datamodel/sets#sets) ================================================================= A set is similar to an array, but deduplicates items. DEFINE FIELD visited ON TABLE traveler TYPE set>; CREATE traveler:one SET visited = [country:canada, country:usa, country:korea, country:japan]; -- Traveler comes back from a trip UPDATE traveler:one SET visited += country:uk; -- And again UPDATE traveler:one SET visited += country:uk; Response[\ {\ id: traveler:one,\ visited: [\ country:canada,\ country:usa,\ country:korea,\ country:japan,\ country:uk\ ]\ }\ ] Internally, a set is identical to an array – even inside a schema definition. The only difference is that a field defined as a set will never hold a duplicate item. Otherwise, a field defined as an `array` can take a `set` as input and vice versa. DEFINE FIELD bank_accounts ON TABLE customer TYPE set; DEFINE FIELD languages ON TABLE customer TYPE array; CREATE customer SET bank_accounts = [\ 55555,\ 55555,\ 98787\ ], languages = [\ "en",\ "ja",\ "kr",\ "kr"\ ]; Output[\ {\ bank_accounts: [\ 55555,\ 98787\ ],\ id: customer:uv6mn62t8td9vzvfogh4,\ languages: [\ 'en',\ 'ja',\ 'kr'\ ]\ }\ ] Casting into a `set` can be a convenient way to deduplicate items in the same way that the [`array::distinct()`](https://surrealdb.com/docs/surrealql/functions/database/array#arraydistinct) function is used. LET $array = [1,1,3,4,4,4,4,4,4]; RETURN [\ $array.distinct(),\ $array\ ]; Output[\ [\ 1,\ 3,\ 4\ ],\ [\ 1,\ 3,\ 4\ ]\ ] For all other behaviour and uses of a set, please see [the page on arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) . [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/sets.mdx) Copy page as Markdown [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/sets.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Booleans | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) Booleans[](https://surrealdb.com/docs/surrealql/datamodel/booleans#booleans) ============================================================================= Boolean values can be used to mark whether a field is `true` or `false`. CREATE person SET newsletter = false, interested = true; Many SurrealDB operators and functions return booleans. CREATE person SET name = "Billy", name_is_billy = name = "Billy", name_is_long = string::len(name) > 10; Response[\ {\ "id": "person:8ii7v7f54a2m1rla1y6b",\ "name": "Billy",\ "name_is_billy": true,\ "name_is_long": false\ }\ ] Boolean values can be written in anycase. CREATE person SET newsletter = FALSE, interested = True, very_interested = trUE; Booleans in `WHERE` clauses[](https://surrealdb.com/docs/surrealql/datamodel/booleans#booleans-in-where-clauses) ----------------------------------------------------------------------------------------------------------------- When performing a query on the database, accessing a record’s ID directly or using a [record range](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ranges) allows performance to be significantly sped up by avoiding the table scan which is used when a `WHERE` clause is included. However, if a `WHERE` clause is unavoidable, performance can still be improved by simplifying the portion after the clause as much as possible. As a boolean is the simplest possible datatype, having a boolean field that can be used in a `WHERE` clause can significantly improve performance compared to a more complex operation. -- Fill up the database a bit with 10,000 records CREATE |person:10000| SET random_data = rand::string(1000), data_length = random_data.len(), is_short = data_length < 10 RETURN NONE; -- Add one outlier CREATE person:one SET random_data = "HI!", data_length = random_data.len(), is_short = data_length < 10 RETURN NONE; -- Function call + compare operation: slowest SELECT * FROM person WHERE random_data.len() < 10; -- Compare operation: much faster SELECT * FROM person WHERE data_length < 10; -- Boolean check: even faster SELECT * FROM person WHERE is_short; -- Direct record access: almost instantaneous SELECT * FROM person:one; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/booleans.mdx) Copy page as Markdown [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) ### On this page * [Booleans in WHERE clauses](https://surrealdb.com/docs/surrealql/datamodel/booleans#booleans-in-where-clauses) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/booleans.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Ranges | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) Ranges[](https://surrealdb.com/docs/surrealql/datamodel/ranges#ranges) ======================================================================= Available since: v2.0.0 A range is composed of `..` and possible delimiters to set the maximum and minimum possible values. The default syntax includes the lower limit and excludes the upper limit. A `=` can be used to make the upper limit inclusive, and `>` can be used to make the lower limit exclusive. -- From 0 up to 9 0..10; -- From 0 up to 10 0..=10; -- From 1 to 9 0>..10; -- From 1 to 10 0>..=10; A range becomes open ended if a delimiter is not specified. -- Anything from 0 and up 0..; -- Anything from 1 and up 0>..; -- Anything up to 99 ..100; -- Anything up to 100 ..=100; -- An infinite range ..; A range can be constructed from any type of value. This is most useful when comparing one value to another. -- All true 'g' IN 'a'..'z'; d"2024-01-01" IN d"2020-01-01"..=d"2025-01-01"; ['London', d"2022-02-02", 5.7] IN ['London', d"2020-01-01"]..=['London', d"2024-12-31"]; -- All false "ㅋㅋㅋ" IN "a".."z"; d"2028-01-01" IN d"2020-01-01"..=d"2025-01-01"; ['Philadelphia', d"2022-02-02", 5.7] IN ['London', d"2020-01-01"]..=['London', d"2024-12-31"]; Ranges in FOR loops[](https://surrealdb.com/docs/surrealql/datamodel/ranges#ranges-in-for-loops) ------------------------------------------------------------------------------------------------- Ranges of integers have the added convenience of being able to be used in a [FOR loop](https://surrealdb.com/docs/surrealql/statements/for) . FOR $year IN 0..=2024 { CREATE historical_events SET for_year = $year, events = "To be added"; } Ranges in WHERE clauses[](https://surrealdb.com/docs/surrealql/datamodel/ranges#ranges-in-where-clauses) --------------------------------------------------------------------------------------------------------- A range can be used in a `WHERE` clause in place of operators like `<` and `>`. This is especially useful when checking for a number that must be within a certain range. Using a range carries two main benefits. One is that it produces shorter code that is easier to read and maintain. SELECT * FROM person WHERE age >= 18 AND age <= 65; SELECT * FROM person WHERE age IN 18..=65; Another benefit is performance. The following code should show a modest but measurable improvement in performance between the first and second `SELECT` statement, as only one condition needs to be checked instead of two. DELETE person; CREATE |person:20000| SET age = (rand::float() * 120).round() RETURN NONE; -- Assign output to a parameter so the SELECT output is not displayed LET $_ = SELECT * FROM person WHERE age > 18 AND age < 65; LET $_ = SELECT * FROM person WHERE age in 18..=65; Casting and functional usage[](https://surrealdb.com/docs/surrealql/datamodel/ranges#casting-and-functional-usage) ------------------------------------------------------------------------------------------------------------------- A range can be cast into an array. 1..3; Output[\ 1,\ 2\ ] This opens up a range of functional programming patterns that are made possible by SurrealDB’s [array functions](https://surrealdb.com/docs/surrealql/functions/database/array) , many of which can use [anonymous functions](https://surrealdb.com/docs/surrealql/datamodel/closures) (closures) to perform an operation on each item in the array. -- Construct an array ( 1..=100) -- Turn it into an array that increments by 10 .map(|$v| $v * 10) -- Turn each number into a object with original and square root value .map(|$v| { original: $v, square_root: math::sqrt($v) }) -- Keep only those with square roots in between 11 and 12 .filter(|$obj| $obj.square_root IN 11..12); Output[\ {\ original: 130,\ square_root: 11.40175425099138f\ },\ {\ original: 140,\ square_root: 11.832159566199232f\ }\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/ranges.mdx) Copy page as Markdown [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) ### On this page * [Ranges in FOR loops](https://surrealdb.com/docs/surrealql/datamodel/ranges#ranges-in-for-loops) * [Ranges in WHERE clauses](https://surrealdb.com/docs/surrealql/datamodel/ranges#ranges-in-where-clauses) * [Casting and functional usage](https://surrealdb.com/docs/surrealql/datamodel/ranges#casting-and-functional-usage) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/ranges.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Regex | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) Regex[](https://surrealdb.com/docs/surrealql/datamodel/regex#regex) ==================================================================== Available since: v3.0.0-alpha.1 A `regex` can be created by casting from a string. The following examples all return `true`. -- Either 'a' or 'b' "a|b" = "a"; -- Either color or colour "col(o|ou)r" = "colour"; -- Case-insensitive match on English color, colour, or French couleur "((?i)col(o|ou)r|couleur)" = "COULEUR"; While `regex` was added as a standalone type in version 2.3.0, regex matching has always been available via the [`string::matches()`](https://surrealdb.com/docs/surrealql/functions/database/string#stringmatches) function. string::matches("a", "a|b"); string::matches("colour", "col(o|ou)r"); string::matches("COULEUR", "((?i)col(o|ou)r|couleur)"); [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/regex.mdx) Copy page as Markdown [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/regex.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # None and Null | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) None and null[](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#none-and-null) ============================================================================================ SurrealDB uses two types called `None` and `Null` to represent two different ways in which data may not exist. While these may appear similar, they have different meanings and are used in different contexts. None values[](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#none-values) ---------------------------------------------------------------------------------------- `None` is used to denote that “something does not exist”, for example, a field which is not present on a record. Because of this, values of `None` can not be stored within records, meaning uses of `None` are typically limited to SurrealQL statements where it is used to denote a value or response that does not exist. ### Example[](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#example) Setting a record field to `None` is analogous to using `UNSET` to remove the field entirely. UPDATE person SET children = NONE; While it may appear that `None` is being written to the `children` field, what is actually happening is that the `children` field is being removed from the record. SELECT * FROM person; -- Returns { id: person:2cz8rj0dc4tktxlkjquc } Null values[](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#null-values) ---------------------------------------------------------------------------------------- `Null` values are used to denote that “something exists, but has no value”. This is useful when a field is present on a record, but the value of that field is unknown or not applicable. Unlike `None`, `Null` is written into records and can be stored as a value. ### Example[](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#example-1) Setting a record field to `Null` will create the field on the record, but denotes that the field is considered empty. UPDATE person SET children = null; In this example, the `children` field is present on the record, but the value of that field is `null`. SELECT * FROM person; -- Returns { id: person:2cz8rj0dc4tktxlkjquc, children: null } When to use None or Null[](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#when-to-use-none-or-null) ------------------------------------------------------------------------------------------------------------------ How you use `None` or `Null` is largely dependent on the context in which you are working. If you are writing SurrealQL and need to denote something that does not exist, such as the absence of a field, use `None`. If you are working with data and need to represent a value which is empty, use `Null`. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/none-and-null.mdx) Copy page as Markdown [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) ### On this page * [None and null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#none-and-null) * [None values](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#none-values) * [Example](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#example) * [Null values](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#null-values) * [Example](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#example-1) * [When to use None or Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null#when-to-use-none-or-null) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/none-and-null.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE BUCKET statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) DEFINE BUCKET statement[](https://surrealdb.com/docs/surrealql/statements/define/bucket#define-bucket-statement) ================================================================================================================= Available since: v3.0.0-alpha.1 > Note > > The `DEFINE BUCKET` statement is currently experimental and subject to change. To use this feature, please ensure you are on the latest supported alpha version of SurrealDB. To enable it, either pass `--allow-experimental files` when [starting the database](https://surrealdb.com/docs/surrealdb/cli/start) > or set the `SURREAL_CAPS_ALLOW_EXPERIMENTAL` environment variable to `files`. The `DEFINE BUCKET` statement lets you create a bucket that can hold files. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/bucket#statement-syntax) --------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE BUCKET [ OVERWRITE | IF NOT EXISTS ] @name [ @backend ] PERMISSIONS @expression [ COMMENT @string ] Example usage[](https://surrealdb.com/docs/surrealql/statements/define/bucket#example-usage) --------------------------------------------------------------------------------------------- A bucket backend can be set as “memory” for non-persistent in-memory storage, or as “file:/”, followed by the path, for storage on disk. ### Memory backend[](https://surrealdb.com/docs/surrealql/statements/define/bucket#memory-backend) The simplest way to experiment with a bucket for files is by using the memory backend: DEFINE BUCKET my_bucket BACKEND "memory"; Once this is defined, `my_bucket` can be accessed by using a file pointer: a path prefixed by an `f`. -- Create a file by adding some content f"my_bucket:/my_book.txt".put("Once there were four children whose names were Peter, Susan, Edmund, and Lucy."); -- Copy it to a new file name f"my_bucket:/my_book.txt".copy("lion_witch_wardrobe.txt"); -- Read the file as bytes f"my_bucket:/lion_witch_wardrobe.txt".get(); -- Cast the bytes to a string f"my_bucket:/lion_witch_wardrobe.txt".get(); Output-------- Query -------- b"4F6E6365207468657265207765726520666F7572206368696C6472656E2077686F7365206E616D657320776572652050657465722C20537573616E2C2045646D756E642C20616E64204C7563792E" -------- Query -------- 'Once there were four children whose names were Peter, Susan, Edmund, and Lucy.' ### File backend[](https://surrealdb.com/docs/surrealql/statements/define/bucket#file-backend) A file backend can be chosen for a bucket by typing `"file:"` and then the rest of the path, if necessary. DEFINE BUCKET my_bucket BACKEND "file:/some_directory"; DEFINE BUCKET my_bucket BACKEND "file:/some_directory"; A check will then be made to see if the `SURREAL_FILE_ALLOWLIST` environment variable contains the path, without which the following error will be generated. 'Bucket backend not supported: Path not allowed' The following command can be used to start running an instance in which a bucket with a file backend can be defined. # Unix SURREAL_FILE_ALLOWLIST="/" surreal start --user root --pass secret --allow-experimental files # Windows (PowerShell) $env:SURREAL_FILE_ALLOWLIST = "/" surreal start --user root --pass secret --allow-experimental files ### Global backend[](https://surrealdb.com/docs/surrealql/statements/define/bucket#global-backend) A global backend can also be selected, allowing all namespaces and databases access to the same file storage. If no backend is selected, the database will search for the environment variable `SURREAL_GLOBAL_BUCKET` and assign this as the global bucket. In this case, files will have a `namespace/database` prefix added (e.g. `my_global_bucket:/test_ns/test_db/somefile.txt`). A second `SURREAL_GLOBAL_BUCKET_ENFORCED` environment variable can also be used, which when set to `true` will enforce usage of the global bucket. If a global backend is set, then a `DEFINE BUCKET` statement can be as short as `DEFINE BUCKET` plus its local name, as the rest of the logic is done via environment variables. DEFINE BUCKET my_bucket; -- Writes to e.g. `my_global_bucket:/test_ns/test_db/my_bucket/my_book.txt` f"my_bucket:/my_book.txt".put("Once there were four children whose names were Peter, Susan, Edmund, and Lucy."); [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/bucket.mdx) Copy page as Markdown [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) ### On this page * [DEFINE BUCKET statement](https://surrealdb.com/docs/surrealql/statements/define/bucket#define-bucket-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/bucket#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/bucket#example-usage) * [Memory backend](https://surrealdb.com/docs/surrealql/statements/define/bucket#memory-backend) * [File backend](https://surrealdb.com/docs/surrealql/statements/define/bucket#file-backend) * [Global backend](https://surrealdb.com/docs/surrealql/statements/define/bucket#global-backend) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/bucket.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Numbers | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) Numbers[](https://surrealdb.com/docs/surrealql/datamodel/numbers#numbers) ========================================================================== In SurrealDB, numbers can be one of three types: 64-bit integers, 64-bit floating point numbers, or 128-bit decimal numbers. Integer numbers[](https://surrealdb.com/docs/surrealql/datamodel/numbers#integer-numbers) ------------------------------------------------------------------------------------------ If a numeric value is specified without a decimal point and is within the range `-9223372036854775808` to `9223372036854775807` then the value will be parsed, stored, and treated as a 64-bit integer. CREATE event SET year = 2022; Floating point numbers[](https://surrealdb.com/docs/surrealql/datamodel/numbers#floating-point-numbers) -------------------------------------------------------------------------------------------------------- If a number value is specified with a decimal point, or is outside of the maximum range specified above, then the number will automatically be parsed, stored, and treated as a 64-bit floating point value. This ensures efficiency when performing mathematical calculations within SurrealDB. CREATE event SET temperature = 41.5; Decimal numbers[](https://surrealdb.com/docs/surrealql/datamodel/numbers#decimal-numbers) ------------------------------------------------------------------------------------------ To opt into 128-bit decimal numbers when specifying numeric values, you can use the `dec` suffix. CREATE product SET price = 99.99dec; The `dec` suffix is an instruction to the parser and not a cast, and is thus preferred when making a decimal. -- Creates the imprecise float 3.888888888888889 and casts it into a decimal as 3.888888888888889dec RETURN 3.8888888888888888; -- Uses the input 3.8888888888888888 to directly create a decimal RETURN 3.8888888888888888dec; Using a specific numeric type[](https://surrealdb.com/docs/surrealql/datamodel/numbers#using-a-specific-numeric-type) ---------------------------------------------------------------------------------------------------------------------- To use a specific type when specifying numeric values, you can cast the value to a specific numeric type or use the appropriate suffix. CREATE event SET year = 2022, temperature = 41.5 + 5f, horizon = 31 + 3dec ; Numeric precision[](https://surrealdb.com/docs/surrealql/datamodel/numbers#numeric-precision) ---------------------------------------------------------------------------------------------- Different numeric types can be compared and used together in calculations. The benefits of floating point numeric values are speed and storage size, but there is a limit to the numeric precision. RETURN 13.5719384719384719385639856394139476937756394756; 13.571938471938473 In addition, when using floating point numbers specifically, mathematical operations can result in a loss of precision (as is normal with other databases). RETURN 0.3 + 0.3 + 0.3 + 0.1; 1.0000000000000002 Common rounding errors can be avoided by performing calculations using decimals. RETURN 0.3dec + 0.3dec + 0.3dec + 0.1dec; 1.0 Mathematical constants[](https://surrealdb.com/docs/surrealql/datamodel/numbers#mathematical-constants) -------------------------------------------------------------------------------------------------------- A set of floating point numeric constants are available in SurrealDB. Constant names are case insensitive, and can be specified with either lowercase or capital letters, or a mixture of both. CREATE circle SET radius = circumference / ( 2 * MATH::PI ); | Constant | | Description | | Value | | | --- | --- | --- | | `MATH::E` | | Euler’s number (e) | | 2.718281828459045 | | | `MATH::FRAC_1_PI` | | 1/π | | 0.3183098861837907 | | | `MATH::FRAC_1_SQRT_2` | | 1/sqrt(2) | | 0.7071067811865476 | | | `MATH::FRAC_2_PI` | | 2/π | | 0.6366197723675814 | | | `MATH::FRAC_2_SQRT_PI` | | 2/sqrt(π) | | 1.1283791670955126 | | | `MATH::FRAC_PI_2` | | π/2 | | 1.5707963267948966 | | | `MATH::FRAC_PI_3` | | π/3 | | 1.0471975511965979 | | | `MATH::FRAC_PI_4` | | π/4 | | 0.7853981633974483 | | | `MATH::FRAC_PI_6` | | π/6 | | 0.5235987755982989 | | | `MATH::FRAC_PI_8` | | π/8 | | 0.39269908169872414 | | | `MATH::INF` | | Positive infinity | | inf | | | `MATH::LN_10` | | ln(10) | | 2.302585092994046 | | | `MATH::LN_2` | | ln(2) | | 0.6931471805599453 | | | `MATH::LOG10_2` | | log10(2) | | 0.3010299956639812 | | | `MATH::LOG10_E` | | log10(e) | | 0.4342944819032518 | | | `MATH::LOG2_10` | | log2(10) | | 3.321928094887362 | | | `MATH::LOG2_E` | | log2(e) | | 1.4426950408889634 | | | `MATH::NEG_INF` | | Negative infinity | | \-inf | | | `MATH::PI` | | Archimedes’ constant (π) | | 3.141592653589793 | | | `MATH::SQRT_2` | | sqrt(2) | | 1.4142135623730951 | | | `MATH::TAU` | | The full circle constant (τ) | | 6.283185307179586 | | Next steps[](https://surrealdb.com/docs/surrealql/datamodel/numbers#next-steps) -------------------------------------------------------------------------------- You’ve now seen how to use numeric values in SurrealDB. For more advanced functionality, take a look at the operators and math functions, which enable advanced calculations on numeric values and sets of numeric values. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/numbers.mdx) Copy page as Markdown [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) ### On this page * [Integer numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers#integer-numbers) * [Floating point numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers#floating-point-numbers) * [Decimal numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers#decimal-numbers) * [Using a specific numeric type](https://surrealdb.com/docs/surrealql/datamodel/numbers#using-a-specific-numeric-type) * [Numeric precision](https://surrealdb.com/docs/surrealql/datamodel/numbers#numeric-precision) * [Mathematical constants](https://surrealdb.com/docs/surrealql/datamodel/numbers#mathematical-constants) * [Next steps](https://surrealdb.com/docs/surrealql/datamodel/numbers#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/numbers.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE FUNCTION statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) `DEFINE FUNCTION` statement[](https://surrealdb.com/docs/surrealql/statements/define/function#define-function-statement) ========================================================================================================================= The `DEFINE FUNCTION` statement allows you to define custom functions that can be reused throughout a database. When using the `DEFINE FUNCTION` statement, you can define a function that takes one or more arguments and returns a value. You can then call this function in other SurrealQL statements. Functions can be used to encapsulate logic that you want to reuse in multiple queries. They can also be used to simplify complex queries by breaking them down into smaller, more manageable pieces. The are particularly useful when you have a complex query that you need to run multiple times with different arguments. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/function#requirements) --------------------------------------------------------------------------------------------- * You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the `DEFINE FUNCTION` statement. * [You must select your namespace and database](https://surrealdb.com/docs/surrealql/statements/use) before you can use the `DEFINE FUNCTION` statement. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/function#statement-syntax) ----------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE FUNCTION [ OVERWRITE | IF NOT EXISTS ] fn::@name( [ @argument: @type ... ] ) { [ @query ... ] [ RETURN @returned ] } [ COMMENT @string ] [ PERMISSIONS [ NONE | FULL | WHERE @condition]] Example usage[](https://surrealdb.com/docs/surrealql/statements/define/function#example-usage) ----------------------------------------------------------------------------------------------- Below shows how you can define a custom function using the `DEFINE FUNCTION` statement, and how to call it. -- It is necessary to prefix the name of your function with "fn::" -- This indicates that it's a custom function DEFINE FUNCTION fn::greet($name: string) { RETURN "Hello, " + $name + "!"; } -- Returns: "Hello, Tobie!" RETURN fn::greet("Tobie"); To showcase a slightly more complex custom function, this will check if a relation between two nodes exists: -- Define a function that checks if a relation exists between two nodes DEFINE FUNCTION fn::relation_exists( $in: record, $tb: string, $out: record ) { -- Check if a relation exists between the two nodes. LET $results = SELECT VALUE id FROM type::table($tb) WHERE in = $in AND out = $out; -- Return true if a relation exists, false otherwise RETURN array::len($results) > 0; }; Optional arguments[](https://surrealdb.com/docs/surrealql/statements/define/function#optional-arguments) --------------------------------------------------------------------------------------------------------- If one or more ending arguments have the `option` type, they can be omitted when you run the invoke the function. DEFINE FUNCTION fn::last_option($required: number, $optional: option) { RETURN { required_present: type::is::number($required), optional_present: type::is::number($optional), } }; RETURN fn::last_option(1, 2); { required_present: true, optional_present: true, }; RETURN fn::last_option(1); { required_present: true, optional_present: false, }; Recursive functions[](https://surrealdb.com/docs/surrealql/statements/define/function#recursive-functions) ----------------------------------------------------------------------------------------------------------- A function is able to call itself, making it a recursive function. One example of a recursive function is the one below which creates a relation between each and every record passed in. Consider a situation in which seven person records exist. First, `person:1` will need to be related to the rest of the `person` records, after which there are no more relations to create for it. Following this, the relations for `person:2` and all the other records except for `person:1` will need to be created, and so on. This can be done in a recursive function by creating all the relations between the first record and the remaining records, after which the function calls itself by passing in all the records except the first. This continues until the function receives less than two records, in which case it ceases calling itself by doing nothing, thereby ending the recursion. DEFINE FUNCTION fn::relate_all($records: array) { IF $records.len() < 2 { -- Don't do anything, ending the recursion } ELSE { LET $first = $records[0]; LET $remainder = $records[1..]; FOR $counterpart IN $remainder { RELATE $first->to->$counterpart; }; fn::relate_all($remainder); } }; CREATE |person:1..7|; fn::relate_all(SELECT VALUE id FROM person); SELECT id, ->to->? FROM person; The last query [can be viewed graphically](https://surrealdb.com/blog/whats-new-in-surrealist-3-2#graph-visualisation) inside Surrealist, leading to an output showing a seven-pointed star. ![An image of a seven-pointed star created visually by relating seven records to each other and displayed inside Surrealist's graph view.](https://surrealdb.com/docs/_astro/recursive_star.DWuwdsd5_Z1ntHDY.webp) Permissions[](https://surrealdb.com/docs/surrealql/statements/define/function#permissions) ------------------------------------------------------------------------------------------- You can set the permissions for a custom function using the `PERMISSIONS` clause. The `PERMISSIONS` clause is mostly used to restrict who can access a function and what data they can access. It can be set to `NONE`, `FULL`, or `WHERE @condition`. * `FULL`: When Full permissions are granted [record](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) users have access to the function. This is the default permission when not specified. * `NONE`: When this permission is granted, [record](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) users have no access to the defined function. * `WHERE @condition`: Permissions are granted to the function based on the specified condition. > Note > > The examples below use the [`Surreal Deal Store`](https://surrealdb.com/docs/surrealql/demo#surreal-deal-store---there-is-a-lot-in-store-for-you-recommended) > dataset. ### Using the `FULL` permission[](https://surrealdb.com/docs/surrealql/statements/define/function#using-the-full-permission) The `FULL` permission grants all users access to the function. The following example defines a function that fetches all products from the `product` table and grants the function full permissions to access the data to all users. ### Using the `NONE` permission[](https://surrealdb.com/docs/surrealql/statements/define/function#using-the-none-permission) The `NONE` permission denies all [record](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) users access to the function. The following example defines a function that fetches all products from the `product` table -- Define a function that fetches all expiration years from the payment_details table and denies access to all none-admin users DEFINE FUNCTION fn::fetchAllPaymentDetails() { RETURN (SELECT stored_cards.expiry_year FROM payment_details LIMIT 5); } PERMISSIONS NONE; RETURN fn::fetchAllPaymentDetails(); ### Using the `WHERE` clause[](https://surrealdb.com/docs/surrealql/statements/define/function#using-the-where-clause) The `WHERE` clause allows you to specify a condition that determines the permissions granted to the function. The condition must evaluate to a boolean value. If the condition evaluates to `true`, the function is granted permissions. If the condition evaluates to `false`, the function is not granted permissions. -- Define a function that fetches all products with the condition that only admin users can access it DEFINE FUNCTION fn::fetchAllProducts() { RETURN (SELECT * FROM product LIMIT 10); } PERMISSIONS WHERE $auth.admin = true; Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/function#using-if-not-exists-clause) --------------------------------------------------------------------------------------------------------------------------- Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define a function only if it does not already exist. You should use the `IF NOT EXISTS` clause when defining a function in SurrealDB if you want to ensure that the function is only created if it does not already exist. If the function already exists, the `DEFINE FUNCTION` statement will return an error. It’s particularly useful when you want to safely attempt to define a function without manually checking its existence first. On the other hand, you should not use the `IF NOT EXISTS` clause when you want to ensure that the function definition is updated regardless of whether it already exists. In such cases, you might prefer using the `OVERWRITE` clause, which allows you to define a function and overwrite an existing one if it already exists, ensuring that the latest version of the function definition is always in use -- Create a FUNCTION if it does not already exist DEFINE FUNCTION IF NOT EXISTS fn::example() {}; Using `OVERWRITE` clause[](https://surrealdb.com/docs/surrealql/statements/define/function#using-overwrite-clause) ------------------------------------------------------------------------------------------------------------------- Available since: v2.0.0 The `OVERWRITE` clause can be used to define a function and overwrite an existing one if it already exists. You should use the `OVERWRITE` clause when you want to modify an existing user definition. If the user already exists, the `DEFINE FUNCTION` statement will overwrite the existing definition with the new one. -- Create an FUNCTION and overwrite if it already exists DEFINE FUNCTION OVERWRITE fn::example {}; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/function.mdx) Copy page as Markdown [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) ### On this page * [DEFINE FUNCTION statement](https://surrealdb.com/docs/surrealql/statements/define/function#define-function-statement) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/function#requirements) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/function#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/function#example-usage) * [Optional arguments](https://surrealdb.com/docs/surrealql/statements/define/function#optional-arguments) * [Recursive functions](https://surrealdb.com/docs/surrealql/statements/define/function#recursive-functions) * [Permissions](https://surrealdb.com/docs/surrealql/statements/define/function#permissions) * [Using the FULL permission](https://surrealdb.com/docs/surrealql/statements/define/function#using-the-full-permission) * [Using the NONE permission](https://surrealdb.com/docs/surrealql/statements/define/function#using-the-none-permission) * [Using the WHERE clause](https://surrealdb.com/docs/surrealql/statements/define/function#using-the-where-clause) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/function#using-if-not-exists-clause) * [Using OVERWRITE clause](https://surrealdb.com/docs/surrealql/statements/define/function#using-overwrite-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/function.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # SPLIT clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) `SPLIT` clause[](https://surrealdb.com/docs/surrealql/clauses/split#split-clause) ================================================================================== The `SPLIT` clause in SurrealQL is used to split the results of a query based on a specific field, particularly when dealing with arrays. This is useful in scenarios where you want to treat each element of an array as a separate row in the result set. It can be particularly helpful in data analysis contexts where you need to work with individual elements of an array separately. Syntax[](https://surrealdb.com/docs/surrealql/clauses/split#syntax) -------------------------------------------------------------------- Clause SyntaxSPLIT [ON] @field Suppose you have a user table with a field emails that contains an array of email addresses for each user. You want to list each email address as a separate record. Here’s how you can use the SPLIT clause in SurrealQL: CREATE user SET name = "John Doe", emails = ["john@example.com", "doe@example.com"]; -- Split the results by each value in the emails array SELECT * FROM user SPLIT emails; Explanation: * `CREATE user SET ...`: This creates a user record with a name and an array of email addresses. * `SELECT * FROM user SPLIT emails`: This query selects all fields from the user table and splits the results based on the emails field. Each email address in the `emails` array will now be in a field of the same name that only contains a single value. Output: The output of the query will be: [\ {\ emails: 'john@example.com',\ id: user:unjgil312jvvxfbdj706,\ name: 'John Doe'\ },\ {\ emails: 'doe@example.com',\ id: user:unjgil312jvvxfbdj706,\ name: 'John Doe'\ }\ ] Using `SPLIT` to restructure collected paths[](https://surrealdb.com/docs/surrealql/clauses/split#using-split-to-restructure-collected-paths) ---------------------------------------------------------------------------------------------------------------------------------------------- One practical use case with `SPLIT` is returning every possible combination of the relations inside multiple graph paths. For instance, take the following data below that represents the relations between Canada the country, its provinces, and their cities. CREATE country:canada; CREATE province:bc, province:alberta; CREATE city:vancouver, city:victoria, city:edmonton, city:calgary; RELATE [city:vancouver, city:victoria]->in->province:bc; RELATE [city:edmonton, city:calgary]->in->province:alberta; RELATE [province:bc, province:alberta]->in->country:canada; A graph query on both of these paths shows all of the provinces and cities. SELECT id AS country, <-in<-province AS provinces, <-in<-province<-in<-city AS cities FROM ONLY country:canada; Output{ cities: [\ city:calgary,\ city:edmonton,\ city:vancouver,\ city:victoria\ ], country: country:canada, provinces: [\ province:alberta,\ province:bc\ ] } Using `SPLIT` in this case transforms the output from a collection of paths centred on the `country:canada` record into an array of objects, each representing every possible combination of cities and provinces inside the country. SELECT id AS country, <-in<-province AS province, <-in<-province<-in<-city AS city FROM country:canada SPLIT city, province; Output[\ {\ city: city:calgary,\ country: country:canada,\ province: province:alberta\ },\ {\ city: city:calgary,\ country: country:canada,\ province: province:bc\ },\ {\ city: city:edmonton,\ country: country:canada,\ province: province:alberta\ },\ {\ city: city:edmonton,\ country: country:canada,\ province: province:bc\ },\ {\ city: city:vancouver,\ country: country:canada,\ province: province:alberta\ },\ {\ city: city:vancouver,\ country: country:canada,\ province: province:bc\ },\ {\ city: city:victoria,\ country: country:canada,\ province: province:alberta\ },\ {\ city: city:victoria,\ country: country:canada,\ province: province:bc\ }\ ] An example of the same query then mapped into a set of unique keys for serialization: (SELECT id, <-in<-province AS province, <-in<-province<-in<-city AS city FROM country:canada SPLIT city, province) .map(|$obj| $obj.id.id() + '|' + $obj.province.id() + '|' + $obj.city.id() ); Output[\ 'canada|alberta|calgary',\ 'canada|bc|calgary',\ 'canada|alberta|edmonton',\ 'canada|bc|edmonton',\ 'canada|alberta|vancouver',\ 'canada|bc|vancouver',\ 'canada|alberta|victoria',\ 'canada|bc|victoria'\ ] [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/split.mdx) Copy page as Markdown [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) ### On this page * [SPLIT clause](https://surrealdb.com/docs/surrealql/clauses/split#split-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/split#syntax) * [Using SPLIT to restructure collected paths](https://surrealdb.com/docs/surrealql/clauses/split#using-split-to-restructure-collected-paths) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/split.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # FROM clause | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Clauses](https://surrealdb.com/docs/surrealql/clauses) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) `FROM` clause[](https://surrealdb.com/docs/surrealql/clauses/from#from-clause) =============================================================================== The `FROM` clause is used to specify the table or view to query. It can also be used to specify targets beyond just a single table or record name. Syntax[](https://surrealdb.com/docs/surrealql/clauses/from#syntax) ------------------------------------------------------------------- Clause SyntaxSTATEMENT [FROM [ONLY] @targets;] Data retrieval[](https://surrealdb.com/docs/surrealql/clauses/from#data-retrieval) ----------------------------------------------------------------------------------- One of the most common use cases for the `FROM` clause is to specify the table or view to query. You can use this clause to pull data from single or multiple tables. All the ways you can use the FROM clause-- Selects all records from both 'user' and 'admin' tables. SELECT * FROM user, admin; -- Selects all records from the table named in the variable '$table', -- but only if the 'admin' field of those records is true. -- Equivalent to 'SELECT * FROM user WHERE admin = true'. LET $table = "user"; SELECT * FROM type::table($table) WHERE admin = true; -- Selects a single record from: -- * the table named in the variable '$table', -- * and the identifier named in the variable '$id'. -- This query is equivalent to 'SELECT * FROM user:admin'. LET $table = "user"; LET $id = "admin"; SELECT * FROM type::thing($table, $id); -- Selects all records for specific users 'tobie' and 'jaime', -- as well as all records for the company 'surrealdb'. SELECT * FROM user:tobie, user:jaime, company:surrealdb; -- Selects records from a list of identifiers. The identifiers can be numerical, -- string, or specific records such as 'person:lrym5gur8hzws72ux5fa'. SELECT * FROM [3648937, "test", person:lrym5gur8hzws72ux5fa, person:4luro9170uwcv1xrfvby]; -- Selects data from an object that includes a 'person' key, -- which is associated with a specific person record, and an 'embedded' key set to true. SELECT * FROM { person: person:lrym5gur8hzws72ux5fa, embedded: true }; -- This command first performs a subquery, which selects all 'user' records and adds a -- computed 'adult' field that is true if the user's 'age' is 18 or older. -- The main query then selects all records from this subquery where 'adult' is true. SELECT * FROM (SELECT age >= 18 AS adult FROM user) WHERE adult = true; ### Using the `ONLY` keyword[](https://surrealdb.com/docs/surrealql/clauses/from#using-the-only-keyword) The `ONLY` keyword can be used to specify that only the specified targets should be retrieved. This is useful when you want to retrieve data from a single table or view. The `ONLY` keyword can be used in conjunction with the `LIMIT` clause to specify that only the specified number of records should be retrieved. This keyword can be particularly useful with SDKs as it can guaranteed to have just a single object and that makes it nicer to deserialise. Using the ONLY keyword-- Select only the 'user' table. SELECT * FROM ONLY user:one ; -- Selects only the first 10 records from the 'user' table. SELECT * FROM ONLY user LIMIT 10; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/from.mdx) Copy page as Markdown [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) ### On this page * [FROM clause](https://surrealdb.com/docs/surrealql/clauses/from#from-clause) * [Syntax](https://surrealdb.com/docs/surrealql/clauses/from#syntax) * [Data retrieval](https://surrealdb.com/docs/surrealql/clauses/from#data-retrieval) * [Using the ONLY keyword](https://surrealdb.com/docs/surrealql/clauses/from#using-the-only-keyword) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/clauses/from.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE EVENT statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) `DEFINE EVENT` statement[](https://surrealdb.com/docs/surrealql/statements/define/event#define-event-statement) ================================================================================================================ Events allow you to define custom logic that is executed when a record is created, updated, or deleted. These events are triggered automatically within the current transaction after data modifications in the record, giving you access to the state of the record [before `$before` and after `$after`](https://surrealdb.com/docs/surrealql/parameters#before-after) the change. > Note > > Events are a side effect of other operations and thus are not triggered when data is [imported](https://surrealdb.com/docs/surrealdb/cli/import) > . ### Key Concepts[](https://surrealdb.com/docs/surrealql/statements/define/event#key-concepts) * **Events**: Triggered after changes (create, update, delete) to records in a table. * **$event**: A preset parameter containing the type of event as a string, will always be one of “CREATE”, “UPDATE”, or “DELETE”. * **$before / $after**: Refer to the record state before and after the modification. Learn more about the `$before` and `$after` parameters in the [parameters documentation](https://surrealdb.com/docs/surrealql/parameters#before-after) . * **$value**: The record in question. For a `CREATE` or `UPDATE` event, this will be the record after the changes were made. For a `DELETE` statement, this will be the record before it was deleted. * **WHEN condition**: Determines when the event should be triggered. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/event#requirements) ------------------------------------------------------------------------------------------ * You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the `DEFINE EVENT` statement. * [You must select your namespace and database](https://surrealdb.com/docs/surrealql/statements/use) before you can use the `DEFINE EVENT` statement. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/event#statement-syntax) -------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE EVENT [ OVERWRITE | IF NOT EXISTS ] @name ON [ TABLE ] @table [ WHEN @expression ] THEN @expression [ COMMENT @string ] ### Clauses:[](https://surrealdb.com/docs/surrealql/statements/define/event#clauses) * **OVERWRITE**: Replaces the existing event if it already exists. * **IF NOT EXISTS**: Only creates the event if it doesn’t already exist. * **WHEN**: Conditional logic that controls whether the event is triggered. * **THEN**: Specifies the action(s) to execute when the event is triggered. * **COMMENT**: Optional comment for describing the event. Example usage[](https://surrealdb.com/docs/surrealql/statements/define/event#example-usage) -------------------------------------------------------------------------------------------- * **Email Change Detection**: Create an event that logs whenever a user’s email is updated. In this example: * The `WHEN` clause checks if the email has changed. * The `THEN` clause records this change in a `log` table. -- Create a new event whenever a user changes their email address -- One-statement event DEFINE EVENT OVERWRITE test ON TABLE user WHEN $before.email != $after.email THEN ( CREATE log SET user = $value.id, // Turn events like "CREATE" into string "email created" action = 'email' + ' ' + $event.lowercase() + 'd', // `email` field may be NONE, log as '' if so old_email = $before.email ?? '', new_email = $after.email ?? '', at = time::now() ); UPSERT user:test SET email = 'old_email@test.com'; UPSERT user:test SET email = 'new_email@test.com'; DELETE user:test; SELECT * FROM log ORDER BY at ASC; Output[\ {\ action: 'email created',\ at: d'2024-11-25T02:59:41.003Z',\ id: log:e3thw1l0q7xiapznar1f,\ new_email: 'old_email@test.com',\ old_email: '',\ user: user:test\ },\ {\ action: 'email updated',\ at: d'2024-11-25T02:59:41.003Z',\ id: log:uaarfyk191jgod06xobm,\ new_email: 'new_email@test.com',\ old_email: 'old_email@test.com',\ user: user:test\ },\ {\ action: 'email deleted',\ at: d'2024-11-25T02:59:41.003Z',\ id: log:mlkag8h1xotglpz9wt2i,\ new_email: '',\ old_email: 'new_email@test.com',\ user: user:test\ }\ ] ### More Complex Logic:[](https://surrealdb.com/docs/surrealql/statements/define/event#more-complex-logic) * **Purchase Event with Multiple Actions**: Log a purchase and establish relationships between the customer and product. DEFINE EVENT purchase_made ON TABLE purchase WHEN $before == NONE THEN { LET $customer = (SELECT * FROM customer WHERE id = $after.customer); LET $product = (SELECT * FROM product WHERE id = $after.product); RELATE $customer->bought->$product CONTENT { quantity: $after.quantity, total: $after.total, status: 'Pending', }; CREATE log SET customer_id = $after.customer, product_id = $after.product, action = 'purchase_created', timestamp = time::now(); }; In this example: * We perform multiple actions when a purchase is created: establishing relationships using the [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) statement and creating a log entry. Specific events[](https://surrealdb.com/docs/surrealql/statements/define/event#specific-events) ------------------------------------------------------------------------------------------------ You can trigger events based on specific events. You can use the variable $event to detect what type of event is triggered on the table. -- CREATE event is triggered when a new record is inserted into the table. -- Here we are updating the status of the post to PUBLISHED -- when a new record is inserted into the publish_post table. DEFINE EVENT publish_post ON TABLE publish_post WHEN $event = "CREATE" THEN ( UPDATE post SET status = "PUBLISHED" WHERE id = $after.post_id ); -- UPDATE event -- Here we are creating a notification when a user is updated. DEFINE EVENT user_updated ON TABLE user WHEN $event = "UPDATE" THEN ( CREATE notification SET message = "User updated", user_id = $after.id, created_at = time::now() ); -- DELETE event is triggered when a record is deleted from the table. -- Here we are creating a notification when a user is deleted. DEFINE EVENT user_deleted ON TABLE user WHEN $event = "DELETE" THEN ( CREATE notification SET message = "User deleted", user_id = $before.id, created_at = time::now() ); -- You can combine multiple events based on your use cases. -- Here we are creating a log when a user is created, updated or deleted. DEFINE EVENT user_event ON TABLE user WHEN $event = "CREATE" OR $event = "UPDATE" OR $event = "DELETE" THEN ( CREATE log SET table = "user", event = $event, happened_at = time::now() ); Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/event#using-if-not-exists-clause) ------------------------------------------------------------------------------------------------------------------------ Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define an event only if it does not already exist. You should use the `IF NOT EXISTS` clause when defining an event in SurrealDB if you want to ensure that the event is only created if it does not already exist. If the event already exists, the `DEFINE EVENT` statement will return an error. It’s particularly useful when you want to safely attempt to define a event without manually checking its existence first. On the other hand, you should not use the `IF NOT EXISTS` clause when you want to ensure that the event definition is updated regardless of whether it already exists. In such cases, you might prefer using the `OVERWRITE` clause, which allows you to define a event and overwrite an existing one if it already exists, ensuring that the latest version of the event definition is always in use -- Create a EVENT if it does not already exist DEFINE EVENT IF NOT EXISTS example ON example THEN {}; Using `OVERWRITE` clause[](https://surrealdb.com/docs/surrealql/statements/define/event#using-overwrite-clause) ---------------------------------------------------------------------------------------------------------------- Available since: v2.0.0 The `OVERWRITE` clause can be used to define an event and overwrite an existing one if it already exists. You should use the `OVERWRITE` clause when you want to modify an existing event definition. If the event already exists, the `DEFINE EVENT` statement will overwrite the existing event definition with the new one. -- Create an EVENT and overwrite if it already exists DEFINE EVENT OVERWRITE example ON example THEN {}; Events and permissions[](https://surrealdb.com/docs/surrealql/statements/define/event#events-and-permissions) -------------------------------------------------------------------------------------------------------------- Queries inside the event always execute without any permission checks, even when triggered by changes made by the currently authenticated user. This can be very useful to perform additional checks and changes that involve tables/records that are inaccessible for the user. Consider a CREATE query sent by a record user that has CREATE access to the `comment` table only: CREATE comment SET post = post:tomatosoup, content = "So delicious!", author = $auth.id ; By having the following event defined, SurrealDB will perform the additional checks and changes: DEFINE EVENT on_comment_created ON TABLE comment WHEN $event = "CREATE" THEN { -- Check if the post allows for adding comments. -- User record doesn't have access to the `post` table. IF $after.post.disable_comments { THROW "Can't create a comment - Comments are disabled for this post"; }; -- Set the `approved` field on the new comment - automatically approve -- comments made by the author of the post. -- For security reasons, record users don't have any permissions for the `approved` field. UPDATE $after.id SET approved = $after.post.author == $after.author; }; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/event.mdx) Copy page as Markdown [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) ### On this page * [DEFINE EVENT statement](https://surrealdb.com/docs/surrealql/statements/define/event#define-event-statement) * [Key Concepts](https://surrealdb.com/docs/surrealql/statements/define/event#key-concepts) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/event#requirements) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/event#statement-syntax) * [Clauses:](https://surrealdb.com/docs/surrealql/statements/define/event#clauses) * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/event#example-usage) * [More Complex Logic:](https://surrealdb.com/docs/surrealql/statements/define/event#more-complex-logic) * [Specific events](https://surrealdb.com/docs/surrealql/statements/define/event#specific-events) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/event#using-if-not-exists-clause) * [Using OVERWRITE clause](https://surrealdb.com/docs/surrealql/statements/define/event#using-overwrite-clause) * [Events and permissions](https://surrealdb.com/docs/surrealql/statements/define/event#events-and-permissions) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/event.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Strings | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) Strings[](https://surrealdb.com/docs/surrealql/datamodel/strings#strings) ========================================================================== Strings can be used to store text values. All string values can include Unicode values, emojis, tab characters, and line breaks. CREATE person SET text = 'Lorem ipsum dolor sit amet'; Strings can be created using single quotation marks, or double quotation marks. CREATE person SET text = "Lorem ipsum dolor sit amet"; Any string in SurrealDB can include Unicode text. CREATE person SET text = "I ❤️ SurrealDB"; Strings can also include line breaks. CREATE person SET text = "This is over multiple lines"; Specifying data type literal values using string prefixes[](https://surrealdb.com/docs/surrealql/datamodel/strings#specifying-data-type-literal-values-using-string-prefixes) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Available since: v1.1.0 ### Overview[](https://surrealdb.com/docs/surrealql/datamodel/strings#overview) In SurrealQL, there are several data types for which literal values are specified using string values, with a prefix indicating the intended type for the value to be interpreted as. Previously, in SurrealQL version `1.0`, literal values of these types were simply specified using a string without any prefix, and SurrealDB would eagerly convert the strings into the relevant data type in any case where the string matched the format expected for that type. However, since SurrealQL version `2.0`, strings are no longer eagerly converted into other data types. Instead, if you want to specify a literal value of one of these data types, you must explicitly use a string with the appropriate prefix. ### String literal values using the `s` prefix[](https://surrealdb.com/docs/surrealql/datamodel/strings#string) The string prefix `s` explicitly tells the parser that the contents of the string are just a string. Since SurrealQL version `2.0`, all strings without a prefix will be plain strings. So the two versions of the same string with and wihout the `s` prefix in the following queries are equivalent: RETURN "5:20"; RETURN s"5:20"; RETURN "5:20" == s"5:20"; Response-------- Query 1 -------- '5:20' -------- Query 2 -------- '5:20' -------- Query 3 -------- true ### Record ID literal values using the `r` prefix[](https://surrealdb.com/docs/surrealql/datamodel/strings#record) The `r` prefix tells the parser that the contents of the string represent a [`record ID`](https://surrealdb.com/docs/surrealql/datamodel/ids) . The parser expects record IDs to have the following format: `table_name:record ID`. > Note > > As of SurrealDB 2.0, without the `r` prefix the type of the value will be `string`. Here is an example of a record ID literal value, specified using a string with the `r` prefix. RETURN r"person:john"; Response-------- Query 1 -------- person:john In the example below, using the [`type::is::string()`](https://surrealdb.com/docs/surrealql/functions/database/type#typeisstring) and [`type::is::record()`](https://surrealdb.com/docs/surrealql/functions/database/type#typeisrecord) functions respectively, you can check the type of the string. RETURN type::is::string("person:john"); RETURN type::is::record("person:john"); RETURN type::is::record(r"person:john"); Response-------- Query 1 -------- true -------- Query 2 -------- false -------- Query 3 -------- true ### Datetime literal values using the `d` prefix[](https://surrealdb.com/docs/surrealql/datamodel/strings#datetime) The `d` prefix tells the parser that the contents of the string represent a [`datetime`](https://surrealdb.com/docs/surrealql/datamodel/datetimes) . The parser expects `datetime` values to have a valid ISO 8601 format. Here are a few examples: RETURN d"2023-11-28T11:41:20.262Z"; --- Sub-second precision included, timezone defaulted to UTC RETURN d"2023-11-28T11:41:20.262+04:00"; --- Sub-second precision included, timezone specified as UTC + 4:00 RETURN d"2023-11-28T11:41:20.262-04:00"; --- Sub-second precision included, timezone specified as UTC - 4:00 RETURN d"2023-11-28T11:41:20Z"; --- Sub-second precision excluded, timezone defaulted to UTC RETURN d"2023-11-28T11:41:20+04:00"; --- Sub-second precision excluded, timezone specified as UTC + 4:00 Response-------- Query 1 -------- d'2023-11-28T11:41:20.262Z' -------- Query 2 -------- d'2023-11-28T07:41:20.262Z' -------- Query 3 -------- d'2023-11-28T15:41:20.262Z' -------- Query 4 -------- d'2023-11-28T11:41:20Z' -------- Query 5 -------- d'2023-11-28T07:41:20Z' ### UUID literal values with the `u` prefix[](https://surrealdb.com/docs/surrealql/datamodel/strings#uuid) The `u` prefix tells the parser that the contents of the string represent a [`uuid`](https://surrealdb.com/docs/surrealql/datamodel/uuid) . The parser expects `uuid` values to follow the format of an UUID, `ffffffff-ffff-ffff-ffff-ffffffffffff`, where each non-hyphen character can be a digit (0-9) or a letter between `a` and `f` (representing a single hexadecimal digit). RETURN u"8c54161f-d4fe-4a74-9409-ed1e137040c1"; Response-------- Query 1 -------- u'8c54161f-d4fe-4a74-9409-ed1e137040c1' ### Byte values using the `b` prefix[](https://surrealdb.com/docs/surrealql/datamodel/strings#bytes) b"0099aaff" ### File paths using the `f` prefix[](https://surrealdb.com/docs/surrealql/datamodel/strings#files) f"bucket:/some/key/to/a/file.txt"; f"bucket:/some/key/with\ escaped"; f"bucket:/some/key".put(b"00aa"); f"bucket:/some/key".get(); ### String prefixes vs. casting[](https://surrealdb.com/docs/surrealql/datamodel/strings#string-prefixes-vs-casting) String prefixes seem outwardly similar to casting, but differ in behaviour. A string prefix is an instruction to the parser to treat an input in a certain way, whereas a cast is an instruction to the database to convert one type into another. As a result, incorrect input with a cast will generate an error: // Change _ to - in both examples to fix the input RETURN "018f0e6a_9b95-7ecc-8a38-aea7bf3627dd"; RETURN "2024_06-06T12:00:00Z"; Response-------- Query 1 -------- "Expected a uuid but cannot convert '018f0e6a-9b95-7ecc-8a38-aea7bf3627d' into a uuid" -------- Query 2 -------- "Expected a datetime but cannot convert '2024-06-06T12:00:00' into a datetime" But the same input using a string prefix will not even parse until the input is valid. // Will not parse in either case until _ is changed to - RETURN u"018f0e6a_9b95-7ecc-8a38-aea7bf3627dd"; RETURN d"2024_06-06T12:00:00Z"; This also allows for immediate error messages on which part of the input is incorrect. As seen in the image below, the parser is able to inform the user that an underscore at column 18 is the issue. ![A screenshot showing how a string prefix allows incorrect UUID input to be identified before a query can be run. In this case, the parser is able to inform the user that an underscore at column 18 is the issue.](https://surrealdb.com/docs/_astro/surrealql-parse-error.g1VFFr3O_1ncJQv.webp) [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/strings.mdx) Copy page as Markdown [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) ### On this page * [Specifying data type literal values using string prefixes](https://surrealdb.com/docs/surrealql/datamodel/strings#specifying-data-type-literal-values-using-string-prefixes) * [Overview](https://surrealdb.com/docs/surrealql/datamodel/strings#overview) * [String literal values using the s prefix](https://surrealdb.com/docs/surrealql/datamodel/strings#string) * [Record ID literal values using the r prefix](https://surrealdb.com/docs/surrealql/datamodel/strings#record) * [Datetime literal values using the d prefix](https://surrealdb.com/docs/surrealql/datamodel/strings#datetime) * [UUID literal values with the u prefix](https://surrealdb.com/docs/surrealql/datamodel/strings#uuid) * [Byte values using the b prefix](https://surrealdb.com/docs/surrealql/datamodel/strings#bytes) * [File paths using the f prefix](https://surrealdb.com/docs/surrealql/datamodel/strings#files) * [String prefixes vs. casting](https://surrealdb.com/docs/surrealql/datamodel/strings#string-prefixes-vs-casting) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/strings.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # LET statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [LET](https://surrealdb.com/docs/surrealql/statements/let) `LET` Statement[](https://surrealdb.com/docs/surrealql/statements/let#let-statement) ===================================================================================== The `LET` statement allows you to create parameters to store any value, including the results of queries or the outputs of expressions. These parameters can then be referenced throughout your SurrealQL code, making your queries more dynamic and reusable. Syntax[](https://surrealdb.com/docs/surrealql/statements/let#syntax) --------------------------------------------------------------------- The syntax for the `LET` statement is straightforward. The parameter name is prefixed with a `$` symbol. SurrealQL SyntaxLET $@parameter [: @type_name] = @value; Example Usage[](https://surrealdb.com/docs/surrealql/statements/let#example-usage) ----------------------------------------------------------------------------------- ### Basic Parameter Assignment[](https://surrealdb.com/docs/surrealql/statements/let#basic-parameter-assignment) You can use the `LET` statement to store simple values or query results. For example, storing a string value and then using it in a `CREATE` statement: -- Define the parameter LET $name = "tobie"; -- Use the parameter CREATE person SET name = $name; ### Storing Query Results[](https://surrealdb.com/docs/surrealql/statements/let#storing-query-results) The `LET` statement is also useful for storing the results of a query, which can then be used in subsequent operations: -- Define the parameter LET $adults = SELECT * FROM person WHERE age > 18; -- Use the parameter UPDATE $adults SET adult = true; ### Conditional Logic with `IF ELSE`[](https://surrealdb.com/docs/surrealql/statements/let#conditional-logic-with-if-else) SurrealQL allows you to define parameters based on conditional logic using `IF ELSE` statements: LET $num = 10; LET $num_type = IF type::is::int($num) { "integer" } ELSE IF type::is::decimal($num) { "decimal" } ELSE IF type::is::float($num) { "float" }; Anonymous Functions[](https://surrealdb.com/docs/surrealql/statements/let#anonymous-functions) ----------------------------------------------------------------------------------------------- You can define anonymous functions also known as closures using the `LET` statement. These functions can be used to encapsulate reusable logic and can be called from within your queries. Learn more about [anonymous functions](https://surrealdb.com/docs/surrealql/datamodel/closures) in the Data model section. Pre-Defined and Protected Parameters[](https://surrealdb.com/docs/surrealql/statements/let#pre-defined-and-protected-parameters) --------------------------------------------------------------------------------------------------------------------------------- SurrealDB comes with [pre-defined parameters](https://surrealdb.com/docs/surrealql/parameters) that are accessible in any context. However, parameters created using `LET` are not accessible within the scope of these pre-defined parameters. Furthermore, some pre-defined parameters are protected and cannot be overwritten using `LET`: LET $before = "Before!"; -- Returns ["Before!"]; RETURN $before; -- Returns the `person` records before deletion DELETE person RETURN $before; -- Returns "Before!" again RETURN $before; Attempting to redefine protected parameters will result in an error: LET $auth = 1; LET $session = 10; Output-------- Query 1 (0ns) -------- "'auth' is a protected variable and cannot be set" -------- Query 2 (0ns) -------- "'session' is a protected variable and cannot be set" Typed LET statements[](https://surrealdb.com/docs/surrealql/statements/let#typed-let-statements) ------------------------------------------------------------------------------------------------- Available since: v2.0.0 Type safety in a `LET` statement can be ensured by adding a `:` (a colon) and the type name after the `LET` keyword. LET $number: int = "9"; Output"Found '9' for param $number, but expected a int" ### Taking advantage of type safety[](https://surrealdb.com/docs/surrealql/statements/let#taking-advantage-of-type-safety) Using typed `LET` statements is a good practice when prototyping code or when getting used to SurrealQL for the first time. Take the following example that attempts to count the number of `true` values in a field by filtering out values that are not `true`, without noticing that the field actually contains strings instead of booleans. The query output ends up being 3, rather than the expected 2. CREATE some:record SET vals = ["true", "false", "true"]; some:record.vals.filter(|$val| $val = true).len(); Output3 Breaking this into multiple typed `LET` statements shows the error right away. LET $vals: array = some:record.vals; LET $len: number = $vals.filter(|$val| $val = true).len(); $len; Output-------- Query 1 -------- "Found 'true' for param $vals, but expected a array" -------- Query 2 -------- 'There was a problem running the filter() function. no such method found for the none type' -------- Query 3 -------- NONE With the location of the error in clear sight, a fix is that much easier to implement. -- Use .map() to turn each string into a bool LET $vals: array = some:record.vals.map(|$val| $val); LET $len: number = $vals.filter(|$val| $val = true).len(); $len; Output2 ### Typed literal statements[](https://surrealdb.com/docs/surrealql/statements/let#typed-literal-statements) Multiple possible types can be specified in a `LET` statement by adding a `|` (vertical bar) in between each possible type. LET $number: int | string = "9"; Even complex types such as objects can be included in a typed `LET` statement. LET $error_info: string | { error: string } = { error: "Something went wrong plz help" }; For more information on this pattern, see the page on [literals](https://surrealdb.com/docs/surrealql/datamodel/literals) . Conclusion[](https://surrealdb.com/docs/surrealql/statements/let#conclusion) ----------------------------------------------------------------------------- The `LET` statement in SurrealDB is versatile, allowing you to store values, results from subqueries, and even define anonymous functions. Understanding how to use `LET` effectively can help you write more concise, readable, and maintainable queries. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/let.mdx) Copy page as Markdown [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) ### On this page * [LET Statement](https://surrealdb.com/docs/surrealql/statements/let#let-statement) * [Syntax](https://surrealdb.com/docs/surrealql/statements/let#syntax) * [Example Usage](https://surrealdb.com/docs/surrealql/statements/let#example-usage) * [Basic Parameter Assignment](https://surrealdb.com/docs/surrealql/statements/let#basic-parameter-assignment) * [Storing Query Results](https://surrealdb.com/docs/surrealql/statements/let#storing-query-results) * [Conditional Logic with IF ELSE](https://surrealdb.com/docs/surrealql/statements/let#conditional-logic-with-if-else) * [Anonymous Functions](https://surrealdb.com/docs/surrealql/statements/let#anonymous-functions) * [Pre-Defined and Protected Parameters](https://surrealdb.com/docs/surrealql/statements/let#pre-defined-and-protected-parameters) * [Typed LET statements](https://surrealdb.com/docs/surrealql/statements/let#typed-let-statements) * [Taking advantage of type safety](https://surrealdb.com/docs/surrealql/statements/let#taking-advantage-of-type-safety) * [Typed literal statements](https://surrealdb.com/docs/surrealql/statements/let#typed-literal-statements) * [Conclusion](https://surrealdb.com/docs/surrealql/statements/let#conclusion) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/let.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE TOKEN statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) > Warning > > This statement is deprecated in favor of [`DEFINE ACCESS ... TYPE JWT`](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) > and [`DEFINE ACCESS ... TYPE RECORD ... WITH JWT`](https://surrealdb.com/docs/surrealql/statements/define/access/record) > . Learn more in [define access documentation](https://surrealdb.com/docs/surrealql/statements/define/access) > . `DEFINE TOKEN` statement[](https://surrealdb.com/docs/surrealql/statements/define/token#define-token-statement) ================================================================================================================ SurrealDB can work with third-party authentication providers such as OpenID Connect providers, OAuth providers and other trusted parties providing JWT (JSON Web Tokens, also referred to in this page as “tokens”). Let’s say that your provider issues your client (e.g. a user or a service) a JWT once it has authenticated. By using the DEFINE TOKEN statement, you can set the public key or shared secret that will be used to verify the authenticity of the token. This verification is performed automatically by SurrealDB when provided with a JWT through any of its interfaces (i.e. the [HTTP REST API](https://surrealdb.com/docs/surrealdb/integration/http) through the “Authorization” header or [any of the SDKs](https://surrealdb.com/docs/surrealdb/integration/sdks) through the “Authenticate” methods) before trusting the claims contained in the token and allowing SurrealQL queries to access the values of those claims. SurrealQL SyntaxDEFINE TOKEN [ OVERWRITE | IF NOT EXISTS ] @name ON [ NAMESPACE | DATABASE | SCOPE @scope ] TYPE @type VALUE @value [ COMMENT @string ] Verification Types[](https://surrealdb.com/docs/surrealql/statements/define/token#verification-types) ------------------------------------------------------------------------------------------------------ When defining a token, its type describes the cryptographic algorithm or specification that will be used to verify the token. This can be an HMAC algorithm, a public-key cryptography algorithm or a remote JWKS object containing all the required information to verify the token. When not specified, the type is defined as the `HS256` HMAC cryptographic algorithm. ### Hash-Based Message Authentication Code (HMAC)[](https://surrealdb.com/docs/surrealql/statements/define/token#hash-based-message-authentication-code-hmac) With HMAC algorithms (`HS256`,`HS384`,`HS512`) the value of the defined token will be the secret used both to sign (by the issuer of the token) and verify (by SurrealDB) the token. Anyone with access to this secret will be able to issue tokens with arbitrary claims which will be trusted by SurrealDB. The following example shows the definition of a token using an HMAC algorithm. -- Specify the namespace and database for the token USE NS abcum DB app_vitalsense; -- Set the name of the token DEFINE TOKEN token_name -- Use this token provider for database authorization ON DATABASE -- Specify the cryptographic signature algorithm used to verify the token TYPE HS512 -- Specify the secret used to sign and verify the authenticity of the token VALUE "sNSYneezcr8kqphfOC6NwwraUHJCVAt0XjsRSNmssBaBRh3WyMa9TRfq8ST7fsU2H2kGiOpU4GbAF1bCiXmM1b3JGgleBzz7rsrz6VvYEM4q3CLkcO8CMBIlhwhzWmy8" ; ### Public-Key Cryptography[](https://surrealdb.com/docs/surrealql/statements/define/token#public-key-cryptography) With public-key cryptography algorithms (`EDDSA`, `ES256`, `ES384`, `ES512`, `PS256`, `PS384`, `PS512`, `RS256`, `RS384`, `RS512`) the value of the defined token will be the public key used to verify the signature of the token. This value is not secret and should be provided by the issuer of the tokens. Tokens will be signed using the private key, known only to the issuer. The public key value should be provided to SurrealDB including its header and footer. Any whitespace will be trimmed. The following example shows the definition of a token using a public-key cryptography algorithm. -- Specify the namespace and database for the token USE NS abcum DB app_vitalsense; -- Set the name of the token DEFINE TOKEN token_name -- Use this token provider for database authorization ON DATABASE -- Specify the cryptographic signature algorithm used to verify the token TYPE RS256 -- Specify the public key used to verify the authenticity of the token VALUE "-----BEGIN PUBLIC KEY----- MUO52Me9HEB4ZyU+7xmDpnixzA/CUE7kyUuE0b7t38oCh+sQouREqIjLwgHhFdhh3cQAwr6GH07D ThioYrZL8xATJ3Youyj8C45QnZcGUif5PkpWXDi0HJSoMFekbW6Pr4xuqIqb2LGxGDVJcLZwJ2AS Gtu2UAfPXbBD3ffiad393M22g1iHM80YaNi+xgswG7qtXE4lR/Lt4s0MeKKX7stdWI1VIsoB+y3i r/OWUvJPjjDNbAsyy8tQmxydv+FUnLEP9TNT4AhN4DXcJ+XsDtW7OWt4EdSVDeKpGbIMvIrh1Pe+ Nilj8UHNyNDHa2AjK3seMo6CMvaIQJKj5o4xGFblFGwvvPD03SbuQLs1FdRjsZCeWLdYeQ3JDHE9 sFG7DCXlpMJcaYT1mf4XHJ0gPekNLQyewTY3Vxf7FgV3GCNjV20kcDFgJA2+iVW2wSrb+txD1ycE kbi8jh0pedWwE40VQWaTh/8eAvX7IHWya/AEro25mq+m6vktNZLbvLphhp586kJK3Tdt3YjpkPre M3nkFWOWurIyKbtIV9JemfwCgt89sNV45dTlnEDEZFFGnIgDnWgx3CUo4XmhICEQU8+tklw9jJYx iCTjhbIDEBHySSSc/pQ4ftHQmhToTlQeOdEy4LYiaEIgl1X+hzRH1hBYvWlNKe4EY1nMCKcjgt0= -----END PUBLIC KEY-----" ; ### JSON Web Key Set (JWKS)[](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) Available since: v1.2.0 With JWKS, a set of JWK (JSON Web Key) objects will be dynamically fetched from a remote location and used to verify tokens following the [RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517) specification. When defining a JWKS token verification method, its value should contain a valid URL that is reachable by SurrealDB and allowed by the configured network [capabilities](https://surrealdb.com/docs/surrealdb/security/capabilities) . This URL should point to a valid JWKS object (as described in [Section 5 of RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517#section-5) ) in the form of a JSON document. This is the recommended method to integrate with authentication providers that support JWKS. Providers like [Google](https://developers.google.com/identity/openid-connect/openid-connect#discovery) , [AWS Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html#amazon-cognito-user-pools-using-tokens-manually-inspect) , [Azure Active Directory](https://azure.github.io/azure-workload-identity/docs/installation/self-managed-clusters/oidc-issuer/jwks.html) , [Auth0](https://auth0.com/docs/secure/tokens/json-web-tokens/json-web-key-sets) , [Keycloak](https://documentation.cloud-iam.com/how-to-guides/configure-remote-jkws.html) or [OneLogin](https://developers.onelogin.com/authentication/tools/jwt) provide JWKS endpoints to verify tokens issued by their services. The following example shows the definition of a token using a JWKS. -- Specify the namespace and database for the token USE NS abcum DB app_vitalsense; -- Set the name of the token DEFINE TOKEN token_name -- Use this token provider for database authorization ON DATABASE -- Specify the JWKS specification used to verify the token TYPE JWKS -- Specify the URL where the JWKS object can be found VALUE "https://example.com/.well-known/jwks.json" ; Validating tokens generated by third-party authentication providers using JWKS ensures that keys can be revoked directly from the third-party service and will no longer be accepted by SurrealDB after the local cache for those keys expires. Likewise, it ensures that token verification will not break if keys are rotated, as any new keys will be automatically fetched from the authentication provider if a JWT is received containing a new key identifier in its `kid` header. To avoid performing requests to the remote URL for each token that is verified, SurrealDB caches every JWKS object that it pulls for a period of 12 hours. The cache can be purged earlier (e.g. in the event a key is compromised) by restarting the SurrealDB server. If a JWT is received containing a reference to a new key identifier in its `kid` header, the JWKS object will be fetched again and updated in the cache if the key identifier is found in the remote JWKS object; this operation will only be performed once every 5 minutes to prevent malicious actors from abusing this process to perform denial of service. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/token#requirements) ------------------------------------------------------------------------------------------ * To `DEFINE TOKEN ... ON NAMESPACE ...` you must have root or namespace level access. * To `DEFINE TOKEN ... ON DATABASE ...` you must have root, namespace, or database level access. * To `DEFINE TOKEN ... ON SCOPE ...` you must have root, namespace, or database level access. * [You must select your namespace and/or database](https://surrealdb.com/docs/surrealql/statements/use) before you can use the `DEFINE TOKEN` statement for database or namespace tokens. Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/token#using-if-not-exists-clause) ------------------------------------------------------------------------------------------------------------------------ Available since: v1.3.0 The `IF NOT EXISTS` clause can be used to define a token only if it does not already exist. If the token already exists, the `DEFINE TOKEN` statement will return an error. -- Create a TOKEN if it does not already exist DEFINE TOKEN IF NOT EXISTS example ON SCOPE example TYPE HS512 VALUE "example"; Using Tokens[](https://surrealdb.com/docs/surrealql/statements/define/token#using-tokens) ------------------------------------------------------------------------------------------ The `DEFINE TOKEN` statement lets you specify the amount of permission granting authority you want to give to a token issuer. You are able to specify if the provider can grant namespace, database, or scope level access to token holders. For this to work, the JWT issued to be used with SurrealDB must contain claims to specify which namespace, database or scope the token bearer is authorized to act on. The following claims should be added to the JWT payload by the issuer of the token: * `exp`: The token expiration Unix time. The token will not be valid after. * `tk`: The name that you chose when defining the token. * `ns`: The namespace that the token is issued for. * `db`: The database that the token is issued for. * `sc`: The scope that the token is issued for. The names of these claims can be in all lowercase (i.e. `tk`) or all uppercase (i.e. `TK`), and can be optionally prefaced with the `https://surrealdb.com` namespace (e.g. `https://surrealdb.com/tk`) in order to separate claims directed to SurrealDB from claims directed to other services. When using a namespace, the claim name can also be used without abbreviation, such as in `https://surrealdb.com/token` or `https://surrealdb.com/scope`. Even when present in the token with a namespace prefix, [SurrealDB claims](https://github.com/surrealdb/surrealdb/blob/main/core/src/iam/token.rs) are directly accessible via the `$token` parameter (e.g. `$token.sc`), whereas custom claims will need to include the namespace prefix (e.g. `$token['https://surrealdb.com/pet_name']`) to be accessed in the same way. The following optional claims are also processed by SurrealDB: * `id`: The identifier of the resource (e.g. user) associated with the token. * `nbf`: The token acceptance Unix time. The token will not be valid before. The expected claims depend on the level at which the token was defined: * For tokens defined `ON NAMESPACE`: `exp`, `tk`, `ns`. * For tokens defined `ON DATABASE`: `exp`, `tk`,`ns`,`db`. * For tokens defined `ON SCOPE`: `exp`, `tk`, `ns`,`db`, `sc`, optionally `id`. For tokens defined `ON NAMESPACE` and `ON DATABASE`, the optional `rl` claim containing an array of capitalized [system user roles](https://surrealdb.com/docs/surrealql/statements/define/user#roles) (e.g. `["Viewer", "Editor", "Owner"]`) can be provided. Doing so will apply the access policy for those roles to any action made using the token. By default, tokens without the `rl` claim will only have the `Viewer` role. When calling any of the SurrealDB interfaces using a JWT, SurrealQL queries will gain access to the claims in the token through the `$token` variable. For example, if the token contains custom claims such as “name” or “email”, the values of those claims will be accessible through `$token.name` and `$token.email`. Additionally, when the `id` claim is present in the token, the fields of the record matching the identifier specified will be accessible through the `$auth` variable. For example, if the value of the `id` claim is `user:73q1bl039y6k8z80v55d`, and user records have fields such as “name” or “email”, then `$auth.name` and `$auth.email` can be used to access those values for the `user:73q1bl039y6k8z80v55d` record specifically, without them being present in the JWT. The signature of the token is verified with method defined when creating the token. If the signature of the token is invalid, calls to SurrealDB interfaces using that token will fail. ### Namespace[](https://surrealdb.com/docs/surrealql/statements/define/token#namespace) Namespace tokens can be used to select, create, update, and delete on all tables in all databases, as well as to define and remove databases and tables from the namespace. -- Specify the namespace for the token USE NS abcum; -- Set the name of the token DEFINE TOKEN token_name -- Use this OAuth provider for namespace authorization ON NAMESPACE -- Specify the cryptographic signature algorithm used to verify the token TYPE HS512 -- Specify the public key so we can verify the authenticity of the token VALUE "sNSYneezcr8kqphfOC6NwwraUHJCVAt0XjsRSNmssBaBRh3WyMa9TRfq8ST7fsU2H2kGiOpU4GbAF1bCiXmM1b3JGgleBzz7rsrz6VvYEM4q3CLkcO8CMBIlhwhzWmy8" ; The namespace token payload should at least include the following claims when used to authenticate with SurrealDB. JWT Payload{ "exp": 2147483647, "tk": "token_name", "ns": "abcum" } ### Database[](https://surrealdb.com/docs/surrealql/statements/define/token#database) Database tokens can be used to select, create, update, and delete on all tables in a specific database, as well as to define and remove tables from the database. -- Specify the namespace and database for the token USE NS abcum DB app_vitalsense; -- Set the name of the token DEFINE TOKEN token_name -- Use this OAuth provider for database authorization ON DATABASE -- Specify the cryptographic signature algorithm used to verify the token TYPE HS512 -- Specify the public key so we can verify the authenticity of the token VALUE "sNSYneezcr8kqphfOC6NwwraUHJCVAt0XjsRSNmssBaBRh3WyMa9TRfq8ST7fsU2H2kGiOpU4GbAF1bCiXmM1b3JGgleBzz7rsrz6VvYEM4q3CLkcO8CMBIlhwhzWmy8" ; The database token payload should at least include the following claims when used to authenticate with SurrealDB. JWT Payload{ "exp": 2147483647, "tk": "token_name", "ns": "abcum", "db": "app_vitalsense" } ### Scope[](https://surrealdb.com/docs/surrealql/statements/define/token#scope) Since the origin of the claims in the JWT is verified, those claims can be used within SurrealQL in the context of a scope in order to provide table and field authorization through an external authenticator using OpenID Connect, OAuth or simply acting as a trusted issuer of a JWT. This can be done by leveraging table permissions to allow or disallow access depending on the values of the claims in the verified token. For example, these claims can be compared with the records in a table to only return those matching certain criteria. The scope for which the token was issued will be accessible to SurrealQL through the `$scope` variable, corresponding to the contents of the `sc` claim. External authorization providers may provide additional scopes that will not be accessible in this way, and instead should be accessed as any other claim through the `$token` variable. Bear in mind that table and field permissions only apply to scope level tokens Access provided by namespace and database tokens is above table-level permissions. When application users will be the ones directly authenticating with JWT, scope tokens are most likely the right choice. The following example shows how scope tokens can be used to grant authorization by verifying that the “email” claim in the token matches the email used as the index of a user table: -- Specify the namespace and database for the token USE NS abcum DB app_vitalsense; -- Necessary in order to define a scope token DEFINE SCOPE users; DEFINE TOKEN token_name ON SCOPE users TYPE RS256 VALUE "-----BEGIN PUBLIC KEY----- MUO52Me9HEB4ZyU+7xmDpnixzA/CUE7kyUuE0b7t38oCh+sQouREqIjLwgHhFdhh3cQAwr6GH07D ThioYrZL8xATJ3Youyj8C45QnZcGUif5PkpWXDi0HJSoMFekbW6Pr4xuqIqb2LGxGDVJcLZwJ2AS Gtu2UAfPXbBD3ffiad393M22g1iHM80YaNi+xgswG7qtXE4lR/Lt4s0MeKKX7stdWI1VIsoB+y3i r/OWUvJPjjDNbAsyy8tQmxydv+FUnLEP9TNT4AhN4DXcJ+XsDtW7OWt4EdSVDeKpGbIMvIrh1Pe+ Nilj8UHNyNDHa2AjK3seMo6CMvaIQJKj5o4xGFblFGwvvPD03SbuQLs1FdRjsZCeWLdYeQ3JDHE9 sFG7DCXlpMJcaYT1mf4XHJ0gPekNLQyewTY3Vxf7FgV3GCNjV20kcDFgJA2+iVW2wSrb+txD1ycE kbi8jh0pedWwE40VQWaTh/8eAvX7IHWya/AEro25mq+m6vktNZLbvLphhp586kJK3Tdt3YjpkPre M3nkFWOWurIyKbtIV9JemfwCgt89sNV45dTlnEDEZFFGnIgDnWgx3CUo4XmhICEQU8+tklw9jJYx iCTjhbIDEBHySSSc/pQ4ftHQmhToTlQeOdEy4LYiaEIgl1X+hzRH1hBYvWlNKe4EY1nMCKcjgt0= -----END PUBLIC KEY-----"; DEFINE TABLE user SCHEMAFULL -- Authorized users can select, update, delete and create user records PERMISSIONS FOR select, update, delete, create -- The current scope must be "users" WHERE $scope = "users" -- The email of the user being queried must match the email claim in the token -- Only matching records will be changed or returned AND email = $token.email ; DEFINE INDEX email ON user FIELDS email UNIQUE; DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value); DEFINE FIELD name ON user TYPE string; DEFINE FIELD nickname ON user TYPE string; DEFINE FIELD picture ON user TYPE string; You may also use permissions clauses to perform additional verification on other JWT claims (e.g. verifying that the iss claim matches a specific principal using $token.iss) that may be required or recommended by a the provider of the token. The scope token payload should at least include the following claims when used to authenticate with SurrealDB. JWT Payload{ "exp": 2147483647, "tk": "token_name", "ns": "abcum", "db": "app_vitalsense", "sc": "users" } [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/token.mdx) Copy page as Markdown [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) ### On this page * [DEFINE TOKEN statement](https://surrealdb.com/docs/surrealql/statements/define/token#define-token-statement) * [Verification Types](https://surrealdb.com/docs/surrealql/statements/define/token#verification-types) * [Hash-Based Message Authentication Code (HMAC)](https://surrealdb.com/docs/surrealql/statements/define/token#hash-based-message-authentication-code-hmac) * [Public-Key Cryptography](https://surrealdb.com/docs/surrealql/statements/define/token#public-key-cryptography) * [JSON Web Key Set (JWKS)](https://surrealdb.com/docs/surrealql/statements/define/token#json-web-key-set-jwks) * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/token#requirements) * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/token#using-if-not-exists-clause) * [Using Tokens](https://surrealdb.com/docs/surrealql/statements/define/token#using-tokens) * [Namespace](https://surrealdb.com/docs/surrealql/statements/define/token#namespace) * [Database](https://surrealdb.com/docs/surrealql/statements/define/token#database) * [Scope](https://surrealdb.com/docs/surrealql/statements/define/token#scope) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/token.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # UPDATE statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) `UPDATE` statement[](https://surrealdb.com/docs/surrealql/statements/update#update-statement) ============================================================================================== The `UPDATE` statement can be used to update existing records in the database. If the record does not exist, the statement will succeed but no records will be updated. > Note > > This statement can not be used to create graph relationships. For that, use the [`RELATE`](https://surrealdb.com/docs/surrealql/statements/relate) > or [`INSERT`](https://surrealdb.com/docs/surrealql/statements/insert) > statement. > Note > > UPDATE on a single record in SurrealDB 1.x will create the record if it does not exist. This behaviour is no longer the case in SurrealDB 2.0. To update and create a record if it does not exist in SurrealDB 2.0, use the [`UPSERT`](https://surrealdb.com/docs/surrealql/statements/upsert) > statement. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/update#statement-syntax) SurrealQL SyntaxUPDATE [ ONLY ] @targets [ CONTENT @value\ | MERGE @value\ | PATCH @value\ | REPLACE @value\ | [ SET @field = @value, ... | UNSET @field, ... ]\ ] [ WHERE @condition ] [ RETURN NONE | RETURN BEFORE | RETURN AFTER | RETURN DIFF | RETURN @statement_param, ... | RETURN VALUE @statement_param ] [ TIMEOUT @duration ] [ EXPLAIN [ FULL ]] ; > Note > > `@target` refers to either record output including an `id` field, or a [record ID](https://surrealdb.com/docs/surrealql/datamodel/ids) > on its own. Example usage[](https://surrealdb.com/docs/surrealql/statements/update#example-usage) -------------------------------------------------------------------------------------- Let’s look at some examples of how to use the `UPDATE` statement. First we’ll create two `person` records with the [`CREATE`](https://surrealdb.com/docs/surrealql/statements/create) statement so that the examples will produce a meaningful output. -- Create a Schemaless person table with a random id CREATE person CONTENT { name: 'John', company: 'Surrealist', skills: ['JavaScript', 'Go' , 'SurrealQL'] }; -- Create another person with a specific id CREATE person:tobie CONTENT { name: 'Tobie', company: 'SurrealDB', skills: ['JavaScript', 'Go' , 'SurrealQL'] }; Let’s say we wanted to update the `person` table with a new field `enjoys` (an array), a new skill `breathing` to the existing `skills` field (another array), add a new numeric field called `dollars`, and a `last_name` field that relies on the existing `name` field to set its value. To do this we would use the following query. -- Update all records in a table -- The `enjoys` field will also be an array. -- The += operator alone is enough to infer the type UPDATE person SET dollars = 50, skills += 'breathing', enjoys += 'reading', full_name = name + ' Mc' + name + 'erson'; Output[\ {\ company: 'Surrealist',\ dollars: 50,\ enjoys: [\ 'reading'\ ],\ full_name: 'John McJohnerson',\ id: person:j1qov2pxey3p8s6hqlev,\ name: 'John',\ skills: [\ 'JavaScript',\ 'Go',\ 'SurrealQL',\ 'breathing'\ ]\ },\ {\ company: 'SurrealDB',\ dollars: 50,\ enjoys: [\ 'reading'\ ],\ full_name: 'Tobie McTobieerson',\ id: person:tobie,\ name: 'Tobie',\ skills: [\ 'JavaScript',\ 'Go',\ 'SurrealQL',\ 'breathing'\ ]\ }\ ] For more specific updates, you can specify a record ID to update a single record. The following query will update the record with the ID `person:tobie` to add “Rust” as a skill. -- Update a record with a specific string id to add a new skill: 'Rust' UPDATE person:tobie SET skills += 'Rust'; Output[\ {\ company: 'SurrealDB',\ dollars: 50,\ enjoys: [\ 'reading'\ ],\ full_name: 'Tobie McTobieerson',\ id: person:tobie,\ name: 'Tobie',\ skills: [\ 'JavaScript',\ 'Go',\ 'SurrealQL',\ 'breathing',\ 'Rust'\ ]\ }\ ] The `-=` operator can be used to remove an item from an array or reduce a numeric value by a certain value. UPDATE person:tobie SET skills -= 'Go', dollars -= 1; Output[\ {\ company: 'SurrealDB',\ dollars: 49,\ enjoys: [\ 'reading'\ ],\ full_name: 'Tobie McTobieerson',\ id: person:tobie,\ name: 'Tobie',\ skills: [\ 'JavaScript',\ 'SurrealQL',\ 'breathing',\ 'Rust'\ ]\ }\ ] You can also remove a field from a record using the `UNSET` keyword or by setting the field to `NONE`. -- Remove the company field by setting it to NONE or using the UNSET keyword UPDATE person:tobie SET company = NONE; UPDATE person:tobie UNSET company; Output[\ {\ dollars: 49,\ enjoys: [\ 'reading'\ ],\ full_name: 'Tobie McTobieerson',\ id: person:tobie,\ name: 'Tobie',\ skills: [\ 'JavaScript',\ 'SurrealQL',\ 'breathing',\ 'Rust'\ ]\ }\ ] Conditional Update with `WHERE` clause[](https://surrealdb.com/docs/surrealql/statements/update#conditional-update-with-where-clause) -------------------------------------------------------------------------------------------------------------------------------------- The `UPDATE` statement supports conditional matching of records using a `WHERE` clause. If the expression in the `WHERE` clause evaluates to `true`, then the respective record will be updated. -- Update all records which match the condition that `company` is not equal to "SurrealDB" UPDATE person SET skills += "System design" WHERE company != "SurrealDB"; Output[\ {\ company: 'Surrealist',\ dollars: 50,\ enjoys: [\ 'reading'\ ],\ full_name: 'John McJohnerson',\ id: person:i5z3i64cpqpo8jtr6jww,\ name: 'John',\ skills: [\ 'JavaScript',\ 'Go',\ 'SurrealQL',\ 'breathing',\ 'System design'\ ]\ },\ {\ dollars: 49,\ enjoys: [\ 'reading'\ ],\ full_name: 'Tobie McTobieerson',\ id: person:tobie,\ name: 'Tobie',\ skills: [\ 'JavaScript',\ 'SurrealQL',\ 'breathing',\ 'Rust',\ 'System design'\ ]\ }\ ] CONTENT clause[](https://surrealdb.com/docs/surrealql/statements/update#content-clause) ---------------------------------------------------------------------------------------- Instead of specifying record data using the `SET` clause, it is also possible to use the `CONTENT` keyword to specify the record data using a SurrealQL object. -- Update all records with the same content UPDATE person CONTENT { name: 'John', company: 'SurrealDB', skills: ['Rust', 'Go', 'JavaScript'], }; -- Oops, now they are both named John. -- Update a specific record with some content UPDATE person:tobie CONTENT { name: 'Tobie', company: 'SurrealDB', skills: ['Rust', 'Go', 'JavaScript'], }; Since version `2.1.0`, a statement with a `CONTENT` clause will bypass `READONLY` fields instead of generating an error. DEFINE FIELD created ON person TYPE datetime DEFAULT d'2024-01-01T00:00:00Z' READONLY; CREATE person:gladys SET age = 90; -- Does not try to modify `created` field, no error UPDATE person:gladys CONTENT { age: 70 }; * [Output before 2.1.0](https://surrealdb.com/docs/surrealql/statements/update#tab-panel-248) * [Output after 2.1.0](https://surrealdb.com/docs/surrealql/statements/update#tab-panel-249) -------- Query -------- [\ {\ age: 90,\ created: d'2024-01-01T00:00:00Z',\ id: person:gladys\ }\ ] -------- Query -------- 'Found changed value for field `created`, with record `person:gladys`, but field is readonly' -------- Query -------- [\ {\ age: 90,\ created: d'2024-01-01T00:00:00Z',\ id: person:gladys\ }\ ] -------- Query -------- [\ {\ age: 70,\ created: d'2024-01-01T00:00:00Z',\ id: person:gladys\ }\ ] REPLACE clause[](https://surrealdb.com/docs/surrealql/statements/update#replace-clause) ---------------------------------------------------------------------------------------- Originally an alias for `CONTENT`, the `REPLACE` clause maintains the previous behaviour regarding `READONLY` fields. If the content following `REPLACE` does not match a record’s `READONLY` fields, an error will be generated. DEFINE FIELD created ON person TYPE datetime DEFAULT d'2024-01-01T00:00:00Z' READONLY; CREATE person:gladys SET age = 90; -- Attempts to change `created` field, error UPDATE person:gladys REPLACE { age: 70 }; -- `created` equals current value, query works UPDATE person:gladys REPLACE { age: 70, created: d'2024-01-01T00:00:00Z' }; Output-------- Query -------- [\ {\ age: 90,\ created: d'2024-01-01T00:00:00Z',\ id: person:gladys\ }\ ] -------- Query -------- 'Found changed value for field `created`, with record `person:gladys`, but field is readonly' -------- Query -------- [\ {\ age: 70,\ created: d'2024-01-01T00:00:00Z',\ id: person:gladys\ }\ ] MERGE clause[](https://surrealdb.com/docs/surrealql/statements/update#merge-clause) ------------------------------------------------------------------------------------ Instead of specifying the full record data using `CONTENT` or one field at a time using `SET`, it is also possible to merge-update only specific fields by using the `MERGE` keyword followed by on object containing the fields which are to be upserted. -- Update certain fields on all records UPDATE person MERGE { settings: { marketing: true, }, }; -- Update certain fields on a specific record UPDATE person:tobie MERGE { settings: { marketing: true, }, }; Output[\ {\ company: 'SurrealDB',\ id: person:i5z3i64cpqpo8jtr6jww,\ name: 'John',\ settings: {\ marketing: true\ },\ skills: [\ 'Rust',\ 'Go',\ 'JavaScript'\ ]\ },\ {\ company: 'SurrealDB',\ id: person:tobie,\ name: 'Tobie',\ settings: {\ marketing: true\ },\ skills: [\ 'Rust',\ 'Go',\ 'JavaScript'\ ]\ }\ ] PATCH clause[](https://surrealdb.com/docs/surrealql/statements/update#patch-clause) ------------------------------------------------------------------------------------ You can also specify changes to be applied to your query response, using the PATCH command which works similar to the [JSON Patch specification](https://jsonpatch.com/) -- Patch the JSON response UPDATE person:tobie PATCH [\ {\ "op": "add",\ "path": "Engineering",\ "value": "true"\ }\ ] Output[\ {\ Engineering: 'true',\ company: 'SurrealDB',\ id: person:tobie,\ name: 'Tobie',\ settings: {\ marketing: true\ },\ skills: [\ 'Rust',\ 'Go',\ 'JavaScript'\ ]\ }\ ] Alter the `RETURN` value[](https://surrealdb.com/docs/surrealql/statements/update#alter-the-return-value) ---------------------------------------------------------------------------------------------------------- By default, the update statement returns the record value once the changes have been made. To change the return value of each record, use the `RETURN` clause, specifying `NONE`, `BEFORE`, `AFTER`, `DIFF`, a comma-separated list of specific fields or ad-hoc fields, or `VALUE` for a single field without its key name. -- Don't return any result UPDATE person SET skills += 'reading' RETURN NONE; -- Return the changeset diff UPDATE person SET skills += 'reading' RETURN DIFF; -- Return the record before changes were applied UPDATE person SET skills += 'reading' RETURN BEFORE; -- Return the record after changes were applied (the default) UPDATE person SET skills += 'reading' RETURN AFTER; -- Return the value of the 'skills' field without the field name UPDATE person SET skills += 'reading' RETURN VALUE skills; -- Return a specific field only from the updated records UPDATE person:tobie SET skills = ['skiing', 'music'] RETURN name, interests; Using a timeout[](https://surrealdb.com/docs/surrealql/statements/update#using-a-timeout) ------------------------------------------------------------------------------------------ When processing a large result set with many interconnected records, it is possible to use the `TIMEOUT` keyword to specify a timeout duration for the statement. If the statement continues beyond this duration, then the transaction will fail, no records will be updated in the database, and the statement will return an error. UPDATE person SET important = true WHERE ->knows->person->(knows WHERE influencer = true) TIMEOUT 5s; UPDATE inside database exports[](https://surrealdb.com/docs/surrealql/statements/update#update-inside-database-exports) ------------------------------------------------------------------------------------------------------------------------ As `UPDATE` before version 2.0.0 used to create a specified record ID if it did not exist, it was used in the `.surql` files generated by the [`surreal export`](https://surrealdb.com/docs/surrealdb/cli/export) command to export existing records in a database. As of version 2.0.0, the [`INSERT`](https://surrealdb.com/docs/surrealql/statements/insert) statement is used instead. The `EXPLAIN` clause[](https://surrealdb.com/docs/surrealql/statements/update#the-explain-clause) -------------------------------------------------------------------------------------------------- When `EXPLAIN` is used: 1. The `UPDATE` statement returns an explanation, essentially revealing the execution plan to provide transparency and understanding of the query performance. 2. The records are not updated. `EXPLAIN` can be followed by `FULL` to see the number of executed rows. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/update.mdx) Copy page as Markdown [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) ### On this page * [UPDATE statement](https://surrealdb.com/docs/surrealql/statements/update#update-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/update#statement-syntax) * [Example usage](https://surrealdb.com/docs/surrealql/statements/update#example-usage) * [Conditional Update with WHERE clause](https://surrealdb.com/docs/surrealql/statements/update#conditional-update-with-where-clause) * [CONTENT clause](https://surrealdb.com/docs/surrealql/statements/update#content-clause) * [REPLACE clause](https://surrealdb.com/docs/surrealql/statements/update#replace-clause) * [MERGE clause](https://surrealdb.com/docs/surrealql/statements/update#merge-clause) * [PATCH clause](https://surrealdb.com/docs/surrealql/statements/update#patch-clause) * [Alter the RETURN value](https://surrealdb.com/docs/surrealql/statements/update#alter-the-return-value) * [Using a timeout](https://surrealdb.com/docs/surrealql/statements/update#using-a-timeout) * [UPDATE inside database exports](https://surrealdb.com/docs/surrealql/statements/update#update-inside-database-exports) * [The EXPLAIN clause](https://surrealdb.com/docs/surrealql/statements/update#the-explain-clause) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/update.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Files | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) Files[](https://surrealdb.com/docs/surrealql/datamodel/files#files) ==================================================================== Available since: v3.0.0-alpha.1 Files are accessed by a path, which is prefixed with an `f` to differentiate it from a regular string. Some examples of file pointers: f"bucket:/some/key/to/a/file.txt"; f"bucket:/some/key/with\ escaped"; To work with the files that can be accessed through these pointers, use the following: * A [`DEFINE BUCKET`](https://surrealdb.com/docs/surrealql/statements/define/table) statement to set up the bucket to hold the files * [Files functions](https://surrealdb.com/docs/surrealql/functions/database/file) such as `file::put()` and `file::get()` DEFINE BUCKET my_bucket BACKEND "memory"; f"my_bucket:/some_file.txt".put("Some text inside"); f"my_bucket:/some_file.txt".get(); f"my_bucket:/some_file.txt".get(); Output-------- Query -------- b"536F6D65207465787420696E73696465" -------- Query -------- 'Some text inside' [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/files.mdx) Copy page as Markdown [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/files.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Futures | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) Futures[](https://surrealdb.com/docs/surrealql/datamodel/futures#futures) ========================================================================== Futures are values which are only computed when the data is selected and returned to the client. Futures can be stored inside records, to enable dynamic values which are always calculated when queried. ### Simple futures[](https://surrealdb.com/docs/surrealql/datamodel/futures#simple-futures) Any value or expression can be used inside a future. This value will be dynamically computed on every access to the record. CREATE person SET accessed_date = { time::now() }; Futures inside schema definitions[](https://surrealdb.com/docs/surrealql/datamodel/futures#futures-inside-schema-definitions) ------------------------------------------------------------------------------------------------------------------------------ A future can be added to a schema definition as well. DEFINE FIELD accessed_at ON TABLE user VALUE { time::now() }; CREATE user:one; SELECT * FROM ONLY user:one; -- Sleep for one second SLEEP 1s; -- `accessed_at` is a different value now SELECT * FROM ONLY user:one; This differs from a non-future `VALUE` clause which is only calculated when it is modified (created or updated), but is not recalculated during a `SELECT` query which does not modify a record. DEFINE FIELD updated ON TABLE user VALUE time::now(); CREATE user:one; SELECT * FROM ONLY user:one; -- Sleep for one second SLEEP 1s; -- `updated` is still the same SELECT * FROM ONLY user:one; Futures depending on statements[](https://surrealdb.com/docs/surrealql/datamodel/futures#futures-depending-on-statements) -------------------------------------------------------------------------------------------------------------------------- If the value of a future is the result of a statement, it must be wrapped in parentheses. DEFINE FIELD random_movie ON app_screen VALUE { (SELECT * FROM ONLY movie ORDER BY RAND() LIMIT 1) }; Futures depending on other fields[](https://surrealdb.com/docs/surrealql/datamodel/futures#futures-depending-on-other-fields) ------------------------------------------------------------------------------------------------------------------------------ Futures can be used to calculate values which dynamically change based on other fields. This value will be dynamically computed, on every access to the record, and will use the other field when it is accessed. DEFINE FUNCTION fn::get_age($birthdate: datetime) -> int { duration::years(time::now() - $birthdate) }; CREATE person SET birthday = "2007-06-22", can_drive = { fn::get_age(birthday) >= 18 } ; Futures can also dynamically access remote records, perform subqueries, or make use of graph traversal. CREATE person SET name = 'Jason', friends = [person:tobie, person:jaime], adult_friends = { friends[WHERE age > 18].name } ; Avoiding infinite recursion[](https://surrealdb.com/docs/surrealql/datamodel/futures#avoiding-infinite-recursion) ------------------------------------------------------------------------------------------------------------------ When defining a future on a field, be sure to avoid any statements that would cause infinite recursion. In the following example, the `random_friend` field is defined by a statement that uses a `SELECT` statement on all the fields of the same `person` table, one of which will also use the same `future` to compute its value. CREATE |person:10| RETURN NONE; DEFINE FIELD random_friend ON person VALUE { (SELECT * FROM ONLY person ORDER BY RAND() LIMIT 1) }; CREATE person; Output'Reached excessive computation depth due to functions, subqueries, or futures' A `SELECT` query that does not access the field defined by a future will avoid the infinite recursion. CREATE |person:10| RETURN NONE; DEFINE FIELD random_friend ON person VALUE { (SELECT VALUE id FROM ONLY person ORDER BY RAND() LIMIT 1) }; CREATE person; Output[\ {\ id: person:d7uyc9m5gtg7r4cvvq7d,\ random_friend: person:fv76w83vbgularvl0dvv\ }\ ] Next steps[](https://surrealdb.com/docs/surrealql/datamodel/futures#next-steps) -------------------------------------------------------------------------------- You’ve now seen how to create dynamically computed properties on records, using either simple values, and values which depend on local and remote record fields. Take a look at the next chapter to understand how types can be cast and converted to other types. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/futures.mdx) Copy page as Markdown [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) ### On this page * [Simple futures](https://surrealdb.com/docs/surrealql/datamodel/futures#simple-futures) * [Futures inside schema definitions](https://surrealdb.com/docs/surrealql/datamodel/futures#futures-inside-schema-definitions) * [Futures depending on statements](https://surrealdb.com/docs/surrealql/datamodel/futures#futures-depending-on-statements) * [Futures depending on other fields](https://surrealdb.com/docs/surrealql/datamodel/futures#futures-depending-on-other-fields) * [Avoiding infinite recursion](https://surrealdb.com/docs/surrealql/datamodel/futures#avoiding-infinite-recursion) * [Next steps](https://surrealdb.com/docs/surrealql/datamodel/futures#next-steps) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/futures.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Literals | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) Literals[](https://surrealdb.com/docs/surrealql/datamodel/literals#literals) ============================================================================= Available since: v2.0.0 A literal is a value that may have multiple representations or formats, similar to an enum or a union type. A literal can be composed of strings, numbers, objects, arrays, or durations. Examples[](https://surrealdb.com/docs/surrealql/datamodel/literals#examples) ----------------------------------------------------------------------------- A literal can be as simple as a declaration that a parameter must be a certain value. LET $nine: 9 = 9; LET $nine: 9 = 10; Response-------- Query -------- NONE -------- Query -------- 'Found 10 for param $nine, but expected a 9' Using `|` allows a literal to be a number of possible options. LET $nine: 9 | "9" | "nine" = "Nein"; Response"Found 'Nein' for param $nine, but expected a 9 | '9' | 'nine'" A literal can contain possible types in addition to possible values. LET $flexible_param: datetime | uuid | "N/A" = "N/A"; LET $flexible_param: datetime | uuid | "N/A" = "2024-09-01"; Literals that include the option to be an array or an object can contain rich data. LET $status: "Ok" | { err: string } = { err: "Forgot to plug it in" }; Literals in database schema[](https://surrealdb.com/docs/surrealql/datamodel/literals#literals-in-database-schema) ------------------------------------------------------------------------------------------------------------------- Literals can be defined inside a database schema by using a [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) statement. DEFINE FIELD error_info ON TABLE info TYPE { error: "Continue" } | { error: "RetryWithId", id: string } | { error: "Deprecated", message: string }; CREATE info SET error_info = { error: "Deprecated", message: "You shouldn't use this anymore" }; -- Doesn't conform to definition, will not work CREATE info SET error_info = "You shouldn't use this anymore"; Response-------- Query -------- [\ {\ error_info: {\ error: 'Deprecated',\ message: "You shouldn't use this anymore"\ },\ id: info:pkckjrri8q1pg12unyuo\ }\ ] -------- Query -------- "Found \"You shouldn't use this anymore\" for field `error_info`, with record `info:dq375w4lv02aj2dj7122`, but expected a { error: 'Continue' } | { error: 'RetryWithId', id: string } | { error: 'Deprecated', message: string }" Matching on literals[](https://surrealdb.com/docs/surrealql/datamodel/literals#matching-on-literals) ----------------------------------------------------------------------------------------------------- While SurrealQL does not have a `match` or `switch` operator, `IF LET` statements can be used to match on a literal, particularly if each possible type is an object. The following shows a similar example to the above except that each object begins with a field containing the name of the type of error. DEFINE FIELD error_info ON TABLE info TYPE { Continue: { message: "" }} | { Retry: { error: "Retrying", after: duration }} | { Deprecated: { message: string }}; Next, we will [define a function](https://surrealdb.com/docs/surrealql/statements/define/function) to handle this field and return a certain type of message depending on the error. Note the following: * The `LET` statement in the first line is simply to shorten the path to the information contained inside `error_info` * `IF LET` statement works here because [IF](https://surrealdb.com/docs/surrealql/statements/throw) involves a check for [truthiness](https://surrealdb.com/docs/surrealql/datamodel/values#values-and-truthiness) , returning `true` as long as it finds a value that is not none, empty, or zero. DEFINE FUNCTION fn::handle_error($data: record) -> string { LET $err = $data.error_info; RETURN IF $err.Continue { "Continue" } ELSE IF $err.Retry { sleep($err.Retry.after); "Now retrying again" } ELSE IF $err.Deprecated { $err.Deprecated.message } }; With the function set up, the `info` records can be inserted and run one at a time through the function. INSERT INTO info [\ { error_info: { Continue: { message: "" } }},\ { error_info: { Retry: { error: "Retrying", after: 1s } }},\ { error_info: { Deprecated: { message: "Thought I said you shouldn't use this anymore" } }}\ ]; LET $info = SELECT * FROM info; fn::handle_error($info[0].id); fn::handle_error($info[1].id); fn::handle_error($info[2].id); Output-------- Query -------- 'Continue' -------- Query -------- -- After waiting 1 second 'Now retrying again' -------- Query -------- "Thought I said you shouldn't use this anymore" [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/literals.mdx) Copy page as Markdown [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) ### On this page * [Examples](https://surrealdb.com/docs/surrealql/datamodel/literals#examples) * [Literals in database schema](https://surrealdb.com/docs/surrealql/datamodel/literals#literals-in-database-schema) * [Matching on literals](https://surrealdb.com/docs/surrealql/datamodel/literals#matching-on-literals) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/literals.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Values | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Values[](https://surrealdb.com/docs/surrealql/datamodel/values#values) ======================================================================= Each of the types mentioned in the data model is a subset of an all-encompassing type called a value. Comparing and ordering values[](https://surrealdb.com/docs/surrealql/datamodel/values#comparing-and-ordering-values) --------------------------------------------------------------------------------------------------------------------- While it is unsurprising that a data type can be compared with itself, it may be surprising that different types can also be compared with each other. RETURN 9 > 1; // Returns true RETURN [] > time::now(); // Also returns true This comparison is possible because every type in SurrealDB is a subset of value, and a comparison of any type with another is also simply a comparison of a value with another value. The order of values from least to greatest is: * `none` * `null` * `bool` * `number` * `string` * `duration` * `datetime` * `UUID` * `array` * `object` * `geometry` * `bytes` * `record` As a result, all of the following return `true`. RETURN [\ null > none,\ true > null,\ 1 > true,\ 'a' > 999999999,\ 1s > 'a',\ time::now() > 1s,\ rand::uuid() > time::now(),\ [] > rand::uuid(),\ {} > [],\ (89.0, 89.0) > {},\ 'Aeon' > (89.0, 89.0),\ person:aeon > 'Aeon'\ ]; Being able to compare a value with any other value is what makes SurrealDB’s record range syntax possible. CREATE time_data:[d'2024-07-23T00:00:00.000Z']; CREATE time_data:[d'2024-07-24T00:00:00.000Z']; CREATE time_data:[d'2024-07-25T00:00:00.000Z']; -- Records from the 24th to the 25th SELECT * FROM time_data:[d'2024-07-24']..[d'2024-07-25']; -- Records from the 24th SELECT * FROM time_data:[d'2024-07-24']..; -- All records SELECT * FROM time_data:[NONE]..; The `..` open-range syntax also represents an infinite value inside a record range query, making it the greatest possible value and the inverse of `NONE`, the lowest possible value. A part of a record range query that begins with `NONE` and ends with `..` will thus filter out nothing. CREATE temperature:['London', d'2025-02-19T00:00:00.000Z'] SET val = 5.5; CREATE temperature:['London', d'2025-02-20T00:00:00.000Z'] SET val = 5.7; -- Return all records as long as index 0 = 'London' SELECT * FROM temperature:['London', NONE]..=['London', ..]; Output[\ {\ id: temperature:[\ 'London',\ d'2025-02-19T00:00:00Z'\ ],\ val: 5.5f\ },\ {\ id: temperature:[\ 'London',\ d'2025-02-20T00:00:00Z'\ ],\ val: 5.7f\ }\ ] Inside a schema, the keyword `any` is used to denote any possible value. DEFINE FIELD anything ON TABLE person TYPE any; Values and truthiness[](https://surrealdb.com/docs/surrealql/datamodel/values#values-and-truthiness) ----------------------------------------------------------------------------------------------------- Any value is considered to be truthy if it is not NONE, NULL, or a default value for the data type. A data type at its default value is one that is empty, such as an empty string or array or object, or a number set to 0. The following example shows the result of the `array::all()` method, which checks to see if all of the items inside an array are truthy or not. RETURN array::all(["", 1, 2, 3]); // false because of "" RETURN array::all([{}, 1, 2, 3]); // false because of {} RETURN array::all(["SurrealDB", { is_nice_database: true }, 1, 2, 3]); // true As [the ! operator](https://surrealdb.com/docs/surrealql/operators) reverses the truthiness of a value, a doubling of this operator can also be used to check for truthiness. RETURN [\ !!"Has a value", !!"", // true, false\ !!true, !!false, // true, false\ !!{ is_nice_database: true }, !!{} // true, false\ ]; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/values.mdx) Copy page as Markdown [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Clauses](https://surrealdb.com/docs/surrealql/clauses) ### On this page * [Comparing and ordering values](https://surrealdb.com/docs/surrealql/datamodel/values#comparing-and-ordering-values) * [Values and truthiness](https://surrealdb.com/docs/surrealql/datamodel/values#values-and-truthiness) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/values.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Data types | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) Data types[](https://surrealdb.com/docs/surrealql/datamodel#data-types) ======================================================================== SurrealQL allows you to describe data with specific data types. These data types are used to validate data and to generate the appropriate database schema. | Type | Description | | --- | --- | | `any` | Use this when you explicitly don’t want to specify the field’s data type. The field will allow any data type supported by SurrealDB. | | `array` | An array of items. The array type also allows you to define which types can be stored in the array and the max length.

* `array`
* `array`
* `array` | | `bool` | Describes whether something is truthy or not. | | [`bytes`](https://surrealdb.com/docs/surrealql/datamodel#bytes) | Stores a value in a byte array.

* `value`
* `bytes` | | `datetime` | An ISO 8601 compliant data type that stores a date with time and time zone. | | `decimal` | Uses BigDecimal for storing any real number with arbitrary precision. | | `duration` | Store a value representing a length of time. Can be added or subtracted from datetimes or other durations. | | `float` | Store a value in a 64 bit float. | | [`geometry`](https://surrealdb.com/docs/surrealql/datamodel#geometry) | [RFC 7946](https://www.rfc-editor.org/rfc/rfc7946 "Link to RFC 7946")
compliant data type for storing geometry in the [GeoJson format](https://geojson.org/ "Link to the GeoJson website")
.

* `geometry`
* `geometry`
* `geometry`
* `geometry`
* `geometry`
* `geometry`
* `geometry`
* `geometry` | | `int` | Store a value in a 64 bit integer. | | `number` | Store numbers without specifying the type. SurrealDB will detect the type of number and store it using the minimal number of bytes. For numbers passed in as a string, this field will store the number in a BigDecimal. | | `object` | Store formatted objects containing values of any supported type with no limit to object depth or nesting. | | `regex` | A compiled regular expression that can be used for matching strings. | | [`literal`](https://surrealdb.com/docs/surrealql/datamodel/literals) | A value that may have multiple representations or formats, similar to an enum or a union type. Can be composed of strings, numbers, objects, arrays, or durations.

* `”a” \| “b”`
* `[number, “abc”]`
* `123 \| 456 \| string \| 1y1m1d` | | `option` | Makes types optional and guarantees the field to be either empty (NONE), or a number.

* `option` | | `range` | A range of possible values. Lower and upper bounds can be set, in the absence of which the range becomes open-ended. A range of integers can be used in a FOR loop.

* `0..10`
* `0..=10`
* `..10`
* `‘a’..‘z’` | | `record` | Store a reference to another record. The value must be a Record ID. Add the record name inside angle brackets to restrict the reference to only certain record names.

* `record`
* `record`
* `record` | | `set` | A set of items. The set type also allows you to define which types can be stored in the set and the max length. Items are automatically deduplicated.

* `set`
* `set`
* `set` | | `string` | Describes a text-like value. | Examples[](https://surrealdb.com/docs/surrealql/datamodel#examples) -------------------------------------------------------------------- ### geometry[](https://surrealdb.com/docs/surrealql/datamodel#geometry) -- Define a field with a single type DEFINE FIELD location ON TABLE restaurant TYPE geometry; -- Define a field with any geometric type DEFINE FIELD area ON TABLE restaurant TYPE geometry; -- Define a field with specific geometric types DEFINE FIELD area ON TABLE restaurant TYPE geometry; ### bytes[](https://surrealdb.com/docs/surrealql/datamodel#bytes) -- Define a field with a single type DEFINE FIELD image ON TABLE product TYPE bytes; -- Create a record with a bytes field and set the value CREATE foo SET value = "bar"; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/index.mdx) Copy page as Markdown [Operators](https://surrealdb.com/docs/surrealql/operators) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) ### On this page * [Examples](https://surrealdb.com/docs/surrealql/datamodel#examples) * [geometry](https://surrealdb.com/docs/surrealql/datamodel#geometry) * [bytes](https://surrealdb.com/docs/surrealql/datamodel#bytes) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/index.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # ACCESS statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) `ACCESS` statement[](https://surrealdb.com/docs/surrealql/statements/access#access-statement) ============================================================================================== Available since: v2.2.0 > Caution > > Currently, the `ACCESS` statement is an experimental feature intended to be used for validating its suitability and security. As such, it may be subject to breaking changes and may present unidentified security issues. Do not rely on this feature in production applications. To enable this and other experimental features related to bearer access, set the `SURREAL_EXPERIMENTAL_BEARER_ACCESS` [environment variable](https://surrealdb.com/docs/surrealdb/cli/env) > to `true`. The `ACCESS` statement can be used to manage access grants. It provides the ability to generate access grants using certain access methods, such as bearer keys defined with the [`DEFINE ACCESS ... TYPE BEARER`](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) statement, as well as the ability to show, revoke and purge such grants. By default, the `ACCESS` statement will default to referencing access methods defined at the current level specified with the [`USE`](https://surrealdb.com/docs/surrealql/statements/use) statement. As with other statements, access methods defined at any level can be referenced by using the `ON` clause. Operations that either create, revoke or purge access grants using the `ACCESS` statement will be logged in the server as long as it is running with the `INFO` level (the default) or any higher verbosity level. These logs are identified by the `surrealdb_core::sql::statements::access` prefix. ### Statement syntax[](https://surrealdb.com/docs/surrealql/statements/access#statement-syntax) SurrealQL SyntaxACCESS @name [ ON [ ROOT | NAMESPACE | DATABASE ] ] [\ GRANT [ FOR USER @name | FOR RECORD @record ]\ | SHOW [ GRANT @id | ALL | WHERE @expression ] \ | REVOKE [ GRANT @id | ALL | WHERE @expression ] \ | PURGE [ EXPIRED | REVOKED [ , EXPIRED | REVOKED ] ] [ FOR @duration ]\ ] `GRANT`[](https://surrealdb.com/docs/surrealql/statements/access#grant) ------------------------------------------------------------------------ The `GRANT` clause creates and returns a grant for a certain subject using the specified access method. This subject can be a [system user](https://surrealdb.com/docs/surrealdb/security/authentication#system-users) or [record user](https://surrealdb.com/docs/surrealdb/security/authentication#record-users) . Access grants can be used to access SurrealDB as that subject until they become expired or revoked. When creating a grant, a secret (e.g. a key) corresponding with the grant will be returned. This secret should be stored securely, as it will no longer be displayed by SurrealDB, instead being printed as `[REDACTED]` whenever the grant details are shown. SurrealQL SyntaxACCESS @name [ ON [ ROOT | NAMESPACE | DATABASE ] ] GRANT [ FOR USER @name | FOR RECORD @record ] #### Example: Grant for Automation using System User[](https://surrealdb.com/docs/surrealql/statements/access#example-grant-for-automation-using-system-user) -- Define system user for automation DEFINE USER automation ON DATABASE PASSWORD 'secret' ROLES VIEWER; -- Define bearer access method to generate API keys DEFINE ACCESS api ON DATABASE TYPE BEARER FOR USER DURATION FOR GRANT 10d; -- Generate bearer grant to be used by the automation ACCESS api GRANT FOR USER automation; Response-- Query 1 NONE -- Query 2 NONE -- Query 3 { ac: 'api', creation: d'2024-12-16T16:15:51.517384293Z', expiration: d'2024-12-26T16:15:51.517386053Z', grant: { id: 'BNb2pS0GmaJz', key: 'surreal-bearer-BNb2pS0GmaJz-5eTfQ5uEu8jbRb3oblqVMAt8' }, id: 'BNb2pS0GmaJz', revocation: NONE, subject: { user: 'automation' }, type: 'bearer' } #### Example: Grant for End-User using Record User[](https://surrealdb.com/docs/surrealql/statements/access#example-grant-for-end-user-using-record-user) -- Create record representing a user CREATE user:1 CONTENT { name: "tobie" }; -- Define bearer access method to generate API keys DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD DURATION FOR GRANT 10d; -- Generate bearer grant to be used by the user ACCESS api GRANT FOR RECORD user:1; Response-- Query 1 [\ {\ id: user:1,\ name: 'tobie'\ }\ ] -- Query 2 NONE -- Query 3 { ac: 'api', creation: d'2024-12-16T16:16:41.996932810Z', expiration: d'2024-12-26T16:16:41.996934501Z', grant: { id: 'sRLEKGxObJuM', key: 'surreal-bearer-sRLEKGxObJuM-iUUFe1vijFDaFDW7jceZJDkX' }, id: 'sRLEKGxObJuM', revocation: NONE, subject: { record: user:1 }, type: 'bearer' } `SHOW`[](https://surrealdb.com/docs/surrealql/statements/access#show) ---------------------------------------------------------------------- The `SHOW` clause displays the details of grants created with a specific access method. The statement allows showing the details of individual grants, all grants or only grants matching a particular SurrealQL expression. Beware that, in situations where grants are automatically created, showing all grants at once may be impractical and filtering is advised. Note that any secrets (e.g. keys) associated with the grant will not be displayed and instead will be shown as `[REDACTED]`. SurrealQL SyntaxACCESS @name [ ON [ ROOT | NAMESPACE | DATABASE ] ] SHOW [ GRANT @id | ALL | WHERE @expression ] #### Example: Showing the details of a specific grant[](https://surrealdb.com/docs/surrealql/statements/access#example-showing-the-details-of-a-specific-grant) -- Create record representing a user CREATE user:1 CONTENT { name: "tobie" }; -- Define bearer access method to generate API keys DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD DURATION FOR GRANT 10d; -- Generate bearer grant to be used by the user ACCESS api GRANT FOR RECORD user:1; Response-- Query 1 [\ {\ id: user:1,\ name: 'tobie'\ }\ ] -- Query 2 NONE -- Query 3 { ac: 'api', creation: d'2024-12-16T16:17:24.903832476Z', expiration: d'2024-12-26T16:17:24.903834523Z', grant: { id: 'JdvDFKMCVYoM', key: 'surreal-bearer-JdvDFKMCVYoM-0ahEAVY6egVdg33Vs5gc6J4h' }, id: 'JdvDFKMCVYoM', revocation: NONE, subject: { record: user:1 }, type: 'bearer' } ACCESS api SHOW GRANT JdvDFKMCVYoM; Response{ ac: 'api', creation: d'2024-12-16T16:17:24.903832476Z', expiration: d'2024-12-26T16:17:24.903834523Z', grant: { id: 'JdvDFKMCVYoM', key: '[REDACTED]' }, id: 'JdvDFKMCVYoM', revocation: NONE, subject: { record: user:1 }, type: 'bearer' } #### Example: Showing the details of all grants for users of a certain name[](https://surrealdb.com/docs/surrealql/statements/access#example-showing-the-details-of-all-grants-for-users-of-a-certain-name) Since the `subject` attribute of grants associated with a record is a record identifier, it can be used as a [record link](https://surrealdb.com/docs/surrealql/datamodel/records) in order to access any record fields. This can be used to filter grants associated with record users matching certain conditions based on arbitrary data. -- Create records representing users CREATE user:1 CONTENT { name: "tobie" }; CREATE user:2 CONTENT { name: "jaime" }; -- Define bearer access method to generate API keys DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD DURATION FOR GRANT 10d; -- Generate bearer grants to be used by the users ACCESS api GRANT FOR RECORD user:1; ACCESS api GRANT FOR RECORD user:2; Response-- Query 1 [\ {\ id: user:1,\ name: 'tobie'\ }\ ] -- Query 2 [\ {\ id: user:2,\ name: 'jaime'\ }\ ] -- Query 3 NONE -- Query 4 { ac: 'api', creation: d'2024-12-16T16:18:57.061692071Z', expiration: d'2024-12-26T16:18:57.061694228Z', grant: { id: 'HaJ19zCnP6RI', key: 'surreal-bearer-HaJ19zCnP6RI-R545vHcTbSCYdHnxIsVnjSFu' }, id: 'HaJ19zCnP6RI', revocation: NONE, subject: { record: user:1 }, type: 'bearer' } -- Query 5 { ac: 'api', creation: d'2024-12-16T16:18:57.063673293Z', expiration: d'2024-12-26T16:18:57.063674755Z', grant: { id: 'ND2ZegEHfUGl', key: 'surreal-bearer-ND2ZegEHfUGl-JGPSr162qJ2bN8kURV8mYaLv' }, id: 'ND2ZegEHfUGl', revocation: NONE, subject: { record: user:2 }, type: 'bearer' } ACCESS api SHOW WHERE subject.record.name = "tobie"; Response[\ {\ ac: 'api',\ creation: d'2024-12-16T16:18:57.061692071Z',\ expiration: d'2024-12-26T16:18:57.061694228Z',\ grant: {\ id: 'HaJ19zCnP6RI',\ key: '[REDACTED]'\ },\ id: 'HaJ19zCnP6RI',\ revocation: NONE,\ subject: {\ record: user:1\ },\ type: 'bearer'\ }\ ] `REVOKE`[](https://surrealdb.com/docs/surrealql/statements/access#revoke) -------------------------------------------------------------------------- The `REVOKE` clause revokes grants created with a specific access method. Revoking a grant ensures that the grant can no longer be used to authenticate. The grant will continue existing in revoked form and the time of the revocation is recorded in the details of the grant. Grants that have already been revoked cannot be revoked again. The statement allows revoking individual grants, all grants or only grants matching a particular SurrealQL expression. SurrealQL SyntaxACCESS @name [ ON [ ROOT | NAMESPACE | DATABASE ] ] REVOKE [ GRANT @id | ALL | WHERE @expression ] ] #### Example: Revoking a specific grant[](https://surrealdb.com/docs/surrealql/statements/access#example-revoking-a-specific-grant) -- Create record representing a user CREATE user:1 CONTENT { name: "tobie" }; -- Define bearer access method to generate API keys DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD DURATION FOR GRANT 10d; -- Generate bearer grant to be used by the user ACCESS api GRANT FOR RECORD user:1; Response-- Query 1 [\ {\ id: user:1,\ name: 'tobie'\ }\ ] -- Query 2 NONE -- Query 3 { ac: 'api', creation: d'2024-12-17T10:36:09.215762475Z', expiration: d'2024-12-27T10:36:09.216227523Z', grant: { id: 'NJ2I2d7OXxN9', key: 'surreal-bearer-NJ2I2d7OXxN9-Oa5LqF36IzfURpo6Bhxy9WMF' }, id: 'NJ2I2d7OXxN9', revocation: NONE, subject: { record: user:1 }, type: 'bearer' } ACCESS api REVOKE GRANT NJ2I2d7OXxN9; Response[\ [\ {\ ac: 'api',\ creation: d'2024-12-17T10:36:09.215762475Z',\ expiration: d'2024-12-27T10:36:09.216227523Z',\ grant: {\ id: 'NJ2I2d7OXxN9',\ key: '[REDACTED]'\ },\ id: 'NJ2I2d7OXxN9',\ revocation: d'2024-12-17T10:36:52.740438379Z',\ subject: {\ record: user:1\ },\ type: 'bearer'\ }\ ]\ ] #### Example: Revoking all grants for users of a certain name[](https://surrealdb.com/docs/surrealql/statements/access#example-revoking-all-grants-for-users-of-a-certain-name) Since the `subject` attribute of grants associated with a record is a record identifier, it can be used as a [record link](https://surrealdb.com/docs/surrealql/datamodel/records) in order to access any record fields. This can be used to filter grants associated with record users matching certain conditions based on arbitrary data. -- Create records representing users CREATE user:1 CONTENT { name: "tobie" }; CREATE user:2 CONTENT { name: "jaime" }; -- Define bearer access method to generate API keys DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD DURATION FOR GRANT 10d; -- Generate bearer grants to be used by the users ACCESS api GRANT FOR RECORD user:1; ACCESS api GRANT FOR RECORD user:2; Response-- Query 1 [\ {\ id: user:1,\ name: 'tobie'\ }\ ] -- Query 2 [\ {\ id: user:2,\ name: 'jaime'\ }\ ] -- Query 3 NONE -- Query 4 { ac: 'api', creation: d'2024-12-17T10:42:35.040901759Z', expiration: d'2024-12-27T10:42:35.040903414Z', grant: { id: 'mjSACes6sej4', key: 'surreal-bearer-mjSACes6sej4-WbEPMgmLTO3Jfg3po4we9m0V' }, id: 'mjSACes6sej4', revocation: NONE, subject: { record: user:1 }, type: 'bearer' } -- Query 5 { ac: 'api', creation: d'2024-12-17T10:42:35.043162877Z', expiration: d'2024-12-27T10:42:35.043164533Z', grant: { id: 'RFilJMRp9lZi', key: 'surreal-bearer-RFilJMRp9lZi-OmflYxXwikDAvm8CNpsWYxd6' }, id: 'RFilJMRp9lZi', revocation: NONE, subject: { record: user:2 }, type: 'bearer' } ACCESS api REVOKE WHERE subject.record.name = "tobie"; Response[\ [\ {\ ac: 'api',\ creation: d'2024-12-17T10:42:35.040901759Z',\ expiration: d'2024-12-27T10:42:35.040903414Z',\ grant: {\ id: 'mjSACes6sej4',\ key: '[REDACTED]'\ },\ id: 'mjSACes6sej4',\ revocation: d'2024-12-17T10:43:23.944198560Z',\ subject: {\ record: user:1\ },\ type: 'bearer'\ }\ ]\ ] `PURGE`[](https://surrealdb.com/docs/surrealql/statements/access#purge) ------------------------------------------------------------------------ The `PURGE` clause completely removes grants created with a specific access method that have already been expired or revoked. In scenarios with very large amount of grants associated with an access method (e.g. when grants are automatically generated), purging inactive grants can improve the performance and experience of auditing grants with the `SHOW` clause. In some very high volume scenarios where grants are created by the hundreds of millions, purging may be necessary to limit the probability of collisions resulting in failure to create new grants. Note that the performance cost of validating a grant is independent from the number of existing grants, regardless of whether they are active or inactive. Beware that any details associated with purged grants will be permanently lost and will no longer be available for auditing purposes. The `FOR` clause can be used to establish a minimum grace period after which expired or revoked grants should be purged. As with other security-sensitive operations related with the `ACCESS` statement, the purging of grants is logged in the SurrealDB server. The clause will return the details of all grants that have successfully been purged and its performance will depend on the number of purged grants. SurrealQL SyntaxACCESS @name [ ON [ ROOT | NAMESPACE | DATABASE ] ] PURGE [ EXPIRED | REVOKED [ , EXPIRED | REVOKED ] ] [ FOR @duration ] ] #### Example: Purging grants that have been expired[](https://surrealdb.com/docs/surrealql/statements/access#example-purging-grants-that-have-been-expired) ACCESS api PURGE EXPIRED; #### Example: Purging grants that have been revoked for more than 90 days[](https://surrealdb.com/docs/surrealql/statements/access#example-purging-grants-that-have-been-revoked-for-more-than-90-days) ACCESS api PURGE REVOKED FOR 90d; #### Example: Purging all grants that have been invalid for more than a year[](https://surrealdb.com/docs/surrealql/statements/access#example-purging-all-grants-that-have-been-invalid-for-more-than-a-year) ACCESS api PURGE EXPIRED, REVOKED FOR 1y; Improving error output[](https://surrealdb.com/docs/surrealql/statements/access#improving-error-output) -------------------------------------------------------------------------------------------------------- To improve the output of an error message inside an `ACCESS` statement, a [`THROW`](https://surrealdb.com/docs/surrealql/statements/throw) statement can be used. Follow these steps to see the output in practice. First, start the SurrealDB server with a root user: surreal start --user root --pass secret Log in as the root user, choose the namespace `test` and database `test`, and run the following `DEFINE ACCESS` command. DEFINE ACCESS account ON DATABASE TYPE RECORD SIGNUP ({ IF $email = "me@me.com" { THROW "That's my email!!!" } ELSE { CREATE user SET email = $email, pass = crypto::argon2::generate($pass)} }) SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) DURATION FOR TOKEN 15m, FOR SESSION 12h ; As the `THROW` statement checks for the email address `me@me.com`, this can be tested using CURL at the [`signup`](https://surrealdb.com/docs/surrealdb/integration/http#signup) endpoint via the following command. curl -X POST -H "Accept: application/json" -d '{"ns":"test","db":"test","ac":"account", "email": "me@me.com", "pass": "strongpassword"}' http://localhost:8000/signup The error output shows the user-defined error message `That's my email!!!`. Output{"code":400,"details":"Request problems detected","description":"There is a problem with your request. Refer to the documentation for further information.","information":"There was a problem with the database: An error occurred: That's my email!!!" In all other cases, the signup process will work, returning a token. curl -X POST -H "Accept: application/json" -d '{"ns":"test","db":"test","ac":"account", "email": "someotheremail@me.com", "pass": "strongpassword"}' http://localhost:8000/signup Output{"code":200,"details":"Authentication succeeded","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE3NDYwNzIxOTAsIm5iZiI6MTc0NjA3MjE5MCwiZXhwIjoxNzQ2MDczMDkwLCJpc3MiOiJTdXJyZWFsREIiLCJqdGkiOiI3YTc0ZjQ5ZS02OWMxLTRiMjMtYmRhNy05YThkNTNjNWFiZmIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJBQyI6ImFjY291bnQiLCJJRCI6InVzZXI6b2tlZjN5bmI4eXJkd2l3b3h1YjEifQ.4t1xwVkl36PeTFuBj0d41D6A-bwCx7LoNoLlj6yokA8wOKwEa1ldjBzTldZEmylLgP5-q8D4wp6f9Y6ntBZyPA","refresh":null} [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/access.mdx) Copy page as Markdown [Statements](https://surrealdb.com/docs/surrealql/statements) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) ### On this page * [ACCESS statement](https://surrealdb.com/docs/surrealql/statements/access#access-statement) * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/access#statement-syntax) * [GRANT](https://surrealdb.com/docs/surrealql/statements/access#grant) * [SHOW](https://surrealdb.com/docs/surrealql/statements/access#show) * [REVOKE](https://surrealdb.com/docs/surrealql/statements/access#revoke) * [PURGE](https://surrealdb.com/docs/surrealql/statements/access#purge) * [Improving error output](https://surrealdb.com/docs/surrealql/statements/access#improving-error-output) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/access.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Demo data | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) Demo data[](https://surrealdb.com/docs/surrealql/demo#demo-data) ================================================================= To quickly test out SurrealDB and SurrealQL functionality, we’ve included two demo datasets here in `.surql` files which you can download and [`import`](https://surrealdb.com/docs/surrealdb/cli/import) into SurrealDB using the CLI Surreal Deal Store - there is a lot in store for you![](https://surrealdb.com/docs/surrealql/demo#surreal-deal-store---there-is-a-lot-in-store-for-you) -------------------------------------------------------------------------------------------------------------------------------------------------------- Surreal Deal Store is our new and improved demo dataset based on our [SurrealDB Store](https://surrealdb.store/) . The dataset is made up of 12 tables using both [graph relations](https://surrealdb.com/docs/surrealql/statements/relate) and [record links](https://surrealdb.com/docs/surrealql/datamodel/records) . In the diagram below, the nodes in pink are the [standard tables](https://surrealdb.com/docs/surrealql/statements/define/table) , the ones in purple represent the [edge tables](https://surrealdb.com/docs/surrealql/statements/relate) which shows relationships between records and SurrealDB as a graph database. While the nodes in gray are the [pre-computed table views](https://surrealdb.com/docs/surrealql/statements/define/table) . ![Surreal Deal Data Model](https://surrealdb.com/docs/_astro/surreal-deal-store-light.BmMOlc8M_3RL54.webp) ### Download[](https://surrealdb.com/docs/surrealql/demo#download) | Dataset | URL | | --- | --- | | [Surreal Deal Store](https://datasets.surrealdb.com/surreal-deal-store.surql) | [https://datasets.surrealdb.com/surreal-deal-store.surql](https://datasets.surrealdb.com/surreal-deal-store.surql) | | [Surreal Deal Store (mini)](https://datasets.surrealdb.com/surreal-deal-store-mini.surql) | [https://datasets.surrealdb.com/surreal-deal-store-mini.surql](https://datasets.surrealdb.com/surreal-deal-store-mini.surql) | ### Import[](https://surrealdb.com/docs/surrealql/demo#import) First, download any of the [available datasets](https://surrealdb.com/docs/surrealql/demo#download) . Secondly, [start the server](https://surrealdb.com/docs/surrealdb/cli/start) . # Create a new in-memory server surreal start --user root --pass secret --allow-all Lastly, use the [import command](https://surrealdb.com/docs/surrealdb/cli/import) to add the dataset. Use the command below to import the [surreal deal store dataset](https://datasets.surrealdb.com/surreal-deal-store.surql) : surreal import --conn http://localhost:8000 --user root --pass secret --ns test --db test surreal-deal-store.surql To import the surreal downloaded the [Surreal Deal store (mini)](https://datasets.surrealdb.com/surreal-deal-store-mini.surql) use the command below: surreal import --conn http://localhost:8000 --user root --pass secret --ns test --db test surreal-deal-store-mini.surql Please be aware that the import process might take a few seconds. ### Using Curl[](https://surrealdb.com/docs/surrealql/demo#using-curl) First, start the surrealdb server # Create a new in-memory server surreal start --user root --pass secret --allow-all Then, download the file and load it into the database # Download the file curl -L "https://datasets.surrealdb.com/surreal-deal-store.surql" -o surreal-deal-store.surql # Load the file into the database using the rest endpoint curl -v -X POST -u "root:root" -H "NS: test" -H "DB: test" -H "Accept: application/json" --data-binary @surreal-deal-store.surql http://localhost:8000/import If you want to use the mini version: # Download the file curl -L "https://datasets.surrealdb.com/surreal-deal-store-mini.surql" -o surreal-deal-store-mini.surql # Load the file into the database using the rest endpoint curl -v -X POST -u "root:root" -H "NS: test" -H "DB: test" -H "Accept: application/json" --data-binary @surreal-deal-store-mini.surql http://localhost:8000/import ### Sample queries[](https://surrealdb.com/docs/surrealql/demo#sample-queries) Here are some sample queries you can run on the Surreal Deal Store dataset. We’ve also included a [Surrealist Mini](https://app.surrealdb.com/mini) below to help you run these queries. > Note > > The query results below have been limited to 4 rows for brevity. If you remove the `LIMIT 4` clause from the queries, you’ll see the full results. [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/demo.mdx) Copy page as Markdown [Overview](https://surrealdb.com/docs/surrealql) [Operators](https://surrealdb.com/docs/surrealql/operators) ### On this page * [Surreal Deal Store - there is a lot in store for you!](https://surrealdb.com/docs/surrealql/demo#surreal-deal-store---there-is-a-lot-in-store-for-you) * [Download](https://surrealdb.com/docs/surrealql/demo#download) * [Import](https://surrealdb.com/docs/surrealql/demo#import) * [Using Curl](https://surrealdb.com/docs/surrealql/demo#using-curl) * [Sample queries](https://surrealdb.com/docs/surrealql/demo#sample-queries) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/demo.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Casting | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) Casting[](https://surrealdb.com/docs/surrealql/datamodel/casting#casting) ========================================================================== In the SurrealDB type system, values can be converted to other values efficiently. This is useful if input is specified in a query which must be of a certain type, or if a user may have provided a parameter with an incorrect type. | Type | Description | | --- | --- | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#array) | Casts the subsequent value into an array | | [`>`](https://surrealdb.com/docs/surrealql/datamodel/casting#arrayt) | Casts the subsequent value into an array of `T` | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#bool) | Casts the subsequent value into a boolean | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#datetime) | Casts the subsequent value into a datetime | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#decimal) | Casts the subsequent value into a decimal | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#duration) | Casts the subsequent value into a duration | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#float) | Casts the subsequent value into a float | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#int) | Casts the subsequent value into a int | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#number) | Casts the subsequent value into a decimal | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#record) | Casts the subsequent value into a record | | [`>`](https://surrealdb.com/docs/surrealql/datamodel/casting#recordt) | Casts the subsequent value into a record of `T` | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#set) | Casts the subsequent value into a set | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#string) | Casts the subsequent value into a string | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#regex) | Casts the subsequent value into a regular expression | | [``](https://surrealdb.com/docs/surrealql/datamodel/casting#uuid) | Casts the subsequent value into a UUID | ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#array) -------------------------------------------------------------------------- The `` casting function converts a range into an array. RETURN 1..=3; [1, 2, 3] `>`[](https://surrealdb.com/docs/surrealql/datamodel/casting#arrayt) ------------------------------------------------------------------------------ The `>` casting function converts a value into an array of the specified type. > Note > > When using this casting function, the value must be an array and each element in the array will be cast to the specified type. RETURN > ["42", "314", "271", "137", "141"]; [42, 314, 271, 137, 141] RETURN > [42, 314, 271, 137, 141]; ["42", "314", "271", "137", "141"] A cast into an array of more than one possible type can also be used. In this case, the cast will attempt to cast into the possible types in order. As such, the `string` in the first query below will be cast into a `datetime` but not in the second. RETURN [\ >["2020-09-09", "21 Jan 2020"],\ >["2020-09-09", "21 Jan 2020"]\ ]; Output[\ [\ d'2020-09-09T00:00:00Z',\ '21 Jan 2020'\ ],\ [\ '2020-09-09',\ '21 Jan 2020'\ ]\ ] An example of even more complex casting which attempts to cast each item in the input array into a `record`, then `record`, then `array>`, and finally `string`. RETURN | array> | string>> [\ 'person:one',\ 'user:two',\ [\ 'user:three',\ 'user:four'\ ],\ 'not_a_person_or_user'\ ]; Output[\ person:one,\ user:two,\ [\ user:three,\ user:four\ ],\ 'not_a_person_or_user'\ ] ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#bool) ------------------------------------------------------------------------ The ` "true"; true RETURN "false"; false ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#datetime) -------------------------------------------------------------------------------- The `` casting function converts a value into a datetime. RETURN "2022-06-07"; d'2022-06-07T00:00:00Z' ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#decimal) ------------------------------------------------------------------------------ The `` casting function converts a value into an infinite precision decimal number. RETURN 13.572948467293847293841093845679289; 13.572948467293847293841093845679289 RETURN "13.572948467293847293841093845679289"; "13.572948467293847293841093845679289" RETURN 1.193847193847193847193487E11; "119384719384.7193847193487" ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#duration) -------------------------------------------------------------------------------- The `` casting function converts a value into a duration. RETURN "1h30m"; 1h30m ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#float) -------------------------------------------------------------------------- The `` casting function converts a value into a floating point number. RETURN 13.572948467293847293841093845679289; 13.572948467293847 RETURN "13.572948467293847293841093845679289"; 13.572948467293847 ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#int) ---------------------------------------------------------------------- The `` casting function converts a value into an integer. RETURN 53; 53 ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#number) ---------------------------------------------------------------------------- The `` casting function converts a value into a `number`. RETURN 13.572948467293847293841093845679289; "13.572948467293847293841093845679289" RETURN "13.572948467293847293841093845679289"; "13.572948467293847293841093845679289" RETURN 1.193847193847193847193487E11; "119384719384.7193847193487" ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#record) ---------------------------------------------------------------------------- The `` casting function converts a value into a record. Keep in mind when using this casting function that if the equivalent record id does not exist, it will not return anything. SELECT id FROM (s"person:hrebrffwm4sr2yifglta"); Output{ id: person:hrebrffwm4sr2yifglta } `>`[](https://surrealdb.com/docs/surrealql/datamodel/casting#recordt) -------------------------------------------------------------------------------- The `>` casting function converts a value into a record. Keep in mind when using this casting function that if the equivalent record id does not exist, it will not return anything. SELECT id FROM (s"person:hrebrffwm4sr2yifglta"); { id: person:hrebrffwm4sr2yifglta } A cast into a number of possible record types can also be used. RETURN [\ >"user:one",\ >>["person:one", "user:two"]\ ]; Output[\ user:one,\ [\ person:one,\ user:two\ ]\ ] `` and `>`[](https://surrealdb.com/docs/surrealql/datamodel/casting#set-and-sett) ---------------------------------------------------------------------------------------------- The `` casting function converts a value into a set. As a set is simply an array with deduplicated items, all of the examples in the [section for arrays](https://surrealdb.com/docs/surrealql/datamodel/casting#array) will work. RETURN [\ >["2020-09-09", "21 Jan 2020"],\ >["2020-09-09", "21 Jan 2020"]\ ]; Output[\ [\ d'2020-09-09T00:00:00Z',\ '21 Jan 2020'\ ],\ [\ '2020-09-09',\ '21 Jan 2020'\ ]\ ] Using a `` cast on an existing array is functionally identical to using the [`array::distinct()`](https://surrealdb.com/docs/surrealql/functions/database/array#arraydistinct) function. LET $array = [1,1,3,4,4,4,4,4,4]; RETURN [\ $array.distinct(),\ $array\ ]; Output[\ [\ 1,\ 3,\ 4\ ],\ [\ 1,\ 3,\ 4\ ]\ ] ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#string) ---------------------------------------------------------------------------- The `` casting function converts a value into a string. RETURN true; "true" RETURN 1.3463; "1.3463" RETURN false; "false" ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#regex) -------------------------------------------------------------------------- The `` casting function converts a value into a regular expression. RETURN "a|b" = "a"; true RETURN "a|b" = "c"; false ``[](https://surrealdb.com/docs/surrealql/datamodel/casting#uuid) ------------------------------------------------------------------------ The `` casting function converts a value into a UUID. SELECT id FROM "a8f30d8b-db67-47ec-8b38-ef703e05ad1b"; [ u'a8f30d8b-db67-47ec-8b38-ef703e05ad1b' ] General notes on casting[](https://surrealdb.com/docs/surrealql/datamodel/casting#general-notes-on-casting) ------------------------------------------------------------------------------------------------------------ ### Syntax and order[](https://surrealdb.com/docs/surrealql/datamodel/casting#syntax-and-order) As the parser ignores spaces and new lines, casting syntax can include spaces or new lines as desired. -- Surrealist formatted syntax RETURN > [\ '9.1',\ 'true',\ 15h\ ]; -- Maybe someone's preferred syntax RETURN > [ '9.1', 'true', 15h ]; When more than one cast type is specified, SurrealDB will attempt to convert into the type in the order specified. In the example above, while the input `'9.1'` could have been converted to a float, the type `string` comes first in the cast syntax and thus `'9.1'` remains as a string. Output[\ '9.1',\ true,\ '15h'\ ] ### Casting vs. affixes[](https://surrealdb.com/docs/surrealql/datamodel/casting#casting-vs-affixes) SurrealDB uses a number of affixes to force the parser to treat an input as a certain type instead of another. These affixes may seem at first glance to be identical to casts, as the following queries show. -- All return a record person:one RETURN r"person:one"; RETURN "person:one"; RETURN >"person:one"; -- Returns a string 'person:one' RETURN "person:one"; -- Both return a decimal 98dec RETURN 98dec; RETURN 98; -- Returns an int 98 RETURN 98; However, casts and affixes work in different ways: * A cast is a way to convert from one type into another. * An affix is an instruction to the parser to treat an input as a certain type. These differences become clear when working with input that is less than ideal or does not work with a certain type. For example, floats by nature become imprecise after a certain number of digits. RETURN [\ 8.888,\ 8.8888888888888888\ ]; Output[\ 8.888f,\ 8.88888888888889f\ ] In this case, a `decimal` can be used which will allow a greater number of digits after the decimal point. However, casting the above numbers into a `decimal` will result in the same inaccurate output. RETURN [\ 8.888,\ 8.888888888888888\ ]; Output[\ 8.888dec,\ 8.88888888888889dec\ ] This is because the parser will first treat the number as a float and then cast it into a `decimal`. However, using the `dec` suffix will inform the parser that the entire input is to be treated as a `decimal` and it will never pass through a stage in which it is a float. RETURN [\ 8.888dec,\ 8.888888888888888dec\ ]; Output[\ 8.888dec,\ 8.888888888888888dec\ ] Similarly, an attempt to cast a number that is too large for an `int` into a `decimal` will not work, as the parser will first attempt to handle the number on the right before moving on to the cast. 9999999999999999999; OutputParse error: Failed to parse number: number too large to fit in target type However, if the same number is followed by the `dec` suffix, the parser will be aware that the input is meant to be treated as a `decimal` from the outset and the query will succeed. RETURN 9999999999999999999dec; [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/casting.mdx) Copy page as Markdown [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) ### On this page * [](https://surrealdb.com/docs/surrealql/datamodel/casting#array) * [>](https://surrealdb.com/docs/surrealql/datamodel/casting#arrayt) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#bool) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#datetime) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#decimal) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#duration) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#float) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#int) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#number) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#record) * [>](https://surrealdb.com/docs/surrealql/datamodel/casting#recordt) * [ and >](https://surrealdb.com/docs/surrealql/datamodel/casting#set-and-sett) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#string) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#regex) * [](https://surrealdb.com/docs/surrealql/datamodel/casting#uuid) * [General notes on casting](https://surrealdb.com/docs/surrealql/datamodel/casting#general-notes-on-casting) * [Syntax and order](https://surrealdb.com/docs/surrealql/datamodel/casting#syntax-and-order) * [Casting vs. affixes](https://surrealdb.com/docs/surrealql/datamodel/casting#casting-vs-affixes) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/casting.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE API statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) DEFINE API statement[](https://surrealdb.com/docs/surrealql/statements/define/api#define-api-statement) ======================================================================================================== Available since: v2.2.0 > Caution > > Currently, this is an experimental feature as such, it may be subject to breaking changes and may present unidentified security issues. Do not rely on this feature in production applications. To enable this, set the `SURREAL_CAPS_ALLOW_EXPERIMENTAL` [environment variable](https://surrealdb.com/docs/surrealdb/cli/start) > to `define_api`. The `DEFINE API` statements allows a custom endpoint to be created. Each endpoint created by a `DEFINE API` statement is located at the `/api/:namespace/:database/:endpoint_name` path. For example, an endpoint for the path `get_users` for the namespace `my_namespace` and database `my_database` will have the path `/api/my_namespace/my_database/get_users`. The response is an object with a combination of the following properties: * `raw` - A boolean that when set to `true` sends the response back as raw bytes or a string. * `status` - A valid HTTP status code. * `headers` - An object of valid header key value pairs. * `body` - A SurrealQL value, automatically encoded back into how the user requested it. When `raw` is set to `true`, this needs to be a string or bytes value. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/api#statement-syntax) ------------------------------------------------------------------------------------------------ SurrealQL SyntaxDEFINE API [ OVERWRITE | IF NOT EXISTS ] @endpoint [ FOR @HTTP_method, .. ] [ MIDDLEWARE @function, .. ] [ THEN { @value } ] [ PERMISSIONS [ NONE | FULL | @expression ]\ \ `DEFINE API` is often used in conjunction with a [capabilities flag](https://surrealdb.com/docs/surrealdb/security/capabilities)\ or [environment variable](https://surrealdb.com/docs/surrealdb/cli/env)\ to disable arbitrary queries, thereby forcing record and anonymous users to interact with the database via API endpoints alone.\ \ Quick example[](https://surrealdb.com/docs/surrealql/statements/define/api#quick-example)\ \ ------------------------------------------------------------------------------------------\ \ Defining an API endpointDEFINE API "/test"\ FOR get, post \ MIDDLEWARE\ api::req::raw_body(false)\ THEN {\ RETURN {\ status: 200,\ body: {\ request: $request.body,\ response: "The server works"\ },\ headers: {\ 'last-modified': time::now(),\ 'expires': time::now() + 4d\ }\ };\ };\ \ An API endpoint can be tested using the [`api::invoke` function](https://surrealdb.com/docs/surrealql/functions/database/api)\ , which takes either the path as a single string or the path along with a request body. It can also be tested via [CURL or other means by directly using the endpoint](https://surrealdb.com/docs/surrealdb/integration/http)\ along with the namespace and database in the headers.\ \ api::invoke("/test");\ \ api::invoke("/test", {\ body: {\ hi: "please",\ give: "me",\ the: "information"\ }\ });\ \ Output-------- Query --------\ \ {\ body: {\ request: NONE,\ response: 'The server works'\ },\ headers: {\ "access-control-allow-origin": '*',\ expires: '2025-02-24T02:43:50.137321Z',\ "last-modified": '2025-02-20T02:43:50.137326Z'\ },\ raw: false,\ status: 200\ }\ \ -------- Query --------\ \ {\ body: {\ request: {\ give: 'me',\ hi: 'please',\ the: 'information'\ },\ response: 'The server works'\ },\ headers: {\ "access-control-allow-origin": '*',\ expires: '2025-02-24T02:43:50.137455Z',\ "last-modified": '2025-02-20T02:43:50.137457Z'\ },\ raw: false,\ status: 200\ }\ \ API paths[](https://surrealdb.com/docs/surrealql/statements/define/api#api-paths)\ \ ----------------------------------------------------------------------------------\ \ The path of a `DEFINE API` statement can be static, such as `"/test"`, dynamic, or the remainder of a URL.\ \ A dynamic path uses a `:` (colon) followed by a name, which will match on anything passed in at that section of a path.\ \ DEFINE API OVERWRITE "/test/:anything_goes" FOR get THEN {\ RETURN {\ body: {\ some: "data"\ }\ }\ };\ \ api::invoke("/test/this_matches");\ api::invoke("/test/same_here");\ api::invoke("/test/but/this/wont/match");\ \ The first two `api::invoke` calls return the output below, but the third returns nothing as `:anything_goes` only applies to a single path segment.\ \ Output{\ body: {\ some: 'data'\ },\ headers: {},\ raw: false,\ status: 200\ }\ \ To match on the remainder of a URL, change the `:` (colon) to a `*` (star).\ \ DEFINE API OVERWRITE "/test/*anything_goes" FOR get THEN {\ RETURN {\ body: {\ some: "data"\ }\ }\ };\ \ api::invoke("/test/this_matches");\ api::invoke("/test/same_here");\ api::invoke("/test/works/with/multiple/paths/now");\ \ All three `api::invoke` calls will now show the following output.\ \ Output{\ body: {\ some: 'data'\ },\ headers: {},\ raw: false,\ status: 200\ }\ \ [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/api.mdx)\ Copy page as Markdown\ \ [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer)\ [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket)\ \ ### On this page\ \ * [DEFINE API statement](https://surrealdb.com/docs/surrealql/statements/define/api#define-api-statement)\ \ * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/api#statement-syntax)\ \ * [Quick example](https://surrealdb.com/docs/surrealql/statements/define/api#quick-example)\ \ * [API paths](https://surrealdb.com/docs/surrealql/statements/define/api#api-paths)\ \ \ Last updated: Loading...\ \ ### Was this page helpful?\ \ #### What did you like?\ \ AccurateEasy to understandSolved my problemHelped me decide to use the productOther\ \ SubmitCancel\ \ #### What went wrong?\ \ Hard to understandIncorrect informationMissing the informationOther\ \ SubmitCancel\ \ [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/api.mdx)\ [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # DEFINE CONFIG statement | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Overview](https://surrealdb.com/docs/surrealql/statements) [Overview](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) `DEFINE CONFIG` statement[](https://surrealdb.com/docs/surrealql/statements/define/config#define-config-statement) =================================================================================================================== The `DEFINE CONFIG` statement allows you to set external configurations on your database. It can be used to configure API middleware and permissions, or to configure how the database’s tables and functions are exposed via the GraphQL API. > Warning > > By running SurrealDB with configurations for the database API or the GraphQL module, you are opting into an experimental feature. While these features are fully functional, they are still considered experimental and may not be as stable as the core SurrealQL module which means we cannot guarantee that it will provide the same security guarantees. They are not recommended for production use. We welcome your feedback and contributions to help improve the feature and make it more robust. Requirements[](https://surrealdb.com/docs/surrealql/statements/define/config#requirements) ------------------------------------------------------------------------------------------- * You **must** be authenticated as a **root**, **namespace**, or **database** user before you can use the `DEFINE CONFIG GRAPHQL` statement. * You **must** select your **namespace** and **database** before you can use the `DEFINE CONFIG GRAPHQL` statement. * You **must** define at least one table in your database for the GraphQL API to function. * You **must** have started the SurrealDB instance with the `SURREAL_CAPS_ALLOW_EXPERIMENTAL` environment variable set to `"graphql"`. Statement syntax[](https://surrealdb.com/docs/surrealql/statements/define/config#statement-syntax) --------------------------------------------------------------------------------------------------- SurrealQL SyntaxDEFINE CONFIG [ OVERWRITE | IF NOT EXISTS ] [ API [ MIDDLEWARE @expression, .. ] [ PERMISSIONS [ NONE | FULL | @expression ] ]\ \ [ GRAPHQL \ [ AUTO | NONE ]\ [ TABLES (AUTO | NONE | INCLUDE table1, table2, ...) ]\ [ FUNCTIONS (AUTO | NONE | INCLUDE [function1, function2, ...] | EXCLUDE [function1, function2, ...]) ] ]\ \ DEFINE CONFIG API[](https://surrealdb.com/docs/surrealql/statements/define/config#define-config-api)\ \ -----------------------------------------------------------------------------------------------------\ \ The `DEFINE CONFIG API` statement can be used to set middleware and permissions in order to alter the behaviour of the database for guest and [record users](https://surrealdb.com/docs/surrealql/statements/define/access/record)\ . This middleware can be used in cases such as rate limiting on the query language and setting how many resources a client can read or alter at a time.\ \ `DEFINE CONFIG API` is often used in conjunction with a [flag](https://surrealdb.com/docs/surrealdb/security/capabilities)\ or [environment variable](https://surrealdb.com/docs/surrealdb/cli/env)\ to disable arbitrary queries, thereby forcing record and anonymous users to interact with the database via API endpoints alone.\ \ The following is an example of a `DEFINE CONFIG` statement that includes a timeout and a single header in the responses of all API endpoints.\ \ DEFINE CONFIG API\ MIDDLEWARE \ api::timeout(10s),\ api::res::headers({\ 'Access-Control-Allow-Origin': '*'\ });\ \ To set the actual API endpoints and their middleware, a [`DEFINE API`](https://surrealdb.com/docs/surrealql/statements/define/api)\ statement is used for each endpoint.\ \ The behaviour of API endpoints can be tested using the `api::invoke` method, or through a regular HTTP call to the endpoint that [includes the namespace and database name](https://surrealdb.com/docs/surrealdb/integration/http#custom-endpoint-at-apinsdbendpoint)\ .\ \ This next example uses `DEFINE CONFIG` to set a single response header, followed by two endpoints that return different output.\ \ DEFINE CONFIG API\ MIDDLEWARE \ api::res::headers({\ 'Access-Control-Allow-Origin': '*'\ });\ \ DEFINE API "/test" FOR get THEN {};\ DEFINE API "/test2" FOR get THEN {\ RETURN {\ body: {\ some: "data"\ }\ }\ };\ \ api::invoke("/test");\ api::invoke("/test2");\ \ The query shows that the endpoints return a combination of the `DEFINE CONFIG` middleware and the response object set in the `DEFINE API` statements.\ \ -------- Query --------\ \ {\ headers: {\ "access-control-allow-origin": '*'\ },\ raw: false,\ status: 200\ }\ \ -------- Query --------\ \ {\ body: {\ some: 'data'\ },\ headers: {\ "access-control-allow-origin": '*'\ },\ raw: false,\ status: 200\ }\ \ Note that the middleware and permissions inside individual `DEFINE API` statements will override the middleware in a `DEFINE CONFIG API` statement. In the following example, the default `10s` timeout is set to a single microsecond for the `"/test"` endpoint, giving the intended query no time to complete.\ \ DEFINE CONFIG API\ MIDDLEWARE \ api::timeout(10s),\ api::res::headers({\ 'Access-Control-Allow-Origin': '*'\ });\ \ DEFINE API OVERWRITE "/test"\ FOR get \ MIDDLEWARE\ api::timeout(1µs)\ THEN {\ RETURN {\ status: 200,\ body: { \ data: (SELECT * FROM person),\ however: "This will probably never return because the timeout is 1 microsecond"\ }\ };\ };\ \ api::invoke("/test");\ \ Output'The query was not executed because it exceeded the timeout'\ \ DEFINE CONFIG GRAPHQL[](https://surrealdb.com/docs/surrealql/statements/define/config#define-config-graphql)\ \ -------------------------------------------------------------------------------------------------------------\ \ The configuration set using the `DEFINE CONFIG GRAPHQL` is essential for enabling GraphQL functionality in your database, specifying which tables and functions should be included or excluded from the GraphQL schema.\ \ The GraphQL configuration defined using this statement dictates how clients interact with your database through GraphQL queries and mutations.\ \ ### Important Notes[](https://surrealdb.com/docs/surrealql/statements/define/config#important-notes)\ \ * The `DEFINE CONFIG GRAPHQL` statement **must** be executed before any GraphQL queries can be made.\ * If you attempt to use the GraphQL API without defining the configuration, you will receive a `NotConfigured` error.\ * If no tables are defined in the database, you will receive an error stating “No tables found in database” when attempting to use the GraphQL API.\ \ ### Example usage[](https://surrealdb.com/docs/surrealql/statements/define/config#example-usage)\ \ -- Define GraphQL configuration\ DEFINE CONFIG GRAPHQL AUTO;\ \ ### Tables Configuration[](https://surrealdb.com/docs/surrealql/statements/define/config#tables-configuration)\ \ The `TABLES` clause in the `DEFINE CONFIG GRAPHQL` statement specifies how tables are exposed via GraphQL. There are four options for the `TABLES` configuration:\ \ * `AUTO`: Automatically include all tables in the GraphQL schema.\ * `NONE`: Do not include any tables in the GraphQL schema.\ * `INCLUDE`: Specify a list of tables to include in the GraphQL schema.\ * `EXCLUDE`: Specify a list of tables to exclude from the GraphQL schema.\ \ #### `AUTO`[](https://surrealdb.com/docs/surrealql/statements/define/config#auto)\ \ When you specify `TABLES AUTO`, all tables in the database are automatically included in the GraphQL schema.\ \ DEFINE CONFIG GRAPHQL TABLES AUTO;\ \ #### `NONE`[](https://surrealdb.com/docs/surrealql/statements/define/config#none)\ \ When you specify `TABLES NONE`, no tables are included in the GraphQL schema.\ \ DEFINE CONFIG GRAPHQL TABLES NONE;\ \ #### `INCLUDE`[](https://surrealdb.com/docs/surrealql/statements/define/config#include)\ \ You can specify a list of tables to include in the GraphQL schema using the `INCLUDE` clause. The list of tables is specified as a comma-separated list without brackets.\ \ DEFINE CONFIG GRAPHQL TABLES INCLUDE user, post, comment;\ \ > Note\ > \ > The `EXCLUDE` option for `TABLES` is currently not implemented.\ \ ### Functions Configuration[](https://surrealdb.com/docs/surrealql/statements/define/config#functions-configuration)\ \ The `FUNCTIONS` clause in the `DEFINE CONFIG GRAPHQL` statement specifies how functions are exposed via GraphQL. There are four options for the `FUNCTIONS` configuration:\ \ * `AUTO`: Automatically include all functions in the GraphQL schema.\ * `NONE`: Do not include any functions in the GraphQL schema.\ * `INCLUDE`: Specify a list of functions to include in the GraphQL schema.\ * `EXCLUDE`: Specify a list of functions to exclude from the GraphQL schema.\ \ #### `AUTO`[](https://surrealdb.com/docs/surrealql/statements/define/config#auto-1)\ \ When you specify `FUNCTIONS AUTO`, all functions in the database are automatically included in the GraphQL schema.\ \ DEFINE CONFIG GRAPHQL FUNCTIONS AUTO;\ \ #### `NONE`[](https://surrealdb.com/docs/surrealql/statements/define/config#none-1)\ \ When you specify `FUNCTIONS NONE`, no functions are included in the GraphQL schema.\ \ DEFINE CONFIG GRAPHQL FUNCTIONS NONE;\ \ #### `INCLUDE`[](https://surrealdb.com/docs/surrealql/statements/define/config#include-1)\ \ You can specify a list of functions to include in the GraphQL schema using the `INCLUDE` clause. The list of functions is specified as a comma-separated list enclosed in square brackets `[]`.\ \ DEFINE CONFIG GRAPHQL FUNCTIONS INCLUDE [getUser, listPosts, searchComments];\ \ #### `EXCLUDE`[](https://surrealdb.com/docs/surrealql/statements/define/config#exclude)\ \ You can specify a list of functions to exclude from the GraphQL schema using the `EXCLUDE` clause. The list of functions is specified as a comma-separated list enclosed in square brackets `[]`.\ \ DEFINE CONFIG GRAPHQL FUNCTIONS EXCLUDE [debugFunction, testFunction];\ \ ### Using `IF NOT EXISTS` clause[](https://surrealdb.com/docs/surrealql/statements/define/config#using-if-not-exists-clause)\ \ The `IF NOT EXISTS` clause can be used to define the GraphQL configuration only if it does not already exist. This is useful when you want to ensure that the configuration is only created if it does not already exist, preventing errors due to duplicate definitions.\ \ -- Define GraphQL configuration only if it does not already exist\ DEFINE CONFIG GRAPHQL IF NOT EXISTS TABLES AUTO FUNCTIONS AUTO;\ \ ### Using `OVERWRITE` clause[](https://surrealdb.com/docs/surrealql/statements/define/config#using-overwrite-clause)\ \ The `OVERWRITE` clause can be used to redefine the GraphQL configuration, overwriting any existing configuration. This is useful when you want to update or modify the existing GraphQL configuration.\ \ -- Redefine GraphQL configuration, overwriting existing configuration\ DEFINE CONFIG OVERWRITE GRAPHQL TABLES INCLUDE user, post FUNCTIONS NONE;\ \ ### Examples[](https://surrealdb.com/docs/surrealql/statements/define/config#examples)\ \ #### Example 1: Include specific tables and functions[](https://surrealdb.com/docs/surrealql/statements/define/config#example-1-include-specific-tables-and-functions)\ \ This example defines a GraphQL configuration that includes specific tables and functions.\ \ DEFINE CONFIG GRAPHQL TABLES INCLUDE user, post FUNCTIONS INCLUDE [getUser, listPosts];\ \ #### Example 2: Automatically include all tables and functions[](https://surrealdb.com/docs/surrealql/statements/define/config#example-2-automatically-include-all-tables-and-functions)\ \ This example defines a GraphQL configuration that automatically includes all tables and functions.\ \ DEFINE CONFIG GRAPHQL TABLES AUTO FUNCTIONS AUTO;\ \ #### Example 3: Exclude specific functions[](https://surrealdb.com/docs/surrealql/statements/define/config#example-3-exclude-specific-functions)\ \ This example defines a GraphQL configuration that includes all functions except specific ones.\ \ DEFINE CONFIG GRAPHQL FUNCTIONS EXCLUDE [debugFunction, testFunction];\ \ ### Error Handling[](https://surrealdb.com/docs/surrealql/statements/define/config#error-handling)\ \ #### NotConfigured Error[](https://surrealdb.com/docs/surrealql/statements/define/config#notconfigured-error)\ \ If you attempt to access the GraphQL endpoint without defining the GraphQL configuration, you will receive a `NotConfigured` error.\ \ Error Response{\ "error": "NotConfigured: GraphQL endpoint is not configured. Please define the GraphQL configuration using DEFINE CONFIG GRAPHQL."\ }\ \ Execute the `DEFINE CONFIG GRAPHQL` statement to define the GraphQL configuration.\ \ -- Define GraphQL configuration\ DEFINE CONFIG GRAPHQL TABLES AUTO FUNCTIONS AUTO;\ \ #### No Tables Found Error[](https://surrealdb.com/docs/surrealql/statements/define/config#no-tables-found-error)\ \ If you have defined the GraphQL configuration but no tables are defined in the database, you will receive an error stating “No tables found in database” when attempting to use the GraphQL API. You can fix this by defining at least one table in your database using the [`DEFINE TABLE`](https://surrealdb.com/docs/surrealql/statements/define/table)\ statement.\ \ Error Response{\ "error": "No tables found in database. Please define at least one table to use the GraphQL API."\ }\ \ **Solution**: Define at least one table in your database.\ \ DEFINE TABLE foo SCHEMAFUL;\ DEFINE FIELD val ON foo TYPE int;\ CREATE foo:1 SET val = 42;\ \ ### Authentication Errors When Accessing Data via GraphQL[](https://surrealdb.com/docs/surrealql/statements/define/config#authentication-errors-when-accessing-data-via-graphql)\ \ **Cause**: Insufficient permissions or incorrect authentication credentials.\ \ **Solution**: Ensure you are authenticated as a user with the necessary permissions and that the permissions on tables and fields are correctly configured.\ \ #### Authentication and Permissions[](https://surrealdb.com/docs/surrealql/statements/define/config#authentication-and-permissions)\ \ * The GraphQL API respects SurrealDB’s authentication and permission model.\ * You must authenticate using the appropriate credentials to access data via GraphQL.\ * Permissions set on tables and fields will affect the data accessible through the GraphQL API.\ * If you attempt to access data without sufficient permissions, you will receive an authentication error.\ \ #### Example: Basic Authentication[](https://surrealdb.com/docs/surrealql/statements/define/config#example-basic-authentication)\ \ -- Define a user with access permissions\ DEFINE USER my_user ON DATABASE PASSWORD 'my_password';\ DEFINE ACCESS user ON DATABASE TYPE RECORD\ SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )\ SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )\ DURATION FOR SESSION 60s, FOR TOKEN 1d;\ \ -- Define a table with permissions\ DEFINE TABLE foo SCHEMAFUL PERMISSIONS FOR select WHERE $auth.email = email;\ DEFINE FIELD email ON foo TYPE string;\ DEFINE FIELD val ON foo TYPE int;\ \ -- Insert data\ CREATE foo:1 SET val = 42, email = "user@example.com";\ CREATE foo:2 SET val = 43, email = "other@example.com";\ \ When querying the GraphQL API as `user@example.com`, only the records where `email = "user@example.com"` will be accessible due to the permissions set.\ \ ### Examples[](https://surrealdb.com/docs/surrealql/statements/define/config#examples-1)\ \ #### Example 1: Defining GraphQL Configuration and Fetching Data[](https://surrealdb.com/docs/surrealql/statements/define/config#example-1-defining-graphql-configuration-and-fetching-data)\ \ -- Define GraphQL configuration to include all tables automatically\ DEFINE CONFIG GRAPHQL TABLES AUTO;\ \ -- Define a table and insert data\ DEFINE TABLE foo SCHEMAFUL;\ DEFINE FIELD val ON foo TYPE int;\ CREATE foo:1 SET val = 42;\ CREATE foo:2 SET val = 43;\ \ Now, you can fetch data via GraphQL:\ \ query {\ foo {\ id\ val\ }\ }\ \ \ **Response:**\ \ {\ "data": {\ "foo": [\ {\ "id": "foo:1",\ "val": 42\ },\ {\ "id": "foo:2",\ "val": 43\ }\ ]\ }\ }\ \ #### Example 2: Including Specific Tables[](https://surrealdb.com/docs/surrealql/statements/define/config#example-2-including-specific-tables)\ \ -- Define GraphQL configuration to include only specific tables\ DEFINE CONFIG OVERWRITE GRAPHQL TABLES INCLUDE foo;\ \ When querying the GraphQL schema, only the included tables will be available.\ \ #### Example 3: Using Limit, Start, Order, and Filter in GraphQL Queries[](https://surrealdb.com/docs/surrealql/statements/define/config#example-3-using-limit-start-order-and-filter-in-graphql-queries)\ \ You can use `limit`, `start`, `order`, and `filter` in your GraphQL queries to control the data returned.\ \ ##### Limit[](https://surrealdb.com/docs/surrealql/statements/define/config#limit)\ \ query {\ foo(limit: 1) {\ id\ val\ }\ }\ \ \ ##### Start[](https://surrealdb.com/docs/surrealql/statements/define/config#start)\ \ query {\ foo(start: 1) {\ id\ val\ }\ }\ \ \ ##### Order[](https://surrealdb.com/docs/surrealql/statements/define/config#order)\ \ query {\ foo(order: { desc: val }) {\ id\ val\ }\ }\ \ \ ##### Filter[](https://surrealdb.com/docs/surrealql/statements/define/config#filter)\ \ query {\ foo(filter: { val: { eq: 42 } }) {\ id\ val\ }\ }\ \ \ ### More Information[](https://surrealdb.com/docs/surrealql/statements/define/config#more-information)\ \ The `DEFINE CONFIG GRAPHQL` statement is essential for enabling and configuring the GraphQL API in SurrealDB. By specifying which tables and functions are included or excluded, you can fine-tune the GraphQL schema to match your application’s needs.\ \ * **Authentication**: Ensure you are authenticated with the appropriate credentials.\ * **Permissions**: Set up permissions on tables and fields to control access via GraphQL.\ * **Configuration**: The GraphQL configuration must be defined before using the GraphQL API.\ * **Error Handling**: Be aware of possible errors when the configuration is missing or incomplete.\ \ > Important\ > \ > Always ensure you have the necessary permissions and have selected the appropriate namespace and database before using the `DEFINE CONFIG GRAPHQL` statement.\ \ Summary[](https://surrealdb.com/docs/surrealql/statements/define/config#summary)\ \ ---------------------------------------------------------------------------------\ \ * Use `DEFINE CONFIG GRAPHQL` to enable and configure the GraphQL API.\ * Define your tables and set up permissions to control data access.\ * Use the `OVERWRITE` and `IF NOT EXISTS` clauses as needed to manage your configuration.\ * Always authenticate with appropriate credentials when accessing the GraphQL API.\ * Utilize GraphQL query features like `limit`, `start`, `order`, and `filter` to control the data returned.\ \ By following these guidelines, you can effectively use the `DEFINE CONFIG GRAPHQL` statement to configure and interact with your SurrealDB database via GraphQL.\ \ [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/config.mdx)\ Copy page as Markdown\ \ [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket)\ [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database)\ \ ### On this page\ \ * [DEFINE CONFIG statement](https://surrealdb.com/docs/surrealql/statements/define/config#define-config-statement)\ \ * [Requirements](https://surrealdb.com/docs/surrealql/statements/define/config#requirements)\ \ * [Statement syntax](https://surrealdb.com/docs/surrealql/statements/define/config#statement-syntax)\ \ * [DEFINE CONFIG API](https://surrealdb.com/docs/surrealql/statements/define/config#define-config-api)\ \ * [DEFINE CONFIG GRAPHQL](https://surrealdb.com/docs/surrealql/statements/define/config#define-config-graphql)\ \ * [Important Notes](https://surrealdb.com/docs/surrealql/statements/define/config#important-notes)\ \ * [Example usage](https://surrealdb.com/docs/surrealql/statements/define/config#example-usage)\ \ * [Tables Configuration](https://surrealdb.com/docs/surrealql/statements/define/config#tables-configuration)\ \ \ * [Functions Configuration](https://surrealdb.com/docs/surrealql/statements/define/config#functions-configuration)\ \ \ * [Using IF NOT EXISTS clause](https://surrealdb.com/docs/surrealql/statements/define/config#using-if-not-exists-clause)\ \ * [Using OVERWRITE clause](https://surrealdb.com/docs/surrealql/statements/define/config#using-overwrite-clause)\ \ * [Examples](https://surrealdb.com/docs/surrealql/statements/define/config#examples)\ \ \ * [Error Handling](https://surrealdb.com/docs/surrealql/statements/define/config#error-handling)\ \ \ * [Authentication Errors When Accessing Data via GraphQL](https://surrealdb.com/docs/surrealql/statements/define/config#authentication-errors-when-accessing-data-via-graphql)\ \ \ * [Examples](https://surrealdb.com/docs/surrealql/statements/define/config#examples-1)\ \ \ * [More Information](https://surrealdb.com/docs/surrealql/statements/define/config#more-information)\ \ * [Summary](https://surrealdb.com/docs/surrealql/statements/define/config#summary)\ \ \ Last updated: Loading...\ \ ### Was this page helpful?\ \ #### What did you like?\ \ AccurateEasy to understandSolved my problemHelped me decide to use the productOther\ \ SubmitCancel\ \ #### What went wrong?\ \ Hard to understandIncorrect informationMissing the informationOther\ \ SubmitCancel\ \ [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/statements/define/config.mdx)\ [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) --- # Record IDs | SurrealQL [![SurrealDB](https://surrealdb.com/docs/_astro/surrealdb.DLMSfbiw_Z1p8kgQ.svg)](https://surrealdb.com/) [![SurrealDB Docs Logo](https://surrealdb.com/docs/_astro/docs.Dkm2LXbZ_1wBMMP.svg)](https://surrealdb.com/docs/surrealdb) Search the docs Enter a search query [Sign in](https://app.surrealdb.com/signin) Search the docs Enter a search query [Overview](https://surrealdb.com/docs/surrealql) [Demo data](https://surrealdb.com/docs/surrealql/demo) [Operators](https://surrealdb.com/docs/surrealql/operators) Data types [Introduction](https://surrealdb.com/docs/surrealql/datamodel) [Arrays](https://surrealdb.com/docs/surrealql/datamodel/arrays) [Booleans](https://surrealdb.com/docs/surrealql/datamodel/booleans) [Bytes](https://surrealdb.com/docs/surrealql/datamodel/bytes) [Casting](https://surrealdb.com/docs/surrealql/datamodel/casting) [Closures](https://surrealdb.com/docs/surrealql/datamodel/closures) [Datetimes](https://surrealdb.com/docs/surrealql/datamodel/datetimes) [Files](https://surrealdb.com/docs/surrealql/datamodel/files) [Formatters](https://surrealdb.com/docs/surrealql/datamodel/formatters) [Futures](https://surrealdb.com/docs/surrealql/datamodel/futures) [Geometries](https://surrealdb.com/docs/surrealql/datamodel/geometries) [Idioms](https://surrealdb.com/docs/surrealql/datamodel/idioms) [Literals](https://surrealdb.com/docs/surrealql/datamodel/literals) [None and Null](https://surrealdb.com/docs/surrealql/datamodel/none-and-null) [Numbers](https://surrealdb.com/docs/surrealql/datamodel/numbers) [Objects](https://surrealdb.com/docs/surrealql/datamodel/objects) [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) [Record references](https://surrealdb.com/docs/surrealql/datamodel/references) [Regex](https://surrealdb.com/docs/surrealql/datamodel/regex) [Sets](https://surrealdb.com/docs/surrealql/datamodel/sets) [Strings](https://surrealdb.com/docs/surrealql/datamodel/strings) [UUIDs](https://surrealdb.com/docs/surrealql/datamodel/uuid) [Values](https://surrealdb.com/docs/surrealql/datamodel/values) Clauses [Introduction](https://surrealdb.com/docs/surrealql/clauses) [EXPLAIN](https://surrealdb.com/docs/surrealql/clauses/explain) [FETCH](https://surrealdb.com/docs/surrealql/clauses/fetch) [FROM](https://surrealdb.com/docs/surrealql/clauses/from) [GROUP BY](https://surrealdb.com/docs/surrealql/clauses/group-by) [LIMIT](https://surrealdb.com/docs/surrealql/clauses/limit) [OMIT](https://surrealdb.com/docs/surrealql/clauses/omit) [ORDER BY](https://surrealdb.com/docs/surrealql/clauses/order-by) [SPLIT](https://surrealdb.com/docs/surrealql/clauses/split) [WHERE](https://surrealdb.com/docs/surrealql/clauses/where) [WITH](https://surrealdb.com/docs/surrealql/clauses/with) Statements [Introduction](https://surrealdb.com/docs/surrealql/statements) [ACCESS](https://surrealdb.com/docs/surrealql/statements/access) [ALTER](https://surrealdb.com/docs/surrealql/statements/alter) [BEGIN](https://surrealdb.com/docs/surrealql/statements/begin) [BREAK](https://surrealdb.com/docs/surrealql/statements/break) [CANCEL](https://surrealdb.com/docs/surrealql/statements/cancel) [COMMIT](https://surrealdb.com/docs/surrealql/statements/commit) [CONTINUE](https://surrealdb.com/docs/surrealql/statements/continue) [CREATE](https://surrealdb.com/docs/surrealql/statements/create) [DEFINE](https://surrealdb.com/docs/surrealql/statements/define) [DEFINE ACCESS](https://surrealdb.com/docs/surrealql/statements/define/access) [BEARER](https://surrealdb.com/docs/surrealql/statements/define/access/bearer) [JWT](https://surrealdb.com/docs/surrealql/statements/define/access/jwt) [RECORD](https://surrealdb.com/docs/surrealql/statements/define/access/record) [DEFINE ANALYZER](https://surrealdb.com/docs/surrealql/statements/define/analyzer) [DEFINE API](https://surrealdb.com/docs/surrealql/statements/define/api) [DEFINE BUCKET](https://surrealdb.com/docs/surrealql/statements/define/bucket) [DEFINE CONFIG](https://surrealdb.com/docs/surrealql/statements/define/config) [DEFINE DATABASE](https://surrealdb.com/docs/surrealql/statements/define/database) [DEFINE EVENT](https://surrealdb.com/docs/surrealql/statements/define/event) [DEFINE FIELD](https://surrealdb.com/docs/surrealql/statements/define/field) [DEFINE FUNCTION](https://surrealdb.com/docs/surrealql/statements/define/function) [DEFINE INDEX](https://surrealdb.com/docs/surrealql/statements/define/indexes) [DEFINE NAMESPACE](https://surrealdb.com/docs/surrealql/statements/define/namespace) [DEFINE PARAM](https://surrealdb.com/docs/surrealql/statements/define/param) [DEFINE SCOPE](https://surrealdb.com/docs/surrealql/statements/define/scope) [DEFINE SEQUENCE](https://surrealdb.com/docs/surrealql/statements/define/sequence) [DEFINE TABLE](https://surrealdb.com/docs/surrealql/statements/define/table) [DEFINE TOKEN](https://surrealdb.com/docs/surrealql/statements/define/token) [DEFINE USER](https://surrealdb.com/docs/surrealql/statements/define/user) [DELETE](https://surrealdb.com/docs/surrealql/statements/delete) [FOR](https://surrealdb.com/docs/surrealql/statements/for) [IF ELSE](https://surrealdb.com/docs/surrealql/statements/ifelse) [INFO](https://surrealdb.com/docs/surrealql/statements/info) [INSERT](https://surrealdb.com/docs/surrealql/statements/insert) [KILL](https://surrealdb.com/docs/surrealql/statements/kill) [LET](https://surrealdb.com/docs/surrealql/statements/let) [LIVE](https://surrealdb.com/docs/surrealql/statements/live) [REBUILD](https://surrealdb.com/docs/surrealql/statements/rebuild) [RELATE](https://surrealdb.com/docs/surrealql/statements/relate) [REMOVE](https://surrealdb.com/docs/surrealql/statements/remove) [RETURN](https://surrealdb.com/docs/surrealql/statements/return) [SELECT](https://surrealdb.com/docs/surrealql/statements/select) [SHOW](https://surrealdb.com/docs/surrealql/statements/show) [SLEEP](https://surrealdb.com/docs/surrealql/statements/sleep) [THROW](https://surrealdb.com/docs/surrealql/statements/throw) [UPDATE](https://surrealdb.com/docs/surrealql/statements/update) [UPSERT](https://surrealdb.com/docs/surrealql/statements/upsert) [USE](https://surrealdb.com/docs/surrealql/statements/use) Functions [Introduction](https://surrealdb.com/docs/surrealql/functions) [Database Functions](https://surrealdb.com/docs/surrealql/functions/database) [API functions](https://surrealdb.com/docs/surrealql/functions/database/api) [Array functions](https://surrealdb.com/docs/surrealql/functions/database/array) [Bytes functions](https://surrealdb.com/docs/surrealql/functions/database/bytes) [Count function](https://surrealdb.com/docs/surrealql/functions/database/count) [Crypto functions](https://surrealdb.com/docs/surrealql/functions/database/crypto) [Duration functions](https://surrealdb.com/docs/surrealql/functions/database/duration) [Encoding functions](https://surrealdb.com/docs/surrealql/functions/database/encoding) [File functions](https://surrealdb.com/docs/surrealql/functions/database/file) [Geo functions](https://surrealdb.com/docs/surrealql/functions/database/geo) [HTTP functions](https://surrealdb.com/docs/surrealql/functions/database/http) [Math functions](https://surrealdb.com/docs/surrealql/functions/database/math) [Meta functions](https://surrealdb.com/docs/surrealql/functions/database/meta) [Not function](https://surrealdb.com/docs/surrealql/functions/database/not) [Object functions](https://surrealdb.com/docs/surrealql/functions/database/object) [Parse functions](https://surrealdb.com/docs/surrealql/functions/database/parse) [Rand functions](https://surrealdb.com/docs/surrealql/functions/database/rand) [Record functions](https://surrealdb.com/docs/surrealql/functions/database/record) [Search functions](https://surrealdb.com/docs/surrealql/functions/database/search) [Sequence functions](https://surrealdb.com/docs/surrealql/functions/database/sequence) [Session functions](https://surrealdb.com/docs/surrealql/functions/database/session) [Sleep function](https://surrealdb.com/docs/surrealql/functions/database/sleep) [String functions](https://surrealdb.com/docs/surrealql/functions/database/string) [Time functions](https://surrealdb.com/docs/surrealql/functions/database/time) [Type functions](https://surrealdb.com/docs/surrealql/functions/database/type) [Value functions](https://surrealdb.com/docs/surrealql/functions/database/value) [Vector functions](https://surrealdb.com/docs/surrealql/functions/database/vector) [JavaScript functions](https://surrealdb.com/docs/surrealql/functions/script) [Arguments](https://surrealdb.com/docs/surrealql/functions/script/arguments) [Built-in functions](https://surrealdb.com/docs/surrealql/functions/script/built-in-functions) [Function context](https://surrealdb.com/docs/surrealql/functions/script/context) [Type conversion](https://surrealdb.com/docs/surrealql/functions/script/type-conversion) [SurrealQL functions](https://surrealdb.com/docs/surrealql/functions/script/surrealql-functions) [SurrealML Functions](https://surrealdb.com/docs/surrealql/functions/ml) [Machine learning functions](https://surrealdb.com/docs/surrealql/functions/ml/functions) [Parameters](https://surrealdb.com/docs/surrealql/parameters) [Transactions](https://surrealdb.com/docs/surrealql/transactions) [Comments](https://surrealdb.com/docs/surrealql/comments) [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) [](https://surrealdb.com/docs/surrealql) [Data types](https://surrealdb.com/docs/surrealql/datamodel) [Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids) Record IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ids) ============================================================================ > Note > > As of `v2.0.0`, SurrealDB no longer eagerly converts a string into a record. An [implicit `r` prefix or cast](https://surrealdb.com/docs/surrealql/datamodel/casting#casting-vs-affixes) > is required instead. 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](https://surrealdb.com/docs/surrealql/statements/select) , [update](https://surrealdb.com/docs/surrealql/statements/update) , and [delete](https://surrealdb.com/docs/surrealql/statements/delete) records, and serve as [links](https://surrealdb.com/docs/surrealql/datamodel/records) from one record to another. Record IDs can be constructed from a number of ways, including [alphanumeric text](https://surrealdb.com/docs/surrealql/datamodel/ids#text-record-ids) , complex Unicode text and symbols, [numbers](https://surrealdb.com/docs/surrealql/datamodel/ids#numeric-record-ids) , arrays, objects, [built-in ID generation functions](https://surrealdb.com/docs/surrealql/datamodel/ids#random-ids) , and [a function to generate an ID from values](https://surrealdb.com/docs/surrealql/functions/database/type#typething) . All of the following are examples of valid record IDs in SurrealQL. company:surrealdb company:w6xb3izpgvz4n0gow6q7 reaction:`🤪` weather:['London', d'2025-02-14T01:52:50.375Z'] 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`](https://surrealdb.com/docs/surrealql/statements/upsert) statement or an [`INSERT`](https://surrealdb.com/docs/surrealql/statements/insert#example-usage) statement with an `ON DUPLICATE KEY UPDATE` clause. Types of Record IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#types-of-record-ids) ---------------------------------------------------------------------------------------------- ### Random IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#random-ids) When you [create a record](https://surrealdb.com/docs/surrealql/statements/create) without specifying the full ID, a random identifier is assigned after the table name. This differs from the traditional default of auto-increment or serial IDs that many developers are used to. CREATE company; Output[\ {\ id: company:ezs644u19mae2p68404j\ }\ ] 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 GUID (the default), sequentially incrementing and temporally sortable ULID Record identifiers, and UUID version 7 Record identifiers. // Generate a random record ID 20 characters in length // Charset: `abcdefghijklmnopqrstuvwxyz0123456789` CREATE temperature:rand() SET time = time::now(), celsius = 37.5; // Identical to the above CREATE statement, because // :rand() is the default random ID format CREATE temperature SET time = time::now(), celsius = 37.5; // Generate a ULID-based record ID CREATE temperature:ulid() SET time = time::now(), celsius = 37.5; // Generate a UUIDv7-based record ID CREATE temperature:uuid() SET time = time::now(), celsius = 37.5; ### Text Record IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#text-record-ids) Text record IDs can contain letters, numbers and `_` characters. CREATE company:surrealdb SET name = 'SurrealDB'; CREATE user_version_2025 SET name = 'Alucard'; To create a record ID with complex characters, use `` ` `` (backticks) around the table name and/or record identifier. CREATE article:`8424486b-85b3-4448-ac8d-5d51083391c7` SET time = time::now(), author = person:tobie; CREATE `Artykuł`:100 SET author = person:`Lech_Wałęsa`; The parts of record IDs with complex characters will display enclosed by a `⟨` and `⟩`. Output-------- Query -------- [\ {\ author: person:tobie,\ id: article:⟨8424486b-85b3-4448-ac8d-5d51083391c7⟩,\ time: d'2025-02-18T01:48:46.364Z'\ }\ ] -------- Query -------- [\ {\ author: person:⟨Lech_Wałęsa⟩,\ id: ⟨Artykuł⟩:100\ }\ ] As the `⟨` and `⟩` characters are used for the complex parts of a record ID, they can be used directly instead of backticks if preferred. Note that these characters are different from `<` and `>` found on standard keyboards. CREATE ⟨📖⟩ SET time = time::now(), author = person:tobie; Output[\ {\ author: person:tobie,\ id: ⟨📖⟩:svk5taacnhnk5000129r,\ time: d'2025-02-17T05:25:04.932Z'\ }\ ] ### Numeric Record IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#numeric-record-ids) If you create a record ID with a number as a string, it will be stored with the `⟨ ⟩` characters to differentiate it from a number. CREATE article SET id = 10; CREATE article SET id = "10"; CREATE article SET id = "article10"; SELECT VALUE id FROM article; As the record ID `article:10` is different from `article:⟨10⟩`, no errors are returned when creating and both records turn up in the output of the `SELECT` statement. Meanwhile, the article with the identifier `article10` does not use the `⟨ ⟩` characters as there is no `article10` number to differentiate it from. Output[\ article:10,\ article:⟨10⟩,\ article:article10\ ] 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. CREATE temperature:17493 SET time = time::now(), celsius = 37.5; CREATE year:29878977097987987979232 SET events = [\ "Galactic senate convenes",\ "Mr. Bean still waits in a field"\ ]; Output-------- Query -------- [\ {\ celsius: 37.5f,\ id: temperature:17493,\ time: d'2025-02-17T06:21:08.911Z'\ }\ ] -------- Query s-------- [\ {\ events: [\ 'Galactic senate convenes',\ 'Mr. Bean still waits in a field'\ ],\ id: year:⟨29878977097987987979232⟩\ }\ ] ### Array-based Record IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#array-based-record-ids) 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](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ranges) , 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, with no limit to the depth of the arrays. Arrays can store any value stored within them, and can store different value types within the same array. CREATE weather:['London', d'2025-02-13T05:00:00Z'] SET temperature = 5.7, conditions = "cloudy"; Output[\ {\ conditions: 'cloudy',\ id: weather:[\ 'London',\ d'2025-02-13T05:00:00Z'\ ],\ temperature: 5.7f\ }\ ] ### Why record ranges are performant[](https://surrealdb.com/docs/surrealql/datamodel/ids#why-record-ranges-are-performant) 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 `player` records that have an array-based record ID that begins with `'mage'`, allowing them to be used in a record range query, as well as a field called `class` that is also `'mage'`, 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 `class` field is equal to `'mage'`. FOR $_ IN 0..10000 { CREATE player:['mage', rand::guid()] SET class = 'mage'; }; LET $_ = SELECT * FROM player:['mage', NONE]..['mage', ..]; LET $_ = SELECT * FROM player WHERE class = 'mage'; If the number of `player` records is extended to a larger number of classes, 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. FOR $_ IN 0..10000 { CREATE player:['mage', rand::guid()] SET class = 'mage'; CREATE player:['barbarian', rand::guid()] SET class = 'barbarian'; CREATE player:['rogue', rand::guid()] SET class = 'rogue'; CREATE player:['bard', rand::guid()] SET class = 'bard'; CREATE player:['sage', rand::guid()] SET class = 'sage'; CREATE player:['psionic', rand::guid()] SET class = 'psionic'; CREATE player:['thief', rand::guid()] SET class = 'thief'; CREATE player:['paladin', rand::guid()] SET class = 'paladin'; CREATE player:['ranger', rand::guid()] SET class = 'ranger'; CREATE player:['cleric', rand::guid()] SET class = 'cleric'; }; LET $_ = SELECT * FROM player:['mage', NONE]..['mage', ..]; LET $_ = SELECT * FROM player WHERE class = 'mage'; ### IDs made with parameters and function calls[](https://surrealdb.com/docs/surrealql/datamodel/ids#ids-made-with-parameters-and-function-calls) Parameters and function calls can be used inside array- and object-based record IDs in the same way as on standalone arrays and objects. LET $now = time::now(); CREATE weather:['Seoul', $now] SET temperature = -2.3, conditions = "cloudy"; CREATE weather:['London', time::now()] SET temperature = 5.3, conditions = "cloudy"; To create a record that uses a parameter or function call as its entire record identifier, the [`type::thing()`](https://surrealdb.com/docs/surrealql/functions/database/type#typething) function can be used. LET $now = time::now(); CREATE type::thing("weather", $now) SET city = 'London'; Output[\ {\ city: 'London',\ id: weather:⟨2025-02-18T02:30:08.563Z⟩\ }\ ] Defining record IDs in a schema[](https://surrealdb.com/docs/surrealql/datamodel/ids#defining-record-ids-in-a-schema) ---------------------------------------------------------------------------------------------------------------------- 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`](https://surrealdb.com/docs/surrealql/statements/define/field) statement. DEFINE FIELD possessions ON TABLE person TYPE option>; DEFINE FIELD friends ON TABLE person TYPE option>>; CREATE person SET possessions = [ book:one, house:one], friends = [ person:one, person:two ]; Be sure to use just `record` instead of `record`, as `` here would imply actual records of a table called `any`. DEFINE FIELD possessions ON TABLE person TYPE option>>; -- Won't work, 'book' and 'house' are not of table 'any' CREATE person SET possessions = [ book:one, house:one ]; -- Actually expects this, which is probably -- not what the DEFINE FIELD intended CREATE person SET possessions = [ any:one, any:two ]; Record ranges[](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ranges) ---------------------------------------------------------------------------------- SurrealDB supports the ability to query a range of records, using the record ID. Record ID range queries retrieve records using the natural sorting order of the record IDs, making a table scan unnecessary. These range queries can be used to query a range of records in a timeseries context. -- Select all person records with IDs between the given range SELECT * FROM person:1..1000; -- Select all records for a particular location, inclusive SELECT * FROM temperature:['London', NONE]..=['London', ..]; -- Select all temperature records with IDs less than a maximum value SELECT * FROM temperature:..['London', '2022-08-29T08:09:31']; -- Select all temperature records with IDs greater than a minimum value SELECT * FROM temperature:['London', '2022-08-29T08:03:39']..; -- Select all temperature records with IDs between the specified range SELECT * FROM temperature:['London', '2022-08-29T08:03:39']..['London', '2022-08-29T08:09:31']; The following example shows the difference in performance between a regular query that uses a `WHERE` clause and a record range scan. FOR $num IN 0..=100000 { CREATE person SET id = $num, num = $num }; -- Assign the output to an unused parameter -- to avoid excessive output LET $_ = SELECT * FROM person WHERE num IN 0..=1000; LET $_ = SELECT * FROM person:0..=1000; Tips and best practices for record IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#tips-and-best-practices-for-record-ids) ------------------------------------------------------------------------------------------------------------------------------------ ### Why choose the right record ID format[](https://surrealdb.com/docs/surrealql/datamodel/ids#why-choose-the-right-record-id-format) Choosing an apt record ID format is especially important because record IDs is SurrealQL are immutable. Take the following `user` records for example: FOR $i IN 0..5 { CREATE user SET user_num = $i, name = "User number " + user_num; }; Each of these `user` records will have a random ID, such as `user:wvjqjc5ebqvfg3aw7g61`. If a decision is made to move away from random IDs to some other form, such as an incrementing number, this will have to be done manually. FOR $user IN SELECT * FROM user { -- Use type::thing to make a record ID -- from the user_num field CREATE type::thing("user", $user.user_num); -- Then delete the old user DELETE $user; }; SELECT * FROM user; The final query returning just the IDs shows that they have been recreated with new IDs. Output[\ {\ id: user:0,\ name: 'User number 0'\ },\ {\ id: user:1,\ name: 'User number 1'\ },\ {\ id: user:2,\ name: 'User number 2'\ },\ {\ id: user:3,\ name: 'User number 3'\ },\ {\ id: user:4,\ name: 'User number 4'\ }\ ] However, record IDs are also used as [record links](https://surrealdb.com/docs/surrealql/datamodel/records) and to create [graph relations](https://surrealdb.com/docs/surrealql/statements/relate) . If this is the case, more work will have to be done in order to recreate the former state. The following example shows five `user` records, which each have a 50% chance of liking each of the other users. FOR $i IN 0..5 { CREATE user SET user_num = $i, name = "User number " + user_num; }; LET $users = SELECT * FROM user; FOR $user IN $users { LET $others = array::complement($users, [$user.id]); FOR $counterpart IN $others { IF rand::bool() { RELATE $user->likes->$counterpart; } } }; Finding out the current relational state can be done with a query like the following which shows all of the graph tables in which a record is located at the `in` or `out` point. The `?` is a wildcard operator, returning any and all tables found at this point of the graph query. SELECT id, ->?->? AS did, <-?<-? AS done_to FROM user; Output[\ {\ did: [\ user:zwfnk4by9gmopf6eeqm0\ ],\ done_to: [\ user:d6bx6sch5li8qmhq3ljl,\ user:ekovipptanvmgr8f48v6\ ],\ id: user:6ycb63zr0k3cpzwel1ga\ },\ {\ did: [\ user:ekovipptanvmgr8f48v6,\ user:6ycb63zr0k3cpzwel1ga,\ user:zk7tpaduzaiuswll58sg\ ],\ done_to: [],\ id: user:d6bx6sch5li8qmhq3ljl\ }\ -- and so on..\ ] Surrealist’s [graph visualization view](https://surrealdb.com/blog/whats-new-in-surrealist-3-2#graph-visualisation) can help as well. ![Surrealist's graph view showing possible output from the previous randomized query in which each of the five user records may or may not like another user. In this case, the output resembles a rhombus with an extra line jutting out from the top left.](https://surrealdb.com/docs/_astro/graph_view.B3oQAj0s_Z12bT6L.webp) With this in mind, here are some of the items to keep in mind when deciding what sort of record ID format to use. ### Meaningful sortable IDs are faster to query[](https://surrealdb.com/docs/surrealql/datamodel/ids#meaningful-sortable-ids-are-faster-to-query) Records are returned in ascending record ID order by default. As the following query shows, a `SELECT` statement on a large number of `user` records with random IDs will show those with record identifiers starting with a large number of zeroes. While the IDs are sortable, the IDs themselves are completely random. CREATE |user:200000| RETURN NONE; SELECT VALUE id FROM user LIMIT 4; Output[\ user:0001th0nnywnczi7mrvk,\ user:000t5r3y7u8stqtecvht,\ user:000tjk1nbi1it1bedplc,\ user:001dfral92ltbdznypcd\ ] For a large number of records, pagination can be used to retrieve a certain amount of records at a time. -- Returns the same four records as above SELECT VALUE id FROM user START 0 LIMIT 2; SELECT VALUE id FROM user START 2 LIMIT 2; Output-------- Query -------- [\ user:0001th0nnywnczi7mrvk,\ user:000t5r3y7u8stqtecvht\ ] -------- Query -------- [\ user:001dfral92ltbdznypcd,\ user:001hv9g1uzh32nophrpo\ ] As record ranges are very performant, consider moving any fields that may be used in a `WHERE` clause into the ID itself. In the following example, a number of `user` records are created using the default random ID, plus a `num` field that tracks in which order the user was created. FOR $num IN 0..100 { CREATE user SET num = $num; sleep(1ms); -- Simulate a bit of time between user creation }; SELECT * FROM user WHERE num IN 50..=51; SELECT * FROM user START 50 LIMIT 2; As the output from the `SELECT` statements show, a `WHERE` clause is needed to find two users starting at a `num` of 50, as `START 50` starts based on the user of the record ID, which is entirely random. -------- Query -------- [\ {\ id: user:pqpeg0edt8kpda907o01,\ num: 50\ },\ {\ id: user:ty6qr7zyob5dh882it08,\ num: 51\ }\ ] -------- Query -------- [\ {\ id: user:hvfp5m5ty7n2k95dbamv,\ num: 70\ },\ {\ id: user:hvfumcmmveuolg4e2h26,\ num: 36\ }\ ] Using a ULID in this case will allow the IDs to remain random, but still sorted by date of creation. FOR $num IN 0..100 { CREATE user:ulid() SET num = $num; sleep(1ms); }; SELECT * FROM user WHERE num IN 50..=51; SELECT * FROM user START 50 LIMIT 2; Not only is the `START 50 LIMIT 2` query more performant, but the entire `num` field could be removed if its only use is to return records by order of creation. Same record IDs for both queries this time-------- Query -------- [\ {\ id: user:01JM1AHN7DDN7XM5KZ2RR2YM1S,\ num: 50\ },\ {\ id: user:01JM1AHN7FS4A3B6RNFCF64H90,\ num: 51\ }\ ] -------- Query -------- [\ {\ id: user:01JM1AHN7DDN7XM5KZ2RR2YM1S,\ num: 50\ },\ {\ id: user:01JM1AHN7FS4A3B6RNFCF64H90,\ num: 51\ }\ ] ### Move exact matches in array-based record IDs to the front[](https://surrealdb.com/docs/surrealql/datamodel/ids#move-exact-matches-in-array-based-record-ids-to-the-front) Take the following `event` records which can be queried as a perfomant record range. CREATE event:[d'2025-05-05T08:00:00Z', user:one, "debug"] SET info = "Logged in"; CREATE event:[d'2025-05-05T08:10:00Z', user:one, "debug"] SET info = "Logged out"; CREATE event:[d'2025-05-05T08:01:00Z', user:two, "debug"] SET info = "Logged in"; The ordering of the ID in this case is likely not ideal, because the first item in the array, a `datetime`, will be the first to be evaluated in a range scan. A query such as the one below on a range of dates will effectively ignore the second and third parts of the ID. SELECT * FROM event:[d'2025-05-05', user:one, "debug"]..[d'2025-05-06', user:one, "debug"]; -- Same result! user name and "debug" are irrelevant -- SELECT * FROM event:[d'2025-05-05']..[d'2025-05-06']; Output[\ {\ id: event:[\ d'2025-05-05T08:00:00Z',\ user:one,\ 'debug'\ ],\ info: 'Logged in'\ },\ {\ id: event:[\ d'2025-05-05T08:01:00Z',\ user:two,\ 'debug'\ ],\ info: 'Logged in'\ },\ {\ id: event:[\ d'2025-05-05T08:10:00Z',\ user:one,\ 'debug'\ ],\ info: 'Logged out'\ }\ ] Instead, the parts of the array that are more likely to be exactly matched (such as `user:one` and `"debug"`) should be moved to the front. CREATE event:[user:one, "debug", d'2025-05-05T08:00:00Z'] SET info = "Logged in"; CREATE event:[user:one, "debug", d'2025-05-05T08:10:00Z'] SET info = "Logged out"; CREATE event:[user:two, "debug", d'2025-05-05T08:01:00Z'] SET info = "Logged in"; Using this format, queries can now be performed for a certain user and logging level, over a range of datetimes. -- Only returns events for user:one and "debug" SELECT * FROM event:[user:one, "debug", d'2025-05-05']..[user:one, "debug", d'2025-05-06']; Output[\ {\ id: event:[\ user:one,\ 'debug',\ d'2025-05-05T08:00:00Z'\ ],\ info: 'Logged in'\ },\ {\ id: event:[\ user:one,\ 'debug',\ d'2025-05-05T08:10:00Z'\ ],\ info: 'Logged out'\ }\ ] ### Auto-incrementing IDs[](https://surrealdb.com/docs/surrealql/datamodel/ids#auto-incrementing-ids) While SurrealDB does not use auto-incrementing IDs by default, this behaviour can be achieved in a number of ways. One is to use the [`record::id()`](https://surrealdb.com/docs/surrealql/functions/database/record#recordid) function on the latest record, which returns the latter part of a record ID (the ‘1’ in the record ID `person:1`). This can then be followed up with the [`type::thing()`](https://surrealdb.com/docs/surrealql/functions/database/type#typething) function to create a new record ID. -- Create records from person:1 to person:10 CREATE |person:1..10|; LET $latest = SELECT VALUE id FROM ONLY person ORDER BY id DESC LIMIT 1; CREATE type::thing("person", $latest.id() + 1); Output[\ {\ id: person:11\ }\ ] When dealing with a large number of records, a more performant option is to use a separate record that holds a single value representing the latest ID. An [`UPSERT`](https://surrealdb.com/docs/surrealql/statements/upsert) statement is best here, which will allow the counter to be initialized if it does not yet exist, and updated otherwise. This is best done [inside a manual transaction](https://surrealdb.com/docs/surrealql/statements/begin) so that the latest ID will be rolled back if any failures occur when creating the next record. BEGIN TRANSACTION; UPSERT person_id:counter SET num += 1; -- Creates a person:1 CREATE type::thing("person", person_id:counter.num); COMMIT TRANSACTION; BEGIN TRANSACTION; -- Latest ID is now 2 UPSERT person_id:counter SET num += 1; -- Whoops, invalid datetime format -- Transaction fails and all changes are rolled back CREATE type::thing("person", person_id:counter.num) SET created_at = '2025_01+01'; COMMIT TRANSACTION; -- Latest ID is still 1 RETURN person_id:counter.num; ### Record IDs are record links[](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ids-are-record-links) As a record ID is a pointer to all of the data of a record, a single record ID is enough to access all of a record’s fields. This behaviour is the key to the convenience of [record links](https://surrealdb.com/docs/surrealql/datamodel/records) in SurrealDB, as holding a record ID is all that is needed for one record to have a link to another. When using a standalone record ID as a record pointer, be sure to use the record ID itself. CREATE person:1 SET data = { some: "demo", data: "for", demonstration: "purposes" }; LET $record = SELECT id FROM person:1; SELECT * FROM $record; The output of the above query is just the `id` field on its own, as the `$record` parameter is an object with an `id` field, not the `id` field (the pointer) itself. Output[\ {\ id: person:1\ }\ ] To rectify this, `id.*` can be used to follow the pointer to the entire data for the record. SELECT id.* FROM $record; Output[\ {\ id: {\ data: {\ data: 'for',\ demonstration: 'purposes',\ some: 'demo'\ },\ id: person:1\ }\ }\ ] Limitations[](https://surrealdb.com/docs/surrealql/datamodel/ids#limitations) ------------------------------------------------------------------------------ At present, definitions for a record ID’s value inside a [`DEFINE FIELD`](https://surrealdb.com/docs/surrealql/statements/define/field) statement are ignored. DEFINE FIELD id ON user VALUE rand::int(1, 1000000000) READONLY; CREATE user; Output[\ {\ id: user:9ixn3oei6o532c2qyixa\ }\ ] To achieve the same behaviour, the `id` field can be set inside the statement to create the record. CREATE user SET id = rand::int(1, 1000000000); Learn more[](https://surrealdb.com/docs/surrealql/datamodel/ids#learn-more) ---------------------------------------------------------------------------- Learn more about record IDs [in this blogpost](https://surrealdb.com/blog/the-life-changing-magic-of-surrealdb-record-ids#the-performance-at-scale) and on this [youtube video](https://www.youtube.com/watch?v=c0cqmWRYP8c) . [Edit this page on GitHub](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/ids.mdx) Copy page as Markdown [Ranges](https://surrealdb.com/docs/surrealql/datamodel/ranges) [Record links](https://surrealdb.com/docs/surrealql/datamodel/records) ### On this page * [Types of Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#types-of-record-ids) * [Random IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#random-ids) * [Text Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#text-record-ids) * [Numeric Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#numeric-record-ids) * [Array-based Record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#array-based-record-ids) * [Why record ranges are performant](https://surrealdb.com/docs/surrealql/datamodel/ids#why-record-ranges-are-performant) * [IDs made with parameters and function calls](https://surrealdb.com/docs/surrealql/datamodel/ids#ids-made-with-parameters-and-function-calls) * [Defining record IDs in a schema](https://surrealdb.com/docs/surrealql/datamodel/ids#defining-record-ids-in-a-schema) * [Record ranges](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ranges) * [Tips and best practices for record IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#tips-and-best-practices-for-record-ids) * [Why choose the right record ID format](https://surrealdb.com/docs/surrealql/datamodel/ids#why-choose-the-right-record-id-format) * [Meaningful sortable IDs are faster to query](https://surrealdb.com/docs/surrealql/datamodel/ids#meaningful-sortable-ids-are-faster-to-query) * [Move exact matches in array-based record IDs to the front](https://surrealdb.com/docs/surrealql/datamodel/ids#move-exact-matches-in-array-based-record-ids-to-the-front) * [Auto-incrementing IDs](https://surrealdb.com/docs/surrealql/datamodel/ids#auto-incrementing-ids) * [Record IDs are record links](https://surrealdb.com/docs/surrealql/datamodel/ids#record-ids-are-record-links) * [Limitations](https://surrealdb.com/docs/surrealql/datamodel/ids#limitations) * [Learn more](https://surrealdb.com/docs/surrealql/datamodel/ids#learn-more) Last updated: Loading... ### Was this page helpful? #### What did you like? AccurateEasy to understandSolved my problemHelped me decide to use the productOther SubmitCancel #### What went wrong? Hard to understandIncorrect informationMissing the informationOther SubmitCancel [Edit](https://github.com/surrealdb/docs.surrealdb.com/edit/main/src/content/doc-surrealql/datamodel/ids.mdx) [Issue](https://github.com/surrealdb/docs.surrealdb.com/issues/new) ---