MySQL Create Table
Creating a MySQL table requires the following information:
- Table name
- Table field names
- Define the data type of each table field
Syntax
The following is the general SQL syntax for creating a MySQL table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Parameter Description:
table_nameis the name of the table you want to create.column1,column2, ... are the column names in the table.datatypeis the data type of each column.
The following is a concrete example that creates a users table users:
Example
CREATE
TABLE
users
(
id
INT
AUTO_INCREMENT
PRIMARY
KEY
,
username
VARCHAR
(
50
)
NOT
NULL
,
VARCHAR
(
100
)
NOT
NULL
,
birthdate
DATE
,
is_active
BOOLEAN
DEFAULT
TRUE
)
;
Example analysis:
id: user id, integer type, auto-increment, used as the primary key.username: username, variable-length string, not allowed to be empty.email: user email, variable-length string, not allowed to be empty.birthdate: the user's birthday, date type.is_active: whether the user is activated, boolean type, with a default value of true.
The above is just a simple example using some common data types including INT, VARCHAR, DATE, and BOOLEAN. You can choose different data types according to your actual needs. The AUTO_INCREMENT keyword is used to create an auto-incrementing column, and PRIMARY KEY is used to define the primary key.
If you want to specify the storage engine, character set, and collation when creating a table, you can use the CHARACTER SET and COLLATE clause:
Example
CREATE
TABLE
mytable
(
id
INT
PRIMARY
KEY
,
name
VARCHAR
(
50
)
)
CHARACTER
SET
utf8mb4
COLLATE
utf8mb4_general_ci;
The above code creates a table using the utf8mb4 character set and the utf8mb4_general_ci collation.
In the following example, we will create the table runoob_tbl in the RUNOOB database:
Example
CREATE
TABLE
IF
NOT
EXISTS
runoob_tbl
(
runoob_id
INT
UNSIGNED
AUTO_INCREMENT
,
runoob_title
VARCHAR
(
100
)
NOT
NULL
,
runoob_author
VARCHAR
(
40
)
NOT
NULL
,
submission_date
DATE
,
PRIMARY
KEY
(
runoob_id
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8;
Example analysis:
-
If you do not want a field to benullyou can set the field's attribute to NOT NULL, as in the runoob_title and runoob_author fields in the example above. If the data entered for that field is empty when operating on the database, an error will be reported.
- AUTO_INCREMENT Defining a column as auto-increment is generally used for primary keys; the value automatically increases by 1.
- PRIMARY KEY The keyword is used to define a column as the primary key. You can use multiple columns to define a primary key, separated by commas ,
- ENGINE sets the storage engine,CHARSET sets the encoding.
Creating a Table from the Command Prompt
You can easily create a MySQL table through the mysql> command window.
You can use the SQL statement CREATE TABLE to create a table.
Example
The following is an example of creating the table runoob_tbl:
Example
root@host# mysql
u root
p
Enter password:
mysql
USE
RUNOOB;
DATABASE
changed
mysql
CREATE
TABLE
runoob_tbl
(
->
runoob_id
INT
NOT
NULL
AUTO_INCREMENT
,
->
runoob_title
VARCHAR
(
100
)
NOT
NULL
,
->
runoob_author
VARCHAR
(
40
)
NOT
NULL
,
->
submission_date
DATE
,
->
PRIMARY
KEY
(
runoob_id
)
->
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8;
Query OK
,
0
ROWS
affected
(
0.16
sec
)
mysql
**Note:** The MySQL statement terminator is the semicolon ; .
Note: -> is the line continuation marker; do not copy it.
Creating a Table with a PHP Script
You can use PHP's mysqli_query() function to create a table in an existing database.
This function takes two parameters and returns TRUE on success, or FALSE otherwise.
Syntax
mysqli_query(connection,query,resultmode);
| Parameters | Description |
|---|---|
| connection | Required. Specifies the MySQL connection to use. |
| query | Required, specifies the query string. |
| resultmode |
Optional. A constant. Can be any of the following values: |
Example
The following example uses a PHP script to create a table:
Create a data table
' ; $sql = " CREATE TABLE runoob_tbl( " . " runoob_id INT NOT NULL AUTO_INCREMENT, " . " runoob_title VARCHAR(100) NOT NULL, " . " runoob_author VARCHAR(40) NOT NULL, " . " submission_date DATE, " . " PRIMARY KEY ( runoob_id ))ENGINE=InnoDB DEFAULT CHARSET=utf8; " ; mysqli_select_db ( $conn , ' RUNOOB ' ) ; $retval = mysqli_query ( $conn , $sql ) ; if ( ! $retval ) { die ( ' Failed to create table: ' . mysqli_error ( $conn ) ) ; } echo " Table created successfully \n " ; mysqli_close ( $conn ) ; ?>After successful execution, you can view the table structure from the command line:
