CREATE DATABASE - MySQL


Overview

CREATE DATABASE creates a new database within the MySQL server. A database is a logical container for organizing data, composed of tables, views, and other objects.

Syntax

CREATE DATABASE [IF NOT EXISTS] database_name

Options/Flags

  • IF NOT EXISTS: If specified, the statement will only execute if the database with the specified database_name does not already exist.

Examples

Simple Usage:

CREATE DATABASE my_database;

Creates a new database named my_database.

Conditional Creation:

CREATE DATABASE IF NOT EXISTS my_database;

Only creates the database if it doesn’t exist.

Common Issues

  • Error 1007 (Can’t create database): Ensure you have sufficient privileges to create databases and that the specified database_name is valid.
  • Error 42S02 (Duplicate database name): The database with the specified database_name already exists. Use the IF NOT EXISTS option to avoid this error.

Integration

Creating a Database and Table:

CREATE DATABASE my_database;
USE my_database;
CREATE TABLE my_table ( ... );

Exporting Data into a Database:

mysqldump source_database | mysql my_database
  • DROP DATABASE: Removes a database.
  • USE: Makes a database the current working database.