MySQL is the most popular open-source relational database management system. It is used to store data into predefined tables and structure it through assigned relations (hence the name relational database).
You can add, change, and extract data using Structured Query Language (SQL). The programming language SQL is the primary tool used to communicate with your MySQL database.
Therefore, to create, modify, and work with relational databases, you need to run the appropriate SQL commands.
In this tutorial, you will find the most important MySQL commands as well as a downloadable cheat sheet.
Below you will find a list of commonly used MySQL commands and their definitions.
To see a full list of commands, please refer to the MySQL Cheat Sheet included at the bottom of the article.
Display the current user name and hostname:
USER()
CREATE USER 'user'@'host';
Grant a specified type of privilege to a user on an object:
GRANT privileges_name ON object TO user;
Set a password for the current user:
SET PASSWORD='password'
Note: Deploy a workload-optimized system for your MySQL database. Check out phoenixNAP’s dedicated servers offering!
Create a new database:
CREATE DATABASE database_name;
Access a database:
USE database_name;
Delete a database (and drop all tables):
DROP DATABASE database_name;
SHOW DATABASES;
SELECT user FROM mysql.user;
Note: To rename a database in MySQL without cPanel, create a new database and import the data. The MySQL command to rename a database was removed in MySQL 5.1.23 for security reasons.
Create a new table in a database with the specified column name and datatype:
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, );
Display the table structure:
DESCRIBE table_name
DROP TABLE table_name;
List all tables in the database:
SHOW TABLES;
To modify columns in a table, use the ALTER TABLE command.
For example, to add a column to a table, use the command:
ALTER TABLE table_name ADD column_name datatype;
Select and retrieve values from all columns in a table:
SELECT * FROM table_name;
Note: If you are interesting in checking the size of the table in MySQL, read our article how to check MySQL database and table size.
CREATE INDEX index_name ON table_name (column1, column2, . );
Delete an index from a table:
ALTER TABLE table_name DROP INDEX index_name;
Note: Never lose MySQL data again. Learn how to backup and restore a MySQL database.
Create a view (virtual table) with the specified content:
CREATE VIEW view_name AS SELECT column1, column2, . FROM table_name WHERE condition;
DROP VIEW view_name;
When creating columns in a table, you need to specify their name and data type. Use MySQL data types to specify what kind of value the column will store.
Common data types:
Note: Instead of setting up a separate MySQL server, you can also deploy MySQL in a Docker container.
MySQL comes with many built-in functions. These functions allow you to manipulate data.
Some of the most commonly used MySQL functions include:
This article includes a one-page MySQL commands reference sheet. You can save the cheat sheet in PDF format by clicking the Download MySQL Cheat Sheet button below.
It is essential to know the most common MySQL commands when working with databases. As there is an extensive number of commands, don’t bother learning them all by heart.
Download the cheat sheet and keep it close at hand to find the command you need.