Home > Database > Mysql Tutorial > What is the sql statement to create a database?

What is the sql statement to create a database?

青灯夜游
Release: 2022-06-09 14:46:34
Original
117280 people have browsed it

The sql statement to create a database is "CREATE DATABASE", and the syntax is "CREATE DATABASE [IF NOT EXISTS] database name [[DEFAULT] CHARACTER SET character set name] [[DEFAULT] COLLATE proofreading rule name];" ; Among them, the "IF NOT EXISTS" option is used to detect whether the specified database exists before creating the database, which can avoid the error of repeated creation when the database already exists.

What is the sql statement to create a database?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In MySQL, you can use the CREATE DATABASE statement to create a database. The syntax format is as follows:

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

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

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

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

Example 2: Specify the character set and collation rules when creating the 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)

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)
Copy after login

At this time, you can use SHOW CREATE DATABASE checked the definition statement of the test_db_char database and found 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)
Copy after login

Related free learning recommendations: mysql video Tutorial

The above is the detailed content of What is the sql statement to create a database?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template