DEV Community

Harsh Mishra
Harsh Mishra

Posted on

2

MySQL Commands Categorized

The Ultimate List of MySQL Commands Categorized by Usage

Mastering MySQL commands is essential for database administrators, developers, and anyone working with data-driven applications. MySQL, one of the most popular relational database management systems (RDBMS), provides a wide range of commands to manage databases efficiently.

This comprehensive guide categorizes MySQL commands based on their usage, ensuring quick reference and easy learning. Whether you're a beginner or an experienced user, this list will be an invaluable resource.


1. Database Management Commands

Creating and Managing Databases

  • Create a new database:
  CREATE DATABASE database_name;
Enter fullscreen mode Exit fullscreen mode
  • List all databases:
  SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode
  • Select a database to use:
  USE database_name;
Enter fullscreen mode Exit fullscreen mode
  • Drop (delete) a database:
  DROP DATABASE database_name;
Enter fullscreen mode Exit fullscreen mode
  • Rename a database (not natively supported, use backup & restore method):
  RENAME TABLE old_database.table_name TO new_database.table_name;
Enter fullscreen mode Exit fullscreen mode
  • Check the current database:
  SELECT DATABASE();
Enter fullscreen mode Exit fullscreen mode

2. Table Management Commands

Creating, Modifying, and Deleting Tables

  • Create a new table:
  CREATE TABLE table_name (
      column1 datatype constraints,
      column2 datatype constraints,
      ...
  );
Enter fullscreen mode Exit fullscreen mode
  • Show all tables in the current database:
  SHOW TABLES;
Enter fullscreen mode Exit fullscreen mode
  • Describe table structure:
  DESC table_name;
Enter fullscreen mode Exit fullscreen mode
  • Alter table structure (add, modify, drop columns):

    • Add a column:
    ALTER TABLE table_name ADD column_name datatype constraints;
    
    • Modify a column:
    ALTER TABLE table_name MODIFY column_name new_datatype;
    
    • Rename a column:
    ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
    
    • Drop a column:
    ALTER TABLE table_name DROP COLUMN column_name;
    
  • Rename a table:

  RENAME TABLE old_table_name TO new_table_name;
Enter fullscreen mode Exit fullscreen mode
  • Drop (delete) a table:
  DROP TABLE table_name;
Enter fullscreen mode Exit fullscreen mode

3. Data Manipulation Commands (CRUD Operations)

Inserting Data

  • Insert a single row:
  INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2');
Enter fullscreen mode Exit fullscreen mode
  • Insert multiple rows:
  INSERT INTO table_name (column1, column2) VALUES 
  ('value1', 'value2'),
  ('value3', 'value4');
Enter fullscreen mode Exit fullscreen mode
  • Insert data while ignoring duplicates:
  INSERT IGNORE INTO table_name (column1, column2) VALUES ('value1', 'value2');
Enter fullscreen mode Exit fullscreen mode
  • Insert data with auto-increment reset:
  INSERT INTO table_name VALUES (NULL, 'value2');
Enter fullscreen mode Exit fullscreen mode

Reading (Retrieving) Data

  • Retrieve all records from a table:
  SELECT * FROM table_name;
Enter fullscreen mode Exit fullscreen mode
  • Retrieve specific columns:
  SELECT column1, column2 FROM table_name;
Enter fullscreen mode Exit fullscreen mode
  • Retrieve unique records:
  SELECT DISTINCT column_name FROM table_name;
Enter fullscreen mode Exit fullscreen mode
  • Filter results with WHERE clause:
  SELECT * FROM table_name WHERE column1 = 'value';
Enter fullscreen mode Exit fullscreen mode
  • Sort results using ORDER BY:
  SELECT * FROM table_name ORDER BY column_name ASC;  -- Ascending
  SELECT * FROM table_name ORDER BY column_name DESC; -- Descending
Enter fullscreen mode Exit fullscreen mode
  • Limit the number of results:
  SELECT * FROM table_name LIMIT 10;
