Home > Database > Mysql Tutorial > body text

Basic knowledge of mysql (mysql novice tutorial)

墨辰丷
Release: 2019-11-26 13:32:34
forward
11321 people have browsed it

This article mainly introduces the basic knowledge of MySQL. A database is a collection of data stored in some organized way. It is a container that saves organized data (usually a file or a group of files). Interested friends You can go to the php Chinese website mysql video tutorial channel to learn more. The Mysql database must be started under the Mysql service.

Start Mysql under windows cd to the mysql\bin directory and start\close the mysql service under the dos window

//启动mysql服务
mysqld --console
//关闭mysql服务
mysqladmin -uroot shutdown
Copy after login

SQL classification

SQL main statements can be divided into the following three categories

  • DDL: Data Definition Language. These statements define different data segments, databases, tables, columns, indexes and other database objects. Commonly used statement keywords mainly include create, drop, alter, etc.

  • DML: Data operation statements, used to add, delete, update and query database records, and check data integrity. Commonly used statement keywords mainly include insert, delete, update, select, etc.

  • DCL data control statements are statements used to control direct permissions and access levels of different data segments. These statements define the database, tables, fields, user access rights and security levels. The main statements include keywords grant, revoke, etc.

DDL statements

are used to create, delete, modify and other operations on objects inside the database. Language, the biggest difference between it and DML statements is that DML only operates on the internal data of the table, and does not involve the definition of the table, modification of the structure, nor other objects. DDL is used more by database administrators (DBAs).

连接mysql服务器 
mysql -uroot -p 
创建数据库test1
create database test1;
显示有哪些数据库
show databases;
//mysql  自动创建的表有
information_schema:主要存储了系统中的一些数据库信息,比如用户表信息、列信息、权限信息、字符集信息、分区信息等等
cluster:存储了系统的集群信息
mysql:存储了系统的用户权限信息。
test:系统自动创建的测试数据库,任何用户都可以访问
选择数据库
use test1
显示test1数据库中创建的所有表
show tables
删除数据库
drop database test1;
创建表
create table emp(ename varchar(10),hiredata date,sal decimal(10,2),deptno int(2));
查看表定义
desc emp;
查看创建表的定义
show create table emp;
删除表
drop table emp;
修改表
alter table emp modify ename varchar(20);
增加表字段
alter table emp add column age int(3);
删除表字段
alter table emp drop column age;
字段改名
alter table emp change age age1 int(4);
change 和modify都可以修改表的定义,不同的是change后面需要写两次列名,不方便,但是change的优点是可以修改列名称,则modify则不能
修改字段排序
alter table emp add birth date after ename;
alter table emp modify age int(3) first;
更改表名
alter table emp rename emp1;
Copy after login

DML statement

refers to the operation of table records in the database, mainly including insert, update and delete of table records and query (select).

插入记录
insert into emp(ename,hiredate,sal,deptno)values('zzx1','2000-01-01','2000',1);
也可以不用指定字段名称,但是values后面的顺序要和字段的排列顺序一致
inset into emp('zzx1','2000-01-01','2000',1);
含可空字段、非空但是含有默认值的字段、自增字段、可以不用再insert后的字段列表里面出现,values后面只写对应字段名称的value,没写的字段可以自动设置为null、默认值、自增的下一个数字

批量增加用逗号隔开
insert into dept values(5,'xxx'),(8,'xxx');

更新记录
update emp set sal=4000 where ename='xxx';

删除记录
delete from emp where ename='doney';

查询记录
select * from emp;
*表示所有记录,也可以用逗号隔开的字段来选择查询

查询不重复的记录
select distinct deptno from emp;

条件查询
用where关键字来实现,可以使用<>!=等多条件可以使用or、and等

排序和限制
desc和asc是排序关键字,desc是降序、asc是升序排列 ORDER BY 排序,默认是升序
select * from emp order by sal;
如果排序字段的值一样,则值相同的字段按照第二个排序字段进行排序,如果只有一个排序字段,则相同字段将会无序排序
select * from emp order by deptno,sal desc;
限制
select * from emp order by sal limit 3;
//前者是起始偏移量,后者是显示行数
select * from emp order by sal limit 1,3;

limit 和order by 一起使用来做分页

聚合
用户做一下些汇总操作

sum(求和),count(*)(记录数),max(最大值),min(最小值)
with rollup 是可选语法,表示是否对分类聚合后的结果进行再汇总
having 表示对分类后的结果在进行条件的过滤。

select deptno,count(1) from emp group by deptno having count(1)>=1;
Copy after login

Table connection

The broad categories are divided into outer joins and inner joins
External links are further divided into left joins and right joins

Left join: Contains all records in the left table even if there are no matching records in the right table.
Right join: Same as above

select ename,detname from emp left join dept on emp.deptno=dept.deptno;
左连接和右连接可以相互转换
Copy after login

Subquery

select * from emp where deptno in(select deptno from dept);
如果子查询记录唯一,可以使用=替代in
select * from emp where deptno =(select deptno from dept limit 1);
Copy after login

Record query
After the data from the two tables are queried according to a certain query, the results are combined and displayed

Union all is to merge the result sets together, and union is to perform a distinct on the result of union all to remove duplicates

select deptno from emp union all select deptno from dept;
select demtno from emp union select deptno from dept;
Copy after login

? xxx to view

如果要查看类别 ? data types 具体的 ? int 
查看语法 如 ? create table
Copy after login

Data type

对于整形数据,MySql还支持在类型名称后面的小括号设置宽度,默认设置为int(11),配合zerofill,
当数字位数不够的时候,用字符‘0’填充
alter table t1 modify id1 int zerofill
Copy after login

For decimals, MySql is divided into two types, floating point numbers and fixed point numbers. Floating-point numbers include float and double, while fixed-point numbers only have decimal. Fixed-point numbers are stored in string form internally in Mysql, which are more accurate than floating-point numbers and are suitable for high-precision data such as currency.

Floating-point numbers and fixed-point numbers The point number can be added to the type name (M, D). M is the number of digits, and D is the number of digits after the decimal point.

Date type

  • DATE represents the year, month and day

  • ##DATETIME represents the year, month, day, hour, minute and second

  • TIME represents hours, minutes and seconds

  • The current system time is usually represented by TIMESTAMP

  • ##TIMESTAMP
