A table is the first structural unit most people define: it is a named home for records. Even a SELECT statement requires a table to be defined before it will work.
As a convenience, the creation of a record in a non-strict database will define the table for you. The following example does not return any errors because the table definition will exist once CREATE is executed.
DEFINE TABLE can also set high-level rules, such as whether new fields are allowed without a definition, whether rows are normal documents or graph edges, whether the table is a pre-computed view, and who may select / create / update / delete.
Individual columns still use DEFINE FIELD; the table statement does not replace that.
You need appropriate auth (root, namespace, or database owner/editor) and an active USE for namespace and database. Full grammar and every clause live under DEFINE TABLE in the reference.
Defining a changefeed on a table
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 returns the mutations recorded in that window. If an entry reflects an update to an existing record and the feed stores differences (INCLUDE ORIGINAL), the diff is a reverse diff, namely the operations needed to reach the state immediately before that write. See DEFINE TABLE for full examples and response shapes.
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 statement.
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.
DROP tables and 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.
DROP tables are useful in combination with events or foreign (view) tables, as you can compute a record and drop the input.
Defining permissions
By default, the permissions on a table will be set to NONE unless otherwise specified.
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.
Table with specialized TYPE clause
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.
With TYPE NORMAL, you can specify a table to only store "normal" records, and not relations.
With TYPE RELATION, you can specify a table to only store relational type content, and restrict what kind of relations can be stored.
Using ENFORCED to ensure that related records exist
As relations are represented by standalone tables, they can be constructed before any linked records exist.
As such, a query on the relation will return nothing until the records it has been defined upon are created.
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.