CREATE TABLE statement creates a table in a MySQL database.
Syntax (full...) | CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table_name (columns) options |
CREATE TABLE table_name select_statement | |
CREATE TABLE table_name LIKE table_name2 | |
Quick Example | CREATE TABLE t (c CHAR); |
Table Name | table_name or database_name.table_name to create the table in the specified database |
Temporary Table | Visible only to the current connection, and dropped automatically when it is closed |
Versions: MySQL 5.x
IF NOT EXISTS clause prevents an error if the table already exists:
CREATE TABLE IF NOT EXISTS t_exists1 ( c1 CHAR );
Oracle:
Oracle does not support IF NOT EXISTS in CREATE TABLE statement, so you have to use a PL/SQL block.
Note that in a PL/SQL block all DDL statements must be executed using EXECUTE IMMEDIATE that accepts a statement as a string:
DECLARE cnt NUMBER; BEGIN SELECT count(*) INTO cnt FROM all_tables WHERE table_name = 'T_EXISTS1'; IF cnt = 0 THEN EXECUTE IMMEDIATE 'CREATE TABLE t_exists1 ( c1 CHAR );'; END IF; END; /
SQL Server:
SQL Server does not support IF NOT EXISTS in CREATE TABLE statement, but you can use OBJECT_ID function to check if a table exists:
IF OBJECT_ID('t_exists1', 'U') IS NULL CREATE TABLE t_exists1 ( c1 CHAR ); GO
PostgreSQL:
PostgreSQL 9.1 supports IF NOT EXISTS clause in CREATE TABLE statement, so no conversion is required:
CREATE TABLE IF NOT EXISTS t_exists1 ( c1 CHAR );