Mysql affairs, indexes, and permissions management

First, the transaction

Second, the index

Third, rights management

Fourth, the three major paradigms


First, the transaction

Nature: either succeed together or fail together

Example: Transfer (must succeed at the same time, or both fail) if the power failure when the transfer did not turn out the money was gone, that's too bad.

Transaction principle: ACID

A (atomic): either succeed together or fail together (only two actions).

C (consistency): the integrity of the data before and after the transaction to be consistent, the total amount will not increase or decrease.

  • No matter how many 100 who turn to, turn out and must own the remaining 100.

I (isolation): Hubuyingxiang data operations between multiple users.

  • Isolate the problem caused when:
    • Dirty read: A transaction reads data to another uncommitted transactions.
    • Non-repeatable read: A transaction reads a row, data read many times inconsistent.
    • Phantom read: a transaction reads another transaction to insert data, resulting in inconsistent before and after the reading.

D (persistent): depending on whether the transaction is committed to the implementation of the success or failure, irreversible after the transaction commits.

  • When the transaction is successful, regardless of whether the power failure, restart the database after the money will not be restored to its original state (state after transfers).
  • When a transaction fails, regardless of whether the power failure, restart the database after the money will be restored to its original state (the state before the transfer).

-- 创建数据库
create database if not exists changeMoney default character set 'utf8' default collate utf8_general_ci;
-- 创建表
create table shop(
`id` bigint(20) not null auto_increment,
`name` varchar(40) not null,
`money` decimal(40) not null,
primary key(`id`)
)engine = INNODB default charset=utf8;

-- 事务
-- 关闭自动提交
set autocommit = 0;	
-- 开启事务
start transaction

update shop set money = money - 1000 where `name` = '张三';
update shop set money = money + 1000 where `name` = '李四';

--提交事务
commit;	
--回滚(恢复到未开启事务时)
rollback;	
-- (开启自动提交)
set autocommit = 1;	

Transaction commit process:
Here Insert Picture Description

Second, the index

Mysql help efficiently get the data structure of the data.

Index in a small amount of data is of little use when a large amount of data can be significantly increased query speed;

Index creation: create [Index] index index name (id_ table _ column name) on the table name (field name);

index:

  • Primary key index (primary key): a unique identifier, a table has only one primary key.

  • The only index (unique key): Avoid duplicate column appears, may be repeated multiple columns can be identified as a unique index.

  • General Index (key / index): default, can be set by key or index keywords.

  • Full-text indexing (FullText): Fast positioning data

influences:

  • After the index is created, query speed becomes faster, but slower insert and modify.

  • The more data in the table, create a slower index.

Index principle:

  • Index not better.
  • Do not indexed data to change frequently.
  • Do not add a small amount of data indexing.
  • Index generally applied to the fields used to query.

Recommended reading: https://blog.codinglabs.org/articles/theory-of-mysql-index.html

Third, rights management

1. Create a user

create user username identified by password;

2, change the current user password (MySQL8 failure)

set password = password (password);

3, modify the specified user password

set password for the username = password (password);

4, rename

rename user on a user name to the new user name;

5, full access to authorized users (authorized user can not give other authorized users)

. Grant all privileges on * * to username;

6, query authority

show grants for username;

Grants for root @ localhost Show; (query root privileges)

7, root user privileges

grant all privileges on *.* to ‘root@localhost’ with grants option;

8, revoke privileges

. Revoke all privileges on * * from user name;

9, delete user

drop user username;

Fourth, database backup

Backup reasons:

  • Database to ensure data is not lost
  • Data transfer

Backup way:

  • A physical copy: Go to the folder where the file mysql, copy the data files.

  • windows: the windows command line using mysqldump to export.

    • Export: mysqldump -h host - u username -p password database [Table 1, Table 2, Table 3, ...]> drive: location or name.

Here Insert Picture Description
Here Insert Picture Description

  • Import:

    • 1, log database, import tables need to import into the database.
    • 2, source drive: / file. **

Here Insert Picture Description


Fourth, the three major paradigms

First Normal Form (1NF):

Each column atomicity, table of only one thing.
Here Insert Picture Description

Second Normal Form (2NF):

To meet the first paradigm, and each table only describe one thing.
Here Insert Picture Description

Third Normal Form (3NF):

Each column of data satisfies the first and second paradigm, and to ensure that the primary key table and are directly related to, but not directly related.
Here Insert Picture Description
Source: https://www.cnblogs.com/wsg25/p/9615100.html

Database design specifications:

Not more than three relational database table.

  • Consider the commercial needs and goals (cost, user experience), database performance is more important.
  • When the performance of regulatory issues, it is necessary to consider the appropriate normative.
  • Some tables deliberately add some redundancy field, so that multi-table queries into a single-table queries.
  • Deliberately increase the number of computed column (from large amounts of data into a small amount of data query: index)
Published 61 original articles · won praise 0 · Views 2162

Guess you like

Origin blog.csdn.net/sabstarb/article/details/105104122