MySQL creates a database

After logging in to the MySQL service, we can use the create command to create a database. The syntax is as follows:

CREATE DATABASE database name;

The following command simply demonstrates the process of creating a database, the data name is RUNOOB:

[[emailprotected]]# mysql -u root -p Enter< span class="pln"> password:****** # After logging in, enter the terminal mysql> create DATABASE RUNOOB; span> 

Use mysqladmin to create a database

As a normal user, you may need specific permissions to create or delete a MySQL database.

So we use the root user to log in here. The root user has the highest authority. You can use the mysql mysqladmin command to create a database.

The following command simply demonstrates the process of creating a database, the data name is RUNOOB:

[< span class="pln">[email protected]]# mysqladmin -u root < span class="pun">-p create RUNOOB Enter password:**** **

After the above command is executed successfully, the MySQL database RUNOOB will be created.


Use a PHP script to create a database

PHP uses the mysqli_query function to create or delete a MySQL database.

This function has two parameters, it returns TRUE when the execution is successful, otherwise it returns FALSE.

Syntax

mysqli_query(connection,query,resultmode); span>
Parameter Description
connection Required. Specifies the MySQL connection to be used.
query Required, specifies the query string.
resultmode

Optional. A constant. Can be any of the following values:

  • MYSQLI_USE_RESULT (if you need to retrieve large amounts of data, please use this)
  • MYSQLI_STORE_RESULT (default)

Example

The following example demonstrates the use of PHP to create a database:

Create a database

$dbhost = localhost :3306; // mysql server host address $dbuser = root; //< span class="hl-comment"> mysql username $dbpass = 123456; // mysql user Name password $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die( connection error: . mysqli_error($conn)); } echo< span class="hl-code"> successfully connected
; $sql = CREATE DATABASE RUNOOB; $retval = mysqli_query($conn,$sql ); if (! $retval ) { die( failed to create the database: . mysqli_error($conn)); } echo The database RUNOOB was successfully created
; mysqli_close($conn); ?>< /span>
< /span>
span>
span>
span>

After the execution is successful, the following result will be returned:

share picture

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

share picture

Create a database

$dbhost = localhost:3306; // mysql server host address $dbuser = root; // mysql username $dbpass = 123456; // mysql username and password $conn = mysqli_connect< span class="hl-brackets">($dbhost, $dbuser, $dbpass< span class="hl-brackets">); if(! $conn ) { die( Connection error: . mysqli_error($conn)); } echo successfully connected
; $sql = CREATE DATABASE RUNOOB; $retval = mysqli_query($conn,$sql )< span class="hl-code">; if(! $retval ) { die( Failed to create database: . mysqli_error($conn)); } echo The database RUNOOB was successfully created
; mysqli_close($conn); ?>
span>
span>
span>
span>

$dbhost = localhost:3306; // mysql server host address $dbuser = root; // mysql username $dbpass< span class="hl-code"> = 123456; // mysql username and password $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die( connection error: . < span class="hl-identifier">mysqli_error($conn)); } echo successfully connected
; $sql = CREATE DATABASE RUNOOB; $retval = mysqli_query($conn,$sql ); if(! $retval ) { die( failed to create database: . mysqli_error($conn)); } echo The database RUNOOB was successfully created
; mysqli_close($conn); ?>
span>
span>
span>
span> span>

$dbhost = localhost:3306< span class="hl-quotes">‘; // mysql server host address $dbuser = root; // mysql username $dbpass = 123456; // mysql username and password $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die( connection error: . mysqli_error($conn)); } echo successfully connected
; $sql = CREATE DATABASE RUNOOB; $retval = mysqli_query($conn,$sql ); if(! $retval ) { die( failed to create database: . mysqli_error($conn)); } echo The database RUNOOB was successfully created
; mysqli_close($conn); ?>
span>
< /span>
< /span>
< /span>
< /span>

Leave a Comment

Your email address will not be published.