EN English
Version: v2.6.0

create

Create a new table.

create($table, $columns, $options)
Return Value
[PDOStatement] The PDOStatement instance for the executed query.

Basic Sample

Define each column as an array. Medoo will combine the parts into a complete column definition.
$database->create("account", [
	"id" => [
		"INT",
		"NOT NULL",
		"AUTO_INCREMENT",
		"PRIMARY KEY"
	],
	"first_name" => [
		"VARCHAR(30)",
		"NOT NULL"
	]
]);
CREATE TABLE IF NOT EXISTS account (
	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	first_name VARCHAR(30) NOT NULL
)

Identity Column

Add "@id" as a list item to define an auto-generated integer primary key named id. Use another name after @, such as "@account_id", to customize the column name. Medoo translates the marker into the appropriate definition for the current database type.
$database->create("account", [
	"@id",
	"email" => [
		"VARCHAR(255)",
		"NOT NULL"
	]
]);
Database Generated definition for @id
MySQL / MariaDB id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
PostgreSQL id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
SQLite id INTEGER PRIMARY KEY AUTOINCREMENT
MSSQL id BIGINT IDENTITY(1,1) PRIMARY KEY
Oracle id NUMBER(19) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
Sybase id BIGINT IDENTITY PRIMARY KEY

Only one identity marker is allowed. The same column must not also be declared as a regular column definition.

Advanced

You can also pass raw strings as column definitions for additional options. The <column_name> syntax is supported as a shortcut for identifier quoting.
$database->create("account", [
	"id" => [
		"INT",
		"NOT NULL",
		"AUTO_INCREMENT"
	],
	"email" => [
		"VARCHAR(70)",
		"NOT NULL",
		"UNIQUE"
	],
	"PRIMARY KEY (<id>)"
], [
	"ENGINE" => "MyISAM",
	"AUTO_INCREMENT" => 200
]);
CREATE TABLE IF NOT EXISTS account (
	id INT NOT NULL AUTO_INCREMENT,
	email VARCHAR(70) NOT NULL UNIQUE,
	PRIMARY KEY (`id`)
)
ENGINE = MyISAM,
AUTO_INCREMENT = 200