The Wayback Machine - https://web.archive.org/web/20241004065708/https://www.geeksforgeeks.org/mysql-create-table/
Open In App

MySQL CREATE TABLE

Last Updated : 05 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Creating tables in MySQL is a fundamental task for organizing and managing data within a database. Tables act as structured containers, similar to spreadsheets, where data is stored in rows and columns. In this article, we will explore the process of creating tables in MySQL using both the Command Line Interface (CLI) and MySQL Workbench.

MySQL CREATE TABLE

MySQL provides multiple methods for creating tables. The two primary methods include using the Command Line Interface(CLI) and the Graphical User Interface(GUI) provided by MySQL Workbench.

MySQL CREATE TABLE using Command Line Client

MySQL Command Line Client allows you to create a table using the CREATE TABLE statement. This method requires specifying the table name, column names, and data types. The syntax is as follows:

CREATE TABLE table_name (
    column1_name datatype constraints,
    column2_name datatype constraints,
    ...
    columnN_name datatype constraints
);

Parameters:

  • column_name: This is the name of each column in the table.
  • datatype: This is the data type of the column (e.g., INT, VARCHAR, DATE, etc.).
  • constraints: These are optional and define rules for the data in the column, such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, etc.

MySQL CREATE TABLE Example

Here’s how you can do it using MySQL Command Line Client:

  • Open MySQL Command Line Client:
mysql -u your_username -p

Replace your_username with your MySQL username. You’ll be prompted to enter your password.

  • Once you’re logged in, use the CREATE TABLE statement to create the employees table:
CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    age INT,
    department VARCHAR(100),
    salary DECIMAL(10, 2)
);
  • To verify that the table was created successfully, you can use the DESCRIBE statement to view the structure of the employees table:

Query:

DESCRIBE employees;

This will display the structure of the employees table, confirming that it has been created with the specified columns.

Output:

Output

Output

Explanation: This output displays the structure of the employees table, providing information about each column as specified in the CREATE TABLE command.

By following these steps, you have successfully created a table in the MySQL command-line interface to store information about employees. The created table is now ready to be populated with data and used in your MySQL database.

MySQL CREATE TABLE using MySQL Workbench

For those who prefer a more visual approach, MySQL Workbench is a powerful tool. Follow these steps to create a table using MySQL Workbench:

To create the Table follow below given steps.

Step 1: Open MySQL Workbench and Connect to Server

  • Launch MySQL Workbench and establish a connection to your server.

Step 2: Create a Database (if necessary)

  • If you haven’t already created a database/schema, create one now.

Step 2: Select The Database

  • Click on the “Schemas” button on the left-hand side of the screen.
  • From the list of databases, select the one you want to work with.
select-database

Select database

Step 4: Creating Table

  1. Click on the “Create a new Table” button in the toolbar.
  • Enter the required details such as table name, column names, data types, and constraints.
  • For example, let’s create a table named “information“.
create-table

Create Table

Hence the Information table is successfully created using the MYSQL Workbench.

Step 5: Verify the Table

  • Once the table is created, you can verify it by using the “DESCRIBE” command in the MySQL Command Line Client.

Step 6: Verification in the Command Line

  • Open MySQL Command Line Client.
  • Connect to your server.
  • Use the “DESCRIBE” command to view the structure of the table.
describe table

Describe Table

By following these steps, you can easily create a table using MySQL Workbench and verify it using the MySQL Command Line Client.

Conclusion

In conclusion, creating tables in MySQL helps you organize and manage your data effectively. This guide showed you two main methods: using the Command Line Interface (CLI) and MySQL Workbench. By learning these methods, you can easily set up and handle MySQL tables, whether you prefer typing commands or using a visual tool. This flexibility makes MySQL a great choice for managing your database.


Previous Article
Next Article

Similar Reads

