Talk about the database

Talk about the database

Come on, sort it out

What is SQL

The full name of SQL is Structure Query Languagestructured query language. It is a language designed for table associations. Our common MySQL is one of them. Today, we will mainly sort out MySQL. MySQL is a relational database. MySQL is the first step in our database entry.

By default, everyone has already installed mysql. Whether it is win or linux, I believe everyone has mastered it. I believe it will not be difficult for you to switch between versions, passwords, and permissions. Let us directly enter MySQL.

MySQL storage engine

InnoDB

InnoDB is the default transactional storage engine of MySQL (after MySQL 5.5.5) . Only when you need features that it does not support, consider using other storage engines.

InnoDB supports foreign key operations.

InnoDB default lock granularity 行级锁, concurrency performance is better, deadlock will occur.

And MyISAM as is, InnoDB storage engine is also .frm文件存储表结构defined, but the difference is, InnoDB table data and index data are stored together, are located on the leaf nodes B + number, and MyISAM table data and index data are separated .

InnoDB support transactional operations with transactional ACID isolation characteristics, the default isolation level is 可重复读(repetable-read)through MVCC(并发版本控制)to achieve. We can solve 脏读and 不可重复读problems.

InnoDB has a secure log file. This log file is used to recover data loss caused by database crashes or other situations to ensure data consistency.

InnoDB and MyISAM support the same index types, but the specific implementation is very different due to the difference in file structure.

In terms of performance of addition, deletion, modification, and checking, if you perform a large number of addition, deletion, and modification operations, it is recommended to use the InnoDB storage engine. It deletes rows during deletion and does not rebuild the table.

MyISAM

The design is simple and the data is stored in a compact format . For read-only data, or the table is relatively small, can tolerate repair operations, you can still use it.

MyISAM does not support 事务operations, and ACID features do not exist. This design is for performance and efficiency considerations.

MyISAM does not support 外键operations. If you forcibly add a foreign key, MySQL will not report an error, but the foreign key does not work.

MyISAM does not support row-level locks . It can only lock the entire table. Shared locks are added to all tables that need to be read when reading, and exclusive locks are added to the table when writing. But while the table has read operations, you can also insert new records into the table, which is called concurrent insert (CONCURRENT INSERT).

MyISAM is stored in three files on the disk, the same file name and table name, extension, respectively .frm(存储表定义), .MYD(MYData,存储数据), MYI(MyIndex,存储索引). What needs special attention here is that MyISAM only 索引文件caches data files.

If the host where the database is located goes down, MyISAM data files are easily damaged and difficult to recover.

Addition, deletion, modification and query performance: SELECT performance is higher, suitable for more queries

View database engine

Enter in our database

show engines;

You can view the engines currently supported by MySQL and the default engine

Insert picture description here

If you need to create the database table of the specified engine, you can add the ENGINE command after the create statement

create table test(id int(10),name varchar(20)) engine = MyISAM;

If not specified, the InnoDB engine is used by default.

Features MYISAM INNODB
Transaction support not support stand by
Data row lock not support stand by
Foreign key constraint not support stand by
Full-text index stand by not support
Table space size Smaller Larger, about 2 times of MYISAM

In a thousand words, MyISAM saves space, is fast, InnDB is safe, and has transactions.

What is a business? Insert a sentence here

Affairs

When it comes to affairs, everyone must think of ACID. When talking about databases, ACID is a rogue. What is ACID? Isn't it Atomicity, Consistency, Isolation, and Durability .

What is a transaction? A transaction is an indivisible sequence of database operations and the basic unit of database concurrency control. The result of its execution must change the database from one consistent state to another consistent state. A transaction is a logical set of operations, which are either executed or not executed .

Four conditions ACID

  • **Atomicity: **Atomicity means that a transaction is an indivisible unit of work. The operations in the transaction either all happen or none happen.
  • **Consistency: **The integrity of the data before and after the transaction must be consistent.
  • **Isolation (isolation): **Transaction isolation means that when multiple users access the database concurrently, the transaction opened by the database for each user cannot be interfered by the operation data of other transactions. Multiple concurrent transactions must be mutually exclusive isolation.
  • **Durability (durability): **Durability means that once a transaction is committed, its changes to the data in the database are permanent, and even if the database fails, it should not have any impact on it.

Since it talks about isolation, let's talk about the isolation level of the database

Before understanding the database isolation level, we must first understand

