Learn how to create, select, and delete databases in MySQL using DDL commands.
DDL – Data Definition Language
CREATE DATABASE – creates a new, empty database container.USE – switches the current working database.DROP DATABASE – permanently deletes a database and everything inside it.USE; all unqualified queries run here.DROP DATABASE removes data permanently (unless backed up).These are the basic MySQL commands for creating, selecting, and deleting a database:
-- Basic database commands in MySQL
-- Create a new database
CREATE DATABASE database_name;
-- Delete an existing database
DROP DATABASE database_name;
-- Select a database for subsequent queries
USE database_name;
You can also avoid errors when the database already exists by using: CREATE DATABASE IF NOT EXISTS database_name;
This example shows how to create a database, switch to it, create a table, and finally drop the database.
-- Create and use EmployeeDB database
CREATE DATABASE EmployeeDB;
-- Switch to the new database
USE EmployeeDB;
-- Create a table inside EmployeeDB
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Salary DECIMAL(10,2)
);
-- Drop the database (this deletes all data inside it)
DROP DATABASE EmployeeDB;
-- Shows the name of the currently selected database
SELECT DATABASE();
-- Shows all databases on the current MySQL server
SHOW DATABASES;
CREATE TABLE, INSERT, or SELECT will run inside this database unless you explicitly reference another database.school_db, inventory_db, or shop_db. Each database keeps its own tables and data independent.SHOW DATABASES;After running SHOW DATABASES;, you might see something like:
information_schema mysql performance_schema sys EmployeeDB
This confirms that EmployeeDB was created successfully and is available on the server.
DROP DATABASE.USE statement to select the correct database before performing any queries.CREATE DATABASE IF NOT EXISTS db_name; to avoid errors if the database already exists.order or select) as database names. If you must, wrap them in backticks, e.g., `order`.hr_db, inventory_db, etc., to keep projects organized.SchoolDB, and inside it create a table Students with columns: StudentID, Name, Class, and DOB.SHOW DATABASES; and verify that SchoolDB appears in the list.SELECT DATABASE(); to confirm which database is currently selected.DROP DATABASE SchoolDB; and observe that the database is removed from SHOW DATABASES;.