️ Creating and Managing Databases in MySQL
1. Creating a Database
You can create a new database using the CREATE DATABASE statement:
✅ Example:
This creates an empty database named bookstore.
2. Viewing Existing Databases
To list all databases on the server:
3. Selecting a Database to Use
Before creating tables or inserting data, you must select the database:
4. Creating a Table Inside a Database
5. Viewing All Tables in a Database
6. Describing a Table (Structure)
To view columns, data types, and constraints:
7. Renaming a Database (Workaround)
MySQL doesn’t support direct renaming of a database. Instead:
-
Create a new database:
-
Export data from the old one and import into the new one (via phpMyAdmin or
mysqldumpcommand). -
Drop the old database if needed:
8. Dropping (Deleting) a Database
⚠️ This permanently deletes the database and all its tables. Use with caution.
9. Backing Up a Database (Using CLI)
If you’re using the command line:
To restore:
Best Practices
| Practice | Why it Matters |
|---|---|
| Use descriptive names | Improves clarity and organization |
| Always backup before deleting | Prevents accidental data loss |
| Normalize your tables | Avoids redundant data |
| Use appropriate data types | Saves space and ensures accuracy |
| Add constraints (PK, FK) | Enforces data integrity |