What is dirty read? Phantom reading? Not repeatable?

  • Dirty Read: A transaction has updated a copy of data, and another transaction has read the same copy of data at this time. For some reason, if the previous RollBack operation is performed, the data read by the next transaction is Would be incorrect.
  • Non-repeatable read: The data is inconsistent in the two queries of a transaction. This may be the original data updated by a transaction inserted between the two queries.
  • Phantom Read: The number of data items in the two queries of a transaction is inconsistent. For example, one transaction queries several rows of data, while another transaction inserts new columns of data at this time. In the next query, you will find that there are several columns of data that it did not have before.

The main difference between non-repeatability and phantom reading is that the focus of non-repeatable reading is modification, while the focus of phantom reading is adding or deleting .

The isolation level of the database

  1. Uncommitted reads. Modifications occurred in the transaction. Even if it is not committed, other transactions are visible. For example, for a number A, the original 50 is changed to 100, but I have not yet committed the change. Another transaction sees the change, and this time the original The transaction is rolled back. At this time, A is still 50, but the A seen by another transaction is 100. It may cause dirty reads, phantom reads, or non-repeatable reads.
  2. Commit and read, for a transaction from the beginning until the commit, any changes made are invisible to other transactions, for example, for a number A was originally 50, and then submitted and modified to 100, this time another transaction before A commits the modification , The read A is 50. Just after reading, A is modified to 100. At this time, another transaction reads and finds that A suddenly becomes 100; dirty reads can be prevented, but phantom reads or non-repeatable reads May still happen
  3. Repeatable read means that the records that are read multiple times for a record are the same. For example, if you read a number A, it is always A, and the A read twice before and after is the same; it can prevent dirty reads and non-repeatable reads. But phantom reading may still happen.
  4. Serializable reads. In the case of concurrent, the results of serialized reads are the same. There is no difference. For example, dirty reads and phantom reads will not occur; this level can prevent dirty reads, non-repeatable reads, and phantoms. read.
Isolation level Dirty read Non-repeatable Phantom reading
READ-UNCOMMITTED
READ-COMMITTED ×
REPEATABLE-READ × ×
SERIALIZABLE × × ×

The default isolation level supported by the MySQL InnoDB storage engine is REPEATABLE-READ (re-readable).

It should be noted here that the difference from the SQL standard is that the InnoDB storage engine uses the Next-Key Lock lock algorithm under the REPEATABLE-READ transaction isolation level , so it can avoid the generation of phantom reads, which is different from other databases. The system (such as SQL Server) is different. Therefore, the default isolation level supported by the InnoDB storage engine is REPEATABLE-READ (re-readable), which can fully guarantee the isolation requirements of transactions, that is, it has reached the SQL standard SERIALIZABLE (serializable) isolation level.

Because the lower the isolation level, the fewer locks the transaction requests, so the isolation level of most database systems is READ-COMMITTED (read submitted content):, but what you need to know is that the InnoDB storage engine uses REPEATABLE-READ by default. Reread) will not have any performance loss .

The InnoDB storage engine generally uses the SERIALIZABLE (serializable) isolation level in the case of distributed transactions.

index

Index overview

All MySQL types can be indexed, use the index to improve the relevant columns is SELECTthe best way to query performance.

First look at the way two commonly used storage engines implement indexing

InnoDB index and MyISAM index

InnoDB uses a clustered index (jù cù suǒ yǐn), while MyISAM uses a non-clustered index. I have read many blogs, and some of them are too professional to understand. In fact, the data storage and index are put together. It is called a clustered index, so when you look up a dictionary, the content to be searched and the page number are together, and the data is found when the index is found. Non-clustered index means that data and index are stored separately.

After understanding the difference between a clustered index and a non-clustered index, let's look at the bottom layer of these two storage types. In fact, the indexes of the two storage types are implemented through the B+ tree in the B tree, which is just a way of implementation. Different.

The difference between InnoDB&MyISAM implements BTree index

  • InnoDB, its data file itself is an index file. Compared with MyISAM, the index file and data file are separated . The table data file itself is an index structure organized by B+Tree. The node data field of the tree saves complete data records. , The key of this index is the primary key of the data table, so the InnoDB table data file itself is the primary index. This is called a "clustered index" or clustered index, and the rest of the indexes are used as auxiliary indexes. The data field of the auxiliary index stores the value of the primary key of the corresponding record instead of the address. This is also different from MyISAM. It searches based on the primary index When you find the node where the key is located, you can retrieve the data; when searching based on the auxiliary index, you need to retrieve the value of the primary key first, and then go through the primary index. Therefore, when designing a table, it is not recommended to use a long field as the primary key, and it is not recommended to use a non-monotonic field as the primary key, which will cause frequent splits of the primary index.

  • In MyISAM, the data field of the B+Tree leaf node stores the address of the data record . During index retrieval, the index is first searched according to the B+Tree search algorithm. If the specified key exists, the value of the data field is retrieved, and then The value of the data field is the data record corresponding to the address read area, which is called "non-clustered index"

