Introduction to Databases and MySQL 📊🛢️

Introduction to Databases and MySQL 📊🛢️
1️⃣ What is a Database?
A database is a structured collection of data that allows for efficient storage, retrieval, and management. It is used in applications ranging from websites and mobile apps to enterprise software.
Types of Databases:
Relational Databases (RDBMS) – Uses tables, rows, and columns (e.g., MySQL, PostgreSQL, Oracle, SQL Server).
NoSQL Databases – Handles unstructured or semi-structured data (e.g., MongoDB, Firebase, Redis).
Cloud Databases – Hosted and managed on cloud platforms (e.g., Amazon RDS, Google Cloud SQL).
2️⃣ What is MySQL?
MySQL is a popular open-source relational database management system (RDBMS) that stores data in tables and uses Structured Query Language (SQL) for data operations.
✔ Fast and scalable 🚀
✔ Free and widely used 🎯
✔ Works with many programming languages (PHP, Java, Python)
3️⃣ Setting Up MySQL
✅ 3.1 Installing MySQL
You can install MySQL in different ways:
Standalone Installation – Download from MySQL website.
Using XAMPP (for PHP Development) – Comes with MySQL and phpMyAdmin.
Docker – docker pull mysql
✅ 3.2 Accessing MySQL
After installation, you can access MySQL via:
Command Line:
mysql -u root -p
phpMyAdmin (GUI for MySQL)
CREATE DATABASE school;
USE school;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT,
email VARCHAR(100) UNIQUE
);
INSERT INTO students (name, age, email)
VALUES ('Alice', 22, 'alice@example.com');
SELECT * FROM students;
SELECT name, age FROM students WHERE age > 20;
UPDATE students SET age = 23 WHERE name = 'Alice';
DELETE FROM students WHERE name = 'Alice';
CREATE TABLE courses (
id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL
);
CREATE TABLE enrollments (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (course_id) REFERENCES courses(id)
);
SELECT students.name, courses.course_name
FROM students
JOIN enrollments ON students.id = enrollments.student_id
JOIN courses ON enrollments.course_id = courses.id;
SELECT students.name, courses.course_name
FROM students
LEFT JOIN enrollments ON students.id = enrollments.student_id
LEFT JOIN courses ON enrollments.course_id = courses.id;
CREATE INDEX idx_student_name ON students(name);
START TRANSACTION;
UPDATE students SET age = 24 WHERE id = 1;
COMMIT;
<?php
$conn = new mysqli("localhost", "root", "", "school");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"] . "<br>";
}
$conn->close();
?>
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
con.close();
$stmt = $conn->prepare("SELECT * FROM students WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();