Create a field of type TIMESTAMP, and the system automatically creates a default value of CURRENT_TIMESTAMP (system date). At the same time, MySql stipulates that a column of TIMESTAMP type field can only have one default value current_timestamp. An error will be reported if modified.


TIMESTAMP Another important feature is related to time zone. When the time is inserted, it is first converted to the local time zone and then stored. When it is taken out from the database, the date is also converted to the local time zone and then displayed. In this way, users in two time zones may see the same time zone differently

查看当前时区
show variables like &#39;time_zone&#39;;
修改时区
set time_zone=&#39;+9.00&#39;;
Copy after login

DATETIME insertion format

YYYY-MM-DD HH:MM:SS 或YY-MM-DD HH:MM:SS 的字符串允许任何标点符号用来做时间部分的间隔符
如92@12@31 11^30^45
YYYYMMDDHHMMSS 或YYMMDDHHMMSS的格式没有间隔符的字符串
Copy after login

String type

CHAR and VARCHAR type

The main difference between the two is the storage method: the length of the CHAR column is fixed to the value declared when creating the table Length, the length can be 0-255; the value in the two VARCHAR columns is of variable length. At the same time, during retrieval, CHAR columns delete trailing spaces, while VARCHAR retains spaces. Since CHAR is a fixed length, its processing speed is much faster than VARCHAR, but its disadvantage is a waste of memory. VARCHAR is used more often.

