DEV Community

Abdul Haseeb
Abdul Haseeb

Posted on

MySQL cheat sheet to help you with the most common tasks

Basic Commands

  • Connect to MySQL:
  mysql -u username -p
Enter fullscreen mode Exit fullscreen mode
  • Show all databases:
  SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode
  • Use a database:
  USE database_name;
Enter fullscreen mode Exit fullscreen mode
  • Show all tables in a database:
  SHOW TABLES;
Enter fullscreen mode Exit fullscreen mode
  • Show table structure:
  DESCRIBE table_name;
Enter fullscreen mode Exit fullscreen mode

Data Types

  • String Types:

    • CHAR(size)
    • VARCHAR(size)
    • TEXT
  • Numeric Types:

    • INT(size)
    • FLOAT(size, d)
    • DOUBLE(size, d)
  • Date and Time Types:

    • DATE
    • DATETIME
    • TIMESTAMP
    • TIME

Table Management

  • Create a table:
  CREATE TABLE table_name (
      column1 datatype,
      column2 datatype,
      column3 datatype,
      PRIMARY KEY (column1)
  );
Enter fullscreen mode Exit fullscreen mode
  • Drop a table:
  DROP TABLE table_name;
Enter fullscreen mode Exit fullscreen mode
  • Alter a table:
  ALTER TABLE table_name
  ADD column_name datatype;
Enter fullscreen mode Exit fullscreen mode

Data Manipulation

  • Insert data:
  INSERT INTO table_name (column1, column2)
  VALUES (value1, value2);
Enter fullscreen mode Exit fullscreen mode
  • Update data:
  UPDATE table_name
  SET column1 = value1, column2 = value2
  WHERE condition;
Enter fullscreen mode Exit fullscreen mode
  • Delete data:
  DELETE FROM table_name
  WHERE condition;
Enter fullscreen mode Exit fullscreen mode

Queries

  • Select data:
  SELECT column1, column2
  FROM table_name
  WHERE condition;
Enter fullscreen mode Exit fullscreen mode
  • Select all data:
  SELECT * FROM table_name;
Enter fullscreen mode Exit fullscreen mode
  • Order by:
  SELECT column1, column2
  FROM table_name
  ORDER BY column1 ASC | DESC;
Enter fullscreen mode Exit fullscreen mode
  • Group by:
  SELECT column1, COUNT(*)
  FROM table_name
  GROUP BY column1;
Enter fullscreen mode Exit fullscreen mode

Joins

  • Inner join:
  SELECT a.column1, b.column2
  FROM table1 a
  INNER JOIN table2 b ON a.common_column = b.common_column;
Enter fullscreen mode Exit fullscreen mode
  • Left join:
  SELECT a.column1, b.column2
  FROM table1 a
  LEFT JOIN table2 b ON a.common_column = b.common_column;
Enter fullscreen mode Exit fullscreen mode
  • Right join:
  SELECT a.column1, b.column2
  FROM table1 a
  RIGHT JOIN table2 b ON a.common_column = b.common_column;
Enter fullscreen mode Exit fullscreen mode

Indexes

  • Create an index:
  CREATE INDEX index_name
  ON table_name (column1, column2);
Enter fullscreen mode Exit fullscreen mode
  • Drop an index:
  DROP INDEX index_name ON table_name;
Enter fullscreen mode Exit fullscreen mode

Backup and Restore

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

User Management

  • Create a user:
  CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Enter fullscreen mode Exit fullscreen mode
  • Grant privileges:
  GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
Enter fullscreen mode Exit fullscreen mode
  • Flush privileges:
  FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode
  • Drop a user:
  DROP USER 'username'@'host';
Enter fullscreen mode Exit fullscreen mode

Feel free to ask if you need more details on any specific topic!

Top comments (0)