Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of adding Database in mysql

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to add databases in mysql. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

The way to add a database to mysql: use the "CREATE DATABASE" syntax to create a database in the syntax format "CREATE DATABASE database name;". The database name must conform to the operating system's folder naming rules, cannot start with a number, and should be as meaningful as possible.

In MySQL, you can use CREATE DATABASE statements to create a database

The simple syntax format is as follows:

CREATE DATABASE

The detailed syntax format is as follows:

CREATE DATABASE [IF NOT EXISTS] [[DEFAULT] CHARACTER SET] [[DEFAULT] COLLATE]

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

Create the name of the database The datastore of MySQL will represent the MySQL database in a directory manner, so the database name must conform to the operating system's folder naming rules, cannot start with a number, and should be as meaningful as possible. Note that there is no case sensitivity in MySQL.

IF NOT EXISTS: make a decision before creating a database, and the operation can only be performed if the database does not currently exist. This option can be used to avoid errors that are created repeatedly because 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 you create the database, the default character set of the system is used.

[DEFAULT] COLLATE: specifies the default proofreading rules for the character set.

MySQL's character set (CHARACTER) and proofreading rules (COLLATION) are two different concepts. Character sets are used to define how MySQL stores strings, and proofreading rules define how strings are compared. Later, we will explain the character set and proofreading rules of MySQL separately.

Example 1: the simplest statement to create a MySQL database

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

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

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

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

Mysql > CREATE DATABASE test_db;ERROR 1007 (HY000): Can't create database 'test_db'; database exists

Prompt cannot create "test_db" database, the database already exists. MySQL does not allow you to create two databases with the same name on the same system.

Similar errors can be avoided by adding an IF NOT EXISTS clause, as shown below:

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

Example 2: specify character set and proofreading rules when creating MySQL database

Use the MySQL command line tool to create a test database named test_db_char, and specify that its default character set is utf8, and the default proofreading rule is utf8_chinese_ci (simplified Chinese, case-insensitive). The entered SQL statement and the execution result are as follows:

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 point, you can use SHOW CREATE DATABASE to view the definition declaration of the test_db_char database and find that the specified character set of the database is utf8, and the running result is 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)" indicates that there is 1 line of information in the collection and the processing time is 0. 00 seconds. A time of 0.00 seconds does not mean that no time is spent, but the time is very short, less than 0.01 seconds.

Thank you for reading! This is the end of the method of adding database in mysql. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report