foreword
hi~, hello everyone! Welcome to my MySQL learning record notes ~. Note that the MySQL version I am using is 5.7.41, and the platform is on the Linux platform.
This note focuses on the basic knowledge of the database and the classification of sql statements. The focus is on the learning of the definition and operation of the database. (Pay special attention to the character set and validation rules)
Table of contents
The character set and verification rules of the database:
1. Store data format - charset(character)
2. Take out the data format-collation
4. Delete the database && backup and restore the database
database foundation
1. What is a database
Taking the Linux environment as an example, we need to understand what a database is.
Simply understand the relationship between the above databases. (mainly under Linux)
Client is mysql /bin/mysql is the client.
MySQL is /sbin/mysqld as the server.
DB is a database, which is equivalent to a directory under Linux.
Table: equivalent to a file in a directory.
Therefore, the mysql database itself is a network server system.
In a narrow sense, we use the files that store data as databases. (The default address of Linux cloud server storage is \var\lib\mysql). But in a broad sense, the combination of mysql client + mysqld server + data files is called a database.
Because of the combination of these three, they will provide a more convenient solution for the software collection of data access services. -mysql database.
Since it is accessing files, doesn't the operating system itself have a file system? Why do we still need a database?
Files or databases can store data.
If you use files, the management of data content needs to be done by the programmer himself! . - Write code to do it yourself in real time.The essence of the database is to provide basic content operations on the content of the file, without manual data management by programmers (users). - (Our disk files are also managed at the OS level -- the focus of management is different)
2.MySQL architecture
The above figure can be briefly summarized as follows:
The mysql client connects to the mysql server.
Connection layer: ensure the integrity of the database system, security policies, etc.
Perform syntax analysis and performance optimization based on the transmitted data.
Storage engine layer. Concerned about what to do, mysql supports plug-in. (The so-called plug-in is a class similar to the definition in C++, which is then organized and called using concerns such as inheritance)
The operating system provides an API to call the underlying files.
For the storage engine (engine): how the database management system stores data, how to index the stored data, and how to update and query data and other technical implementation methods. -accomplish
From the perspective of the operating system, MySQL Server is the code of an application process, and from the perspective of the network, it is a type of application layer protocol.
3. SQL classification
SQL is a structured query language. Applied to major databases, using grammatical rules to operate on file data. (will be used by mysqld for a parsing)
For different operations of SQL, it is usually divided into the following three categories:
DDL: Data Definition Language - maintains the structure for storing data
Usually involves establishing some kind of data structure. For example, build a database, build a table, modify the database, and modify the table structure (no user data is involved). The keywords involved are: CREATE DROP ALTER, etc.
DML: Data Manipulation Language - operate on data
At this time, there is no change to the structure of the table or the structure of the library, but a direct operation on the data content.
Another DQL was derived at this time: Data Query Language (SELECT). The keywords involved in DML are (INSERT, UPDATE, DELETE)
DCL: Data Control Language - responsible for managing permissions and transactions
Just like user management in Linux, we also need to control user permissions and so on. The keywords involved are (GRANT, REVOKE)
In addition, in addition to the above keywords, SHOW is also frequently used to view the substructures under the current structure.
Before creating the database, we need to connect to the mysqld server and use the mysql client to connect.
mysql [-h ip -P 3306] -uroot -p
[] is the part that can be omitted. ip is the public network IP corresponding to the mysql server (mysqld). The uppercase P points to its default port (the default port is 3306), the u indicates the user, and the lowercase p means to enter the password. The password can follow, but it is displayed in plain text, just enter the password below.
However, on this machine, the following steps are usually performed directly:
mysql -uroot -p
If not connected, make sure your mysql service is started.
On Linux, systemctl start mysqld starts (restart restarts, stop stops the service)
Windows is net start mysql.
1. Create a database
The syntax for creating a database is:
create database [if not exists] db_name [create_specification [, create_sepcification] ......];
Parameter explanation :
database: Indicates that the operation is a database.
if not exists: Indicates that there is no creation
db_name: the name of the database
create_specification:
[default] character set charset_name(utf8)
[default] collate collation_name(utf8_general_ci)Where character set can be changed to charset=? collate can also be changed to collate=?
Indicates that the encoding (input) verification rule (output) is manually set, and if not set, the mysql configuration is inherited by default.
Note : You don’t need to write in [], and the subsequent configuration will inherit the configuration in the mysql environment by default.
Note that here is an explanation for Create_specification (creation specification).
The character set and verification rules of the database:
We know that the data stored at the bottom of the computer are all binary. Then if we want to store data (text) that our humans can understand or take it out, we must formulate a certain code to standardize the input and take out of binary, so as to get the desired data.
Common encodings in the world such as utf-8.
The database is naturally the same. However, since there are many kinds of encodings on the market and may correspond to different scenarios, two sets are designed: encoding (character set) and specification of verification rules.
1. Store data format - charset(character)
In mysql, use the command show variables like 'character_set_database' to view the default encoding format of the database supported by the current database.
Note that variables is a variable that exists in mysql, similar to the environment variables in the program environment, check some variables set globally.
It can be seen that the default encoding format supported by my MySQL is utf8. (Because it was configured in the my.cnf file at the beginning). We can also use fuzzy queries to find the encoding formats currently supported by other components of MySQL by default.
show variables like 'character_set%';
We can also view all the encoding formats that can be supported under the current MySQL , and the corresponding default verification rules .
show charset;
2. Take out the data format-collation
Similarly, use the command show variables like 'collation_database' to view the default validation rules of the current database. (The verification rule is the rule for taking out the data format)
You can also perform fuzzy queries to view other default verification rules: show variables like 'collation_%' ;
You can also view all the validation rules supported by the current database and the corresponding encoding formats.
show collation;
For the same encoding but different verification rules, there are differences before.
For example, the following two verification rules of utf8:
You can see that utf8_general_ci is the default, but the verification rule utf8_bin is not the default. Is there any difference between them?
utf8_general_ci is not case-sensitive when checking, but utf8_bin is case-sensitive.
An example can be given as follows: (Note that it involves table building operations, just understand)
-Create a database and view the database optimization statement:
- Create a table to simply insert data and find:
2. Find the database
First of all, when we have created the database, we can take a look at an optimized statement made by MySQL for us, using the statement:
show create database database name
For example, the database test_1 we created above:
And, we can also view all databases created under the current account:
show databases;
Similarly, we can see that the database is actually a file directory in the files saved in the database (under the /var/lib/mysql directory by default) .
3. Modify the database
Modifying the database is to modify the properties of the database when it was created. For properties, there are only encoding and verification rules. We can use the keyword alter.
alter database database name [charset=?(character set ?) collate=?(collate ?)]
Of course, you can also only set the encoding format, so that the verification rule is the default one, and you can view the default verification rule of the encoding through show charset~
4. Delete the database && backup and restore the database
Before backing up and restoring the database, we can first understand a command to see how many people are currently connected to mysqld, and what was the last operation.
show processlist;
For example, only one host is up at the beginning, but now two hosts are connected, and they are recognized.
Backup database :
In fact, we learned from the above that our database is actually a directory in Linux. So we have backed up this directory with cp -r soon?
But don't do this, because the database service program itself is above the operating system. If you bypass this layer, what should you do with the database service?
Therefore, before backing up, we need to ensure that the mysql service is started. Use another client specially prepared by mysql-mysqldump, which is specially designed for backup, and save the database we want to back up or just a part of the data as a sql file . form is preserved .
mysqldump -P port number -u username -p password -B backup database name >> sql file path
>> is equivalent to appending and inserting, if no redirection is appended, it will be output directly to the screen.
-B indicates that the database is to be backed up. If you only want to back up the table, you don't need to add B, and just add the database name and table name after it.
We still use test_1 as an example. We know that we have modified the verification rules of this database above. There is a table test in it, and there are two data A and a. Now we will back it up and save it in the test.sql file.
It can be found that there are operations for building databases and tables, all of which are saved for us by using SQL statements.
In addition, if you only want to save the data of the table, you can do this:
So database backup: 1. Backup data; 2. Backup operation statements. Operating language backup, you can check the context, and it is more flexible and efficient.
Delete the database:
Now we want to delete the database test_1, it is very simple, just use the keyword drop.
drop database database name
Note that if you delete the database, all the data in it will be deleted, so you must think twice, think twice, think twice when deleting the database.
For example, now we delete the test_1 database. Although deleted, we backed up a test_1 database before, so how do we restore the database?
Restore the database:
source sql file path;
Just execute the above sentence (if you did not add the -B option when you backed up the database before, you need to create the database yourself first, and then use it in use)
It can be found that the data is restored after executing a lot of sql statements.