Sample MySQL commands

On this page
  1. 1. Creating a database
  2. 2. Choosing a database
  3. 3. Create a table
  4. 4. Inserting data
  5. 5. Update data
  6. 6. Deletion of data
  7. 7. SELECT Queries
  8. 8. Creating Indexes
  9. 9. Deleting Tables and Databases
  10. Summary

MySQL is a popular database management system that is widely used to store and manage data in various applications and websites. In this article, we’ll discuss some key example MySQL commands that are essential for creating, managing, and executing database queries.

1. Creating a database

The first step in working with MySQL is to create a database that will store data for our application. We can do this with the command CREATE DATABASE:

CREATE DATABASE database_name;

2. Choosing a database

To start working on a particular database, we need to select it. The USE command allows us to:

USE database_name;

3. Create a table

Tables are the primary way to store data in a database. We can create a table using the . Example:

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first name VARCHAR(50),
    surname VARCHAR(50),
    age INT
);

4. Inserting data

To insert data into a table, we use the INSERT INTO:

INSERT INTO employees (first name, surname, age) VALUES ('John', 'Smith', 30);

5. Update data

If we need to update the existing data in the table, we use the UPDATE:

UPDATE employees SET age = 31 WHERE id = 1;

6. Deletion of data

Command DELETE Used to delete data from a table. For example:

DELETE FROM employees WHERE id = 2;

7. SELECT Queries

To retrieve data from a table, we use the SELECT. Below is an example of a query:

SELECT * FROM employees WHERE age > 25;

This example will return all records from the “employees” table where the age is greater than 25 years old.

8. Creating Indexes

Indexes allow you to speed up database queries. We can create an index using the command CREATE INDEX:

CREATE INDEX idx_age ON employees (age);

9. Deleting Tables and Databases

To delete a table, we use the DROP TABLE:

DROP TABLE employees;

To delete a database, we use the command DROP DATABASE:

DROP DATABASE database_name;

Summary

MySQL offers many useful commands for database management. It is worth knowing the basic ones in order to effectively create, update and retrieve data from the database. As you gain experience, you can learn more advanced operations and query optimization. MySQL is a powerful tool for working with data, which is used in many areas of computer science and business.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.