A lesson of the day: "MySQL Database" create, find, delete 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 数据库名;

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

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

Use mysqladmin to create a database

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

So here we use the root user to log in. The root user has the highest authority and 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 NOWCODER:

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

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

Use PHP script to create database

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

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

grammar

mysqli_query(connection,query,resultmode);

 

Instance

The following example demonstrates the use of PHP to create a 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 '连接成功<br />';
$sql = 'CREATE DATABASE NOWCODER';
$retval = mysqli_query($conn,$sql );
if(! $retval )
{
    die('创建数据库失败: ' . mysqli_error($conn));
}
echo "数据库 NOWCODER 创建成功\n";
mysqli_close($conn);
?>

After successful execution, the following result is returned:

连接成功
数据库 NOWCODER 创建成功

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

连接成功
创建数据库失败:Can't create database 'NOWCODER'; database exists

 

MySQL select database

After you connect to the MySQL database, there may be multiple databases that can be operated, so you need to select the database you want to operate.

Select MySQL database from the command prompt window

You can easily select a specific database in the mysql> prompt window. You can use SQL commands to select the specified database.

Instance

The following example selects the database NOWCODER:

[root@host]# mysql -u root -p
Enter password:******
mysql> use NOWCODER;
Database changed

After executing the above command, you have successfully selected the NOWCODER database, which will be executed in the NOWCODER database in subsequent operations.

Note: All database names, table names, and table fields are case sensitive. So you need to enter the correct name when using SQL commands.

Use PHP script to select MySQL database

PHP provides the function mysqli_select_db to select a database. The function returns TRUE after successful execution, otherwise it returns FALSE.

grammar

mysqli_select_db(connection,dbname);

 

Instance

The following example shows how to use the mysqli_select_db function to select a 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 '连接成功';
mysqli_select_db($conn, 'NOWCODER' );
mysqli_close($conn);
?>

MySQL delete database

Log in to the MySQL server as an ordinary user. You may need specific privileges to create or delete MySQL databases, so we use the root user to log in. The root user has the highest privileges.

In the process of deleting the database, you must be very careful, because after executing the delete command, all data will disappear.

drop command to delete the database

drop command format:

drop database <数据库名>;

For example, delete the database named NOWCODER:

mysql> drop database NOWCODER;

Use mysqladmin to delete the database

You can also use the mysql  mysqladmin  command to execute the delete command in the terminal.

The following example deletes the database NOWCODER:

[root@host]# mysqladmin -u root -p drop NOWCODER
Enter password:******

After executing the above delete database command, a prompt box will appear to confirm whether to delete the database:

Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
 
Do you really want to drop the 'NOWCODER' database [y/N] y
Database "NOWCODER" dropped

Delete database using PHP script

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

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

grammar

mysqli_query(connection,query,resultmode);

 

Instance

The following example demonstrates the use of PHP mysqli_query function to delete the 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 '连接成功<br />';
$sql = 'DROP DATABASE NOWCODER';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('删除数据库失败: ' . mysqli_error($conn));
}
echo "数据库 NOWCODER 删除成功\n";
mysqli_close($conn);
?>
执行成功后,数结果为:
连接成功
数据库 NOWCODER 删除成功

Note:  When using a PHP script to delete a database, there will be no confirmation message to delete, and the specified database will be deleted directly, so you must be careful when deleting the database.

If you see this, it happens that you like programming and want to know more programming knowledge, click below to learn to find me, free professional technical guidance + learning materials spree

 

Guess you like

Origin blog.csdn.net/Python6886/article/details/112689196