This MySQL cheatsheet provides a comprehensive list of essential commands for managing and manipulating databases. From creating and modifying tables to querying and updating data, this reference guide covers all the key commands you need to know to effectively work with MySQL. Whether you're a beginner or an experienced user, this cheatsheet is a valuable resource for quickly finding the command you need.
- Connect to MySQL:
mysql -u [username] -p
- Show all databases:
SHOW DATABASES;
- Create a new database:
CREATE DATABASE [database name];
- Select a database:
USE [database name];
- Show all tables in a database:
SHOW TABLES;
- Create a new table:
CREATE TABLE [table name] ([column name] [data type], [column name] [data type], ...);
- Display the structure of a table:
DESCRIBE [table name];
- Insert data into a table:
INSERT INTO [table name] ([column name], [column name], ...) VALUES ([value], [value], ...);
- Select data from a table:
SELECT [column name], [column name], ... FROM [table name];
- Update data in a table:
UPDATE [table name] SET [column name] = [new value] WHERE [condition];
- Delete data from a table:
DELETE FROM [table name] WHERE [condition];
- Select data with a condition:
SELECT [column name], [column name], ... FROM [table name] WHERE [condition];
- Select data with a join:
SELECT [column name], [column name], ... FROM [table name1] JOIN [table name2] ON [table name1.[column name] = [table name2].[column name];
- Order data by a column:
SELECT [column name], [column name], ... FROM [table name] ORDER BY [column name];
- Limit the number of rows returned:
SELECT [column name], [column name], ... FROM [table name] LIMIT [number];
- Count number of rows:
SELECT COUNT([column name]) FROM [table name];
- Sum of a column:
SELECT SUM([column name]) FROM [table name];
- Average of a column:
SELECT AVG([column name]) FROM [table name];
- Group by a column:
SELECT [column name], [aggregate function]([column name]) FROM [table name] GROUP BY [column name];
- Create a new user:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
- Grant permissions to user:
GRANT [permission] ON [database name].[table name] TO 'username'@'host';
- Revoke permissions:
REVOKE [permission] ON [database name].[table name] FROM 'username'@'host';
- Show all users:
SELECT User, Host FROM mysql.user;
- Change a user's password:
SET PASSWORD FOR 'username'@'host' = PASSWORD('new_password');
- Rename a table:
ALTER TABLE [table name] RENAME TO [new table name];
- Add a column to a table:
ALTER TABLE [table name] ADD [column name] [data type];
- Modify a column in a table:
ALTER TABLE [table name] MODIFY [column name] [data type];
- Drop a column from a table:
ALTER TABLE [table name] DROP COLUMN [column name];
- Create an index:
CREATE INDEX [index name] ON [table name] ([column name]);
- Drop an index:
DROP INDEX [index name] ON [table name];
- Create a primary key:
ALTER TABLE [table name] ADD PRIMARY KEY ([column name]);
- Create a foreign key:
ALTER TABLE [table name] ADD FOREIGN KEY ([column name]) REFERENCES [referenced table name] ([referenced column name]);
- Create a unique constraint:
ALTER TABLE [table name] ADD UNIQUE ([column name]);
- Create a check constraint:
ALTER TABLE [table name] ADD CHECK (condition);
- Create a trigger:
CREATE TRIGGER [trigger name] [trigger action] [event] ON [table name] FOR EACH ROW [trigger code];
- Drop a trigger:
DROP TRIGGER [trigger name];
- Create a stored procedure:
CREATE PROCEDURE [procedure name] ([parameter name] [data type], ...) BEGIN [procedure code]; END;
- Call a stored procedure:
CALL [procedure name]([parameter value], ...);
- Drop a stored procedure:
DROP PROCEDURE [procedure name];
- Create a view:
CREATE VIEW [view name] AS SELECT [column name], ... FROM [table name];
- Drop a view:
DROP VIEW [view name];
- Create a backup:
mysqldump -u [username] -p [database name] > [file name].sql
- Restore a backup:
mysql -u [username] -p [database name] < [file name].sql
- Show current user:
SELECT CURRENT_USER();
- Show server version:
SELECT VERSION();
- Show system variables:
SHOW VARIABLES;
- Show status:
SHOW STATUS;
- Show processlist:
SHOW FULL PROCESSLIST;
- Kill a process:
KILL [process id];
- Exit MySQL:
EXIT;
Note: replace the square bracketed placeholders with actual values.
Top comments (0)