🔰 MySQL Create Database Tutorial
📘 What is a Database?
A database is a structured collection of data stored electronically. MySQL allows you to create and manage databases to store information such as user data, product details, or customer records.
🛠 Prerequisites
Before creating a database in MySQL, make sure you:
- Have MySQL installed (via XAMPP, WAMP, LAMP, or MySQL Workbench)
- Have access to the MySQL command-line client or phpMyAdmin
✅ Basic Syntax to Create a Database
CREATE DATABASE database_name;
database_name
: Replace this with the name you want to give your database.
📥 Example: Creating a Simple Database
CREATE DATABASE student_records;
This command creates a new database named student_records
.
🔒 Creating a Database with Character Set and Collation (Optional)
CREATE DATABASE ecommerce
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
CHARACTER SET utf8mb4
: Supports emojis and most international characters.COLLATE utf8mb4_unicode_ci
: Specifies sorting and comparison rules.
💡 View Existing Databases
To list all available databases:
SHOW DATABASES;
📂 Select a Database to Use
USE student_records;
This tells MySQL that all subsequent commands will be executed on the student_records
database.
🚫 Drop/Delete a Database (Be Careful!)
DROP DATABASE student_records;
⚠️ Warning: This will permanently delete the database and all its data.
💻 Creating a Database using MySQL Workbench
- Open MySQL Workbench
- Connect to your server
- Right-click on the Schemas area
- Click Create Schema
- Enter the database name and click Apply
🌐 Creating a Database using phpMyAdmin
- Open phpMyAdmin in your browser
- Click on the Databases tab
- Enter a database name
- Click Create
📝 Best Practices
- Use lowercase names with underscores (e.g.,
user_data
) - Avoid using MySQL reserved keywords as database names
- Always back up data before deleting databases
🎯 Summary
Task | Command |
---|---|
Create DB | CREATE DATABASE dbname; |
View DBs | SHOW DATABASES; |
Select DB | USE dbname; |
Delete DB | DROP DATABASE dbname; |