Home >Database >Mysql Tutorial >How to join a database in mysql?

How to join a database in mysql?

青灯夜游
青灯夜游Original
2020-10-21 14:38:3911378browse

How to add a database in mysql: Use the "CREATE DATABASE" syntax to create a database, the syntax format is "CREATE DATABASE database name;". The database name must comply with the folder naming rules of the operating system, cannot start with a number, and should be as meaningful as possible.

How to join a database in mysql?

In MySQL, you can use the CREATE DATABASE statement to create a database.

The simple syntax format is as follows:

CREATE DATABASE <数据库名>;

Detailed syntax The format is as follows:

CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>] 
[[DEFAULT] COLLATE <校对规则名>];

[ ] The content in is optional. The syntax is as follows:

  • 75a531a7de735e17c67a53ba0f5274e3: The name of the created database. The data storage area of ​​MySQL will represent the MySQL database in a directory format, so the database name must comply with the folder naming rules of the operating system, cannot start with a number, and should be as meaningful as possible. Note that MySQL is not case sensitive.

  • IF NOT EXISTS: Make a judgment before creating the database. The operation can only be performed if the database does not currently exist. This option can be used to avoid duplicate creation errors when the database already exists.

  • [DEFAULT] CHARACTER SET: Specifies the character set of the database. The purpose of specifying the character set is to avoid garbled data stored in the database. If you do not specify a character set when creating the database, the system's default character set is used.

  • [DEFAULT] COLLATE: Specifies the default collation rule for the character set.

MySQL's character set (CHARACTER) and collation rules (COLLATION) are two different concepts. Character sets are used to define how MySQL stores strings, and collation rules define how to compare strings. We will explain MySQL's character set and collation rules separately later.

Example 1: The simplest statement to create a MySQL database

Create a database named test_db in MySQL. Enter the SQL statement CREATE DATABASE test_db; in the MySQL command line client to create a database. The entered SQL statement and execution results are as follows.

mysql> CREATE DATABASE test_db;
Query OK, 1 row affected (0.12 sec);

In the "Query OK, 1 row affected (0.12 sec);" prompt, "Query OK" means that the above command was executed successfully, and "1 row affected" means that the operation only affected one row of records in the database. "0.12 sec" records the time of operation execution.

If you enter the CREATE DATABASE test_db; statement again, the system will give an error message, as shown below:

mysql> CREATE DATABASE test_db;
ERROR 1007 (HY000): Can&#39;t create database &#39;test_db&#39;; database exists

The prompt cannot create the "test_db" database because the database already exists. MySQL does not allow the creation of two databases with the same name on the same system.

You can add the IF NOT EXISTS clause to avoid similar errors, as shown below:

mysql> CREATE DATABASE IF NOT EXISTS test_db;
Query OK, 1 row affected (0.12 sec)

Example 2: Specify the character set and collation rules when creating a MySQL database

Use the MySQL command line tool to create a test database, name it test_db_char, specify its default character set as utf8, and the default collation rule as utf8_chinese_ci (Simplified Chinese, case-insensitive). Enter the SQL statement and execution results. As shown below:

mysql> CREATE DATABASE IF NOT EXISTS test_db_char
    -> DEFAULT CHARACTER SET utf8
    -> DEFAULT COLLATE utf8_chinese_ci;
Query OK, 1 row affected (0.03 sec)

At this time, you can use SHOW CREATE DATABASE to view the definition statement of the test_db_char database and find that the specified character set of the database is utf8. The running results are as follows:

mysql> SHOW CREATE DATABASE test_db_char;
+--------------+-----------------------------------------------------+
| Database     | Create Database                                     |
+--------------+-----------------------------------------------------+
| test_db_char | CREATE DATABASE `test_db_char` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+--------------+-----------------------------------------------------+
1 row in set (0.00 sec)

" 1 row in set (0.00 sec)" means there is 1 row of information in the set, and the processing time is 0.00 seconds. A time of 0.00 seconds does not mean that no time was spent, but that the time is very short, less than 0.01 seconds.

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

Statement:
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