MySQL Create Table

A table is actual storage object in MySQL. CREATE TABLE is used to create table in MySQL Server.

Syntax to MySQL Create Table:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] [partition_options]

A table creation command requires three things:

  • Name of the table(tbl_name)
  • Names of fields(column names)
  • Definitions for each field(Data Types are int,varchar, date,…)

MySQL Create Table Examples:

1. Create Table using command line:

CREATE TABLE emp(empno int, empname varchar(30), deptno int, doj date,salary int not null, PRIMARY KEY(empno));

Visual Representation:
MySQL Create Table

To verify table or created or not:

SHOW tables;

To see the table structure:

DESCRIBE emp;

2. Create Table using MySQL Workbench:

1. Open MySQL Workbench by navigating to Start -> MySQL -> MySQL Workbench.

2. Then connect to MySQL Server by entering password.

3. Then, expand the database in which we want to create table. Right click on tables and select Create Table.

4. Then, provide the name of table, storage engine(default is InnoDB), column names, data types.

5. Then, click on apply. Once we click on Apply, we will receive ‘Review Script to applied to the database”. Review it and click apply again.

6. Click on finish. Then, expand tables in schema’s sections. Then, new table will be appeared as shown below.

3. Create Table using PHP Script:

The following program is an example to create a table using PHP script:

Same explained in the below video,