Update One Table with Another Table's Values in MySQL
Sometimes we need to update a table data with the values from another table in MySQL. Doing this helps in efficiently updating tables while also maintaining the integrity of the database. This is mostly used for automated updates on tables. We can update values in one table with values in another table, using an ID match. ID match ensures that the
3 min read
Functions in MySQL | Numeric, String and Date Time Functions in MySQL
In MySQL, functions play a crucial role in performing various operations on data, such as calculations, string manipulations, and date handling. These built-in functions simplify complex queries and data transformations, making it easier to manage and analyze data within a database. In this article, we will look at these different categories of MyS
4 min read
MySQL Can't Specify Target Table For Update in FROM Clause
The "MySQL can't specify target table for update in FROM clause" error is a common challenge encountered when attempting to update a table based on a subquery that refers to the same table in the FROM clause. This restriction is in place to prevent ambiguous or unintended updates that might arise from self-referencing subqueries. So, in this articl
3 min read
How to Find Records From One Table Which Don't Exist in Another MySQL
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides a variety of rich features to create databases and tables, insert data in them, and further manipulate them as the system evolves. In this article, w
5 min read
How to Return Pivot Table Output in MySQL
Pivoting a table in MySQL involves transforming rows into columns, which can be particularly useful for data analysis and reporting. While MySQL does not have a native PIVOT function like some other database systems, you can achieve pivoting using conditional aggregation with the CASE statement. So, In this article, we will explore how can I return
5 min read
MySQL Describe Table
MySQL is an open-source relational database management system (RDBMS). It is one of the most popular databases globally, known for its reliability, and scalability. MySQL is designed to run on various operating systems, including Windows, Linux, macOS, and more. It is known for its high performance, quick retrieval, and efficient storage. It provid
6 min read
MySQL RENAME TABLE Statement
The MySQL RENAME TABLE statement is a simple yet powerful command that allows you to change the name of an existing table in your database. This can be useful for various reasons, such as updating table names to better reflect their content or restructuring your database without losing any data. By using RENAME TABLE, you can easily and quickly ren
5 min read
MySQL DROP TABLE Statement
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides various rich features to create databases and tables, insert data in them, and further manipulate them as the system evolves. In this article, we are
6 min read
How to Get the Datatype of Table Columns in MySQL?
When working with MySQL databases, knowing the datatype of table columns is essential to maintain data integrity and avoid errors. This guide covers methods to check column datatypes, such as using SHOW COLUMNS and querying INFORMATION_SCHEMA.COLUMNS. Understanding column datatypes helps in designing databases, ensuring data validation, and optimiz
4 min read
How to Update Table Rows Using Subquery in MySQL
Updating table rows using subqueries in MySQL enables precise modifications based on specific conditions or values from other tables. This technique leverages subqueries within the SET or WHERE clauses of the UPDATE statement, allowing dynamic and context-specific updates. This guide covers the syntax, methods, and examples of updating rows with ag
5 min read
MySQL Temporary Table
Do you want to store the intermediate results of your query in some table but only for a given DB session and not persist for the lifetime? Is there some way to hold temporary data for a short time without making it permanent on the database forever? If you have ever thought about this and wondered how to do that, then you have arrived at the right
5 min read
Introduction to MySQL Table Partitioning
The MySQL table partitioning feature divides large tables into smaller, more manageable partitions. Each partition can be thought of as a separate sub-table with its own storage engine, indexes, and data. Partitioning is particularly useful for improving query performance reducing the index size and enhancing data management in scenarios where tabl
6 min read
MySQL REPAIR TABLE
MySQL is an open-source Relational Database Management system that stores the data in the form of rows and columns and SQL is the language that is used to store, manipulate, and retrieve the data. "REPAIR TABLE" is one of the useful commands in MySQL and is used for repairing the tables in case the tables get corrupted. The corruption of the tables
5 min read
MySQL Derived Table
Structured Query Language (SQL) is a powerful tool for managing and querying relational databases, and MySQL is one of the most widely used database management systems. In MySQL, derived tables offer a flexible and efficient way to manipulate and analyze data within a query. In this article, we will get inside the concept of MySQL-derived tables, e
5 min read
MySQL Common Table Expressions
One of the most powerful features added in MySQL version 8.0 is common table expressions, which allow for the construction of temporary result sets within a single SQL query. In our daily life queries, we often use common table expressions and it makes our work easier. In this article, we will understand the Common Table Expression with examples an
6 min read
Cloning Table in MySQL
Duplicating a table in MySQL involves creating a new table that replicates an existing table's structure and/or data. This capability is valuable for creating backups, setting up test environments, or transferring data between databases. In this article, we will explore various methods for duplicating tables in MySQL, including copying only the tab
3 min read
MySQL - CHECKSUM TABLE Statement
The CHECKSUM TABLE statement in MySQL is a useful tool to verify the integrity of tables and detect any changes or corruption in the data stored within them. This statement calculates a checksum value for a table and its contents, allowing users to compare the results across backups, replicas, or after operations that may modify the data. In this a
5 min read
How to Update a Table Data From Another Table in SQLite
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. The SQLite offers some features which are that it is a serverless architecture, quick, self-contained, reliable, full-featured SQL database engine. SQLite does not require any server to perform queries and operations on the database. In this article
3 min read
How to Select Rows from a Table that are Not in Another Table?
In MySQL, the ability to select rows from one table that do not exist in another is crucial for comparing and managing data across multiple tables. This article explores the methods to perform such a selection, providing insights into the main concepts, syntax, and practical examples. Understanding how to identify and retrieve rows that are absent
3 min read
How to Create a MySQL REST API
Creating a REST API is important for enabling communication between different software systems. MySQL is one of the most popular relational database management systems which serves as the backbone for data storage in web applications. In this article, we will learn how to create a REST API using MySQL as the database backend. We will cover the step
6 min read
How to Create Database Objects in MYSQL
MySQL, the widely used relational database management system, offers a robust set of tools for creating and managing database objects. To organize and manipulate data effectively, users can utilize SQL commands such as CREATE TABLE, CREATE VIEW, CREATE INDEX, and CREATE PROCEDURE. In this article, We will explore the creation of these database obje
4 min read
MySQL Create Database Statement
The MySQL CREATE DATABASE statement is used to create a new database. It allows you to specify the database name and optional settings, such as character set and collation, ensuring the database is ready for storing and managing data. In this article, we are going to learn how we can create databases in the MySQL database management system through
4 min read
MySQL CREATE INDEX Statement
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manage and manipulate data. The CREATE INDEX statement is an important tool for enhancing database performance. By creating indexes, MySQL can quickly locate and retrieve relevant data, reducing the need for full table scans and minimizing res
4 min read
MySQL CREATE VIEW Statement
MySQL, an open-source relational database management system, offers a variety of features to manage and manipulate data efficiently. One of these features is the CREATE VIEW statement, which allows you to create a virtual table known as a view. A view provides a way to simplify complex queries, enhance security, and present data in a specific forma
5 min read
How to Create id with AUTO_INCREMENT in MySQL?
Primary keys in databases uniquely identify each record, ensuring data integrity and efficient retrieval. AUTO_INCREMENT, a feature in MySQL, automatically assigns unique numeric identifiers to new rows, simplifying data management. Understanding these concepts is crucial for designing robust database schemas that optimize data storage and retrieva
6 min read
MySQL Create Trigger
Database triggers are specialized procedures that automatically respond to certain events on a table or view. These events include actions such as INSERT, UPDATE, or DELETE. Triggers can be used to enforce complex business rules, maintain audit trails, or synchronize data across tables. In MySQL, triggers are useful concepts for automating and maki
4 min read
MySQL CREATE USER Statement
The CREATE USER statement in MySQL is an essential command used to create new user accounts for database access. It enables database administrators to define which users can connect to the MySQL database server and specify their login credentials. In this article, We will learn about MySQL CREATE USER Statement in detail and so on. CREATE USER in M
4 min read
Create Unique Constraint with NULL Columns in MySQL
Unique constraint in MySQL ensures that each value in the column is unique. If a column contains a NULL value, it is also treated as a unique value, but if a column contains multiple NULL values it can not have a unique constraint. In this article we will look over how we can Create unique constraints with NULL columns in MySQL, using the syntax, m
3 min read
SQLite Create Table
SQLite is a database engine. It does not require any server to process queries. It is a kind of software library that develops embedded software for television, smartphones, and so on. It can be used as a temporary dataset to get some data within the application due to its efficient nature. It is preferable for small datasets. SQLite CREATE TABLESQ
2 min read
Create Table in MariaDB
MariaDB is an open-source RDBMS that has become famous for its speed, and scalability. MariaDB Stores data in tables with structured relationships between them. In terms of working with databases, one crucial element involves the construction of tables for organizing and storing data effectively. In this article, we will discuss creating tables in
4 min read