Home > Database > Mysql Tutorial > body text

How to create a new database in MySQL?

WBOY
Release: 2023-07-30 23:05:34
Original
6945 people have browsed it

How to create a new database in MySQL?

MySQL is an open source relational database management system that is widely used in various web applications and software systems. Before using MySQL, we need to create a database to store and manage data. This article will detail how to create a new database in MySQL and provide corresponding code examples.

Step 1: Connect to the MySQL server
First, we need to connect to the MySQL server. You can use command line tools (such as MySQL Shell, MySQL Command Line) or visual tools (such as Navicat, phpMyAdmin) to connect. The following is an example of using the command line tool to connect:

mysql -h 主机地址 -u 用户名 -p
Copy after login

Among them, the host address refers to the address of the MySQL server, the username refers to the username used to connect to MySQL, and the -p parameter indicates that a password is required. After executing the above command, you will be prompted to enter a password. Enter the password and press Enter to connect to the MySQL server.

Step 2: Create a new database
After the connection is successful, we can create a new database. In MySQL, use the CREATE DATABASE statement to create a database. The syntax is as follows:

CREATE DATABASE 数据库名;
Copy after login

where the database name refers to the name of the database to be created.

The following is a specific example:

CREATE DATABASE mydb;
Copy after login

The above code will create a database named mydb.

Step 3: Confirm whether the database is created successfully
After creating the database, we can use some commands to confirm whether the database is created successfully. First, you can use the SHOW DATABASES statement to display all databases on the current MySQL server, as shown below:

SHOW DATABASES;
Copy after login

After executing the above command, a list of all created databases will be displayed, which should include the one we just created mydb database.

In addition, we can also switch to the database we just created through the USE statement. The syntax of the USE statement is as follows:

USE 数据库名;
Copy after login

For example, we can use the following command to switch to the mydb database:

USE mydb;
Copy after login

Step 4: Close the connection to the MySQL server
After creating the database, We can choose to close the connection to the MySQL server. You can use the QUIT command or press Ctrl C to close the connection.

In summary, the above are the detailed steps and corresponding code examples for creating a new database in MySQL. Create a new database by connecting to the MySQL server and executing the CREATE DATABASE statement, and confirm whether the database is created successfully through the SHOW DATABASES and USE statements. After creating the database, you can choose to close the connection to the MySQL server.

I hope this article will help you understand how to create a new database in MySQL! If you have any questions, please feel free to leave a message to discuss.

The above is the detailed content of How to create a new database in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template