DEV Community

Cover image for πŸŒ™ Late Night Chronicles: #5 Your First MySQL Commands πŸš€
Shreyash Ogale
Shreyash Ogale

Posted on

πŸŒ™ Late Night Chronicles: #5 Your First MySQL Commands πŸš€

Till now, we understood MySQL, SQL, and Data Types.

Now it’s time to move from theory β†’ practical.

Let’s actually write some MySQL commands step by step πŸ‘‡


πŸ”Ή Create Database

A database is simply a container that holds your tables and data.

CREATE DATABASE company;
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This creates a new database named company.

πŸ‘‰ Output:

Query OK, 1 row affected

πŸ”Ή Use Database

Before performing any operations, we need to select the database we want to work with.

USE company;
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This tells MySQL to use the company database for all upcoming operations.

πŸ‘‰ Output:

Database changed

πŸ”Ή Create Table

A table stores data in rows and columns.

CREATE TABLE emp (
  empno CHAR(4),
  ename VARCHAR(25),
  sal FLOAT,
  city VARCHAR(15),
  dob DATE
);

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This creates a table emp with employee details

πŸ‘‰ Output:

Query OK, 0 rows affected

πŸ”Ή Show Tables

This command is used to check all tables in the current database.


SHOW TABLES;

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This displays all tables inside the selected database.

πŸ‘‰ Output:

+----------------+
| Tables_in_company |
+----------------+
| emp            |
+----------------+

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Insert Data

This command is used to add records into a table.

INSERT INTO emp VALUES 
('1', 'Aakash', 5000, 'Mumbai', '1995-10-01');

INSERT INTO emp (empno, sal, ename, city, dob) 
VALUES ('2', 6000, 'Mahesh', 'Mirzapur', '1991-06-08');

INSERT INTO emp (empno, sal) 
VALUES ('3', 7000);

INSERT INTO emp VALUES 
('4', 'Ajay', NULL, NULL, NULL);

INSERT INTO emp VALUES 
('5', NULL, 5000, NULL, NULL);

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ These commands insert employee records into the emp table.

πŸ‘‰ Output:

Query OK, 1 row affected

πŸ”Ή Insert Multiple Rows

We can insert multiple rows in one query.

INSERT INTO emp VALUES 
('1', 'A', 5000, 'Mumbai', '1990-04-05'),
('2', 'B', 5000, 'Delhi', '1991-06-15');

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This inserts multiple records at once.

πŸ‘‰ Output:

Query OK, 1 row affected

πŸ”Ή View Data

Used to fetch all data from the table.

SELECT * FROM emp;

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This shows all rows and columns from the table.
πŸ‘‰ * means all columns.

πŸ‘‰ Output:

+-------+--------+------+-----------+------------+
| empno | ename  | sal  | city      | dob        |
+-------+--------+------+-----------+------------+
| 1     | Aakash | 5000 | Mumbai    | 1995-10-01 |
| 2     | Mahesh | 6000 | Mirzapur  | 1991-06-08 |
| 3     | NULL   | 7000 | NULL      | NULL       |
| 4     | Ajay   | NULL | NULL      | NULL       |
| 5     | NULL   | 5000 | NULL      | NULL       |
| 1     | A      | 5000 | Mumbai    | 1990-04-05 |
| 2     | B      | 5000 | Delhi     | 1991-06-15 |
+-------+--------+------+-----------+------------+
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Select Specific Columns

Used when we only need certain columns.

SELECT empno, ename FROM emp;
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This displays only selected columns.

πŸ‘‰ Output:

+-------+--------+
| empno | ename  |
+-------+--------+
| 1     | Aakash |
| 2     | Mahesh |
| 3     | NULL   |
| 4     | Ajay   |
| 5     | NULL   |
| 1     | A      |
| 2     | B      |
+-------+--------+

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή What we learned so far

Up to this point, we have covered the core basics of working with MySQL:

  • How to create a database to store data
  • How to select (use) a database before working on it
  • How to create a table with proper structure
  • How to view tables inside a database
  • How to insert data into a table (single & multiple rows)
  • How to retrieve all data using SELECT *
  • How to fetch specific columns based on our need

πŸš€ What’s next?

In the next post, we’ll dive into:

πŸ‘‰ WHERE clause, operators, and real query logic

This is where things start getting interesting β€” filtering, conditions, and real-world queries

Top comments (0)