Chapter 1 Database Operations
1. Operation of the library
1. Create a database
(1) Grammar
create database 数据库名称;
Suppose we want to create a database named D1, we can write the MySQL statement in the figure below.
(2) Character set and verification rules
a. Definition
A character set is a collection of characters as the name implies. But this character set contains not only characters, but also the numeric code corresponding to each character . For example, the character set we commonly use in c++ and c: ASCII table.
After understanding the character set, what are the verification rules?
The verification rules of a character set refer to the criteria used when comparing the size of characters in a character set. For example, if we compare the letters a and B, obviously, we will get different comparison results from different angles. Therefore, the comparison and verification rules for unifying characters came into being. At the same time, different verification rules also have different comparison results.
So what data sets and validation rules do we have in our MySQL?
We can see it with the following two statements:
View the dataset:
show charset;
View validation rules:
show collation;
When we just created the database, we did not specify the data set and validation rules of the database. In this case, the database we created will use the default character set and validation rules.
Generally, the default character set used by the system is: utf8 , and the verification rule is: utf8_ general_ ci .
At the same time, we can enter the following statement to view the default character set and verification rules of the system:
show variables like 'character_set_database';
show variables like 'collation_database';
(3) Create databases with different character sets and verification rules
We can also use different character sets and validation rules.
The syntax is as follows:
create database 数据库名称 charset = 数据集名称 collate 校验规则名称;
Create a db2 database using the utf8 character set.
create databases db2 charset = utf8;
Create a db3 database that uses the utf character set and the collation rule is utf8_general_ci.
create database db3 charset=utf8 collate utf8_general_ci;
2. Check the database
(1) Grammar
show databases;
(2) Example
3. Display the creation statement
(1) Grammar
show create database 数据库名称;
(2) Example
In the above example, there is a point that the author needs to explain. /*!40100 .......*/
It is not a comment, but that if the current MySQL version is greater than 4.01, then execute this sentence.
4. Modify the database
(1) Grammar
Modify the character set of the database
alter database 数据库名称 charset = 字符集名称;
Modify the validation rules of the database
alter database 数据库名称 collate 校验规则名称;
(2) Example
Change the character set of the D1 database to gbk;
modify the verification rules of the database:
5. Delete the database
(1) Grammar
drop database 数据库名称;
(2) Example
5. Enter the database
(1) Grammar
use 数据库名称;