The core Tableland SQL parser accepts an SQL statement list which is a semicolon-separated list of statements. Each SQL statement in the statement list is an instance of one of the following specific statement types. All other standard SQL statement types are unavailable (at the moment). Each statement type is associated with a well-known SQL command (see following sections). In general, the entire Tableland SQL API can be summarized in seven command/statement types: CREATE TABLE, INSERT, UPDATE, DELETE, SELECT, GRANT, REVOKE.

<aside> ⚠️ The statement and data types provided here are part of the official minimal Tableland SQL specification. Additional functionality may be available in practice. However, it is not recommended that developers rely on SQL features outside of this minimal specification in the long-term.

</aside>

CREATE TABLE

The CREATE TABLE command is used to create a new table on Tableland. A CREATE TABLE command specifies the following attributes of the new table:

Structure

CREATE TABLE *table_name* ( [
  { *column_name* *data_type* [ *column_constraint* [,  ... ] ]
  | table_constraint }
	[, ...]
] );

where column_constraint has structure

[ CONSTRAINT constraint_name ]
{ NOT NULL |
  CHECK ( expression ) |
  DEFAULT default_expr |
  UNIQUE index_parameters |
  PRIMARY KEY index_parameters |
}

and table_constraint has structure

[ CONSTRAINT constraint_name ]
{ CHECK ( expression ) |
  UNIQUE ( column_name [, ... ] ) |
  PRIMARY KEY ( column_name [, ... ] )

Details

Table Identifiers/Names

Every CREATE TABLE statement must specify a fully-qualified table identifier (id) as the name of the new table. The fully-qualified table identifier has the following structure:

table_id = {prefix}_{chain_id}_{token_id}

Where the id prefix is optional, and may include any characters from the regular expression ([A-Za-z0-9\\_]+), but cannot start with a number. A prefix string may be up to 32 bytes in length. In practice, long names with spaces must be slug-ified with underscores. For example, “my amazing table" would become "my_amazing_table". The last two components of the table id, must be the chain id and the token id, which are numeric values separated by an underscore. For example, a valid table id without a prefix looks like _42_0, whereas a valid table id with a prefix might look like dogs_42_0.

<aside> ⚠️ It is not up to the caller to determine what token id to use in a CREATE TABLE statement. The token id is a monotonically-increasing numeric value which is provided by the smart contract that is processing the create statements. See the On-Chain API Specification for details on the smart contract calls used to generate CREATE TABLE statements in practice.

</aside>

Table identifiers must be globally unique. The combination of chain id and monotonically increasing token id ensures this is the case in practice. As such, the addition of a user-defined prefix string is an aesthetic feature that most developers will find useful (but is not required). The maximum (slug-ified) prefix length is 32 bytes.

<aside> ℹ️ Tableland also supports quoted identifiers (for table names, column names, etc). This allows callers to use SQL Keywords (see next section) as part of identifiers, etc. There are some limitations to this, and it does not circumvent any other naming constraints.

</aside>

Reserved Keywords

The SQL standard specifies a large number of keywords which may not be used as the names of tables, indices, columns, databases, or any other named object. The list of keywords is often so long that few people can remember them all. For most SQL code, your safest bet is to never use any English language word as the name of a user-defined object.

If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:

The list below shows all possible reserved keywords used by Tableland (or SQLite). Any identifier that is not on the following element list is not considered a keyword to the SQL parser in Tableland:

ADD, ALL, ALTER, ALWAYS, AND, ANY, AS, ASC, AUTOINCREMENT, BETWEEN, BLOB, BY, CASE, CAST, CHECK, COLLATE, COMMIT, CONSTRAINT, CREATE, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, DEFAULT, DEFERRABLE, DELETE, DESC, DISTINCT, DROP, ELSE, END, ESCAPE, EXCEPT, EXISTS, FALSE, FILTER, FIRST, FOREIGN, FROM, GENERATED, GLOB, GRANT, GROUP, HAVING, IN, INDEX, INSERT, INT, INTEGER, INTERSECT, INTO, IS, ISNULL, JOIN, KEY, LAST, LIKE, LIMIT, MATCH, NONE, NOT, NOTHING, NOTNULL, NULL, NULLS, NUMERIC, OFFSET, ON, OR, ORDER, PRIMARY, REAL, REFERENCES, REGEXP, RETURNING, REVOKE, SELECT, SET, STORED, TABLE, TEXT, THEN, TO, TRANSACTION, TRUE, UNION, UNIQUE, UPDATE, USING, VALUES, VIRTUAL, WHEN, WHERE

<aside> ℹ️ You can also find the most up to date list of keywords used by Tableland in the reference parser implementation. See Implementation

</aside>

<aside> ⚠️ Table names that begin with sqlite, system or registry are also reserved for internal use. It is an error to attempt to create a table with a name that starts with these reserved names.

</aside>

Column Definitions and Constraints

Every CREATE TABLE statement includes one or more column definitions, optionally followed by a list of table constraints. Each column definition consists of the name of the column, followed by the declared type of the column (see Data Types), then one or more optional column constraints. Included in the definition of column constraints for the purposes of the previous statement is the DEFAULT clause, even though this is not really a constraint in the sense that it does not restrict the data that the table may contain. The other constraints, NOT NULL, CHECK, UNIQUE, and PRIMARY KEY constraints, impose restrictions on the table data.

<aside> ⚠️ The number of columns in a table is limited by the MaxColumns validator configuration parameters (defaults to 24). A single character fields in a table cannot store more than MaxTextLength bytes of data (defaults to 1024). The number of rows in a table is limited by the MaxRowCount validator configuration parameter (defaults to 100,000). This values are all configurable at the network-level, and may change in the future.

</aside>

<aside> ⚠️ In practice, a CREATE TABLE statement must be sent as a single top-level statement (i.e., it must be provided in a statement list of length one).

</aside>

<aside> 🚧 Feature At Risk: FOREIGN KEY constraints of the form FOREIGN KEY(column_name) REFERENCES table_id(column_name) are currently not supported across Tableland tables. Instead, dynamic JOINs can be used to reference columns in remote tables. However, inclusion of FOREIGN KEY constraints are being considered for inclusion in the Tableland SQL specification with some specific limitations. In particular, key constraint actions would be restricted to SET NULL or SET DEFAULT (see the section called SQLite foreign key constraint actions at the link below). See SQLite Foreign Key

</aside>

Column Defaults

The DEFAULT clause specifies a default value to use for the column if no value is explicitly provided by the user when doing an INSERT. If there is no explicit DEFAULT clause attached to a column definition, then the default value of the column is NULL. An explicit DEFAULT clause may specify that the default value is NULL, a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses. For the purposes of the DEFAULT clause, an expression is considered constant if it contains no sub-queries, column, or table references, or string literals enclosed in double-quotes instead of single-quotes.

Each time a row is inserted into the table by an INSERT statement that does not provide explicit values for all table columns the values stored in the new row are determined by their default values, as follows:

Generated Columns

A column that includes a GENERATED ALWAYS AS clause is a generated column:

CREATE TABLE table_id (
    ...,
    column_name data_type { GENERATED ALWAYS } AS (*expression*) { STORED | VIRTUAL }
);

Generated columns (also sometimes called "computed columns") are columns of a table whose values are a function of other columns in the same row. Generated columns can be read, but their values can not be directly written. The only way to change the value of a generated column is to modify the values of the other columns used to calculate the generated column.

The GENERATED ALWAYS keywords at the beginning of the constraint and the VIRTUAL or STORED keyword at the end are all optional. Only the AS keyword and the parenthesized expression are required. If the trailing VIRTUAL or STORED keyword is omitted, then VIRTUAL is the default.

The value of a VIRTUAL column is computed when read, whereas the value of a STORED column is computed when the row is written. STORED columns take up space in the database file, whereas VIRTUAL columns use more CPU cycles when being read.

Features and Limitations

Primary Key

Each table in Tableland may have at most one PRIMARY KEY. If the keywords PRIMARY KEY are added to a column definition, then the primary key for the table consists of that single column. Or, if a PRIMARY KEY clause is specified as a separate table constraint, then the primary key of the table consists of the list of columns specified as part of the PRIMARY KEY clause. The PRIMARY KEY clause must contain only column names. An error is raised if more than one PRIMARY KEY clause appears in a CREATE TABLE statement. The PRIMARY KEY is optional.

Each row in a table with a primary key must have a unique combination of values in its primary key columns. If an INSERT or UPDATE statement attempts to modify the table content so that two or more rows have identical primary key values, that is a constraint violation. Related, the SQL standard is that a PRIMARY KEY should always be NOT NULL, so Tableland enforces this constraint.

<aside> 🚧 Feature At Risk: It is not currently possible to ALTER TABLE after it has been created. As such, table structure in Tableland is considered immutable. The Tableland core development team is currently evaluating whether to allow ALTER TABLE only in the case of adding new columns.

</aside>

DELETE

The DELETE command removes records from the table identified by the table id.

Structure

DELETE FROM table_id [ WHERE condition ]

Details

If the WHERE clause is not present, all records in the table are deleted. If a WHERE clause is supplied, then only those rows for which the WHERE clause boolean expression is true are deleted. Rows for which the expression is false or NULL are retained.

INSERT

The INSERT command creates new rows in a table identified by the table id.

Structure

INSERT INTO table_id [ ( *column_name* [, ...] ) ] VALUES (
  { expression } [, ...]
);

or

INSERT INTO table_id DEFAULT VALUES;

Details

An INSERT statement creates one or more new rows in an existing table. If the column_name list after table_name is omitted then the number of values inserted into each row must be the same as the number of columns in the table. In this case the result of evaluating the left-most expression from each term of the VALUES list is inserted into the left-most column of each new row, and so forth for each subsequent expression. If a column_name list is specified, then the number of values in each term of the VALUE list must match the number of specified columns. Each of the named columns of the new row is populated with the results of evaluating the corresponding VALUES expression. Table columns that do not appear in the column list are populated with the default column value (specified as part of the CREATE TABLE statement), or with NULL if no default value is specified.

The alternative INSERT ... DEFAULT VALUES statement inserts a single new row into the named table. Each column of the new row is populated with its default value, or with a NULL if no default value is specified as part of the column definition in the CREATE TABLE statement.

UPDATE

An UPDATE statement is used to modify a subset of the values stored in zero or more rows of the database table identified by the table id.

Structure

UPDATE table_name
    SET { column_name = { expression | DEFAULT } } [, ...]
    [ WHERE condition ];

Details

If the UPDATE statement does not have a WHERE clause, all rows in the table are modified by the UPDATE. Otherwise, the UPDATE affects only those rows for which the WHERE clause boolean expression is true. It is not an error if the WHERE clause does not evaluate to true for any row in the table; this just means that the UPDATE statement affects zero rows.

The modifications made to each row affected by an UPDATE statement are determined by the list of assignments following the SET keyword. Each assignment specifies a column-name to the left of the equals sign and a scalar expression to the right. For each affected row, the named columns are set to the values found by evaluating the corresponding scalar expressions. If a single column-name appears more than once in the list of assignment expressions, all but the rightmost occurrence is ignored. Columns that do not appear in the list of assignments are left unmodified. The scalar expressions may refer to columns of the row being updated. In this case all scalar expressions are evaluated before any assignments are made.

<aside> ℹ️ An assignment in the SET clause can be a parenthesized list of column names on the left and a ROW value of the same size on the right. For example, consider the following two “styles” of UPDATE statements: UPDATE table_id SET (a,b)=(b,a); or UPDATE table_id SET a=b, b=a;.

</aside>

GRANT/REVOKE

The GRANT and REVOKE commands are used to define low-level access privileges for a table identified by table name and id.

Structure

GRANT { INSERT | UPDATE | DELETE } [, ...]
    ON { [ TABLE ] table_name [, ...] }
    TO role [, ...]

REVOKE { INSERT | UPDATE | DELETE } [, ...] 
    ON { [ TABLE ] table_name [, ...] }
    FROM role [, ...]

Details

The GRANT command gives specific privileges on a table to one or more role. These privileges are added to those already granted, if any. By default, the creator of a table (as specified by a public ETH address) has all (valid) privileges on creation. The owner could, however, choose to revoke some of their own privileges for safety reasons.

Related, if a table is created with an access controller contract specified, or if an address with sufficient privileges updates a table’s access control rules to use a controller contract, then all command-based access control rules are ignored in favor of the controller contract access control. In other words, if a controller contract is set, GRANT/REVOKE is disabled. See On-Chain API Specification for further details on specifying and controlling access via a controller smart contract.

<aside> ⚠️ Currently, the only allowable privileges for granting are INSERT, UPDATE and DELETE. Note that SELECT privileges are not required at this time, as SELECT statements are not access controlled (all reads are allowed). See the SELECT section for further details.

</aside>

Roles (role) in Tableland are defined by an Ethereum public-key based address. Any (hex string encoded) ETH address is a valid Tableland role, and as such, privileges can be granted to any valid ETH address. In practice, ETH address strings must be specified as string literals using single quotes (e.g., '0x181Ec6E8f49A1eEbcf8969e88189EA2EFC9108dD').

<aside> ℹ️ Only a table owner has permission to GRANT or REVOKE access privileges to other roles/accounts.

</aside>

Conversely to the GRANT command, the REVOKE command removes specific, previously granted access privileges on a table from one or more roles. All role definitions and allowable privileges associated with granting privileges also apply to revoking them.