MySQL – Notes
1. Introduction
MySQL is an open-source relational database management system (RDBMS).
Used to store, manage, and retrieve data in databases.
Works with SQL (Structured Query Language) for database operations.
Developed by Oracle Corporation.
Popular for web applications, e.g., with PHP, Python, Java.
2. Features of MySQL
Open-source & free
High performance
Cross-platform (Windows, Linux, Mac)
Scalable & flexible
Supports transactions
Secure – Supports user privileges and passwords
Supports multiple storage engines (InnoDB, MyISAM)
3. MySQL Databases
Database: Collection of related data organized in tables.
Table: Structure to store data in rows (records) and columns (fields).
Example Table: students
| id | name | age | grade |
|---|---|---|---|
| 1 | Dinesh | 25 | A |
| 2 | Ravi | 23 | B |
4. MySQL Data Types
Numeric Types:
INT,FLOAT,DOUBLE,DECIMALString Types:
CHAR(n),VARCHAR(n),TEXT,BLOBDate & Time Types:
DATE,DATETIME,TIMESTAMP,TIME,YEARBoolean:
BOOLEANorTINYINT(1)
5. MySQL Basic Commands
Create Database
CREATE DATABASE mydb;
Use Database
USE mydb;
Drop Database
DROP DATABASE mydb;
Show Databases
SHOW DATABASES;
6. MySQL Table Commands
Create Table
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade CHAR(1)
);
Drop Table
DROP TABLE students;
Alter Table
ALTER TABLE students ADD COLUMN email VARCHAR(50);
ALTER TABLE students DROP COLUMN email;
ALTER TABLE students MODIFY COLUMN age INT(3);
Show Tables
SHOW TABLES;
7. CRUD Operations
Insert Data
INSERT INTO students (id,name,age,grade) VALUES (1,'Dinesh',25,'A');
Select Data
SELECT * FROM students;
SELECT name, age FROM students WHERE grade='A';
Update Data
UPDATE students SET age=26 WHERE name='Dinesh';
Delete Data
DELETE FROM students WHERE name='Ravi';
8. Filtering & Sorting
WHERE Clause
SELECT * FROM students WHERE age>20;
ORDER BY
SELECT * FROM students ORDER BY age DESC;
LIMIT
SELECT * FROM students LIMIT 5;
LIKE (Pattern Matching)
SELECT * FROM students WHERE name LIKE 'D%';
9. Aggregate Functions
| Function | Description |
|---|---|
| COUNT() | Count rows |
| SUM() | Sum values |
| AVG() | Average value |
| MIN() | Minimum value |
| MAX() | Maximum value |
Example:
SELECT COUNT(*) FROM students;
SELECT AVG(age) FROM students;
10. Grouping
GROUP BY – Group rows with same value
SELECT grade, COUNT(*) FROM students GROUP BY grade;
HAVING – Filter groups
SELECT grade, COUNT(*) FROM students GROUP BY grade HAVING COUNT(*)>1;
11. Joins
Combine data from multiple tables
INNER JOIN
SELECT students.name, marks.score
FROM students
INNER JOIN marks
ON students.id = marks.student_id;
LEFT JOIN
SELECT students.name, marks.score
FROM students
LEFT JOIN marks
ON students.id = marks.student_id;
RIGHT JOIN
SELECT students.name, marks.score
FROM students
RIGHT JOIN marks
ON students.id = marks.student_id;
FULL OUTER JOIN (not directly in MySQL, use UNION)
12. Indexes
Improve search speed on tables
CREATE INDEX idx_name ON students(name);
DROP INDEX idx_name ON students;
13. Keys
Primary Key – Unique identifier for each row
Foreign Key – Links one table to another
CREATE TABLE marks (
id INT PRIMARY KEY,
student_id INT,
score INT,
FOREIGN KEY (student_id) REFERENCES students(id)
);
14. Views
Virtual table based on query
CREATE VIEW top_students AS
SELECT name, grade FROM students WHERE grade='A';
SELECT * FROM top_students;
15. Transactions
Group of queries executed together
START TRANSACTION;
UPDATE students SET age=26 WHERE id=1;
UPDATE students SET grade='B' WHERE id=2;
COMMIT; -- Save changes
ROLLBACK; -- Undo changes
16. MySQL Functions
String Functions:
CONCAT(),UPPER(),LOWER(),SUBSTRING()Numeric Functions:
ROUND(),CEIL(),FLOOR(),ABS()Date Functions:
NOW(),CURDATE(),DATE_ADD(),DATEDIFF()
17. User & Privileges
Create user
CREATE USER 'dinesh'@'localhost' IDENTIFIED BY 'password';
Grant privileges
GRANT ALL PRIVILEGES ON mydb.* TO 'dinesh'@'localhost';
FLUSH PRIVILEGES;
Revoke privileges
REVOKE ALL PRIVILEGES ON mydb.* FROM 'dinesh'@'localhost';
18. Backup & Restore
Backup database
mysqldump -u root -p mydb > backup.sql
Restore database
mysql -u root -p mydb < backup.sql
19. Important Notes
SQL commands not case-sensitive (
SELECT=select)Use
;to terminate statementsAlways use WHERE clause with
UPDATEandDELETEto avoid affecting all rows
