Synopsis
Description
TheCREATE TABLE command is used to create a new empty table, with columns, indexes and constraints, in the current database.
Once created, unless set as NOT READY, the table’s definition is fixed and cannot be altered, additional indexes cannot be added or removed.
When index and constraint names are not specified, they are auto-generated by RegattaDB.
Notes:
- Single-column constraints may be specified using column_constraint or constraint_clause. Multi-column constraints must be specified using constraint_clause.
- Single-column indexes may be specified using index_def or index_clause. Multi-column indexes must be specified using index_clause.
- The constraints UNIQUE, PRIMARY KEY and FOREIGN KEY (REFERENCES) require an index. You may specify parameters for the index using index_def (when the constraint is defined as column_constraint), or using the index_clause.
- Index names must be globally unique. If an index name is not specified, RegattaDB generates a unique name for it.
- If index_parameters are not specified for an index, or if index_def or index_clause is not specified for a constraint that requires an index, RegattaDB chooses a random storage device for the index.
- Only one index can be created on a specific ordered set of columns. However, a column (or an ordered set of columns) may appear in the ordered set of multiple indexes. For example, for a table with columns a, b, c, one can create indexes for (a), (a, b), (b, a), (a, b, c), (c, a, b).
- Two index clauses on the same column or ordered set of columns are not allowed.
- The maximum number of columns specified in a composite index is 32.
- An index may be created on a column or an ordered set of columns if there is a constraint that requires an index or if there is an index_clause on that ordered set of columns, or if there is an index_def in the column_clause. In any case, RegattaDB creates only one index per column or ordered set of columns.
- Index parameters may be specified only once for an index. It may be specified only in the index_clause if one exists, otherwise it may be specified in the index_def that is specified in a column_clause.
- When index parameters are not specified, RegattaDB selects a single random storage device for the index.
Parameters
SET READY
Set the table as ready to accept Data Manipulation Language (DML) commands.
Once a table is set as ready, changes to its columns, constraints and indexes are not allowed. This is the default behavior.
SET NOT READY
Set the table as not ready to accept DML commands, and allow further changes to its definition such as creating new indexes and adding new columns.
Note: A table with zero columns can be created as long as it’s set to NOT READY. At least one column is required in order to set its state to READY.
table_name
The name of the table to be created.
The table name must not exist in the database, and conform with the Identifiers Limitations.
column_name
The name of the column to be created in the new table. Column names should be unique within the table and conform with the Identifiers Limitations.
data_type
The data type of the column. For more information on the supported data types, refer to Data Types.
Column Constraints
CONSTRAINT constraint_name
Specify a name for a column
NULL
The column is allowed to contain NULL values. This is the default.
Note: This is not really a constraint, and it’s supported only for compatibility with some old legacy SQL applications. Its use is discouraged.
NOT NULL
The column is not allowed to contain NULL values.
DEFAULT default_expr
An optional default value to set in the column level. If no default is specified then the default value is NULL. (Note that when the default value is NULL and NOT NULL constraint is specified, you cannot insert rows with the DEFAULT keyword.)
A possible default values are either a literal (e.g, DEFAULT 'my default' or DEFAULT 1) or a variable-free expression that returns a single value
(suitable to store in a single table cell) that is being evaluated at the time the row is created (e.g., DEFAULT NOW() for current date/timestamp (depends on the column data type)). Note that expressions which include other columns in the current table are not allowed.
UNIQUE
The UNIQUE constraint specifies that the column can contain only unique values. The constraint therefore enforces that any two rows in the same table must differ in this column (note that RegattaDB is case-sensitive) (e.g., ‘Regatta’ <> ’REGATTA’ <> ‘regatta’).
Note: If the UNIQUE constraint is specified, the NOT NULL constraint is required.
Unique constraints implicitly create an index on that column, hence the index clause is required.
PRIMARY KEY
The PRIMARY KEY constraint specifies this column can contain only unique, non-NULL values. Only one primary key can be specified for a table. Since PRIMARY KEY implies UNIQUE and NOT NULL – if any of the latter two is specified for the same column, it is ignored (as it is already implied by the PRIMARY KEY constraint).
Note: PostgreSQL allows specifying the combination of PRIMARY KEY with NULL constraint, but it ignores the latter and requires only non-NULL values. In RegattaDB, specifying this contradictory combination of constraints results in an error.REFERENCES reftable [ refcolumn ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ]
This clause specifies a foreign key constraint and links data in that table column with another table
primary key. The referenced column must be the PRIMARY KEY constraint in the referenced table, in
the same database. With this constraint, a value inserted into the referencing column must match a row
in the referenced table. If refcolumn is omitted, the primary key column of the reftable is used.
Reference constraints implicitly create an index on that column, hence the index clause is required.
A foreign key column is allowed to be NULL. If it is NULL, the row is not required to have a match in the
referenced table.
Foreign key restrictions:
- The referencing column and the reference column must be of the same data type, and have the same type parameters (e.g. a
VARCHAR(200)column cannot reference aVARCHAR(100)column.) - A column cannot reference another column in the same table.
- A column can only reference a primary key column
- A column can reference at most one other column. In other words, a column cannot have more than one “foreign key” constraint.
DELETE this clause specifies the action to perform when a referenced row in the referenced table is being deleted.
ON UPDATE this clause specifies the action to perform when a referenced column in the referenced table is being updated to a new value in the referenced column.
Referential_action
RESTRICT The DELETE/UPDATE fails if the check identifies that a foreign key constraint is about to be violated.
Updating a referenced column is not allowed, and the explicit RESTRICT option is supported for compatibility reasons (but that’s also the default behavior).
INDEX index_parameter
This clause specifies that an index shall be created for the pertinent column.
The index column size should not exceed the index maximum column size. See Database Limitations
This clause is required if REFERENCES, UNIQUE or PRIMARY KEY constraints are specified, and is optional otherwise.
Table Constraints
CONSTRAINT constraint_name
Optionally set a name for a constraint. Constraint names must be unique within table.
NOT NULL column_name
The same as the corresponding column constraint.
UNIQUE ( column_name [, ...] )
The same as the corresponding column constraint, but you may also specify multiple columns whose combination of values in the same row must be uniqe.
Note: If the UNIQUE constraint is specified for a set of columns, the NOT NULL constraint is required for each of the columns.
PRIMARY KEY ( column_name [, ...] )
The same as the corresponding column constraint, but you may also specify multiple columns.
FOREIGN KEY ( column_name [, ...] ) REFERENCES reftable [ ( refcolumn [, ...] ) ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ]
The same as the column constraint, but you may also specify multiple columns in this table and in the referenced table. The sets of columns must be of identical size and data types.
Storage parameters
Storage parameters, specified after the keywordWITH, are optional for both tables and indexes. Possible storage parameters:
devices
A list of one or more device names, separated by commas, surrounded by parentheses. Device names shall be specified as they appear in the output of the SHOW DEVICES. This parameter is required for both tables and indexes.
range_separators
Specify key range separators for index segments.
If the number of devices (specified in the devices parameter) is 1, this parameter shall not be specified or shall be set to an empty list. If the number of devices is more than 1, this parameter is required and the number of values must be one less than the number of devices.
Each separator is a value of the indexed column, or a list of values in case the index is on multiple columns. When there are multiple indexed columns, each separator may contain all or part of the participating columns’ values. If the number of values is smaller than the number of columns, the values correspond to the indexed columns in the order they appear in the index clause.
Examples:
- The range separators must be literal constants that match the column data type, and cannot be
NULL. - The list of values must be strictly monotonically increasing.
- The specified separator values are the first values in each range, starting with the second one. (In the first example above, ‘j’ is the first value within the second range and ‘p’ is the first value in the third range).
NULLvalues are always stored in the beginning of the first range.- A device can appear multiple times in the device list, in such case multiple ranges will be stored on the same device.
Examples
Create a table with a primary key:customers and set it ready for DML. Set the column c_custkey as the primary key,
defined with an index, and the c_name column with the NOT NULL constraint.
The primary key index is using the device (m1d2) - note that it has only 1 device therefore the
RANGE_SEPARATORS clause is not needed.
The column c_name has an index on it with the following devices:
- Device m1d2 stores values lower than or equal to ‘j’
- Device m2d5 stores values greater than ‘j’ and lower than or equal to ‘p’,
- Device m3d3 stores values greater than ‘p’.
orders with a foreign key referencing the primary key of the customers table.
Set the table as NOT READY for DML, so it can still be modified with the ALTER TABLE command.
The table primary key (o_order_id) is using an index stored on device m1d2
The o_name and o_date columns have a constraint prohibiting NULL values. The o_date also has a default value of the current date (by using the function NOW()).
The o_custkey column is a foreign key to the primary key of table customers. It has the NOT NULL constraint, and it must contain values that exist in the primary key (c_custkey) in the table customers. This foreign key column has an index, stored on the device m1d3.
The table itself is stored on the device m1d4.
Create a table with a calculated column:
transactions. Set the transaction_date column with the default of setting the current time.
Compatibility
TheCREATE TABLE command conforms to the SQL standard, with the following exceptions:
SET READY|NOT READYis a RegattaDB extension.- The
WITHclause is a RegattaDB extension; storage parameters are not in the standard.