MySQL
MySQL

Databases

A database is a named container that holds related tables. A single MySQL server can host many databases at once for example, one for a blog, one for a school system, one for an online store completely separate from each other. Grouping tables into a database keeps related data together and unrelated data apart. A students database might hold students, grades, and attendance tables; it has no reason to also contain a store's products table. You can see what databases exist on a server with SHOW DATABASES;, and switch into one to work with it using USE database_name;. Once you've run USE, your following commands (like SELECT or CREATE TABLE) apply inside that database by default. Think of a MySQL server as a filing cabinet, each database as one labeled drawer, and each table inside it as one folder of related records.

Why this matters

  • ·Separate databases keep unrelated projects from ever mixing or colliding.
  • ·One server can safely host many databases for many different apps at once.
  • ·SHOW DATABASES and USE make it quick to see and switch between projects.

Common mistakes

  • ·Running a command without first USE-ing the right database can affect the wrong project.
  • ·Deleting a database (DROP DATABASE) removes every table and every row inside it there is no undo.
  • ·Giving databases vague names makes it easy to lose track of which one holds what.
Your codePHP
Expected output
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql               |
| performance_schema |
| school              |
+--------------------+
4 rows in set

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

Yes. A single MySQL server commonly hosts many separate databases for many separate projects.

Practice questions

Question 1

Write the command that lists every database on the server.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Write the command that switches your session into a database called library.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

What does a database contain?