In mysql, you can use the "CREATE DATABASE" statement to add a database. The syntax is "CREATE DATABASE [IF NOT EXISTS] library name [CHARACTER SET character set name] [COLLATE proofreading rule name];".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
In mysql, you can add a database by using the "CREATE DATABASE" statement to create a new database.
The syntax format for creating a database is as follows:
CREATE DATABASE [IF NOT EXISTS] <数据库名> [[DEFAULT] CHARACTER SET <字符集名>] [[DEFAULT] COLLATE <校对规则名>];
[ ] The content in
is optional. The syntax is as follows:
#Database name:
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.
For example, to create a database named mytestdb, you can execute the CREATE DATABASE statement followed by the database name: mytestdb
, if there is no database in the current MySQL server: mytestdb
, then the creation is successful, as shown below:
CREATE DATABASE IF NOT EXISTS mytestdb;
After executing this statement, MySQL returns a message to notify whether the new database has been successfully created.
SHOW DATABASES
SHOW DATABASES
statement displays all databases in the MySQL database server. You can use the SHOW DATABASES
statement to view the database you want to create, or to view all databases on the database server before creating a new database, for example:
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testdb | | mytestdb | +--------------------+ 5 rows in set
In this MySQL database server there are 6 databases. information_schema
, performance_schema
and mysql
are the default databases available when we install MySQL, while mytestdb
and testdb
are New database created. Selecting the Database to Use Before using a specified database, you must tell MySQL which database to use by using the USE statement.
USE database_name;
You can use the USE statement to select the sample database (mytestdb) as follows:
USE mytestdb;
From now on, all operations (such as querying data, creating new tables, or calling stored procedures) Will have an impact on the current database (i.e. mytestdb).
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to add a database in mysql. For more information, please follow other related articles on the PHP Chinese website!