2020 Could you figure out these problems, say you understand the database

Programmers have a piece has been widely circulated: big deal to us, "delete library on foot."

Thus, as a programmer, do not understand how the database can do it, then the database in the end is Gesha it as a Java engineer, usually dealing with the database, and really a lot, in fact, the so-called CRUD for database CRUD operations.

According to the introduction Baidu Encyclopedia, the database is "in accordance with the data structure to organize, store and manage data warehouse." Is a long-term storage in the computer, organized, shared, unified management of data collection.

The database is stored together in a certain way, can be shared with multiple users, with as little redundancy, and application data set independently of each other, can be regarded as electronic file cabinets - premises storage of electronic documents, users can add the data file, query, update, or delete operation.

It is not did not understand, well, simply, like MySQL database is used to store structured data, such as a student information data, a data item, or a student purchases of consumer data.

Usually we think of Excel frequently used, and in fact quite like the database, the database is actually a form, there are many types of data, such as strings, such as digital, then such long text, and so on.

And such a database is also called a relational database, a typical representative of MySQL.

Look at Baidu Baike introduction, relational database, refers to the use of the relational model to organize the data in the database, which is stored in the form of rows and columns of data in order to understand the user, relational database rows and columns in this series are called table, a set of database tables. User to retrieve data by querying the database, and query execution code is used to define a certain area of ​​the database. Relational model can be simply understood as a form of two-dimensional model, and a relational database is a data organization and the relationship between the two-dimensional tables.

In addition to a relational database, in recent years there are many nosql (not only sql) rise in the database, such as MongoDB, and a map database, columnar databases and so on, these databases are not much use for novice programmers, so we restrict our discussion on relational databases.

Relational database is the most we usually use programmers, database type is also the most simple and approachable, so learning MySQL database generally start from such a relational database, it is a simple to learn, and secondly, it's free.

Learning database, start SQL start

Speaking of MySQL, you have to talk about the SQL, sql, according to Wikipedia is this statement

Structured Query Language (Structured Query
Language) referred to as SQL, it is a special-purpose programming language, is a database query and programming language for accessing data and query, update and manage a relational database system.

For different databases, sql wording may have some differences, but generally are the same, it took MySQL, usually we use the most is the select, update, delete and insert, and such operations are collectively referred to as a data manipulation language DML (Data Manipulation Language), the user can achieve the basic operation of the database through it.

Of course, there are a number of operating ddl databases and tables, database schema definition language DDL (Data Definition Language), a language used to describe real-world entities in the database to be stored. In simple terms, ddl is actually used to perform build tables, delete tables, change table structure operations

I used to write the sql not much, after all, a simple CRUD sql do not need more complex skills, it is up to do table joins.

Then later, I started doing some projects, contacted a number of ORM framework, such as hibernate and mybatis, these frameworks can even make you completely write sql, just call some CRUD API that can be, it makes me so long the time overlooked the importance of SQL.

In fact, learning sql most complicated place is not some high-usage, but in practical applications and optimize the scene. Two sql, probably because of the simple difference will be a great performance gap, the difference may be the point, there is no such index went, whether it is a full table scan, or which index went up, and this only in performance analysis the actual scene makes sense.

Of course, for the novice to learn sql syntax is in the first place, before a gradual transition to the use, optimizes, will be analyzed.

The basic principles of database

Learning database, you need to lay the foundation, database theory is a required course for computer Coban, non-Coban I chose the self-study, I was also holding that this college textbook "Introduction to Database Systems" from the school.

Teaching this kind of thing, or certainly, we want to know more theoretical content mainly includes several aspects

1, the database model which include: hierarchical model, network model and relational model, relational database we use is based on the relational model implementation.

2, basic and sql relational database: understand the basic concepts of relational databases, to understand the basic use of the sql, at least CRUD and get to know some of the common usage

3, security and integrity of the database, in fact, the two parts of theoretical things more boring, actually corresponds to the actual scene, the security is to protect data security, including access control and data backup, and integrity is through a number of conventions and specifications limit the storage contents of the database, for example, we can use the master key, the only key, a non waited in vain to limit the value field is required.

4, relational data theory this part is very interesting, but also more complex, talked about the database paradigm theory, from a paradigm to four paradigms, each with its own requirements and usage, the Internet company a "fourth paradigm" is to use this concept to named.

5, the last part of the transaction and locks, are two important components of the database, transaction and lock transaction ACID properties can be guaranteed a set of database operations, ideal for scenes requiring data consistency, and lock the database not only to achieve transaction basis, also can be flexibly adapted to different application scenarios database, we can also be accomplished by locking and releasing sql statement for concurrency scenarios, especially useful.

The principle of MySQL

After learning basic database finished, the next step was to learn MySQL, after all, very often our database application is MySQL.

In fact, a lot of knowledge in the MySQL database and the foundation we mentioned above similar, but returned to the MySQL implementation level, we have to look at the MySQL storage engine.

MySQL storage engine into innodb and myisam, I believe we face questions for both engines saw a lot of difference, such as innodb support services, support row-level locking, and myisam not supported.

Now that innodb is basically the mainstream, so we basically discuss MySQL when discussing innodb. For the realization of the principle of MySQL, so I think about something we need to learn.

First of all, we understand MySQL in what type of data, how general usage, and then try to use MySQL to run several sql statements, building a database built form, plus index plus the primary key, in short, these practices can help above you learn better .

MySQL wants to better understand the principles, we must understand the overall architecture of the MySQL
Here Insert Picture Description
client

That is, we often use visualization tools, such as Navicat for MySQL

Server

Our program is the installation of MySQL, open it in fact it is a MySQL server process

sql execution layer

sql execution layer is responsible for implementation of parsing sql, which contains many complex components such as interpreter, parser, optimizer, etc., executives will generate a sql execution plan, which often are we analyze the performance of sql an important reference content

Storage Engine layer

Innodb storage engine is a layer, such as organization and data to be stored, how to establish and manage an index and so on, plus how to lock, how to achieve the transaction, the contents of this layer are to be considered.

File system layer

Under the storage engine layer is the file system, and how to interact with the database data and file systems, this layer is to do things.

index

I have to say, a database index is definitely the most frequently investigated, but also the most test sites content.

For example, give you a sql, it can hit the index, which index to hit, if you want to hit an index, you should modify how, when this interview question is not often seen, they change so many questions in fact interviewer just want to examine your understanding for the index.

There is, of indexing data structures, as is one of the test sites are very popular, in fact, the index is based on B + tree to achieve, I do not know what is b + tree, please go back and look at the data structure.

In simple terms, it is a multi-way search tree, dedicated less time to complete the data retrieval because it is lower than the height of a binary tree, and compared to a normal b-tree, its non-leaf nodes only play the role of index , while the leaf nodes is the order of the series, so it is very suitable for the search tree.

If you understand this, then the face of such questions can be more calm face of some, in fact, the interviewer wants to know that you know what the extent of the b + tree only.

Transactions and locks

In addition to the index, the database is probably the most sophisticated aspects of the transaction and locked.

Take the ACID properties of a transaction, you need to understand the implementation principle behind each capital letter, for example, is how to achieve atomicity, consistency is how to ensure that what the principle behind yes.

We usually operate with library database transaction might be used in the spring of transaction templates, execute within a transaction, either all succeed or fail, this is atomic.

Independently of each other between the two transactions, this is the isolation, of course, this is also related to the transaction isolation level.

Including transaction isolation level read uncommitted, read committed, repeatable read and sequence of each transaction isolation levels are suitable for database read and write some scenes, a lot of times, we all need to get to know the level of isolation behind the principle, can be more well suited for it.

MySQL in the default Repeatable Read isolation level, the level basically to ensure the implementation of our business as expected, in MySQL, this transaction isolation level can even solve the problem of phantom reads.

Behind the MySQL transaction, in fact, there is a hidden boss, that is the lock on the database, many transaction isolation level is achieved through a lock, such as repeatable read just add a row lock can be achieved, while the Magic will need to read lock plus gap next-key lock is achieved.

Row-level locking and table-level locks are two locks in MySQL, the table-level locking the name suggests, it will directly lock table, only one transaction can access, and row-level locking is not actually lock is a row in MySQL, the lock added to the index, and the index corresponding data are often more than one line, so the row-level locking is only "row-level locking" on theoretical

Four Principles of database transactions

ACID transaction database four principles: A for Atomicity, i.e. atomicity.

                                         C表示Consistency,即一致性。

                                         I表示Isolation,即隔离性。

                                         D表示Durability,即持久性。

These four principles should find out about the database are outsize. But the real interview when they were asked them, can not be a drop put up, and speak clearly the whole story is not much. I think mainly because we are too polite translation, unlike the English so intuitive, it is difficult to name implies. Another reason is that when we learn to understand not deep enough, just know the reason, do not know why exactly. The so-called know these, I do not know why.
Here Insert Picture Description
Finally put learning materials
Here Insert Picture Description
above are some of my own thoughts, to share out the welcome to correct me, the way to find a wave of concernHere Insert Picture Description

Published 22 original articles · won praise 7 · views 7586

Guess you like

Origin blog.csdn.net/ZYQZXF/article/details/104635306