Lesson 9 of 60 – MySQL Databases
15%

MySQL Databases

A MySQL database is a structured collection of related data. MySQL allows us to create databases, store tables inside them, and manage information using SQL commands.

Note: A database is a container for related tables and other database objects. Before working with tables, you normally create or select a database.

1. What is a Database?

A database is an organized collection of information that can be stored, accessed, and managed efficiently.

For example, a school database may contain information about students, courses, teachers, and fees.

School Database
      ↓
Students
Courses
Teachers
Fees

2. What is a MySQL Database?

A MySQL database is a database managed by the MySQL Database Management System.

It can contain tables, views, stored procedures, functions, and other database objects.

3. Database and Table

A database can contain multiple tables.

school
   |
   ├── students
   ├── teachers
   ├── courses
   └── fees

Each table stores a specific type of related information.

4. Creating a Database

The CREATE DATABASE statement is used to create a new database.

CREATE DATABASE school;

This creates a database named school.

5. CREATE DATABASE IF NOT EXISTS

You can use IF NOT EXISTS to avoid an error if the database already exists.

CREATE DATABASE IF NOT EXISTS school;

If the database already exists, MySQL does not create another copy with the same name.

6. Showing Databases

The SHOW DATABASES command displays databases that the current MySQL user can access.

SHOW DATABASES;

7. Selecting a Database

The USE command selects a database for the current session.

USE school;

After this command, SQL operations can be performed on objects inside the school database.

8. Checking the Current Database

The DATABASE() function can be used to check the currently selected database.

SELECT DATABASE();

If no database has been selected, the result can be NULL.

9. Database Names

Choose clear and meaningful names for databases.

Examples:

school
library
hospital
shop
company

Meaningful names make database management easier.

10. Creating Tables Inside a Database

After selecting a database, you can create tables inside it.

USE school;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);

11. Viewing Tables

Use SHOW TABLES to display tables in the currently selected database.

SHOW TABLES;

The command does not display tables from every database. It works with the selected database.

12. Database Structure

A simple MySQL database structure can look like this:

Database
   |
   ├── Table 1
   │     ├── Column
   │     ├── Column
   │     └── Column
   |
   └── Table 2
         ├── Column
         ├── Column
         └── Column

13. Multiple Databases

A MySQL server can contain multiple databases.

MySQL Server
   |
   ├── school
   ├── library
   ├── hospital
   └── company

A user can work with a database according to their permissions.

14. Switching Between Databases

You can switch from one database to another using the USE command.

USE school;

USE library;

The second command makes library the current database.

15. Database with Tables Example

CREATE DATABASE college;

USE college;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    course VARCHAR(100)
);

CREATE TABLE courses (
    id INT PRIMARY KEY,
    course_name VARCHAR(100)
);

Here, the college database contains two tables.

16. Adding Data to Database Tables

Data is normally stored inside tables rather than directly inside the database.

INSERT INTO students
(id, name, course)
VALUES
(1, 'Rahul', 'Python');

17. Reading Data from a Database

Use the SELECT statement to retrieve information from tables.

SELECT * FROM students;

The query retrieves all columns and rows from the students table.

18. Fully Qualified Table Name

You can specify the database name before the table name.

SELECT *
FROM school.students;

This is useful when working with multiple databases.

19. Database and Application

Web applications often use databases to store application data.

For example:

PHP Application
      ↓
MySQL Database
      ↓
Students Table
      ↓
Student Records

20. Database for a Library System

A library application could use a database named library.

library
   |
   ├── books
   ├── students
   ├── issues
   └── returns

Each table can store information about one part of the library system.

21. Database for a School System

A school management system could contain:

school
   |
   ├── students
   ├── teachers
   ├── courses
   ├── attendance
   └── fees

22. Database Permissions

MySQL users may have different permissions on databases.

For example, one user may be allowed to read data while another user may be allowed to create, update, or delete database objects.

Permissions help control access to database resources.

23. Database Names and Case

Database name behavior can depend on the operating system and MySQL configuration.

For portable applications, it is a good practice to use simple and consistent database naming conventions.

school_db
library_db
company_db

24. Checking Database Selection

Before creating tables or running queries, check which database is currently selected.

SELECT DATABASE();

This helps prevent accidentally working in the wrong database.

25. Database Commands Summary

Command Purpose
CREATE DATABASE Creates a database
SHOW DATABASES Lists accessible databases
USE Selects a database
SELECT DATABASE() Shows the current database

26. Avoiding Unnecessary Databases

Create databases according to the actual requirements of your application.

For example, a single school management application normally does not need a separate database for every table.

school
   ├── students
   ├── teachers
   ├── fees
   └── attendance

27. Database Workflow

1. Connect to MySQL
2. Create database
3. Select database
4. Create tables
5. Insert data
6. Read data
7. Update data
8. Delete data

This is a common basic workflow when developing a MySQL application.

28. Common Database Errors

Some common database-related errors include:

  • Database does not exist
  • Database name is misspelled
  • User does not have permission
  • No database has been selected
  • Trying to create an existing database without suitable handling

29. Complete Database Example

CREATE DATABASE school;

SHOW DATABASES;

USE school;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    course VARCHAR(100)
);

INSERT INTO students
(id, name, course)
VALUES
(1, 'Rahul', 'Python'),
(2, 'Amit', 'Java');

SELECT * FROM students;

30. Practical MySQL Database Example

Suppose you are creating a student management system. You can create a database called student_management.

CREATE DATABASE student_management;

USE student_management;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    mobile VARCHAR(15),
    course VARCHAR(100)
);

INSERT INTO students
(id, name, mobile, course)
VALUES
(1, 'Rahul Kumar', '9876543210', 'Python'),
(2, 'Amit Kumar', '9876501234', 'Java');

SELECT * FROM students;

This example demonstrates how a database can act as the main container for application tables and data.

📌 Key Points

  • A MySQL database is a container for related database objects.
  • A database can contain multiple tables.
  • CREATE DATABASE creates a new database.
  • SHOW DATABASES displays accessible databases.
  • USE selects a database.
  • SELECT DATABASE() shows the currently selected database.
  • SHOW TABLES displays tables in the selected database.
  • Data is normally stored inside tables within a database.
  • A MySQL server can contain multiple databases.
  • Database permissions control what users can do with databases and their objects.

🧠 Quick Quiz

Question: Which command is used to select a database for use?