In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to create a database 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 database can be regarded as a container for storing data objects, including tables, views, triggers, stored procedures and so on, in which tables are the most basic data objects. Before you create data objects in a MySQL database, you need to create a database.
Recommended course: MySQL tutorial.
In MySQL, you can use CREATE DATABASE statements to create a database in the following syntax format:
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 as a directory, so the database name must conform to the operating system's folder naming conventions, and note that it is not case-sensitive 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 default character set for the database.
[DEFAULT] COLLATE: specifies the default proofreading rules for the character set.
MySQL character set (CHARACTER) and proofreading rules (COLLATION) are two different concepts: character set is used to define the way MySQL stores strings, and proofreading rules define the way to compare strings to solve the problem of sorting and character grouping.
There is an one-to-many relationship between character sets and proofreading rules, and each character set corresponds to at least one proofreading rule. MySQL supports nearly 200 proofreading rules for 39 character sets.
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)
If you enter the above statement again, an error message will be given, as follows:
Mysql > CREATE DATABASE test_db;ERROR 1007 (HY000): Can't create database 'test_db'; database exists
MySQL does not allow you to create two databases with the same name on the same system.
If you add an IF NOT EXISTS clause, you can avoid similar errors, as follows:
Mysql > CREATE DATABASE IF NOT EXISTS test_db;Query OK, 1 row affected (0.12 sec)
Example: 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.05 sec)
To prevent character confusion, MySQL sometimes needs to specify a character set explicitly when creating a database; in the Chinese mainland region, the commonly used character sets are utf8 and gbk.
Utf8 can store all the characters in the world and can be used in any country. The default proofreading rule is utf8_general_ci, and utf8_general_ci can be used for Chinese.
Gbk can only store the characters involved in Chinese, which is not universal. The default proofreading rule is gbk_chinese_ci.
Thank you for reading! What is the method of creating a database in mysql is shared here, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.