MySQL Create Database
After logging into the MySQL service, we can use the create command to create a database. The syntax is as follows:
CREATE DATABASE 数据库名;
The following command simply demonstrates the process of creating a database, with the data name RUNOOB:
[root@host]# mysql -u root -p
Enter password:****** # 登录后进入终端
mysql> create DATABASE RUNOOB;
The basic syntax for creating a database is as follows:
CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name];
If you want to specify some options when creating a database, you can use other parameters of the CREATE DATABASE statement. For example, you can specify the character set and collation:
Example
CREATE
DATABASE
mydatabase
CHARACTER
SET
utf8mb4
COLLATE
utf8mb4_general_ci;
If the database already exists, executing CREATE DATABASE will result in an error.
To avoid this, you can add the IF NOT EXISTS clause to the CREATE DATABASE statement:
Example
CREATE
DATABASE
IF
NOT
EXISTS
mydatabase;
Creating a Database with mysqladmin
mysqladmin is a command-line tool provided by MySQL for performing administrative tasks.
With mysqladmin, you can perform various database management operations, including creating databases.
The following is the basic syntax for creating a database with mysqladmin:
mysqladmin -u your_username -p create your_database
-uThe parameter is used to specify the MySQL username.-pThe parameter indicates that a password needs to be entered.createis the operation to perform, indicating the creation of a database.your_databaseis the name of the database to be created.
When using a regular user, you may need specific privileges to create or drop a MySQL database.
Here we log in as the root user. The root user has the highest privileges and can use the mysql mysqladmin command to create a database.
After executing the above command, you will be prompted to enter a password. Just enter the password and press Enter.
The following command simply demonstrates the process of creating a database, with the data name RUNOOB:
[root@host]# mysqladmin -u root -p create RUNOOB
Enter password:******
Once the above command executes successfully, the MySQL database RUNOOB will be created.
If you want to specify the character set and collation when creating a database, you can use the -default-character-set and -default-collation parameters:
mysqladmin -u your_username -p create your_database \
--default-character-set=utf8mb4 \
--default-collation=utf8mb4_general_ci
The above code creates a database using the utf8mb4 character set and the utf8mb4_general_ci collation.
Please note that when executing these commands, make sure the MySQL server is running and that you have sufficient privileges to perform these operations.
If you want to use mysqladmin to connect to the MySQL server to perform other administrative tasks, such as checking server status, restarting the server, etc., you can use a command of the following form:
mysqladmin -u your_username -p your_command
Here, your_command is the specific administrative command you wish to execute.
For example, to check the status of the MySQL server, you can use:
mysqladmin -u your_username -p status
This will ask you to enter a password and display information about the server status.
Creating a Database with a PHP Script
PHP uses the mysqli_query function to create or drop a MySQL database.
This function takes two parameters and returns TRUE on success, or FALSE otherwise.
Syntax
mysqli_query(connection,query,resultmode);
| Parameters | Description |
|---|---|
| connection | Required. Specifies the MySQL connection to use. |
| query | Required, specifies the query string. |
| resultmode |
Optional. A constant. Can be any of the following values: |
Example
The following example demonstrates creating a database with PHP:
Create the database
' ; $sql = ' CREATE DATABASE RUNOOB ' ; $retval = mysqli_query ( $conn , $sql ) ; if ( ! $retval ) { die ( ' Failed to create database: ' . mysqli_error ( $conn ) ) ; } echo " Database RUNOOB created successfully \n " ; mysqli_close ( $conn ) ; ?>After successful execution, the following result is returned:

If the database already exists, the following result is returned after execution:
