![]() |
VOOZH | about |
To create a new database in MySQL, we use CREATE DATABASE command followed by the database name. Database names cannot contain spaces; if needed, an underscore (_) can be used instead.
Syntax:
CREATE DATABASE database_name;It helps organize data by providing a separate container to store related tables and other objects. Let’s create a database named GeeksForGeeks:
Query:
CREATE DATABASE GeeksForGeeks;Output
Note: If you try to create a database with a name that already exists, you’ll see an error. To avoid this, either choose a new name or use IF NOT EXISTS clause to only create database if it doesn't already exist.
Query:
CREATE DATABASE IF NOT EXISTS GeeksForGeeks;To confirm that a new database has been created, use:
Query:
SHOW DATABASES;Output
After selecting the database using the USE command, all operations will be performed within it.
Once your database is created, we can switch to the database to begin adding tables, inserting data and running queries. To switch to your new database, use the USE command.
Syntax:
USE database_name;Query:
USE GeeksForGeeks;If you ever want to delete a database, use:
Query:
DROP DATABASE GeeksForGeeks;This permanently deletes the database.
Note: Once dropped, all data inside the database will be lost permanently.