Home >Database >Mysql Tutorial >How to create a new database in MySQL?
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
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 数据库名;
where the database name refers to the name of the database to be created.
The following is a specific example:
CREATE DATABASE mydb;
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;
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 数据库名;
For example, we can use the following command to switch to the mydb database:
USE mydb;
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!