mysql基础

触发器:6个

例:

create trigger tri_stu
before insert on t_student
for each row begin update t_class set t_count
=t_count+1 where t_id=new.t_classid; end;
触发器中不一定必须有new或者old
如:
t_stu2这张表做新增,删除,修改,t_record表中记录下来  
create trigger tri_stu
before insert on t_stu2
for each row
begin
insert into t_record(t_event,t_creatTime) values('student-insert',now());
end;

before insert/update/delete

after insert/update/delete 

 

  1. new:表示t_student表即将要插入的那条记录
  2. new.t_classId : 该学生的班级编号
  3. t_class表中,该学生所属的班的人数需要加1  ,where  t_id = new.t_classId;  

 

  1. old:即将要删除的t_student表的那一整条记录
  2. old.t_classId: 要删除的学生的班级编号
  3. where t_id = old.t_classId ,限制条件,限定就必须是要删的这个学生所在的班级,人数-1

 

insert新增没有old,只有new ,

update 更新 既有old,也有new .

delete 删除只有old,没有new.

如果查看创建的触发器

information_schema数据库下的 triggers表下

use information_schema;

select * from triggersG;

or  select * from information_schema.triggers;

查看呢触发器

show triggersG;

删除触发器

drop trigger tri_stuInsert;



表复制

只复制表结构(不会复制主键类型和自增方式,没有数据)---create table studentNew select * from           t_student where 1=2(不满足的条件即可);

复制表结构及表数据(没有复制主键和自增方式)----create table studentNew select * from t_student;

          create table studentNew select * from t_student where 1=1(满足的条件);

只复制表数据  (新表必须先创建好)

insert into studentNew5  select * from t_student; 



变量 分类

1.局部变量: java方法中的局部变量一样,作用范围最小的
只在定义变量的函数体内,语句块内,存储过程中有效,超出作用域不可见

2.用户变量
:在整个连接期间有效,退出重新登录,失效了
以 "@"开头


3.会话变量
单次连接期间有效 324个

查看当前的会话变量

show session variables G;

show variablesG;

3.全局变量
313个
在服务启动期间有效 ,即使退出登录,再重新登录,全局变量的设置仍旧有效
除非停止mysqL服务,重新开启服务,全局变量就失效了

查看所有的全局变量
show global variables G;


注意:有一部分全局变量和会话变量重名,但是作用域不同的。

存储过程 

dos命令下

1.在存储过程pro_test1中,声明3个局部变量,自带默认值的,可以用set/select给赋值。 

1.局部变量: java方法中的局部变量一样,作用范围最小的
只在定义变量的函数体内,语句块内,存储过程中有效,超出作用域不可见
delimiter && create procedure pro_test1() begin declare num1 int default 10; declare num2 int; declare num3 int; set num2=30; select 30 into num3; select num1,num2,num3; end &&
delimiter ;

调用存储过程
call pro_test1();


2.存储过程传入的参数 ,需要指定类型 ,参数同样是局部变量 create procedure
存储过程名(in 输入的参数, out 输出的参数, inout 参数可以输入也可以输出)

2.用户变量
:在整个连接期间有效,退出重新登录,失效了 
以 "@"开头

 in---输入

out---输出

delimiter &&
create procedure pro_test2(in num1 int,in num2 int)
begin
set num1=20;
select 30 into num2;
select num1,num2;
end &&
delimiter ;

调用存储过程
set @a=1;
set @b=2
call pro_test2(@a,@b)

3.
局部变量可以用sql语句的查询结果赋值 ,也可以将局部变量用到 insert select ,update及delete语句中

创建存储过程命名为add ,接收输入2个参数 ,在begin-end代码块内部声明变量c ,默认值为0 ,用于存储2个参数的和,并返回两个参数的和 

delimiter &&

create procedure pro_add(in num1 int,in num2 int,out result int)

begin

declare c int default 0;

set c = num1+num2;

set result = c;

end &&

delimiter ;

调用

set @a=15,@b=13,@c=0;

call pro_add(@a,@b,@c);

select @c;



分支结构&循环结构

接收一个考试分数,如果分数>90分,输出优秀,>80分,输出良好,>70,输出中等,>60,输出及格,其他情况输出再接再厉。  

---------------------1---------------------------------
delimiter &&
create procedure pro_ceshi6(in num1 int)
begin
declare result varchar(10) default '';
if num1>=90 then set result='优秀';
elseif num1>=80 then set result='良好';
elseif num1>=70 then set result='及格';
else set result='再接再厉';
end if;
select result;
end &&
delimiter ;

--------------------------2------------------------------
delimiter &&
create procedure pro_ceshi(in num1 int)
begin
declare result varchar(10) default ' ';
case truncate(num1/10,0)
when 10 then set result='优秀';
when 9 then set result='优秀';
when 8 then set result='良好';
else set result='再接再厉';
end case ;
select result;
end &&
delimiter ;


----------计算0-num之和   ,num你输入   while , repeat,loop---------
 leave :退出循环 ,break  
 iterate:结束本次循环,继续下一次循环,continue  
mark:自定义的循环标签名,可以任意取名,也可以省略

---while---
delimiter &&
create procedure pro_ceshi(in num int)
begin
declare sum int default 0l;
declare i int default 0;

mark:while i<=num do
set sum=sum+i;
set i=i+1;
end while mark;

select sum;
end &&
delimiter ;

---loop---
delimiter &&
create procedure pro_ceshi(in num int)
begin
declare sum int default 0;
declare i int default 0;

mark:loop
set sum=sum+i;
set i=i+1;
if i>sum then 
leave mark;
end if ;
end loop.mark;
select sum;
end &&
delimiter ;

----repeat---
delimiter &&
create procedure pro_ceshi(in num int)
begin 
declare sum int default 0;
declare i int default 0;
mark:repeat
set sum=sum+i;
set i=i+1;
until i>num
end repeat mark;
select sum;
end &&
delimiter ;


 索引

索引是建在 列上 ,对于数据量大的表来说,设置了索引的列,查询速度会提高。

更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度。在没有索引的情况下,数据库会遍历全部数据后选择符合条件的;而有了相应的索引之后,数据库会直接在索引中查找符合条件的选项

索引的分类:

  • 单列索引
    •   A.普通索引
    •   B.唯一索引
    •   C.主键索引
    • 以上设置主键索引的3种写法,primary key,不能换成primary index
      普通索引中的key 可以换成index
      唯一索引中的key 可以换成index

  • 组合索引
    •    index  multi_idx(t_personId,t_personName,t_age)
      • or  key  multi_idx(t_personId,t_personName,t_age)
      • 查询时是否带了组合索引里的第1列 t_personId
      • 遵循最左前缀的时候,字段的顺序可以调整的,但是一定要包含组合索引里的第1列

  • 全文索引
    •   只有MyIsAM引擎支持全文索引,其他的存储引擎都不支持。
    •  fulltext index fullTxt_info(t_personInfo) 
    • or  key index fullTxt_info(t_personInfo)
  • 空间索引
    •   存储引擎(表类型)必须是MyIsAM ,设置空间索引的列的类型只能选用geometry 
      • spatial index spat_Idx(t_personId)  or   spatial key spat_Idx(t_personId)

查询是否使用了索引   explain

explain  select * from t_person3 where   t_age=10G;

原文地址:https://www.cnblogs.com/MRCH/p/11963205.html