After talking about so many indexes, how many kinds are there? Listed here

There are mainly the following indexes in MySQL

  • 全局索引(FULLTEXT): Global index. Currently, only MyISAM engine supports global index. Its appearance is to solve the problem of low efficiency of fuzzy query for text, and it is limited to CHAR, VARCHAR and TEXT columns.

  • 哈希索引(HASH): Hash index is the only data structure of key-value pairs used in MySQL, which is very suitable as an index. HASH index has the advantage of one-time positioning. It does not need to look up node by node like a tree, but this kind of search is suitable for searching for a single key. For range search, the performance of HASH index will be very low. By default, the MEMORY storage engine uses HASH index, but it also supports BTREE index.

  • B-Tree 索引: B means Balance. BTree is a balanced tree. It has many variants. The most common one is B+ Tree, which is widely used by MySQL.

  • R-Tree 索引: R-Tree is rarely used in MySQL and only supports geometry data type. The only storage engines that support this type are MyISAM, BDb, InnoDb, NDb, Archive. Compared with B-Tree, R-Tree's advantage lies in its scope. Find.

Then why use indexes?

1. By creating a unique index, the uniqueness of each row of data in the database table can be guaranteed

2. It can greatly speed up the retrieval of data, which is also the main reason for creating an index

3. Help the server avoid sorting and temporary tables

4. Turn random IO into sequential IO

5. It can speed up the connection between the table and the table, especially in the realization of the reference integrity of the data is particularly meaningful

The index can be created when the table is created, or it can be created separately

Why can index improve query speed?

When the DB executes a SQL statement, the default method is to scan the entire table according to the search conditions, and join the search result set when it encounters a matching condition. If we add an index to a field, the query will first locate the number of rows with a specific value in the index list at a time, greatly reducing the number of matching rows traversed, so the query speed can be significantly increased.

MySQL's official definition of an index is: an index is a data structure that helps MySQL obtain data efficiently. It can be understood as: index is a data structure.

Index extension reading (B-Tree&B+ Tree, etc.):

http://blog.codinglabs.org/articles/theory-of-mysql-index.html

Database paradigm

The paradigm is a table structure with minimal redundancy, and the 3 paradigms are as follows

First Normal Form (1NF)

The first normal form: to ensure the atomicity of each column, each column is the smallest data unit (also known as the smallest atomic unit) that cannot be subdivided, then the first normal form (1NF) is satisfied

Insert picture description here

Second Normal Form (2NF)

Second normal form: First, the first normal form is satisfied, and the primary key column in the table does not have partial dependence on the primary key. Each table is required to describe only one thing.
Insert picture description here

Third Normal Form (3NF)

Third Normal Form: The second normal form is satisfied, and there is no transitive dependency on non-primary key columns in the table. In addition to the primary key order number, the customer name depends on the non-primary key customer number.

Insert picture description here

MySQL & lock have to say

Insert picture description here

In the relational database, you can according to the lock granularity database lock points of row-level locks (INNODB engine), table-level lock (MYISAM engine) and a page-level locks (BDB engine).

Locks used by MyISAM and InnoDB storage engines:

  • MyISAM uses table-level locking.
  • InnoDB supports row-level locking and table-level locking. The default is row-level locking

Row Lock & Table Lock & Page Level Lock

Row lock

Row-level lock is the finest type of lock in Mysql, which means that only the row of the current operation is locked. Row-level locks can greatly reduce conflicts in database operations. The locking granularity is the smallest, but the locking overhead is also the largest. Row-level locks are divided into shared locks and exclusive locks.

Features: high overhead and slow locking; deadlock will occur; the smallest locking granularity, the lowest probability of lock conflicts, and the highest concurrency.

Table lock

Table-level lock is the lock with the largest locking granularity in MySQL. It means to lock the entire table of the current operation. It is simple to implement and consumes less resources. It is supported by most MySQL engines. The most commonly used MYISAM and INNODB both support table-level locking. Table-level locks are divided into table shared read locks (shared locks) and table exclusive write locks (exclusive locks).

Features: low overhead, fast locking; no deadlock; large locking granularity, the highest probability of issuing lock conflicts, and the lowest concurrency.

Page-level lock

Page-level locks are a type of lock that has a locking granularity between row-level locks and table-level locks in MySQL. Table-level locking is fast, but there are many conflicts, and row-level conflicts are few, but the speed is slow. Therefore, a compromised page level is adopted, and a set of adjacent records is locked at a time.

Features: The overhead and lock time are between table locks and row locks; deadlocks will occur; the locking granularity is between table locks and row locks, and the concurrency is average.

From the lock type can be divided into:

Shared lock & exclusive lock

Shared lock

Also called read lock. When the user wants to read data, add a shared lock to the data. Multiple shared locks can be added at the same time.

Exclusive lock

Also called write lock. When the user wants to write data, an exclusive lock is added to the data. Only one exclusive lock can be added, and it is mutually exclusive with other exclusive locks and shared locks.

tips:

When these two locks can be added and when can’t be added is very simple, just like you go to a hotel (simulating user operation data behavior), you can check the room (read lock), you can also do the house (write lock), you are viewing the room Sometimes (you add a read lock, others can also add a read lock), others can also come and watch with you (multiple shared locks are added at the same time), but if you stay overnight (add a write lock), others cannot live with you (Only one exclusive lock can be added).

Optimistic and pessimistic locking in the database

Pessimistic lock : Assuming that concurrency conflicts will occur, shield all operations that may violate data integrity. After querying the data, the transaction is locked until the transaction is committed. Implementation: Use the lock mechanism in the database

Optimistic locking : Assuming that no concurrency conflicts will occur, it is only checked for data integrity violations when the operation is submitted. When modifying data, the transaction is locked, and the lock is performed by version. Implementation method: Generally, the version number mechanism or CAS algorithm is used.

Usage scenarios: read more and write less (optimistic lock), write more (pessimistic lock).

Is there a deadlock? Of course there is

If there are resources and someone uses them, a deadlock may occur.

Deadlock refers to the phenomenon that two or more transactions occupy each other on the same resource and request to lock each other's resources, which leads to a vicious circle.

Common ways to solve deadlock

1. If different programs will access multiple tables concurrently, try to agree to access the tables in the same order, which can greatly reduce the chance of deadlock.

2. In the same transaction, try to lock all the resources needed at once to reduce the probability of deadlock;

3. For business parts that are very prone to deadlock, you can try to upgrade the lock granularity, and reduce the probability of deadlock through table-level locking;

Causes and solution example expansion:

https://blog.csdn.net/tr1912/article/details/81668423

MySQL basic syntax

(Really not making up the word count)

mysql -uroot -proot  -- 连接数据库
exit;  -- 退出连接

update mysql.user set authentication=PASSWORD('yourpassword') where user='root'; -- 修改root密码
flush privileges;  -- 刷新权限

-- 所有的语句都使用;结尾
show databases;  -- 查询数据库列表
use school;    -- 切换数据库 use 数据库名

show tables;  -- 查询数据库中所有的表
describe student; -- 显示数据库中所有的表的信息

create database westos; -- 创建数据库

Operations on the library

创建库		
		CREATE  DATABASE  [IF NOT EXISTS] 库名
[DEFAULT] CHARACTER SET 字符名 |  [DEFAULT] COLLATE 校对规则 

Here beginners recommend the next database visualization software, such as Navicat for mysql, sqlyog, etc., which all have a visual operation database, but these commonly used operating instructions must be mastered

查看库
	SHOW DATABASES
	SHOW CREATE DATABASE 库名【查看数据库创建时的详细信息】
删除库
	DROP DATABASE  [IF EXISTS]  库名 
修改库
	ALTER  DATABASE  [IF NOT EXISTS] 库名
	[DEFAULT] CHARACTER SET 字符名   | [DEFAULT] COLLATE 校对规则
备份库中的数据和
	mysqldump -u 用户名 -p 数据库名 > 文件名.sql【window命令】
	Source 文件名.sql【在库下执行】

	mysql -uroot -p mydb1<c:\test.sql  (window命令)

Operations on the table

增加表
		CREATE TABLE 表名(      列名    类型          )
修改表
	ALTER TABLE 表名
		ADD ( 列名  数据类型 );
	ALTER TABLE 表名
  		MODIFY( 列名  数据类型 );
查看表
		SHOW TABLES;
		SHOW CREATE TABLE 表名【查看表的创建细节】
		DESC 表名【查看表的结构】
删除表
		ALTER TABLE表名 
		DROP(列名);

Operate on the data in the table

	增加
		INSERT INTO 表名 ( 列名..)  VALUES  (数据..);
	修改
		UPDATE  表名  SET 列名=.. , 列名=WHERE=条件 ;
	删除
		DELETE FROM 表名  WHERE=条件;
		TRUNCATE TABLE【先摧毁整张表,再创建表结构】
	查看
		SELECT 列名
        FROM 表名,
        WHERE 条件,
        GROUP BY 列名,
        HAVING BY,
        ORDER BY 列名

Common questions

https://blog.csdn.net/ThinkWon/article/details/104778621

Guess you like

Origin blog.csdn.net/weixin_43876186/article/details/108535197
Recommended