Knowledgebase

How to Create a Table in MySQL?

MySQL is a powerful database management system that helps store and organise data efficiently. But before storing any data, you need a structured place to keep it—this is where tables come in!

You have to think of a table as an Excel sheet with rows and columns. Each row holds a record, and each column represents a specific data type, like names, emails, or phone numbers.

If you are new to MySQL and wondering how to create a table, don’t worry! This guide will walk you through the process in simple steps with easy-to-understand explanations.


Also Read: How to Install phpMyAdmin with XAMPP?


What is a Table in MySQL?

A table in MySQL is a structured format used to store data in a database. Each table consists of columns (fields) and rows (records).

Imagine you have a notebook where you write down your daily expenses. Each page represents a specific category (food, transport, or shopping), and each entry records details like date, amount, and description.

A table in MySQL works the same way! It organizes information into a structured format using rows and columns.

Think of it like an Excel sheet!

Here’s a quick example:

ID

Name

Email

Age

1

John Doe

john@example.com

25

2

Jane Doe

jane@example.com

28


In this example:

  • ID is a unique identifier.
  • Name stores text values.
  • Email stores email addresses.
  • Age stores numerical values.

Now, let’s learn how to create a table in MySQL using the Command Line, MySQL Workbench, and cPanel.


Also Read: How to reset the password for a MySQL database?


Ways to Create Table in MySQL

Method 1: Using the Command Line

Using the command line is the most direct and efficient way to interact with MySQL. It allows developers and database administrators to quickly create, modify, and manage databases without relying on graphical tools.

➔ First, open your MySQL command line and login by typing:

mysql -u root -p

➔ Press ‘Enter,’ then enter your password when prompted.

➔ Before creating a table, choose the database where you want to store it. Use this command to ensure that the table is created inside the selected database.

USE your_database_name;

*Note: Replace your_database_name with your database name.

➔ Now, use the CREATE TABLE command to define your table structure. Let’s create a Users table:

CREATE TABLE Users (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    Email VARCHAR(255) UNIQUE,
    Age INT,
    Signup_Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

➔ To check if your table was created successfully, you have to run:

SHOW TABLES;

➔ To see the table structure, use:

DESCRIBE Users;

➔ To insert data into the table, you have to use the INSERT INTO command:

INSERT INTO Users (Name, Email, Age) VALUES (‘John Doe’, ‘john@example.com’, 25);

➔ If you want to verify the data:

SELECT * FROM Users;

This will show all the records in the Users table.


Also Read: How To Remove Directory in Linux With Command?


Method 2: Using MySQL Workbench

MySQL Workbench is a graphical interface that makes it easier to manage MySQL databases without needing to write SQL commands manually. It is great for beginners and those who prefer a visual approach.

➔ Firstly, open your MySQL Workbench. To launch it, connect it to your MySQL server.

➔ In that, select the database where you want to create the table.

➔ To create a table, you have to click on the ‘Schemas’ tab on the left sidebar.

Click on schemas tab

➔ Select your database and right-click on the ‘Tables’ folder.

Right click on the tables folder

➔ From the options, click ‘Create Table.’

Click on create table

➔ You have to enter the table name (e.g., New Users).

Enter table name

➔ Now, you have to define table columns! You have to add columns by specifying Name, Data Type, and Constraints.

For example:

  • ID → INT, Primary Key, Auto Increment
  • Name → VARCHAR(100), NOT NULL
  • Email → VARCHAR(255), UNIQUE
  • Age → INT
  • Signup_Date → TIMESTAMP, DEFAULT CURRENT_TIMESTAMP

➔ Once done, it’s time to save the table. Click ‘Apply’ and review the generated SQL query.

Click on Apply to save table

➔ Once done, it’s time to verify the table. To do this, go to the ‘Tables’ section under your database and find the newly created ‘New Users’ table.

➔ Right-click on it and choose Select Rows – Limit 1000 to view its structure.

View table's structure

Read More: How Do I Connect MySQL Database Via MySQL Workbench?


Method 3: Using cPanel (phpMyAdmin)

cPanel provides a user-friendly way to manage databases using phpMyAdmin. This method is ideal for beginners or website owners who use shared or VPS hosting and need a simple interface.

➔ Log into cPanel

Cpanel login

➔ Once logged in, scroll down to the ‘Databases’ section and click on ‘phpMyAdmin.’

Select phpmyadmin from database

➔ In phpMyAdmin, find and select your database from the left panel.

Select database from left panel

➔ To create a table, click on the ‘Structure’ tab.

Click on structure to create table

➔ Under the ‘Create Table’ section, you have to enter the table name (e.g., New Users).

Enter table name - phpmyadmin

➔ In that, you have to define the number of columns (e.g., 5).

Enter number of columns

➔ Once you define the number of columns, click ‘Create.’

Click on create table - phpmyadmin

➔ Now, you have to define table columns, for each column, you have to specify:

  • Name (e.g., ID, Name, Email, Age, Signup_Date)
  • Type (e.g., INT, VARCHAR(100), TIMESTAMP)
  • Attributes (e.g., Primary Key, Auto Increment for ID)
Define table columns

➔ Once done, You have to click ‘Save.’

➔ After saving, you need to verify the table. To do this, go back to the phpMyAdmin main page.

➔ In that, click on the ‘Users’ table and select ‘Browse’ to see its structure.


Also Read: How to Change MySQL Database User Password From cPanel?


Conclusion

And there you have it! You now know how to create a table in MySQL using three different methods:

  • Command Line
  • MySQL Workbench
  • cPanel

No matter which method you choose, creating tables is an essential step in organizing your database effectively.

Start practising today, and soon, managing databases will feel like second nature!