MySql Chapter-1

A complete beginner-to-advanced guide on mastering MySQL, from installation and CRUD operations to joins, views, transactions, triggers, stored procedures, and performance optimization

Back

๐Ÿง  1. What is DBMS (Database Management System)?

A DBMS is software that interacts with end-users, applications, and the database itself to capture and analyze data.

๐Ÿ”น Key Functions:

  • Data storage, retrieval, and update
  • User management and authentication
  • Transaction management (ACID properties)
  • Backup and recovery
  • Data abstraction and security

๐Ÿ”น Examples:

  • Relational DBMS: MySQL, PostgreSQL, Oracle
  • NoSQL DBMS: MongoDB, Redis, Cassandra

๐Ÿ†š 2. SQL vs NoSQL Databases

FeatureSQL (Relational)NoSQL (Non-Relational)
Data FormatTables with rows and columnsDocument, Key-Value, Graph, Column
SchemaFixed schema (structured)Dynamic schema (flexible)
ScalabilityVertical (add resources to one server)Horizontal (add more servers)
ExamplesMySQL, PostgreSQL, OracleMongoDB, Firebase, Cassandra
Query LanguageSQLNo fixed language, uses JSON-style queries
Best Use CaseBanking, ERP, structured dataReal-time apps, IoT, big data

๐Ÿ”น Example:

  • SQL:
SELECT * FROM users WHERE age > 30;
  • NoSQL (MongoDB):
db.users.find({ age: { $gt: 30 } });

๐Ÿฌ 3. Introduction to MySQL

MySQL is a popular open-source relational database based on SQL. It is widely used for:

  • Web applications (e.g., WordPress)
  • Data warehousing
  • E-commerce and analytics

๐Ÿ”น MySQL Key Features:

  • Supports transactions
  • Foreign key constraints
  • High performance and reliability
  • Open-source with enterprise support

๐Ÿ”น Create a Database

CREATE DATABASE mydb;

๐Ÿ”น Use a Database

USE mydb;

๐Ÿ”น Show Databases

SHOW DATABASES;

๐Ÿ”น Drop a Database

DROP DATABASE mydb;

๐Ÿ”น Create Table

CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100),
  email VARCHAR(100) UNIQUE,
  age INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

๐Ÿ”น Alter Table

Add a Column:

ALTER TABLE users ADD gender VARCHAR(10);

Modify Column:

ALTER TABLE users MODIFY age SMALLINT;

Rename Column:

ALTER TABLE users CHANGE name full_name VARCHAR(100);

Drop Column:

ALTER TABLE users DROP COLUMN gender;

Rename Table:

RENAME TABLE users TO customers;

๐Ÿ”น Drop Table

DROP TABLE users;

๐Ÿ”น Describe Table Structure

DESCRIBE users;

๐Ÿ” 6. SELECT (Basic Query)

๐Ÿ”น Select All Rows

SELECT * FROM users;

๐Ÿ”น Select Specific Columns

SELECT name, age FROM users;

๐Ÿ”น Where Condition

SELECT * FROM users WHERE age > 25;

๐Ÿ”น Order By

SELECT * FROM users ORDER BY age DESC;

๐Ÿ”น Limit Results

SELECT * FROM users LIMIT 5;

๐Ÿ”น Aliases

SELECT name AS full_name FROM users;

โœ๏ธ 7. INSERT Query (Basic)

๐Ÿ”น Insert One Record

INSERT INTO users (name, email, age) 
VALUES ('Suraj Kumar', 'suraj@example.com', 24);

๐Ÿ”น Insert Multiple Records

INSERT INTO users (name, email, age) VALUES
('Karan', 'karan@example.com', 28),
('Asha', 'asha@example.com', 32);

๐Ÿ”ง Bonus: UPDATE and DELETE

๐Ÿ”น Update Records

UPDATE users SET age = 25 WHERE name = 'Suraj Kumar';

๐Ÿ”น Delete Records

DELETE FROM users WHERE age < 20;

Written by

Suraj Kumar Jha

At

Tue Jul 15 2025