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;
π 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;
π 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
);
π 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;
π This displays all tables inside the selected database.
π Output:
+----------------+
| Tables_in_company |
+----------------+
| emp |
+----------------+
πΉ 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);
π 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');
π 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;
π 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 |
+-------+--------+------+-----------+------------+
πΉ Select Specific Columns
Used when we only need certain columns.
SELECT empno, ename FROM emp;
π This displays only selected columns.
π Output:
+-------+--------+
| empno | ename |
+-------+--------+
| 1 | Aakash |
| 2 | Mahesh |
| 3 | NULL |
| 4 | Ajay |
| 5 | NULL |
| 1 | A |
| 2 | B |
+-------+--------+
πΉ 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)