How does MySQL create a database?Create MySQL database command/statement/syntax

MySQLHow to create a database?createMySQL databaseCommand/Statement/Syntax

MySQL create database


Create a database using mysqladmin

With a normal user, you may need specific privileges to create or delete MySQL databases.

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

Examples

The following command simply demonstrates the process of creating a database named chenweiliang:

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

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


Create database using PHP script

PHP uses the mysqli_query function to create or delete MySQL databases.

The function has two parameters and returns TRUE if the execution is successful, otherwise it returns FALSE.

grammar

mysqli_query(connection,query,resultmode);
参数Description
connectionRequired.Specifies the MySQL connection to use.
QueryRequired, specifies the query string.
result modeOptional.a constant.Can be any of the following values:

  • MYSQLI_USE_RESULT (use this if you need to retrieve a lot of data)
  • MYSQLI_STORE_RESULT (default)

Examples

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

create database

<?
php
$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die('连接错误: ' . mysqli_error($conn));
}
echo '连接成功
';
$sql = 'CREATE DATABASE chenweiliang';
$retval = mysqli_query($conn,$sql );
if(! $retval )
{
 die('创建数据库失败: ' . mysqli_error($conn));
}
echo "数据库 chenweiliang 创建成功\n";
mysqli_close($conn);
?>

After successful execution, the following results are returned:

connection succeeded

The database chenweiliang was created successfully

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How does MySQL create a database?Create MySQL Database Commands/Statements/Syntax" to help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-459.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top