Back to top
Documentation SurrealQL Statements DEFINE statement

DEFINE statement

The DEFINE statement can be used to specify authentication access and behaviour, global parameters, table configurations, table events, schema definitions, and indexes.

Statement syntax

DEFINE [
	NAMESPACE @name
	| DATABASE @name
	| LOGIN @name ON [ NAMESPACE | DATABASE ] [ PASSWORD @pass | PASSHASH @hash ]
	| TOKEN @name ON [ NAMESPACE | DATABASE | SCOPE @scope ] TYPE @type VALUE @value
	| SCOPE @name
		[ SESSION @duration ]
		[ SIGNUP @expression ]
		[ SIGNIN @expression ]
	| TABLE @name
		[ DROP ]
		[ SCHEMAFULL | SCHEMALESS ]
		[ AS SELECT @projections
			FROM @tables
			[ WHERE @condition ]
			[ GROUP [ BY ] @groups ]
		]
		[ PERMISSIONS [ NONE | FULL
			| FOR select @expression
			| FOR create @expression
			| FOR update @expression
			| FOR delete @expression
		] ]
	| EVENT @name ON [ TABLE ] @table WHEN @expression THEN @expression
	| FIELD @name ON [ TABLE ] @table
		[ TYPE @type ]
		[ VALUE @expression ]
		[ ASSERT @expression ]
		[ PERMISSIONS [ NONE | FULL
			| FOR select @expression
			| FOR create @expression
			| FOR update @expression
			| FOR delete @expression
		] ]
	| INDEX @name ON [ TABLE ] @table [ FIELDS | COLUMNS ] @fields [ UNIQUE ]
]

DEFINE NAMESPACE

Allows you to instantiate namespaces as part of SurrealDB's multi-tenancy model.

DEFINE DATABASE

Allows you to instantiate named databases.

DEFINE LOGIN

Allows you to create user accounts.

DEFINE TOKEN

Allows you to authenticate on SurrealDB with third-party OAuth tokens.

DEFINE SCOPE

Allows you to create scopes to set authentication and access rules.

DEFINE TABLE

Declare the name of a table, make it schemafull or schemaless, create projections, and set table level permissions

DEFINE EVENT

Allows you to specify conditions that trigger the execution of custom logic.

DEFINE FIELD

Allows you to instantiate named fields on a table.

DEFINE INDEX

Allows you to create database indexes on table fields.