MySQL schema features
Decisions about views, CREATE TABLE LIKE and AS SELECT, temporary tables, check constraints, generated columns, invisible columns, functional indexes and expression types (D70 to D78).
D70 Views are stored queries, read as derived tables#
A view is a document in a hidden table of its database holding its name, column list, query text, check option and other properties. Wherever a FROM names a view, the query is read as a derived table; unqualified table names in the view get the view's database, so it means the same from any database. Views of views expand to 16 levels. CREATE runs the query with no rows first, so unknown tables and columns are refused as in MySQL. The resolver is shared with common table expressions.
D71 CREATE TABLE ... LIKE and CREATE TABLE ... [AS] SELECT#
LIKE copies columns, defaults and indexes (not rows, not foreign keys) by reading SHOW CREATE TABLE. AS SELECT runs the query once, makes the table from its columns and adds the rows through the INSERT machinery, so IGNORE, REPLACE and keys behave as for INSERT; if the rows cannot be added, the table is dropped again. Column types follow the result columns.
D72 Temporary tables#
A temporary table is an ordinary table under a hidden name that includes the connection id. The session keeps the list and redirects names, so the temporary table hides a base table of the same name, is not listed by SHOW TABLES, and is dropped when the session ends.
D73 CHECK constraints#
Checks are kept in the table definition and evaluated against every row about to be written by any statement that writes rows. A false condition is error 3819, and a failing statement undoes its rows. Adding a check to a table checks the rows already in it. A multi-row insert into a table with a check is written row by row.
D74 Generated columns#
Both VIRTUAL and STORED columns are worked out when the row is written and kept in the row, so reads, indexes and unique keys on them need nothing else; only SHOW CREATE TABLE and DESCRIBE tell them apart. Columns are computed in column order, and adding a generated column fills the old rows. The error numbers are MySQL's.
D75 Invisible columns and invisible indexes#
An invisible column is left out of SELECT * and out of the columns an INSERT without a column list takes, but can be named elsewhere. This reuses the same hidden mark that the row key has, so no other code changes. Invisible indexes are kept in the definition and reported by SHOW INDEX; a primary key cannot be made invisible.
D76 Functional indexes#
As MySQL does, an expression in a key becomes a hidden, stored, invisible generated column and the index is made on it, so unique keys, updates and DROP INDEX use the code every index uses. The column is typed from the expression without evaluating it, and hidden columns are left out of DESCRIBE and information_schema.COLUMNS.
D77 The rules for generated columns, functional indexes and checks; the index answers the expression#
A comparison with the exact expression of a functional index is taken as a comparison with the hidden column, so the filter goes to Raft DB and uses the index; the expression must be written as in the index. The same decision lists what such expressions may not contain (non-deterministic functions, subqueries, variables, aggregates), with MySQL's errors. It closes the gaps left by D74 and D76.
D78 The type of an expression is worked out as MySQL does#
The column that CREATE TABLE ... AS SELECT makes for a computed value, and the hidden column of a functional index, get the type MySQL gives the expression, worked out without evaluating it. Before, such columns were varchar(256). The rules (display widths, int versus bigint, decimal(p+4, s+4) for division, double for floats and text in arithmetic, lengths of string function results) were found by trying expressions on a MySQL server.