MySQL
SQL (Structured Query Language):
Definition: SQL is a standard programming language used to communicate
with and manage data in relational databases. It allows you to perform
operations like creating, reading, updating, and deleting data.
Example:
CREATE TABLE users (
id INT,
name VARCHAR(50)
);
MySQL: (My Structured Query Language):
Definition: MySQL is an open-source relational database management system
(RDBMS) that uses SQL to store, manage, and retrieve data. It provides the
software platform where SQL commands are executed.
Example:
CREATE DATABASE testdb; # Create a database in MySQL
USE testdb; # Select the database
# Create a table and insert data (using SQL commands)
CREATE TABLE users (
id INT,
name VARCHAR(50)
);
INSERT INTO users (id, name) VALUES (1, 'John');
SELECT * FROM users;
Steps for Installation:
dnf update -y && dnf upgrade -y
Installed MySQL Server:
sudo dnf install mysql-server
Enabled and started MySQL:
sudo systemctl start mysqld
sudo systemctl enable --now mysqld
Ran secure installation:
sudo mysql_secure_installation
You set root password
Login as root:
mysql -u root -p
Creating user, Database and givng user permissions:
CREATE USER 'Zoha'@'localhost' IDENTIFIED BY '1234';
SELECT User, Host FROM mysql.user;
Give Permission to User:
GRANT ALL PRIVILEGES ON my_db1.* TO 'Zoha'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE my-db2;
f
SHOW DATABASES;
Create
Table
and
insert values:
USE my_db1;
-- Create table
CREATE TABLE employee (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2)
);
Insert into Table:
INSERT INTO employee (name, department, salary) VALUES
('Alice', 'HR', 50000.00),
('Bob', 'IT', 60000.00),
('Charlie', 'Finance', 55000.00),
('Diana', 'Marketing', 52000.00);
View Values of the Table:
SELECT * FROM employee;
1. Database (Schema) Management
• Create a database
CREATE DATABASE company;
• View databases
SHOW DATABASES;
• Use a database
USE company;
• Delete a database
DROP DATABASE company;
2. Table Management
• Create a table
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2)
);
• View tables
SHOW TABLES;
• Describe table structure
DESCRIBE employees;
• Delete table
DROP TABLE employees;
3. Insert, Read, Update, Delete (CRUD)
• Insert data
INSERT INTO employees (name, department,
salary)
VALUES ('Alice', 'IT', 50000.00);
• Read data
SELECT * FROM employees;
• Update data
UPDATE employees SET salary = 55000 WHERE name
= 'Alice';
• Delete data
DELETE FROM employees WHERE name = 'Alice';
4. User Management & Privileges
• Create user
CREATE USER 'john'@'localhost' IDENTIFIED BY
'password123';
• Grant privileges
GRANT ALL PRIVILEGES ON company.* TO
'john'@'localhost';
• Show users
SELECT user, host FROM mysql.user;
• Drop user
DROP USER 'john'@'localhost';
5. Keys & Constraints
• Primary Key – ensures unique rows
• Foreign Key – relates two tables
• Unique, Not Null, Default constraints
Example:
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) UNIQUE
);
6. Filtering & Conditions
• WHERE clause
SELECT * FROM employees WHERE department =
'IT';
• Operators: =, >, <, LIKE, IN, BETWEEN
7. Joins (for multiple tables)
• INNER JOIN
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d ON e.department =
d.dept_id;
• LEFT JOIN, RIGHT JOIN, FULL JOIN (basic knowledge is enough
at start).
Backup & Restore
• Export (dump)
mysqldump -u root -p company > company.sql
• Import
mysql -u root -p company < company.sql