Mysql has never learned notes before and the difference between Mysql 5.5 and 8.0, and the wrong record during installation

The difference between MYISAM and INNODB

Insert picture description here
Some conventional operations
MYISAM save space and are faster.
INNODB has high security, transaction processing, multi-table and multi-user operations. The
difference between delete and Truncate. The
same point: data can be deleted, but the table structure will not be deleted.
Difference:
Truncate is reset, since Increment the column, the counter will be reset to zero.
Truncate will not affect the transaction.
When we delete the problem, restart the database, phenomenon.
If the engine is InnoDB, the auto- increment column will start from 1 (because it exists in memory).
If the engine is MyISAM, continue from the previous one. Starting from the increment (the ones stored in the file will not be lost)
remove duplicate keywords

select distinct '字段名' from 表名 

Commonly used functions in Mysql

-- 常用的函数
-- 数学的
SELECT ABS(8)  --绝对值
SELECT CEILING(0.1) --向上取整
SELECT FLOOR(8.1)--向下取整
SELECT RAND() --返回一个0~1 之间的随机数
SELECT SIGN(10) --判断一个数的符号 0就是0 负数时-1  正数是 1
-- 字符串函数
SELECT CHAR_LENGTH('我爱憨憨')  --字符串的长度
SELECT CONCAT('我','爱','憨憨')  --拼接字符串
SELECT INSERT('I am  meinv',1,2'very')  --查询加替代
SELECT LOWER('ssssssss')  --小写字母
SELECT UPPER('SSSSSSSSSSSS'--大写字母)
SELECT INSTR('fjj','f') --返回第一次出现的字符串的索引
SELECT REPLACE('我爱憨憨','我','爱','憨憨')  -替换
SELECT SUBSTR(''','','憨憨'',4,6) --返回指定的字符串
SELECT REVERSE('我','爱','憨憨') --反转
--查询某个字段后替换
select replace(字段,'替换前的值','替换后的值') from 表名 where 字段 like '字段%'
-- 时间和日期函数
SELECT CURRENT_DATE --获取当前日期
SELECT CURDATE()--获取当前日期
SELECT NOW() --获取当前的时间
SELECT LOCALTIME() --本地时间
SELECT SYSDATE() --系统时间

Aggregate function

count(字段)  --求总和的
count(*),count(1) --之间的区别?
--如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。 
--因为count(*),自动会优化指定到那一个字段。所以没必要去count(1),用count(*),sql会帮你完成优化的 因此:count(1)和count(*)基本没有差别! 
--count(1) and count(字段)
--两者的主要区别是
--(1) count(1) 会统计表中的所有的记录数,包含字段为null 的记录。
--(2) count(字段) 会统计该字段在表中出现的次数,忽略字段为null 的情况。即不统计字段为null 的记录。
--执行效果上:  
--count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL  
--count(1)包括了忽略所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULL  
--count(列名)只包括列名那一列,在统计结果的时候,会忽略列值为空(这里的空不是只空字符串或者0,而是表示null)的计数,即某个字段值为NULL时,
--统计。
--执行效率上:  
--列名为主键,count(列名)会比count(1)快  
--列名不为主键,count(1)会比count(列名)快  
--如果表多个列并且没有主键,则 count(1) 的执行效率优于 count(*)  
--如果有主键,则 select count(主键)的执行效率是最优的  
--如果表只有一个字段,则 select count(*)最优。
avg() --平均值
sum() --求总和
max() --最大值
min() --最小值
order by  排序 配合having 使用

MD 5 encryption
What is MD5?
Mainly enhance algorithm complexity and irreversibility.
MD5 is irreversible. The specific value of MD5 is the same as the
characteristics of the database.
Atomicity: either all succeeds or both fail.
Consistency: The data integrity before and after the transaction must be consistent.
Persistence: transaction commit, Once the transaction is committed and irreversible, it is persisted in the database.
Isolation: The transaction isolation level is when multiple users access the database concurrently, the database opens the transaction for each user, and the operation data of other transactions cannot interfere with each other. Isolation
level
Uncommitted read: dirty read phenomenon, transaction A reads the uncommitted data of transaction B;

Submitted read: Non-repeatable read phenomenon solves the phenomenon of dirty reads. Two Select operations are performed in the same transaction, first read once, and then another transaction performs update operations on the data you read, so read twice before and after Take two different data;

Repeatable read: solve the phenomenon of non-repeatability, phantom reading, the data obtained after transaction Aupdate database, transaction BInsert adds a lot of data that meet the query conditions of transaction A, and transaction A can be read when it is read again The data that meets their update conditions but is updated;

Serialization: To solve all problems, data operations are not allowed for other transactions to perform any operations on the data, sync lock;
indexing
helps Mysql to efficiently obtain the data structure of the data. The
index is
the classification of the data structure index
. The primary key in a table There can only be one index, and the unique index can have multiple
primary key indexes.
Unique identification. The primary key cannot be repeated. Only one column can be used as the primary key
unique index to
avoid duplicate columns. The unique index can be repeated. Multiple columns can be identified and unique. Index general
index
By default, the
full-text index set by the index and key keywords is only available
under a specific database engine, MyISAm
quickly locates data

--索引的使用
--1,在创建表的时候给字段增加索引
--2,创建完毕后,增加索引
--显示所有的所有信息
show index from 表名
--增加一个全文索引
alter table 表名  add fulltext index '字段名'(‘索引名’)
explain select * from 表名 --分析sql 执行的状况 

The role of the index
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

After adding the index, the query speed is much faster.
My table structure

CREATE TABLE `test`.`Untitled`  (
  `id` bigint(0) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '用户昵称',
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户邮箱',
  `phone` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '手机号',
  `gender` tinyint(0) UNSIGNED NULL DEFAULT 0 COMMENT '性别(0:男;1:女)',
  `password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
  `age` tinyint(0) NULL DEFAULT 0 COMMENT '年龄',
  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0),
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'app用户表' ROW_FORMAT = Dynamic;
-- --查看数据库有多少数据
select count(1) from app_user
-- 当我们查询一个手机号的时候
SELECT * FROM app_user WHERE phone  ='13817917100';
-- --查看
EXPLAIN select * from app_user WHERE phone ='13817917100'
-- 给phone 字段添加一行索引
CREATE INDEX id_app_user_phone ON app_user(phone)
-- --查看
EXPLAIN select * from app_user WHERE phone ='13817917100'

The difference between Mysql 5.5 and Mysql 8.0
This is what I discovered this afternoon. It turns out that I have been using 5.5. When writing about timestamps, I found that 5.5 cannot be written like this. Insert picture description here
I'm fine. Baidu has a little difference. By the way, I installed the latest version of mysql/
errors encountered during installation.
Insert picture description here
Modify my.ini. The
Insert picture description here
second error is because you did not open cmd as an administrator, resulting in an error when downloading mysql.
Finally, remember that it is generated by default. If you open it with a visual tool, you will be prompted to change the new password.
After the installation is complete, I first saw this when building the table, and then I
Insert picture description here
realized that it turned out that he jumped directly from 5.7 to 8.0. It is said that the performance has improved a lot. do not know. Only summarize the following points
Insert picture description here

Account security
old version of the command

grant all privileges on *.* to '用户名'@'主机' identified by '密码';

The new version needs to be executed separately

create user '用户名'@'主机' identified by '密码';
grant all privileges on *.* to '用户名'@'主机';

Using the previous one will report an error to the
optimizer index
1, that is, the hidden index (invisible index) that you see
will not be used by the optimizer

Application scenario
Soft delete
deletes an index. If you delete the wrong index, you can only add it back through the index. For some large databases, it is more performance-consuming; in order to avoid deleting mistakes, you can first set it to no It can be seen that the optimizer will not use it at this time, but the background is still being maintained. After confirming, delete it.
Gray release is
similar to soft delete. If you want to test some index functions or may use this index later, you can set it as a hidden index first, which will not affect the existing query. After the test, it is determined that the index is needed. It is set to the visible index.
command

--如果是不隐藏,则不需要后面的invisible关键字
create index 索引名称 on 表名(字段名) invisible;
--查询某一张表的索引,执行如下命令:
show index from 表名
--使用explain语句查看查询优化器对索引的使用情况
explain select * from 表名 where 条件;
--设置已经存在的索引为可见或者隐藏 
alter table 表名 alter index 索引名 visible;
alter table 表名 alter index 索引名 invisible;

The primary key cannot be set to hidden
2. Descending index
Mysql 8.0 began to support descending index, only InnoDB engine supports descending order, so it must be BTREE descending index. Mysql 8.0 does not perform implicit sorting on group by operations.
3. Function
index is used in functional index index. The expression
supports json. The index of the data node is a
window function based on the function of the virtual column.
1 Atomic ddl operation
MySQL5.7 executes the drop command drop table t1, t2; if t1 exists, t2 does not exist, it will prompt t2 table does not exist, but The t1 table will still be deleted.
MySQL8.0 executes the same drop command, it will prompt that the t2 table does not exist, and the t1 table will not be deleted, ensuring atomicity.
2. Self-
  incrementing column persistence MySQL5.7 and its previous versions, the MySQL server restarts, it will rescan the maximum value of the primary key of the table, if the data with id=100 has been deleted before, but if the maximum value of the current record in the table is 99, then after scanning, the id of the next record is 100 instead of 101.
  
  MySQL8.0 writes the maximum value of the increment counter to the redo log every time it changes, and writes it to the engine's private system table at each checkpoint. The problem of auto-incrementing the primary key will not be repeated.
  The principle of
  indexing    Indexes are not as many as possible
  Do not index the process change data
  Small data volume tables do not need to be indexed
  Indexes are generally added to the commonly used query fields

Guess you like

Origin blog.csdn.net/m0_46937429/article/details/113462632