create table vc (v varchar(4),c char(4))
insert into vc values(&#39;ab  &#39;,&#39;ab  &#39;);
selelct length(v),length(c) from vc
//4,2
Copy after login
Copy after login

Enumeration

create table vc (v varchar(4),c char(4))
insert into vc values(&#39;ab  &#39;,&#39;ab  &#39;);
selelct length(v),length(c) from vc
//4,2
Copy after login
Copy after login

set type

set type can select multiple members at one time

create table t2 (col set(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;));
INSERT into t2 VALUE (&#39;a,b&#39;),(&#39;a,d,a&#39;),(&#39;a,b&#39;),(&#39;a,c&#39;),(&#39;a&#39;);
对于(a,d,a)这个包含重复成员的集合只取一次 结果为’a,d&#39;
Copy after login

Operator

p==/==除法获取商
MOD==%==除法获取余数
Copy after login

## The difference between #= and <=>

cannot be used for null comparison, the latter can be

between 使用格式 a between min and max 等价于 a>=min and a<=max
in的使用格式 a in(value1,value2...);
like 使用格式如 a like %123%,当字符串含有123则返回1 否则返回0
REGEXP 使用格式 str REGEXP str_pat 当str字符串中含有str_pat 相匹配的字符串,则返回1
Copy after login

bit operation

# #OperatorFunction&andorxor##~bit XOR>Bit shift right<<Bit shift left

常用函数

字符串函数

|
^
函数功能
CONCAT(s1,s2,s3…)连接s1到sn的字符串(任何字符串和null拼接都是null)
insert(str,x,y,instr)将字符串str从x位置开始,y字符长的子串替换为字符串instr
lower(str)将字符串str中所有字符变为小写
UPPER(str)大写
LEFT(str,x)返回字符串str最左边x个字符
RIGHT(str,x)返回字符串str最右边的x个字符
LPAD(str,n,pad)用字符串pad对str最左边进行填充,直到长度为n个字符串长度
PRPAD(str,n,pad)用字符串pad对str最右边进行填充,直到长度为n个字符串长度
LTRIM(str)去掉字符串str左侧的空格
RIGHT(str)去掉字符串str行尾的空格
REPEAT(str,x)返回str重复x次的结果
REPLACE(Str,a,b)用字符串b替换字符串str中所有出现的字符串a
STRCMP(s1,s2)比较字符串s1和s2
TRIM(str)去掉行尾和行头的空格
SUBSTRING(str,x,y)返回字符串str x位置起y字符串长度的字串

数字函数

函数功能
ABS(X)返回x的绝对值
CEIL(X)返回大于x的最小整数值
FLOOR(X)返回小于x的最大整数值
MOD(x,y)返回x/y的模
RAND()返回0-1内的随机值
ROUND(x,y)返回参数x的四舍五入的有y位小数的值
TRUNCATE(x,y)返回数值x截断为y位小树的结果

日期和时间函数

函数功能
CURDATE()返回当前日期
CURTIME()返回当前时间
NOW()返回当前的日期和时间
UNIX_TIMESTAMP(date)返回date的unix时间戳
FROM_UNIXTIME返回UNIX时间戳的日期值
WEEK(date)返回日期date为一年中的第几周
YEAR(date)返回日期date的年份
HOUR(time)返回time的小时值
MINUTE(time)返回time的分钟值
MONTHNAME(date)返回date的月份名
DATE_FROMATE(date,fmt)返回按字符串fmt格式化日期date值
DATE_ADD(date,interval expr type)返回一个日期或时间值加上一个时间间隔的时间值
DATEDIFF(expr,expr2)返回起始时间expr和结束时间expr2之间的天数

流程函数

函数功能
IF(value,t f)如果value是真 返回 t;否则返回f
IFNULL(value1,value2)如果value1不为空,返回value1,负责返回value2
CASE WHEN[value1] THEN[value2]…ELSE[default] END如果value1是真,返回result1否则返回defalut
case [expr] WHEN[value1] THEN[value2]…ELSE[default] END如果expr等于value1,返回result1否则返回defalut

实例

create table salary(userid int ,salary decimal(9,2));
insert into salary values(1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,null);
select * from salary
select if(salary>2000,&#39;high&#39;,&#39;low&#39;) from salary;
select ifnull(salary,0) from salary;
select case when salary <=2000 then &#39;low&#39; else &#39;high&#39; end from salary;
select case salary when 1000 then &#39;low&#39; when 2000 then &#39;mid&#39; else &#39;high&#39; end from salary;
Copy after login
Copy after login

其他函数

函数功能
DATABASE()返回的确数据库库名
VERSION()返回当前数据库版本
USER()返回当前登录用户名
INET_ATON(IP)返回ip地址的数字表示
INET_NTOA(num)返回数字代表的ip地址
PASSWORD(str)返回字符串str加密版本
MD5()返回字符串的md5值

MySql引擎
MySql支持的存储引擎包括MyISAM、InnoDB、BDB、MEMORY、MERGE、EXAMPLE、NDB Cluster、ARCHIVE、CSV、BLACKHOLE、FEDERATED等,其中InnoDB和BDB提供事务安全表,用户可以选择不同的数据存储引擎来提高应用的效率

创建表如果不指定存储引擎,系统默认使用默认存储引擎,MySql5.5之前的默认引擎是MyISAM,5.5之后改为InnoDB。如果要修改默认的存储引擎,可以在参数文件中设置default-table-type.

create table salary(userid int ,salary decimal(9,2));
insert into salary values(1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,null);
select * from salary
select if(salary>2000,&#39;high&#39;,&#39;low&#39;) from salary;
select ifnull(salary,0) from salary;
select case when salary <=2000 then &#39;low&#39; else &#39;high&#39; end from salary;
select case salary when 1000 then &#39;low&#39; when 2000 then &#39;mid&#39; else &#39;high&#39; end from salary;
Copy after login
Copy after login

MyISAM
MyISAM 不支持事务、也不 不支持外键,其优点是速度快,对事务完整性没有要求。以SELECT和INSERT为主的应用基本上都就可以使用这个表

InnoDB
InnoDB存储引擎提供了具有提交、回滚和崩溃恢复能力的事务安全。但是对比MyISAM的存储引擎,InnoDB写的处理效率差一些,并且会占用更多的磁盘空间以保留数据和索引。

create table autoincre_demo (i smallint not null auto_increment,name varchar(10),primary key(i))engine=innodb;insert into autoincre_demo values(1,&#39;1&#39;),(0,&#39;2&#39;),(null,&#39;3&#39;)
Copy after login

如果插入空或者0,则实际插入的将是自动增长后的值。
可以通过以下语句强制设置自动增加列的初始值,默认从1开始,但是该强制的默认值是保留到内存中,如果数据库从起,这个强制的默认值会丢失,就需要数据库启动后重新设置

ALTER TABLE *** auto_increment =n
Copy after login

MEMORY
memory 存储引擎使用存在于内存中的内容来创建表,每个MEMORY表实际对应一个磁盘文件,格式是.fm,MEMORY表的访问非常快,因为它的数据是放在内存中,并且默认使用HASH索引,但是一旦服务关闭,表中的数据就会

alter table t2 engine=memory;
show TABLE status like &#39;t2&#39;
给memory表创建索引。可以指定hash索引还是btree索引
create index mem_hash using hash on tab_memory(city_id);
Copy after login

在启动MySql服务的时候使用–init-file选项,把INSERT INTO … SELECT或LOAD DATA INFILE这样的语句放入这个文件中,就可以在服务启动时从持久稳固的数据源装载表
服务器需要足够的内存来维持同一时间使用的MEMORY表,当不需要MEMORY表的内容,要释放MEMORY表的内存,执行DELETE FROM或 TRUNCATE TABLE 或者是DROP TABLE
每个MEMORY表中可以放置的数据量的大小,受max_heap_table_size系统变量的约束,初始值是16mb,可以根据需要加大、
MEMORY类型的存储引擎主要用在那些内容变化不平凡的表,或作为统计操作的中间结果表,便于高效的对中间结果进行分析并得到最终的统计结果。

TokuDB
TokuDB是第三方的存储引擎,是一个高性能、支持事务处理的MySql和MariaDB的存储引擎,具有高扩展性、高压缩、高效率的写入性能,支持大多数在线的DDL操作
TokuDB 特别适用的场景

  • 日志数据,因为日志数据通常插入频繁且储存量大

  • 历史数据,通常不会有在写的操作,可以利用TokuDB的高压缩特性进行存储

  • 在线DDL频繁的场景

几种常用存储引擎的适用环境

  • MyISAM:如果应用是以读操作和插入操作为主,只有很少的更新和删除操作,并且对事务的完整性,并发性要求不高,那么选择这个引擎非常合适

  • Innodb:用于事务的处理,支持外键。如果应用对事务的完整性较高的要求,在并发条件下要求数据的一致性,数据除了插入和查询外,还包括很多的更新和删除操作,那么Innodb存储引擎比较适合

  • MEMORY:将所有数据都存在RAM中,如果需要快速定位记录和其他类似数据的环境下,可以提供极快的访问,缺陷在于对表大小的限制,太大的表无法缓存在内存中,其次是要确保表的数据是可恢复的.

  • MERGE:用于将一系列等同MyISAM表以逻辑方式组合在一起,并作为一个对象引用它们。MERGE表的优点在于可以突破单个MyISAM表大小的限制,并且通过将不同的表分布在多个磁盘上,可以有效的改善MERGE表的访问效率

Text与BLOB
如果保存少量字符串会选择CHAR和VARCHAR 但是保存较大文本时,选择text或blob,两者主要差别是blob能用来保存二进制数据如图片;而text只能保存字符数据

BLOB与TEXT引起的性能问题,特别是在执行大量的删除数据时,删除操作会留下很大的空洞,以后填入这些空洞的记录在插入的性能上会有影响,建立定期使用OPTIMIZE TABLE对这类表进行碎屏整理

optimize table t
Copy after login

使用合成的索引来提供大文本字段的查询性能

合成索引就是根据大文本字段的内容建立一个散列值,并把值存储在单独的数据列中,接下来就是通过检索散列值找到数据行,但是只能做到精确匹配不能使用范围搜索。可以使用MD5,SHA1,CRC32 等生成散列值,使用精确匹配,在一定程度上减少了I/O,提高了查询效率。如果散列算法生成的字符串带有尾部空格,就不要存储在CHAR或VARCHAR列中,它会受尾部空格的影响

如果需要对BLOB或CLOB字段进行模糊查询,MySQL提高前缀索引,也就是只为字段的前n列创建索引
desc select * from t where context like &#39;beijing%&#39; \G;
Copy after login

注意事项

  • 在不必要的时候避免检索大型的BLOB或TEXT:如SELECT * 查询,尽量从符合条件的数据行中检索BLOB或TEXT指

  • 把BLOB或TEXT列分离到单独表中:在某些环境下,如果把这些数据列移动到第二张数据表中,可以把原数据表中的数据列转换为固定长度的数据行格式,减少主表的碎片,可以得到固定长度数据行的性能优势。还可以在运行SELECT * 查询的时候不会通过网络传输大量的BLOB或TEXT指

设计索引的原则

  • 搜索的索引列,不一定是所要选择的列。最适合索引的列是出现在where字句中的列,或连接字句中指定的列,而不是出现在select关键字后的列表中的列

  • 使用唯一索引.考虑到某列中的值分布,索引的列基础越大,索引的效果越好。入存放出生日期的列具有各部相同的值,很容易区分,但是记录性别的列,只含有男和女对此类进行索引没有多大好处

  • 使用短检索。如果对字符串进行检索,应该指定一个前缀长度。例如:一个CHAR(200)列,如果前10个或20个字符内,多数值是唯一的,那么就不要对整个列进行检索。对前10个或20个字符进行检索能够节省大量索引空间,是查询更快。

  • 利用最左前缀。在创建一个n列索引时,实际是创建了MySQL可利用的n个索引。多列索引可起几个索引的作用,因为可利用索引最左边的列级来匹配。

  • 不要过度索引。每个索引都是占用额外的磁盘空间,并降低写操作的性能。在修改表内容的时候,索引必须进行相应的更新,有时候需要重构。如果有一个索引很少被用到,那么会不必要的减缓表的修改速度。此外,mysql在生成一个执行计划时,要考虑各个索引,这也要花费时间。创建多余的索引给查询优化带来了更多的工作

  • 对于Innodb,记录默认会按照一定的顺序排序,如果有明确的定义主键,则按照主键排序顺序保存。

存储过程和函数

什么是存储过程和函数

存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数
可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提供数据处理的效率是有好处的。

存储过程很函数的区别在于函数必须有返回值,而存储过程没有,储存过程的参数可以使用IN,OUT,INOUT类型,而函数的参数只能是IN类型的。如果有函数从其他类型的数据库迁移到MySQL,那么就可能因此需要将函数改造成存储过程。

存储过程和函数的相关操作

在对储存过程和函数操作时,需要首先确认用户是否具有相应的权限。例如,创建存储过程或者函数需要CREATE ROUTINE权限,修改或者删除存储过程或者函数需要ALTER ROUTINE权限,执行过程或者函数需要EXECUTE权限

创建一个新的过程 film_in_stock,该过程用来检查 film_id和store_id对应的inventory是否满足要求,并且返回满足的inventory_id 以及满足要求的记录数

CREATE PROCEDURE film_in_stock(in p_fim_id int,in p_store_id int,out p_film_count int)
READS sql data
begin
  select inventory_id
  from inventory
  where film_id =p_film_id
  and store_id=p_store_id
  and inventory_in_stock(inventory_id);
  SELECT found_rows() into p_film_count;
end $$
Copy after login

通常在创建过程和函数之前,都会通过DELIMITE $$命令将语句的结束符从';'修改成其他符号,这里使用‘$$’,这样在过程和函数中的';'就不会被MySql,解释成语句的结束而错误。在存储过程或者函数创建完成 通过‘DELIMITER;'命令在将结束符改回成';'

调用过程

CALL film_in_stock(2,2,@a);
Copy after login

存储过程的好处在于处理逻辑都封装在数据库端,调用者不需要了解中间的处理逻辑,一旦逻辑改变,只需要修改存储过程,对调用者的程序没有影响

删除存储过程或者函数

一次只能删除一个存储过程或者函数,删除需要ALTER ROUTINE权限

drop procedure film_in_stock;
Copy after login

查看存储过程或者函数状态

show procedure status like &#39;film_in_stock&#39;;
Copy after login

查看存储过程的函数定义

show create procedure film_in_stock
Copy after login

变量使用

存储过程和函数中可以使用变量,在MySql 5.1版本中,变量不区分大小写

变量的定义

通过DECLARE可以定义一个局部变量,该变量的作用范围只能在BEGIN...END中,可以用在嵌套块中

定义一个DATE类型的变量

DECLARE last_month_start date;
Copy after login

变量赋值 可以直接赋值,或者通过查询赋值。直接赋值使用set,可以赋常量或者赋表达式

set var_name=expr [,var_name=expr]...
set last_month_start=date_sub(current_date(),interval month);
select col_name[,...] into var_name[,...] table_expr;
Copy after login

定义条件和处理

delimiter $$
create procedure actor_insert()
begin
 declare continue handler for sqlstate &#39;23000&#39; set @x2=1;
 set @x=1;
 insert into actor(actor_id,first_name,last_name) values(201,&#39;test&#39;,&#39;201&#39;);
 set @x=2;
 insert into actor(actor_id,first_name,last_name) values(1,&#39;test&#39;,&#39;1&#39;);
 set @x=3;
end ;
$$
Copy after login

调用处理函数时遇到主键重的错误会按照定义的处理方式去处理,由于定义的是CONTINUE 会继续执行下面的语句

还支持EXIT表示终止

光标使用

声明光标
declare cursor_name cursor for select_statement
open光标
open cursor_name
fetch光标
fetch cursor_name into var_name[,var_name]...
close光标
close cursor_name
delimiter $$
create procedure payment_stat()
begin
 declare i_staff_id int;
 declare d_amount decimal(5,2);
 declare cur_payment cursor for select staff_id,amount from payment;
 declare exit handler for not found close cur_payment;
  set @x1=0;
  set @x2=0;
  open cur_payment;
 REPEAT
   FETCH cur_payment into i_staff_id,d_amount;
    if i_staff_id =2 then
    set @x1=@x1+d_amount;
    else
    set @x2=@x2+d_amount;
    end if;
 until 0 end repeat;
 close cur_payment;
 end;
 $$
Copy after login

变量,条件,处理程序,光标都是通过DECLARE定义的,她们之间是有先后顺序要求的。变量和条件必须在最前面声明,然后才能是光标的声明,最后才可以是处理程序的声明

控制语句

case 
 when i_staff_id =2 then
 set @x1=@x1+d_amount;
 else
 set @x2=@x2+d_amount;

loop 和leave结合

create procedure actor_insert()
begin
 set @x=0;
 ins:loop
  set @x=@x+1;
  if @x=100 then
  leave ins;
  end if;
  insert into actor(first_name,last_name) values(&#39;Test&#39;,&#39;201&#39;);
  end loop ins;
end;
$$

inerate 语句作用是跳过当前循环的剩下语句,直接进入下一轮循环

create procedure actor_insert()
begin
 set @x=0;
 ins:loop
 set @x=@x+1;
 if @x=10 then
 leave ins;
 elseif mod(@x,2)=0 then
 iterate ins;
 end if;
 insert into actor(actor_id,first_name,last_name) values(@x+200,&#39;test&#39;,@x);
 end loop ins;
end;
$$

repeat 语句 有条件的循环控制语句,当满足条件的时候退出循环
repeat
  fetch cur_payment into i_staff_id,d_amount;
  if i_staff_id =2 then
   set @x1=@x1+d_amount;
  else
   set @x2=@x2+d_amount;
  end if;
 until 0 end repeat;

while
delimiter $$
create procedure loop_demo()
begin
 set @x=1,@x1=1;
 repeat
   set @x=@x+1;
  until @x>0 end repeat;
  while @x<1 do
   set @x=@x+1;
  end while;
 end;
 $$

//创建事件调度器
CREATE EVEN test_event_1 ON SCHEDULE
EVERY 5 SECOND
DO
INSERT INTO dept(deptno,deptname)
VALUES(3,&#39;3&#39;);
//查看本地调度器状态
 show variables like &#39;%scheduler%&#39;;
 //打开调度器
 set global event_scheduler=1;
 //查看后台进程
 show processlist;
 //创建一个新的定时器 定时清空表,防止表变大,这类触发器非常适合去定期清空临时表或者日志表
 create event trunc_test
 on schedule every 1 minute
 do truncate table test;

 禁用调度器或者删除
 alter event test_event_1 disable;
 drop event test_event_1;
Copy after login
事件调度器说明
优势MySQL事件调度器部署在数据库内部由DBA或专人统一维护和管理,避免将一些数据库相关的定时任务部署到操作系统层,减少操作系统管理员产生误操作的风险,对后续的管理和维护也非常有益。例如,后续进行数据库迁移时无需再迁移操作系统层的定时任务,数据库迁移本身已经包含了调度事件的迁移
使用场景事件调度器适用于定期收集统计信息,定期清理历史数据,定期数据库检查(例如,自动监控和回复slave失败进程)
注意事项在繁忙且要求性能的数据库服务器上要慎重部署和启用调度去;过于复杂的处理更适合程序实现;开启和关闭事件调度器需要具有超级用户权限

事务控制和锁定语句

  • MySQL支持对MyISAM和MEMORY存储引擎的表进行表级锁定,对InnoDB存储引擎的表进行行集锁定。默认情况下是自动获得。

  • LOCK TABLES 可以用于锁定当前线程获得的表,如果表被其他线程锁定,当前线程一直等待到可以获取现有锁定为止。

  • UNLOCK TABLES 可以释放当前线程获得的任何锁定,当前线程执行另一个LOCK TABLES时,或当与服务器的连接被关闭时,所有由当前线程锁定的表被隐式地解锁。

session_1session_2
获取表film_text 的read锁定 lock table fim_text read
当前seesion可以查询记录 select * from fim_text其他seesion也可以查询select * from fim_text

其他session更新锁定表会等待锁 update fim_text …. 处于等待状态
释放锁 unlock tables等待

sesion获取锁,更新成功

Transaction control

mysql supports local transactions through set autocommit, start transaction, commit, rollback and other statements. By default, MySQL automatically commits (autocommit). If explicit commit and rollback are required to commit and rollback the transaction, then you need to start the transaction through explicit transaction control commands, which is obviously different from Oracle's transaction management. place.

  1. start transaction or begin statement can start a new transaction

  2. commit and rollback are used to commit or rollback the transaction.

  3. chain and release clauses are used to define operations after transaction submission or rollback respectively. chain will immediately start a new transaction and have the same isolation level as the previous transaction. release The connection with the client will be disconnected.

  4. set autocommit can modify the submission method of the current connection. If set autocommit=0 is set, all transaction reads after setting need to be committed or rolled back through explicit commands.

If you only need transaction control for certain statements, it is more convenient to use the start transaction statement to start a transaction, so that after the transaction ends, you can automatically return to the automatic submission mode. If you want all None of the transactions are automatically submitted, so it is more convenient to control the transaction by modifying autocommit.

start transation and commit and chain

session_1session_2
Query select * from actor No data from table actorQuery select * from actor No data
Start a transaction start transaction; insert into actor …

Query actor select * from actor is still empty
commit

If you query again, select * from actor…
##Automatically submit inset into actor…You can query the newly inserted select *from actor from the table Re-use star transaction to start a transaction start transaction; insert into actor…; use commit and chain command to submit commit and chain; at this time start a new transaction, insert into…The data just inserted cannot be found select * from actor...##Use commit to submit commit;if During the lock table period, using the start transaction command to start a new transaction will cause an unlock tables to be executed
session_1session_2






The newly inserted one can be queried

session_1Query for an actor_id=201, the result is empty select * from actor where actor_id=201;Add a write lock to the table lock table actor writeInsert data insert into actor(actor_id,..)values(201,..)Rollback record rollbackUse the start transaction command to restart a transaction

Therefore, in the same transaction, it is best not to use different storage engines, otherwise special processing will be required for non-transaction tables during rollback. Because commit and rollback can only commit and rollback tables of transaction type.
Normally, only committed transactions are recorded in the binary log, but if a transaction contains non-transactional tables, the rollback operation will also be recorded in the binary log to ensure that updates to non-transactional tables can be Be copied to the slave database (slave).
In a transaction, you can specify a rollback part of the transaction by defining a savepoint, but you cannot specify a part of the transaction to be submitted. For complex applications, multiple different savepoints can be defined. When different conditions are met, different savepoints can be rolled back. It should be noted that if a savepoint with the same name is defined, the savepoint defined later will overwrite the previous definition. For savepoints that are no longer needed, you can delete them using the release savepoint command.
Transaction rollback

session_2
Similarly, the result of querying from the table is empty


To the table The actor's read operation is blocked select * from actor where actor_id=201
Wait
Wait
Waiting for

to start a transaction, the table lock is released and you can query; select …where actor_id=201

Data found
##session_1session_2 Querying the record of first_name='Simon' from the table is empty select * from….where first_name='simon' Querying the record of first_name='Simon' from the table being empty select * from….where first_name ='simon'Start a transaction and insert a piece of data start transaction; inset ….values('simon'…)Query the newly inserted data select * from…where first_name='simon'Cannot find the newly inserted record of session1 from the actor select * from … where first_name='simon'With dataNo dataDefine a savepoint named test savepoint test; insert into …values(…,tom ) Query two pieces of data select *… Still can’t query data select * …Rollback to the savepoint just defined rollback to savepoint testA piece of data is queried from the table actor and returned the next day Roll select * from ….Still unable to query dataSubmit commitInquiry toInquiry to

分布式事务的使用

MySql从5.0.3开始支持分布式事务,当前分布式事务只支持InnoDb存储引擎。一个分布式事务会涉及多个行动,这些行动本身是事务性。所有行动都必须一起成功完成,或者一起被回滚

在mysql中,使用分布式事务的应用程序涉及一个或多个资源管理器和一个事务管理器。

  1. 资源管理器(rm)用于提供通向事务资源的途径。数据库服务器是一种资源管理器,该管理器必须可以提交或回滚由rm管理的事务。如:多台mysql数据库作为多台资源管理器或者几台mysql服务器和几台oracle服务器作为资源管理器。

  2. 事务管理器(tm)用于协调作为一个分布式事务一部分的事务。tm与管理每个事务的rm s进行通信。在分布式事务中,各个单个事务均是分布式事务的“分支事务”。分布式事务和各个分支通过一种命名方法进行标示。

执行分布式的过程分为两阶段提交,发生时间有分布式事务的各个分支需要进行的行动已经被执行之后

  1. 在第一阶段,所有分支呗预备好,即它们被TM告知要准备提交。通常,这意味着用于管理分支的每个RM会记录对于被稳定保存的分支的行动。分支指示是否它们可以这么做,这些结果被用于第二阶段

  2. 在第二阶段,TM告知Rms是否要提交或回滚,如果在预备分支时,所有的分支指示它们将能够提交,则所有的分支被告知要提交。如果在预备时,有任何分支指示它将不能提交,则所有分支呗告知回滚。

语法

xa start xid 用于启动一个带给定xid值的xa事务。每个xa事务必须有一个唯一的xid值,因此该值当前不能被其他xa事务使用

xa grtid[,beual[,formatId]] grtid 是一个分布式事务比较符,相同的分布式事务应该使用相同的gtrid,这样可以明确知道XA事务属于哪个分布式事务

bequal 是一个分支限定符,默认值是空值。对于一个分布式事务中的每个分支事务,bqual指是唯一的

formatId是一个数值,要用来标志 由gtrid和bqual值使用的格式,默认是1

xa end xid[suspend [for migrate]]
xa prepare xid
Copy after login

使事务进入prepare 状态,也就是两阶段提交的第一个提交阶段

xa commit xid[one phase]
xa rollback xid
Copy after login

用来提交和回滚具体的分支事务

xa recover 返回当前数据库中处于PREPARE状态的分支事务的具体信息

分布式的关键在于如何确保分布式事务的完整性,以及在某个分支出现问题时的故障解决,xa的相关命令就是提供给应用如何在多个独立的数据库之间进行分布式事务的管理,包括启动一个分支事务、使事务进入准备阶段以及事务的实际提交回滚操作等,

例子





session_1 in DB1session_2 in DB2
在数据库DB1 启动一个分布式的一个分支事务,xid 的gtrid 为 “test”,bqual为”db1”: xa start ‘test’,’db1’;分支事务插入一个数据 insert into actor(…)values(…) 对分支事务1进行第一阶段提交,进入prepare状态 :xa end ‘test’,’db1’; xa prepare ‘test’,’db1’在数据库DB2 启动分布式事务 “test”的另外一个分支事务,xid的gtrid为”test”.bqual为”db2”; xa start ‘test’,’db2’: 分支事务2在表film_actor 更新数据 最后 xa end ‘test’,’db2’ xa prepare ‘test’,’db2’
xa recover 查看当前分支事务状态xa recover 查看当前分支事务状态
两个事务进入准备提交状态,如果之前遇到任何错误,都应该回滚到所有分支,以确保事务的正确
xa commit ‘test’,’db1’xa commit ‘test’,’db2’

如果分支事务在执行到prepare状态是,数据库异常,且不能再支持启动,需要备份和binlog来回复数据,

SQL Mode

在MySql中,SQLMode常用来解决下面几类问题

  1. 通过设置SQL Mode,可以完成不同严格程度的数据校验,有效的保障数据准确性。

  2. 通过设置SQL Mode,为ANSI模式,来保证大多数SQL符合标准的Sql语法,这样应用在不同数据库之间进行迁移时,则不需要对业务SQL进行较大的修改

  3. 在不同数据库之间进行数据迁移之前,通过设置SQL Mode可以使MySQL上的数据更方便地迁移到目标数据库中

查看 SQL Mode命令

select @@sql_mode
Copy after login

插入一个出国实际定义值的大小varchar(10)

insert into value(&#39;123400000000000000000000000000000&#39;);
//查看warning内容
show warnings
select * from t 这里对插入的数据进行截取前10位
Copy after login

设置SQL Mode为 严格模式

set session sql_mode=&#39;STRICT_TRANS_TABLES&#39;
Copy after login

再次插入insert into value('123400000000000000000000000000000'); 直接给出ERROR,而不是warning

SQL Mode常见功能

校验日期是合法性

set seesion sql_mode=&#39;ANSI&#39;
insert into t values(&#39;2007-04-31&#39;)
Copy after login

结果是 插入值变成'0000-00-00 00:00:00' 并且系统给出warning 而在TRADITIONAL模式下,直接提示日期非法,拒绝插入,同时Mode(x,0)也会报错

qidon NO_BACKSLASH_ESCAPES模式,使反斜杠成为普通字符,在导入数据时,如果数据含有反斜杠字符,你们启动NO_BACKSLASH_ESCAPES模式,保证数据的正确性

启动PIPES_AS_CONCAT。将||视为字符串连接符,在Oracle等数据库中,||被视为字符串的连接操作符,所以在其他数据库中含有||操作符的sql在MySql将无法执行,为了解决这个问题mysql提供了PIPES_AS_CONCAT模式、

MySql分区

MySql从5.1版本开始支持分区,分区是指按照一定的规则,数据库把一个表分解成多个更小的,更容易管理的部分。就访问数据库的应用而言,逻辑上只有一个表或一个索引,但是实际上这个表可能由数10个物理分区对象组成,每个分区都是一个独立的对象,可以独自处理,可以作为表的一部分进行处理。分区对应用而言是完全透明的,不影响应用的业务逻辑

优点

  • 和单个磁盘或者文件系统分区相比,可以存储更多数据

  • 优化查询。在where子句中包含分区条件,可以只扫描必要的一个或多个分区来提高查询效率;同时在涉及SUM()和COUNT()这类聚合函数的查询时,可以容易的在每个分区上并行处理,最终只需要汇总所有分区的结果

  • 对于已经过期或者不需要保存的数据,可以通过删除与这些数据有关的分区来快速删除数据

  • 跨多个磁盘来分散数据查询,以获得更大的查询吞吐量

分区有利于管理非常大的表,它采用 分而治之的逻辑,分区引入分区键的概念,分区键用于根据某个区间键,特定值列表或者HASH函数执行数据的聚集,让数据根据规则分布在不同的分区中,让一个大对象变成一些小对象

show VARIABLES like &#39;%partition%&#39; 查看是否支持分区
Copy after login

Mysql支持大部分存储引起如MyISAM,INNODb,Memory等存储引擎,创建分区,在5.1版本中,同一个分区表的所以分区必须使用同一个存储引擎;在同要给表上,不能对一个分区使用MyISAM引擎和Innodb引擎,但是在同一个MySQL服务器服务器上,甚至同一个数据库中,对于不同的分区表使用不同的存储引擎

分区类型

  • range分区:基于一个给定连续区间范围,把数据分配到不同的分区。

  • LIST分区:类似RANGE分区,区别在LIST分区是基于枚举出的值列表分区,RANGE是基于给定的连续区间范围分区

  • HASH分区:基于给定的分区个数,把数据分配到不同的分区

  • KEY分区:类似HASH分区

在5.1版本中,RANGE分区,LIST分区,HASH分区要求分区键都是int类型,key分区,可以使用其他类型(除了BLOB和TEXT类除外)作为分区键
分区表的主键/唯一键必须包含分区键,不能使用主键/唯一键,要么分区表的主键/唯一键都必须包含分区键,分区的名字是不区分大小写的

range分区

CREATE TABLE emp(
    id int not null,
    ename varchar(30),
    hired date not null DEFAULT &#39;1970-01-01&#39;,
    separated date NOT null DEFAULT &#39;9999-12-21&#39;,
    job varchar(30) not null,
    store_id int not null
)
partition by range(store_id)(
    PARTITION p0 VALUES less than (10),
    PARTITION p1 VALUES less than (20),
    PARTITION p2 VALUES less than (30)
);
//上述的分区方案将storid,1-9分到p0区,10-19分到p1区,等如果插入大于30,会出现错误,因为没有规则保护大于30的

INSERT into emp VALUES(&#39;2322&#39;,&#39;milk&#39;,&#39;1993-12-23&#39;,&#39;1993-12-23&#39;,&#39;click&#39;,19);//可以

//Table has no partition for value 40
INSERT into emp VALUES(&#39;2322&#39;,&#39;milk&#39;,&#39;1993-12-23&#39;,&#39;1993-12-23&#39;,&#39;click&#39;,40);

添加分区
alter  table emp add partition(partition p3 values less than maxvalue);
maxvalue表示最大的可能的整数值

mysql 支持在values less than 语句中加入表达式
比如以日期作为分区
CREATE TABLE emp(
    id int not null,
    ename varchar(30),
    hired date not null DEFAULT &#39;1970-01-01&#39;,
    separated date NOT null DEFAULT &#39;9999-12-21&#39;,
    job varchar(30) not null,
    store_id int not null
)
partition by range(year(separated ))(
    PARTITION p0 VALUES less than (1995),
    PARTITION p1 VALUES less than (2000),
    PARTITION p2 VALUES less than (2005)
);
MySQl 5.5改进了range分区给你,通过支持非整数分区,创建日期分区就不需要通过函数进行转换
partition by range(separated )(
    PARTITION p0 VALUES less than (&#39;1996-01-01&#39;),
    PARTITION p1 VALUES less than (&#39;2001-01-01&#39;),
    PARTITION p2 VALUES less than (&#39;2006-01-01&#39;)
);
Copy after login

rang分区的功能适用一下情况
- 当需要删除过期的数据,只需要简单的alter table emp drop partition p0 来删除p0 分区中的数据。对于具有上百万条记录的表来说,删除分区要比运行一个delete语句有效的多
- 经常运行包含分区键的查询,MyySql可以很快地确定只有某一个或者某些分区需要扫描,因为其他分区不可能包含有符合该where字句的任何记录。例如检索id大于25的记录数,MySql只需要扫描p2分区即可

explain partition select count(1) from emp where store_id>=25
Copy after login

List分区
List分区是建立离散的值列表告诉数据库特定的值属于哪个分区,LIST分区在很多方面类似于RANGE分区,区别在于LIST分区是从属于一个枚举列表,RANGE分区是从属于一个连续区间值的集合

create table expenses(
  expense_date date not null,
  category int,
  amount decimal(10,3)
)partition by list (category)(
   partition p0 values in(3,5),
   partition p1 values in(1,10),
   partition p2 values in(4,9),
   partition p3 values in(2),
   partition p4 values in(6)
)
Copy after login

LIST分区不存在类似于VALUES LESS THAN MAXVALUE这样的值 在MYSQL5.5支持非整数分

Columns分区
Column分区是5.5引入的分区类型,引入Columns分区解决了MySQL5.5版本之前RANGE和LIST分区值值支持整数分区,从而导致需要额外的函数计算得到整数值或者通过额外的转换表来转换为整数在分区的问题
Column分区可以细分为RANGE Columns分区和LIST Columns分区,RANGE Columns分区和LIST Columns分区都支持整数,日期时间,字符串三大数据类型
对于Range分区和List分区,Colums分区的亮点除了支持数据类型增加之外,还支持多列分区

create table rc3(a int,b int)
parition by range columns(a,b)(
 parition p01 values less than(0,10),
 parition p01 values less than(10,10),
 parition p01 values less than(10,20),
 parition p01 values less than(maxvalue,maxvalue)
)
Copy after login

Hash分区
hash分区主要是分数热点读,确保数据在预先确定个数的分区中尽可能平均分布。对一个表执行HASH分区时,Mysql会对分区间应用一个散列函数,以确定数在n个分区中的那个分区中。
mysql支持两种hash分区,常规的hash分区和线性hash分区,常规的hash使用取模算法,线性hash分区使用的一个线性的2的幂的运算法则

create table emp(id int not null.ename varchar(30),hired date not null default &#39;1907-01-01&#39;,sparated date null null default &#39;8888-12-31&#39;,job varchar(30) not null,store_id int not null) partition by hash(store_id)partitions 4;
Copy after login

这里创建了一个常规的hash 使用 partition by hash(expr)其中expr是某列值或一个整数值的表达式返回值。 partition num 对分区类型,分区键,分区个数进行定义,上述基于store_id列hash分区,表被分为4个分区

我们可以计算出它被保存在哪个分区中假设,假设记录的分区编号为N,那么N=MOD(expr,num),例如emp表中有4个分区,插入一个store_id为234的 mod(234,4)=2,倍保存在第二个分区

表达式‘expr’可以是MySQL中有效的任何函数或者是其他表达式,只要它们返回一个既非常数也非随机数的整数。每当插入更新删除一行数据,这个表达式就需要计算一次,意味着非常复杂的表达式可能会引起性能问题
常规的HASH分区通过去模的方式去讲数据平均分布在每个分区上,让每个分区管理的数据都减少,提高了查询的效率;可是当我们需要增加分区或者合并分区的时候,问题就出现了,假设原来是5个常规hash分区,现在需要新增一个常规hash分区,原来的去模算法是mod(expr,5)根据余数0-4分布在5个分区上,现在新增一个分区,取模算法变为mod(expr,6)根据余数0-5分区在6个分区中,原来5个分区的数据大部分都需要通过重新计算重新分区,常规的hash在分区管理上带来的代价太大了。不适合灵活变动分区的需求,Mysql提供了线性hash分区

create table emp(id int not null.ename varchar(30),hired date not null default &#39;1907-01-01&#39;,sparated date null null default &#39;8888-12-31&#39;,job varchar(30) not null,store_id int not null) partition by linear hash(store_id)partitions 4;

计算编号为n的分区
首先找到下一个大于等于num的2的幂,这个值设为v,v的计算公司
v=power(2,ceiling(log(2,num)))
 =power(2,ceiling(log(2,4)))
 =power(2,ceiling(2))
 =power(2,2)
 =4
其次设置n=f(column_list)&(v-1),现在计算store_id=234对应的n值
n=f(column_list)&(4-1)
 =234&(4-1)
 =2
当n》=num设置n=n&(v-1)
对于store_id=234由于n=2《4,所以直接判断这个会被存放到第二分区
Copy after login

线性hash分区的优点在于,在分区维护上(包含增加,删除,合并,拆分分区)时,Mysql能够处理得更加迅速;缺点是对比常规hash分区的时候,线性hash分布不太均衡

key分区
按照key分区进行分区非常类似于按照hash进行分区,只不过hash分区允许使用用户自定义的表达式,而KEY分区不行使用用户自定义的表达式,需要使用MySQl服务器提供的hash函数;同时hash分区只支持整数分区,而key分区支持除了blob
or text类型外其他类型的列作为分区键

create table emp(id int not null.ename varchar(30),hired date not null default &#39;1907-01-01&#39;,sparated date null null default &#39;8888-12-31&#39;,job varchar(30) not null,store_id int not null) partition by key (job)partitions 4;

如果不知道分区键,默认为主键,没有主键会选择非空唯一键作为分区键
Copy after login

子分区
子分区是分区表对每个分区的再次分割。又被称为复合分区,mysql5.1开始支持对已经通过range或者list分区了的表在进行子分区

create table ts(id int,purchased date) partition by range(year(purchased)) subpartition by hash(to_days(purchased))subpartitions 2(partition p0 values less than (1900),partition p0 values less than (2000),partition p0 values less than (maxvalue))
Copy after login

在分区中的null值
在mysql不禁止分区键上使用null,分区键可能是一个字段或者一个用户定义的表达式,一般情况下,mysql的分区把null当做零值,或者一个最小值处理

分区管理

删除分区
alter table emp_date drop partition p2;
增加分区
alter table emp_date add partition(partition p5 value less than(2025))

拆分p3分区,分为p2和p3分区
分区
alter table emp_date reorganize partition p3 into(partition p2 values less than(2005),parition p3 values less than (2015));

合并分区
alter table emp_date reogranize partition p1,p2,p3 into(partition p1 values less than (2015))

重新定义list分区时,只能重新定义相邻的分区,不能跳过list分区进行重新定义
Copy after login

hash&key管理

不能以range和list分区表删除分区的方式,而是跳过alter table coalesce partition 来合并或分区
以原先4个分区为例
alter table emp coalesce partition 2 //减少分区到2个

alter table emp coalesc partition 8 //不能增加分区

要增加分区
alter table emp add partition partitions 8;
Copy after login

相关推荐: 

 关于mysql 基础知识的总结 

 PHP 和 MySQL 基础教程(一) 

 PHP 和 MySQL 基础教程(二) 

 PHP 和 MySQL 基础教程(三) 

 PHP 和 MySQL 基础教程(四)

mysql手册教程://m.sbmmt.com/course/37.html

mysql视频教程://m.sbmmt.com/course/list/51.html

The above is the detailed content of Basic knowledge of mysql (mysql novice tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!