Enter fullscreen mode Exit fullscreen mode
  • Search for patterns using LIKE:
  SELECT * FROM table_name WHERE column_name LIKE '%value%';
Enter fullscreen mode Exit fullscreen mode
  • Search for values within a range:
  SELECT * FROM table_name WHERE column_name BETWEEN 10 AND 50;
Enter fullscreen mode Exit fullscreen mode

Updating Data

  • Update specific records:
  UPDATE table_name SET column1 = 'new_value' WHERE column2 = 'condition';
Enter fullscreen mode Exit fullscreen mode
  • Update multiple columns:
  UPDATE table_name SET column1 = 'new_value', column2 = 'new_value2' WHERE column3 = 'condition';
Enter fullscreen mode Exit fullscreen mode

Deleting Data

  • Delete specific records:
  DELETE FROM table_name WHERE column_name = 'value';
Enter fullscreen mode Exit fullscreen mode
  • Delete all records from a table (without deleting the structure):
  DELETE FROM table_name;
Enter fullscreen mode Exit fullscreen mode
  • Truncate a table (faster deletion, resets auto-increment):
  TRUNCATE TABLE table_name;
Enter fullscreen mode Exit fullscreen mode

4. Indexing and Optimization

Creating and Managing Indexes

  • Create an index:
  CREATE INDEX index_name ON table_name (column_name);
Enter fullscreen mode Exit fullscreen mode
  • Create a unique index:
  CREATE UNIQUE INDEX index_name ON table_name (column_name);
Enter fullscreen mode Exit fullscreen mode
  • Show indexes in a table:
  SHOW INDEX FROM table_name;
Enter fullscreen mode Exit fullscreen mode
  • Drop an index:
  DROP INDEX index_name ON table_name;
Enter fullscreen mode Exit fullscreen mode

5. Joins and Relationships

Types of Joins

  • Inner Join:
  SELECT t1.*, t2.* FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id;
Enter fullscreen mode Exit fullscreen mode
  • Left Join:
  SELECT t1.*, t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id;
Enter fullscreen mode Exit fullscreen mode
  • Right Join:
  SELECT t1.*, t2.* FROM table1 t1 RIGHT JOIN table2 t2 ON t1.id = t2.id;
Enter fullscreen mode Exit fullscreen mode
  • Full Outer Join (not natively supported in MySQL, use UNION):
  SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
  UNION
  SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
Enter fullscreen mode Exit fullscreen mode

6. User and Privilege Management

  • Create a new user:
  CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Enter fullscreen mode Exit fullscreen mode
  • Grant privileges to a user:
  GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
Enter fullscreen mode Exit fullscreen mode
  • Show user privileges:
  SHOW GRANTS FOR 'username'@'host';
Enter fullscreen mode Exit fullscreen mode
  • Revoke user privileges:
  REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'host';
Enter fullscreen mode Exit fullscreen mode
  • Delete a user:
  DROP USER 'username'@'host';
Enter fullscreen mode Exit fullscreen mode

7. Backup and Restore

  • Backup a database:
  mysqldump -u root -p database_name > backup.sql
Enter fullscreen mode Exit fullscreen mode
  • Restore a database:
  mysql -u root -p database_name < backup.sql
Enter fullscreen mode Exit fullscreen mode
  • Backup all databases:
  mysqldump -u root -p --all-databases > all_databases_backup.sql
Enter fullscreen mode Exit fullscreen mode

8. Performance Monitoring

  • Show running processes:
  SHOW PROCESSLIST;
Enter fullscreen mode Exit fullscreen mode
  • Show open connections:
  SHOW STATUS WHERE variable_name = 'Threads_connected';
Enter fullscreen mode Exit fullscreen mode
  • Optimize a table:
  OPTIMIZE TABLE table_name;
Enter fullscreen mode Exit fullscreen mode
  • Check table status:
  CHECK TABLE table_name;
Enter fullscreen mode Exit fullscreen mode

This comprehensive MySQL guide covers everything from database management to advanced optimization. Whether you're setting up a new database, performing queries, or managing users, these commands will help you efficiently work with MySQL. 🚀

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay