MySQL create database


MySQL creates a database

There are three ways for mysql to create a database:

1 .Use the create command to create a database

We can use the create command to create a database after logging in to the MySQL service. The syntax is as follows:

CREATE DATABASE 数据库名;

The following command simply demonstrates creating a database. The process, the data name is DEMO:

[root@host]# mysql -u root -p   
Enter password:******  # 登录后进入终端mysql> create DATABASE DEMO;

2. Use mysqladmin to create the database

Use an ordinary user, you may need specific permissions to Create or delete a MySQL database.

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

The following command simply demonstrates the process of creating a database. The data name is DEMO:

[root@host]# mysqladmin -u root -p create DEMO Enter password:******

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

3. Use PHP script to create a database

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

This function has two parameters and returns TRUE when executed successfully, otherwise it returns FALSE.

Syntax

mysqli_query(connection,query,resultmode);
ParametersDescription
connection Required. Specifies the MySQL connection to use.​
queryRequired, specifies the query string.​
resultmode ​

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

  • MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)

  • MYSQLI_STORE_RESULT (default)

Example

The following example demonstrates using PHP to create a database:

<?php
$dbhost = 'localhost'; // Mysql server host address
$dbuser = 'root';                                                                                                                                                                             , $dbpass);
if(! $conn )
{
die('Connection error: ' . mysqli_error($conn));
}
echo 'Connection successful<br />';
$sql = 'CREATE DATABASE DEMO';
$retval = mysqli_query($conn,$sql );
if(! $retval )
{
die( 'Failed to create database: ' . mysqli_error($conn));
}
echo "Database DEMO created successfully\n";
mysqli_close($conn);
?>

After successful execution, the following results will be returned:

Image 1.jpg

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


##Related video tutorial recommendations: 未标题-1.jpg

MySQL Create a data table
  • mysql database knowledge

  • mysql video tutorial