Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Create Table

MySQL Create Table

Creating a MySQL table requires the following information:

Syntax

The following is the general SQL syntax for creating a MySQL table:

CREATE TABLE table_name (

    column1 datatype,

    column2 datatype,

    ...

);

Parameter Description:

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

,

    email

VARCHAR

(

100

)

NOT

NULL

,

    birthdate

DATE

,

    is_active

BOOLEAN

DEFAULT

TRUE

)

;

Example analysis:

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:


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:

  • MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)
  • MYSQLI_STORE_RESULT (default)
  • |

    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: