Cover Image for MySQL Commands Cheat-Sheet
95 views

MySQL Commands Cheat-Sheet

The MySQL commands cheat sheet that includes some of the most commonly used commands and SQL statements in MySQL:

1. Logging into MySQL:

   mysql -u username -p

Replace username with your MySQL username. You will be prompted to enter your password.

2. Creating a Database:

   CREATE DATABASE database_name;

Replace database_name with the name you want for your new database.

3. Using a Database:

   USE database_name;

Switch to the specified database.

4. Creating a Table:

   CREATE TABLE table_name (
       column1 datatype,
       column2 datatype,
       ...
   );

Replace table_name with the name of your table, and define columns and their data types.

5. Inserting Data:

   INSERT INTO table_name (column1, column2, ...)
   VALUES (value1, value2, ...);

6. Retrieving Data:

   SELECT * FROM table_name;

Replace table_name with the name of your table.

7. Updating Data:

   UPDATE table_name
   SET column1 = value1, column2 = value2, ...
   WHERE condition;

8. Deleting Data:

   DELETE FROM table_name WHERE condition;

9. Adding a Column:

   ALTER TABLE table_name
   ADD column_name datatype;

10. Modifying a Column:

   ALTER TABLE table_name
   MODIFY column_name new_datatype;

11. Renaming a Column:

   ALTER TABLE table_name
   CHANGE old_column_name new_column_name datatype;

12. Deleting a Column:

   ALTER TABLE table_name
   DROP column_name;

13. Adding a Primary Key:

   ALTER TABLE table_name
   ADD PRIMARY KEY (column_name);

14. Adding a Foreign Key:

   ALTER TABLE table_name
   ADD FOREIGN KEY (column_name) REFERENCES reference_table(reference_column);

15. Creating an Index:

   CREATE INDEX index_name ON table_name (column_name);

16. Listing Databases:

   SHOW DATABASES;

17. Listing Tables:

   SHOW TABLES;

18. Describing a Table:

   DESCRIBE table_name;

Lists the structure of the table.

19. Exporting Data:

   mysqldump -u username -p database_name > filename.sql

Exports the specified database to an SQL file.

20. Importing Data:

   mysql -u username -p database_name < filename.sql

Imports data from an SQL file into the specified database.

This cheat sheet provides you with the basic MySQL commands for creating databases, tables, and manipulating data. There are many more advanced commands and SQL statements available for more complex database operations, but these should cover most common use cases.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS