CREATE TABLE table_name ( create_definition,... )
create_definition:
column_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
[ PRIMARY KEY ] [reference_definition]
or PRIMARY KEY ( index_column_name,... )
or KEY [index_name] KEY( index_column_name,...)
or INDEX [index_name] ( index_column_name,...)
or UNIQUE [index_name] ( index_column_name,...)
or FOREIGN KEY index_name ( index_column_name,...) [reference_definition]
or CHECK (expr)
type:
TINYINT[(length)] [UNSIGNED] [ZEROFILL]
or SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
or MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
or INT[(length)] [UNSIGNED] [ZEROFILL]
or INTEGER[(length)] [UNSIGNED] [ZEROFILL]
or BIGINT[(length)] [UNSIGNED] [ZEROFILL]
or REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
or DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
or FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
or DECIMAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
or NUMERIC[(length,decimals)] [UNSIGNED] [ZEROFILL]
or CHAR(length) [BINARY],
or VARCHAR(length) [BINARY],
or DATE
or TIME
or TIMESTAMP
or DATETIME
or TINYBLOB
or BLOB
or MEDIUMBLOB
or LONGBLOB
or TINYTEXT
or TEXT
or MEDIUMTEXT
or ENUM(value1,value2,value3...)
or SET(value1,value2,value3...)
index_column_name:
column_name [ (length) ]
reference_definition:
REFERENCES table_name [( index_column_name,...)]
[ MATCH FULL | MATCH PARTIAL]
[ ON DELETE reference_option]
[ ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
See section Column types.
The FOREIGN KEY, CHECK and REFERENCE syntax are only for compatibility. (To make it easier to port code from other SQL servers and run applications that create tables with references). They don't actually do anything. See section What functionality is missing in MySQL.
If a column doesn't have a DEFAULT value and is not declared as NOT NULL, the default value is NULL.
If a column doesn't have a DEFAULT value and is declared as NOT NULL, MySQL will automaticly assign a default value for the field.
INT(5) ZEROFILL a value of 5 is retrieved as 00005.
BINARY means that the column will be compared case sensitive. The
default is that all strings are compared case insensitive according to
ISO-8859-1 Latin1. BINARY is 'sticky' which means that if a column
marked BINARY is used in a expression, the whole expression is
compared BINARY.
column_name(length) syntax one can specify an index which is only
a part of a string column. This can make the index file much smaller.
BLOB and TEXT types.
isamchk utility to
reorganise tables.
VARCHAR
columns with a length of one or two are changed to CHAR. When using one
VARCHAR column all CHAR columns longer than 2 are changed to
VARCHAR's. This doesn't affect the usage of the column in any way;
In MySQL VARCHAR is just a different way to store characters.
MySQL does the conversion because it will save space and make the
table faster.
See section What are the different row formats? Or when to use VARCHAR/CHAR?
INSERT/UPDATE all strings (CHAR and
VARCHAR) are silently chopped/padded to the maximal length given
by CREATE. All end spaces are also automatically removed. For example
VARCHAR(10) means that the column can contain strings with a
length up to 10 characters.
NULL value.
REGEXP and RLIKE) uses
ISO8859-1 (Latin1) when deciding the type of a character.
Go to the first, previous, next, last section, table of contents.