当前位置:网站首页>Mysql database (IV) transactions and functions
Mysql database (IV) transactions and functions
2022-07-06 15:10:00 【Hand pluckable Xinchen】
1, Business
Concept : Is to put a group SQL Statements are executed in the same batch , If one SQL Statement error , Then all of the SQL Will be cancelled .
2, The principle of affairs
3, The way to implement a transaction
4, Steps to implement a transaction
-- Operation of transaction
-- 1, Turn off auto submit
SET autocommit = 0;
-- 2, Open transaction
START TRANSACTION;
-- 3, Perform a group of SQL sentence
update bank set bmoney = bmoney-1000 where bname=' Ma Zhi ';
update bank set bmoney = bmoney+1000 where bname=' Liu Xinmiao ';
-- 4, End the business
-- 4.1 Submit
COMMIT;
-- 4.2 Roll back
ROLLBACK;
SET autocommit = 1;5,MySQL The four isolation levels
(1)Read Uncommitted( Read uncommitted content ) Also known as “ Dirty reading ”
(2)Read Committed( Read submissions Also called non repeatable )
(3)Repeatable Read( Can be reread ) Add the corresponding “ Fantasy reading ”
(4)Serializable( Serializable )

-- View the current isolation level
select @@global.transaction_isolation,@@transaction_isolation;6,MySQL function
| Function name | Return value |
|---|---|
| curdate() | Returns the current date |
| curtime() | Return to the current time |
| now() | Returns the current date and time |
| date_format(date,fmt) | As specified fmt Format format date date value |
| year(date) | Return date date A year of (1000~9999) |
| month(date) | return date The month of (1~12) |
| day(date) | return date Day of |
# function
-- Must appear in SQL In the sentence , Not alone
-- Date function
SELECT CURDATE();
SELECT CURTIME();
SELECT NOW();
SELECT DAYOFWEEK(NOW()) - 1 week ;
-- Please check the students whose birthdays are in that month
SELECT * FROM student WHERE MONTH(birthday) = MONTH(NOW());
-- Format date
SELECT DATE_FORMAT(NOW(),'%Y-%c-%d');7, Calculate the difference between dates
-- Time difference ( Days )
SELECT DATEDIFF(NOW(),'2022-5-1');
-- Time difference calculation date
-- + How long after the date - How long ago was the date
SELECT NOW() + INTERVAL 14 DAY;
SELECT * FROM student WHERE MONTH(birthday) = MONTH(NOW());8, Common string functions
| Function name | Return value |
|---|---|
| concat(s1,s2...sn) | take s1,s2...,sn Concatenated into a string |
| concat_ws(sep,s1,s2...sn) | take s1,s2...,sn Concatenated into a string , And use sep Character spacing |
-- String function
SELECT CONCAT('hello',' Hello ');
SELECT CONCAT_WS('!','hello',' Hello ');8, Common mathematical functions
| Function name | Return value |
|---|---|
| ceiling(x) | Return is greater than the x The minimum integer value of |
| floor(x) | Back to less than x The maximum integer value of |
| round(x,y) | Returns the parameter x The rounding of is y A decimal value |
| truncate(x,y) | Return to digital x Truncate to y The result of decimal places |
-- Rounding up
SELECT CEILING(68.15);
-- Rounding down
SELECT FLOOR(68.15);
-- rounding
SELECT ROUND(68.55,1);
-- truncation
SELECT TRUNCATE(68.55,1);-- Aggregate functions
SELECT ssex,COUNT(*),GROUP_CONCAT(sname) FROM student GROUP BY ssex;9, The slow query
Concept :MySQL Default 10 No response in seconds SQL result , Slow query .
# The slow query
select count(*) from emp;
select count(1) from emp;
select count(eid) from emp;
show create table emp;
CREATE TABLE `emp` (
`eid` bigint(20) DEFAULT NULL,
`ename` varchar(10) DEFAULT NULL,
`esex` varchar(5) DEFAULT NULL,
`ebirthday` datetime DEFAULT NULL,
`ehisday` datetime DEFAULT NULL,
`job` varchar(15) DEFAULT NULL,
`emoney` decimal(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- View connections
show status like 'connections';
-- Slow query status OFF close ON Turn on
Show variables like '%slow_query%';
-- Turn on slow query
set global slow_query_log='ON';
set global log_output='TABLE';
-- The number of slow queries
show status like 'slow_queries';
select * From mysql.slow_log ;
select convert(sql_text using utf8) sql_text from mysql.slow_log
ALL: This connection type performs a full scan on each of the previous records Union , This is generally worse , It should be avoided as much as possible .
explain select eid,ename,esex,ebirthday,ehisday,job ,emoney
from emp where eid = 20000;10, Indexes
Concept : An index is a structure that sorts the values of a column or columns in a database table , Use indexes to quickly access specific information in database tables .
characteristic :
(1) Efficiency : Using index can improve the efficiency of database query .
(2) integrity : Users can accelerate the connection between meters , Achieve referential integrity between tables .
(3) Uniqueness : The index can ensure the uniqueness of the checked data .
(4) Special abilities : By using index , In the process of query , Using the optimize hide tool , Improve system performance .
shortcoming :
(1) Although the index greatly improves the query speed , At the same time, it will reduce the speed of updating the table , Such as on the table INSERT、
11, Classification of indexes
(1) primary key Defining a primary key for a table in a database diagram will automatically create a primary key index .
(2) unique index Rows with the same index value are not allowed , This prevents duplicate indexes or key values .
(3) General index The most basic index type , There's no such thing as uniqueness .
(4) Full-text index The key technology of search engine , Used to retrieve text information , It can be words or paragraphs .
alter table emp add primary key (eid);
explain select eid,ename,esex,ebirthday,ehisday,job ,emoney
from emp where eid = 44040;
explain select * from emp where ename = ' Weicui '
-- Add general index
alter table emp add index(ename);create table wenzhang(
wid int PRIMARY KEY auto_increment,
title varchar(20),
content text,
zuozhe varchar(20),
FULLTEXT(title,content,zuozhe) with parser ngram
);
insert into wenzhang(title,content,zuozhe)
values
(' Xi'an past ',' This is an ancient city , There are many people in this city , factory , building ',' Old Xue '),
(' Shanxi past ',' This is an ancient city , There are many people here , factory , Architecture ',' Old Xu '),
(' The past of the earth ',' This is an ancient planet , There are many people here ',' Lao Liu is in Xi'an '),
(' Galactic past ',' This is a department , I plan to build a Xi'an outside this department ',' Peng ');
-- It is a kind of fuzzy query
select * from wenzhang where match(title,content,zuozhe) AGAINST(' Xi'an ');
select * from wenzhang where match(title,content) AGAINST(' Xi'an ');
alter table wenzhang add fulltext(title,content) with parser ngram;
-- Look at the index
show index from student;
cardinality / count(*) The closer the 1 The better The closer the 0 Worse
show index from emp;
边栏推荐
- Report on the double computer experiment of scoring system based on 485 bus
- Opencv recognition of face in image
- Description of Vos storage space, bandwidth occupation and PPS requirements
- Stc-b learning board buzzer plays music
- Maximum nesting depth of parentheses in leetcode simple questions
- DVWA exercise 05 file upload file upload
- China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
- UCORE lab2 physical memory management experiment report
- Quaternion -- basic concepts (Reprint)
猜你喜欢

王爽汇编语言详细学习笔记二:寄存器

王爽汇编语言学习详细笔记一:基础知识

软件测试有哪些常用的SQL语句?

China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '

Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code

Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
![[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases](/img/31/875b08d752ecd914f4e727e561adbd.jpg)
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases

Install and run tensorflow object detection API video object recognition system of Google open source

Description of Vos storage space, bandwidth occupation and PPS requirements
软件测试面试要问的性能测试术语你知道吗?
随机推荐
Investment operation steps
软件测试方法有哪些?带你看点不一样的东西
Software testing interview summary - common interview questions
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
STC-B学习板蜂鸣器播放音乐
C language do while loop classic Level 2 questions
王爽汇编语言学习详细笔记一:基础知识
软件测试Bug报告怎么写?
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
How to rename multiple folders and add unified new content to folder names
Public key box
[HCIA continuous update] working principle of static route and default route
Query method of database multi table link
Cc36 different subsequences
ucore lab7 同步互斥 实验报告
王爽汇编语言详细学习笔记二:寄存器
Example 071 simulates a vending machine, designs a program of the vending machine, runs the program, prompts the user, enters the options to be selected, and prompts the selected content after the use
Keil5 MDK's formatting code tool and adding shortcuts
CSAPP Shell Lab 实验报告