classCreateTable
final class CreateTable
implements Statement, Relation
uses Snapshot
@visibility public.A request to create a table from column, index and constraint definitions, a query, or both.
Rule: MYSQL-CREATE-TABLE-001. The statement provides one table declaration (MYSQL-TABLE-DECLARATION-001); it executes nothing and changes no context, and neither TEMPORARY nor IF NOT EXISTS changes what it declares (a temporary table hides a permanent table of the same name for the session only). The statement node is the one relation occurrence of the new table: its relation fact resolves to the declaration the statement provides, the same object, and holds its row shape. The query of CREATE TABLE ... SELECT is derived as an independent query that does not see the new table. DEFAULT expressions, generated column expressions, CHECK conditions, functional key parts and the partitioning are derived in the scope of MYSQL-DEFINITION-SCOPE-001, whose only visible relation is that occurrence. MySQL 5.x accepts TEMPORARY written more than once, and a PARTITION BY clause inside the parenthesis that opens the query (MYSQL-ENCLOSED-PARTITIONING-001); both are kept so the statement is written back as it was. Diagnostics: MYSQL-TABLE-PROBLEMS-001. Source: https://dev.mysql.com/doc/refman/8.4/en/create-table.html, https://dev.mysql.com/doc/refman/8.4/en/create-table-select.html, https://dev.mysql.com/doc/refman/8.4/en/create-temporary-table.html. Status: Implemented.
$create = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20), note TEXT NULL)');
$table = $create->declarations()[0];
[count($table->columns), $table->columns[0]->nullability, $table->columns[2]->nullability] // => [3, \SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability::NotNull, \SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability::Nullable]$create = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('CREATE TABLE t (a INT, CHECK (a > 0))');
$create->facts->scalar($create->statement->elements[1]->condition->left)->resolution->slot->column === $create->declarations()[0]->columns[0] // => trueProperties§
public list<TableElement> $elementspublic list<TableOption> $optionspublic QualifiedName $namepublic Partitioning|null $partitioning = nullpublic TableQuery|null $query = nullpublic int $temporaryWords = 0public bool $ifNotExists = falsepublic bool $enclosed = falseMethods§
public function __construct(
public QualifiedName $name,
list<TableElement> $elements = [],
list<TableOption> $options = [],
public Partitioning|null $partitioning = null,
public TableQuery|null $query = null,
public int $temporaryWords = 0,
public bool $ifNotExists = false,
public bool $enclosed = false,
)Parameters
$name | QualifiedName | The table name |
$elements | list<TableElement> | The column, index and constraint definitions in order; empty when no list is written |
$options | list<TableOption> | The table options in order |
$partitioning | Partitioning|null | The PARTITION BY clause |
$query | TableQuery|null | The query whose rows fill the table |
$temporaryWords | int | How many times TEMPORARY is written; MySQL 8.0 and later accept it once |
$ifNotExists | bool | Whether IF NOT EXISTS is written |
$enclosed | bool | Whether the partitioning is written inside the parenthesis that opens the query (MySQL 5.x) |
Calls 6
- static-call Check::input() line 91
- static-call Check::listOf() line 93
- class-const TableElement::class() line 93
- class-const TableOption::class() line 94
- method-call EnclosedQuery::accepts() line 96
- new EnclosedQuery line 96
public function temporary(): boolTells whether the table is TEMPORARY.
Returns
boolpublic function deriveStatement(Derivation $derivation): voidDerives the definition: its declaration, its problems and the expressions inside it.
Parameters
$derivation | Derivation |
Calls 7
- method-call Derivation::relation() line 114
- method-call Derivation::environment() line 114
- new TableTargets line 116
- method-call TableTargets::scope() line 117
- method-call TableTargets::implicit() line 117
- new ElementFacts line 118
- method-call ElementFacts::element() line 120
public function deriveRelation(Derivation $derivation, Environment $environment): RelationFactDerives the query, provides the declaration and reports the problems of the definition; the fact resolves to that declaration.
Parameters
$derivation | Derivation | |
$environment | Environment |
Returns
Calls 12
- method-call Derivation::query() line 130
- method-call Derivation::environment() line 130
- method-call TableDeclaration::table() line 131
- new TableDeclaration line 131
- method-call Derivation::declare() line 132
- new TableProblems line 133
- method-call TableProblems::report() line 134
- method-call TableProblems::selected() line 136
- new RelationFact line 139
- method-call TableTargets::shape() line 139
- new TableTargets line 139
- new DeclaredTable line 139
public function render(Output $out): voidWrites the definition.
Parameters
$out | Output |
Calls 8
- method-call Output::keyword() line 147
- method-call Output::name() line 156
- class-const NameUse::Qualifier() line 156
- class-const NameUse::Relation() line 158
- method-call EnclosedQuery::write() line 160
- new EnclosedQuery line 160
- method-call Output::symbol() line 165
- method-call Output::node() line 168
Test cases 64§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 3
Other tests reaching this symbol 61
CreateTableRuleTest::testModernLowersCreateTablecallsTableDefinitionRulesTest::testStatementLowersCreateTablecallsColumnFlagsTest::testFlagsAppliesTheAttributesInOrdercallsColumnFlagsTest::testInvisibleFollowsTheLastVisibilityAttributecallsColumnFlagsTest::testNullabilityFollowsTheFlagscallsElementFactsTest::testDeclarationAnswersTheColumnDeclarationcallsElementFactsTest::testElementResolvesTheReferencescallsElementFactsTest::testOptionsAcceptsTableOptionscallsElementFactsTest::testReferencesAnswersTheInlineReferencecallsElementFactsTest::testSpecificationDerivesTheExpressionscallsEnclosedQueryTest::testAcceptsNeedsAParenthesizedFirstOperandcallsEnclosedQueryTest::testWriteWritesTheOperandsOfALeadingUnioncallsEnclosedQueryTest::testWriteWritesThePartitioningInsideTheFirstParenthesiscallsTableDeclarationTest::testColumnDeclaresOneColumnDefinitioncallsTableDeclarationTest::testFindAnswersThePositionOfADefinedColumncallsTableDeclarationTest::testPrimaryColumnsNamesTheTableLevelKeyColumnscallsTableDeclarationTest::testSplitKeepsInvisibleColumnsImplicitcallsTableTargetsTest::testScopeSeesTheTableOnlycallsCollateAttributeTest::testDeriveAttributeDerivesNothingcallsColumnDefinitionTest::testDeriveElementDerivesTheSpecificationInTheTablecallsCommentAttributeTest::testDeriveAttributeDerivesNothingcallsDefaultExpressionTest::testDeriveAttributeResolvesColumnsOfTheTablecallsDefaultLiteralTest::testDeriveAttributeDerivesTheValuecallsEnforcementAttributeTest::testDeriveAttributeDerivesNothingcallsEngineAttributeTest::testDeriveAttributeDerivesNothingcallsFormatAttributeTest::testDeriveAttributeDerivesNothingcallsGeneratedColumnTest::testColumnAttributesAnswersTheAttributescallsGeneratedColumnTest::testDataTypeAnswersTheTypecallsKeywordAttributeTest::testDeriveAttributeDerivesNothingcallsOnUpdateTest::testDeriveAttributeDerivesTheValuecallsOrdinaryColumnTest::testColumnAttributesAnswersTheAttributesInOrdercallsOrdinaryColumnTest::testDataTypeAnswersTheTypecallsSridAttributeTest::testDeriveAttributeDerivesNothingcallsStorageAttributeTest::testDeriveAttributeDerivesNothingcallsCheckConstraintTest::testDeriveElementResolvesTheConditionInTheTablecallsColumnPartTest::testDeriveKeyPartDerivesNothingcallsExpressionPartTest::testDeriveKeyPartResolvesTheExpressionInTheTablecallsForeignKeyTest::testDeriveElementDerivesNothingcallsIndexDefinitionTest::testDeriveElementDerivesTheKeyPartscallsIndexCommentTest::testRenderWritesTheOptioncallsIndexEngineAttributeTest::testRenderWritesTheOptioncallsIndexParserTest::testRenderWritesTheOptioncallsIndexUsingTest::testRenderWritesTheOptioncallsIndexVisibilityTest::testRenderWritesTheOptioncallsKeyBlockSizeTest::testRenderWritesTheOptioncallsReferencesTest::testDeriveReferencesResolvesAnotherTablecallsReferencesTest::testDeriveReferencesResolvesTheTableBeingDefinedcallsAutoextendSizeOptionTest::testRenderWritesTheOptioncallsCharsetOptionTest::testRenderWritesTheOptioncallsCollationOptionTest::testRenderWritesTheOptioncallsEngineOptionTest::testRenderWritesTheOptioncallsInsertMethodOptionTest::testRenderWritesTheOptioncallsNumberOptionTest::testRenderWritesTheOptioncallsRowFormatOptionTest::testRenderWritesTheOptioncallsSecondaryEngineOptionTest::testRenderWritesTheOptioncallsStartTransactionTest::testRenderWritesTheOptioncallsStorageOptionTest::testRenderWritesTheOptioncallsTablespaceOptionTest::testRenderWritesTheOptioncallsTextOptionTest::testRenderWritesTheOptioncallsUnionOptionTest::testRenderWritesTheOptioncallsTableQueryTest::testRenderWritesTheDuplicateHandlingAndTheQuerycalls
Relations§
Instantiated in 4
- new SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule::legacy() packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:66
- new SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule::enclosed() packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:83
- new SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule::partitioned() packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:102
- new SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule::rest() packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:192
Type declarations 7
- type SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:78
- type SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:100
- type SqlSemantics\Platform\MySql\Lowering\TableDefinition\CreateTableRule packages/sql-semantics-mysql/src/Lowering/TableDefinition/CreateTableRule.php:177
- type SqlSemantics\Platform\MySql\Rules\TableDefinition\TableDeclaration packages/sql-semantics-mysql/src/Rules/TableDefinition/TableDeclaration.php:84
- type SqlSemantics\Platform\MySql\Rules\TableDefinition\TableDeclaration packages/sql-semantics-mysql/src/Rules/TableDefinition/TableDeclaration.php:103
- type SqlSemantics\Platform\MySql\Rules\TableDefinition\TableProblems packages/sql-semantics-mysql/src/Rules/TableDefinition/TableProblems.php:54
- type SqlSemantics\Platform\MySql\Rules\TableDefinition\TableProblems packages/sql-semantics-mysql/src/Rules/TableDefinition/TableProblems.php:117