Mysql database advanced usage of views, transactions, indexes, self-connections, user management example analysis

Mysql database advanced usage of views, transactions, indexes, self-connections, user management example analysis

This article uses examples to describe advanced usage of MySQL database: views, transactions, indexes, self-connections, and user management. Share with you for your reference, the details are as follows:

view

A view is a reference to several basic tables, a virtual table that only queries the field types and constraints of the statement execution results, and does not store specific data (if the basic table data changes, the view will also change accordingly). It facilitates operations, especially query operations, reduces complex SQL statements, and enhances readability.

1.————Create a view:

create view view name (usually starts with v_) as query statement;

2.————View View:

select * from view name;

3.————Delete view:

drop view view name;

Transactions

1. Features: Four major features ACID

(1) Atomicity: A transaction must be considered as an indivisible minimum unit of work. All operations in the entire transaction must either be committed successfully or fail and be rolled back. It is impossible for a transaction to execute only part of the operations.

(2) Consistency: The database always transitions from one consistent state to another consistent state.

(3) Isolation: Changes made by a transaction are not visible to other transactions until they are finally committed.

(4) Durability: Once a transaction is committed, the changes it makes will be permanently saved to the database.

2. Use:

(1) Open a transaction: After opening a transaction and executing a modification command, the changes will be saved in the local cache instead of in the physical table.
begin; or start transaction;

(2) Rollback transaction: Abandon the changed data in the cache, indicating that the transaction execution failed, and return to the state at the beginning of the transaction.
rollback;

(3) Commit transaction: Maintain data changes in the cache to the physical table.
commit;

index

An index is a special file (the index on an InnoDB table is a component of the table space). It contains the location information of all records in the table. In more general terms, a database index is like the table of contents at the front of a book, which can speed up database queries.

1. Use:

(1)————View index:

show index from table name;

(2)————Create index:

create index index name on table name (field name (length));

(3)————Delete index:

drop index index name on table name;

2. Verify performance:

(1) Enable runtime detection:

set profiling=1;

(2) Find the 100,000th piece of data:

select * from test_index where title='ha-99999';

(3) Create an index for the table:

create index title_index on test_index(title(10));

(4) Find the 100,000th piece of data:

select * from test_index where title='ha-99999';

(5) Check the execution time:

show profiles;

Summary: Indexes can greatly improve query speed, but the time to create an index is much slower than the query speed. Therefore, it is not suitable to use indexes when the data table is frequently updated. It is recommended to use indexes when the data table is large and has not been updated for a long time. It is not recommended in other situations.

User Management

1.————View users:

select host,user,authentication_string from user;

————View user permissions:

show grants for username@host;

2.————Create users and grant permissions:'

grant permission name on database name.* to 'user name'@'host' identified by 'password';

3.————Modify permissions:

grant permission name on database name.* to 'user name'@'host' where grant option;

Refresh permissions:

flush privileges;

4.————Change password:

update user set authentication_string=password('new password') where user='user name';

5.————Delete user:
uninstall:

drop drop user 'username'@'host';

Manual Removal:

delete from user where user='username';

Refresh permissions:

flush privileges;

Self-join

Aggregate data from multiple similar tables into one table and connect them to themselves for querying.

1.————Usage:

select * from table name as alias 1 inner join table name as alias 2 on condition;

2.————Case:


aid is the number, pid is the number to which it belongs, query all cities in Shanxi Province:

select city.* from areas as cityinner join areas as province 
on city.pid=province.aidwhere province.atitle='Shanxi Province'; 

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Detailed analysis of MySQL index transactions
  • MySQL Database Indexes and Transactions
  • Detailed explanation of transactions and indexes in MySQL database
  • Summary of MySql index, lock, and transaction knowledge points
  • MySql knowledge points: transaction, index, lock principle and usage analysis
  • Mysql transaction index knowledge summary

<<:  Practical way to build selenium grid distributed environment with docker

>>:  Calling Baidu Map to obtain longitude and latitude in Vue

Recommend

How to change the domestic source of Ubuntu 20.04 apt

UPD 2020.2.26 Currently Ubuntu 20.04 LTS has not ...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

Modify the maximum number of mysql connections and configuration files in docker

1. Find the mysql image docker ps 2. Enter the mi...

Centos7 installation and configuration of Mysql5.7

Step 1: Get the MySQL YUM source Go to the MySQL ...

XHTML Getting Started Tutorial: Simple Web Page Creation

Create your first web page in one minute: Let'...

How to permanently change the host name in Linux

If you want to change your host name, you can fol...

202 Free High Quality XHTML Templates (2)

Following the previous article 202 Free High-Qual...

Summary of Problems in Installing MySQL 5.7.19 under Linux

The first time I installed MySQL on my virtual ma...

Mac node deletion and reinstallation case study

Mac node delete and reinstall delete node -v sudo...

Example of how to mosaic an image using js

This article mainly introduces an example of how ...

In-depth explanation of nginx location priority

location expression type ~ indicates to perform a...

Detailed explanation of several ways to create objects and object methods in js

This article is the second article about objects ...

How to insert a link in html

Each web page has an address, identified by a URL...

Install Docker on CentOS 7

If you don't have a Linux system, please refe...