SurrealDB
SurrealDB Docs Logo

Enter a search query

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.

Ensure that your database and namespace exist and you have started your database before using the Sql command option.

Statement syntax

SurrealQL Syntax
USE [ NS @ns ] [ DB @db ];

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 to check the current namespace and database.

INFO FOR NS; -- Check the current Namespace
INFO FOR DB; -- Check the current Database

USE statement behaviour when resource does not exist

Available since: v3.0.0-alpha.8

The behaviour of the USE statement differs depending on which mode the database server is run in.

When run in regular mode, a USE statement will create the namespace or database indicated if it does not already exist.

USE NS ns; -- Output: NONE (success)
(INFO FOR ROOT).namespaces; -- Output: { ns: 'DEFINE NAMESPACE ns' }

In strict mode, a resource will not be created unless it is already defined. In this case, the USE statement will return an error.

USE NS ns; -- Output: "The namespace 'ns' does not exist" DEFINE NS ns; USE NS ns; -- Now defined, no error
Edit this page on GitHub