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

An example of mysql creating a database

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Editor to share with you an example of creating a database by mysql. I hope you will gain a lot after reading this article. Let's discuss it together.

In mysql, you can use the "CREATE DATABASE" statement to create a database with the syntax format "CREATE DATABASE [IF NOT EXISTS] database name [CHARACTER SET character set name] [COLLATE proofreading rule name];".

In MySQL, you can use CREATE DATABASE statements to create a database in the following syntax format:

CREATE DATABASE [IF NOT EXISTS] database name [[DEFAULT] CHARACTER SET character set name] [[DEFAULT] COLLATE proofreading rule name]

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

Database name: the name of the database you created. 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: the simplest statement to create an 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) after reading this article, I believe you have a certain understanding of mysql database creation examples, want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

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