MySQL Commands Cheat Sheet
This cheat sheet provides a quick reference to the most commonly used MySQL commands:
Command | Description |
---|---|
mysql -u [username] -p [database] |
Connect to a MySQL database as a specific user. |
exit |
Exit the MySQL command-line tool. |
CREATE DATABASE [database]; |
Create a new database. |
DROP DATABASE [database]; |
Delete an existing database. |
SHOW DATABASES; |
List all databases on the MySQL server. |
USE [database]; |
Select a database to work with. |
CREATE TABLE [table] ([column definitions]); |
Create a new table with the specified column definitions. |
DESCRIBE [table]; |
Show the structure of a table. |
DROP TABLE [table]; |
Delete an existing table. |
SHOW TABLES; |
List all tables in the current database. |
CREATE USER '[username]'@'localhost' IDENTIFIED BY '[password]'; |
Create a new MySQL user account. |
GRANT ALL PRIVILEGES ON [database].* TO '[username]'@'localhost'; |
Grant all privileges to a MySQL user account on a specific database. |
FLUSH PRIVILEGES; |
Reload all the privileges from the grant tables in the MySQL database. |
INSERT INTO [table] ([columns]) VALUES ([values]); |
Insert a new record into a table. |
SELECT [columns] FROM [table]; |
Retrieve data from one or more tables. |
UPDATE [table] SET [column] = [value] WHERE [condition]; |
Update existing records in a table. |
DELETE FROM [table] WHERE [condition]; |
Delete records from a table. |
mysqldump -u [username] -p [database] > [filename].sql |
Backup a database to a .sql file. |
mysql -u [username] -p [database] < [filename].sql |
Restore a database from a .sql file. |