WebServer.com.my

Portal Home > Knowledgebase > Scripting, Component & Database > MySQL > How to create table in MySQL?


How to create table in MySQL?




The following CREATE TABLE statement is used to create a table in MySQL.

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type
);

* Data type may be vary depending on your column category. Please refer to the list below for more details:-

1) Text types
CHAR( ) = A fixed section from 0 to 255 characters long.
VARCHAR( ) = A variable section from 0 to 255 characters long.
TINYTEXT = A string with a maximum length of 255 characters.
TEXT = A string with a maximum length of 65535 characters.
BLOB = A string with a maximum length of 65535 characters.
MEDIUMTEXT = A string with a maximum length of 16777215 characters.
MEDIUMBLOB = A string with a maximum length of 16777215 characters.
LONGTEXT = A string with a maximum length of 4294967295 characters.
LONGBLOB = A string with a maximum length of 4294967295 characters.

The ( ) brackets allow you to enter a maximum number of characters will be used in the column. For instance: VARCHAR(20)

2) Number types
TINYINT( ) = -128 to 127 normal; 0 to 255 UNSIGNED.
SMALLINT( ) = -32768 to 32767 normal; 0 to 65535 UNSIGNED.
MEDIUMINT( ) = -8388608 to 8388607 normal; 0 to 16777215 UNSIGNED.
INT( ) = -2147483648 to 2147483647 normal; 0 to 4294967295 UNSIGNED.
BIGINT( ) = -9223372036854775808 to 9223372036854775807 normal; 0 to 18446744073709551615 UNSIGNED.
FLOAT = A small number with a floating decimal point.
DOUBLE( , ) = A large number with a floating decimal point.
DECIMAL( , ) = A DOUBLE stored as a string , allowing for a fixed decimal point.

The integer types have an extra option called UNSIGNED. Normally, the integer goes from an negative to positive value. Using an UNSIGNED command will move that range up so it starts at zero instead of a negative number.

3) Date types
DATE = YYYY-MM-DD.
DATETIME = YYYY-MM-DD HH:MM:SS.
TIMESTAMP = YYYYMMDDHHMMSS.
TIME = HH:MM:SS.

4) Misc types
ENUM( ) = Short for ENUMERATION which means that each column may have one of a specified possible values.
SET = Similar to ENUM except each column may have more than one of the specified possible values.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read