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.
Data types
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. |
An array of items. | |
A value that can be either | |
Stores a value in a byte array. | |
An RFC 3339 compliant data type that stores a date with time and time zone. | |
Data type for storing decimal floating point numbers. | |
Store a value representing a length of time. Can be added or subtracted from datetimes or other durations. | |
Data type for storing floating point numbers. Larger or extremely precise values should be stored as a decimal. | |
geometry | RFC 7946 compliant data type for storing geometry in the GeoJson format . |
Store a value in a 64 bit signed integer. Values can range between | |
Store numbers without specifying the type. | |
Store formatted objects containing values of any supported type including nested objects or arrays. | |
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. | |
A regular expression that can be used for matching strings. | |
A record ID. Table names can be added inside angle brackets to restrict to certain table names. | |
A set of items. | |
A value composed of text or text-like characters such as emojis. |
Examples
Examples of the geometry type:
-- Define a field with a single type
DEFINE FIELD location ON TABLE restaurant TYPE geometry<point>;
-- Define a field with any geometric type
DEFINE FIELD area ON TABLE restaurant TYPE geometry<feature>;
-- Define a field with specific geometric types
DEFINE FIELD area ON TABLE restaurant
TYPE geometry<polygon|multipolygon|collection>;Examples of the bytes type:
-- 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 = <bytes>"bar";Type expressions
Type expressions are not standalone types, but expressions to indicate which types are permitted.
Type | Description |
|---|---|
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. | |
option | Makes types optional and guarantees the field to be either empty (NONE) or some other type. Syntactic sugar for |
Examples
Example of an option in a schema:
DEFINE FIELD friends ON TABLE person TYPE option<array<person>>;Example of a literal type:
DEFINE FIELD error_msg ON TABLE log TYPE
{ code: 200, message: string } |
{ code: 404, message: string };As an option is syntactic sugar for type | NONE, an option is also simply another type of literal. This field definition is identical to the option example above.
DEFINE FIELD friends ON TABLE person TYPE array<person> | NONE;