</>
Skip to content
MySQL lessons (7/48)

MySQL — Database

Creating Databases

-- Create database
CREATE DATABASE mydb;

-- Create if not exists
CREATE DATABASE IF NOT EXISTS mydb;

-- Create with character set
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Using Databases

-- Select database
USE mydb;

-- Show current database
SELECT DATABASE();

-- Show all databases
SHOW DATABASES;

Modifying Databases

-- Alter database
ALTER DATABASE mydb CHARACTER SET utf8mb4;

-- Drop database
DROP DATABASE mydb;

-- Drop if exists
DROP DATABASE IF EXISTS mydb;

Database Examples

-- Create multiple databases
CREATE DATABASE IF NOT EXISTS shop;
CREATE DATABASE IF NOT EXISTS blog;
CREATE DATABASE IF NOT EXISTS portfolio;

-- Show databases
SHOW DATABASES;

-- Use a database
USE shop;

-- Create tables in the database
CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    price DECIMAL(10,2)
);

Database Best Practices

  1. Use meaningful names
  2. Back up regularly
  3. Use IF EXISTS before dropping
  4. Set proper character sets
  5. Use foreign keys for relationships

Mini Practice

Write MySQL code that:

  1. Creates a database
  2. Shows all databases
  3. Uses a database
  4. Drops a database

Up Next

Next: Learn about Creating Databases.

Related Topics

Frequently Asked Questions about Database

What is Database in MySQL?

Database is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Database?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Database.

Why is Database important in MySQL?

Database is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.