MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The CREATE DATABASE
statement in MySQL is used to create a new database in MySQL.
Prerequisites:
Tutorial:
To start the mysql
command-line client, open your terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
The basic syntax for using the CREATE DATABASE
statement is as follows:
CREATE DATABASE database_name;
Replace database_name
with the name of the database you want to create.
For example, to create a database named mydb
, you would use:
CREATE DATABASE mydb;
This will create a new database named mydb
.
When creating a database, you can specify the character set and collation by using the CHARACTER SET
and COLLATE
clauses. For example:
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
This will create a new database named mydb
with a character set of utf8mb4
and a collation of utf8mb4_unicode_ci
.
EXIT;
By using the CREATE DATABASE
statement in MySQL, you can easily create new databases. This can be useful when you want to set up a new project, create a testing environment, or create a separate database for different parts of your application.
Creating a New Database with MySQL CREATE DATABASE:
CREATE DATABASE database_name;
CREATE DATABASE mydatabase;
Specifying Character Set and Collation in MySQL CREATE DATABASE:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_general_ci;
Setting Default Options for a Database in MySQL:
CREATE DATABASE mydatabase DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
MySQL CREATE DATABASE and Permissions:
GRANT ALL PRIVILEGES ON mydatabase.* TO 'username'@'localhost' IDENTIFIED BY 'password';
Checking if a Database Exists Before Creating it in MySQL:
CREATE DATABASE IF NOT EXISTS mydatabase;
Creating Multiple Databases with a Single MySQL Statement:
CREATE DATABASE db1, db2, db3;
MySQL CREATE DATABASE and Storage Engine Options:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB;