MySQL(阶段二)

入门的第二阶段掌握下面这几点

一、视图

二、索引

三、自定义函数

四、存储过程

五、触发器

六、数据库权限与备份

----------------------------------------------------------------------------------------------------------------------------------------------------------

一、视图 

·创建视图的语法格式 

create  

view  视图名 [(列名1,列名2...)]

as  复杂查询|简单查询

[with  检查约束];

 EX:

create   

view  'student'.'view_student'

as 

(

select  student.id,student.name,subject.name,subject.id

from  student,subject

where student.subjectId = subject.id

);


·修改视图语法格式 

create  [or  replace]

view  视图名 [(列名1,列名2...)]

as  各种查询语句

[with  约束条件];

PS:or  replace表示创建的新视图覆盖以前的同名视图。

EX:

create  or  replace

view  'test'.'view_student'

as

(

select  sc.id,sc.score,st.name,st.age

from sc,st

where sc.id = st.id and st.age = 18

);


·删除视图语法格式 

drop  view  [if  exists]

视图名 [,视图名*]

PS:if  exists用来防止删除时因为没有该视图出现的错误。

EX:

drop  view  if  exists

student_view;


·查看视图语法格式 

describe  视图名;

----------------------------------------------------------------------------------------------------------------------------------------------------------

二、索引 

·创建索引语法格式 

create  index 索引名

on  表名(列名+);

EX:

create  index  idx_name

on  student(name);


·创建唯一索引语法格式 

create  unique  index  索引名

on  表名(列名+);

EX

create  unique  index  idx_name

on  student(name,age,sex);


·查看索引语法格式 

show  index  from  表名;


·删除索引语法格式 

drop  index  索引名  on  表名;

----------------------------------------------------------------------------------------------------------------------------------------------------------

一下这些东西在三、四、五中都是适用的:

自定义变量:declare  变量名  数据类型;

赋值:set  变量名 = 值;

不确定参数:@[a-z](可以吧它放在参数列表中,不过要实现在控制台中用set  @a = 1;这样的语句为它赋值)

条件判断语句:if(判别式)then  ... ... end  if;

循环语句:while(判别式)do ... ... end  while;

()是自由加的;

----------------------------------------------------------------------------------------------------------------------------------------------------------

三、自定义函数 

·创建自定义函数语法格式 

create  function  函数名 ([参数*])

returns  数据类型

begin

...  ...

return  xxx;

end

EX:

create  function  'student'.'myfunction'(id  varchar(10),str1 varchar(10),str2  varchar(10))

returns  varchar(5)

begin 

declare  int_1  int;

declare  int_2  int;

if(id  is  not  null) then

select  score  into  int_1

from  scoreinfo

where  student_id = id  and  subject_id = str1;


select  score  into  int_2

from  scoreinfo  

where  student_id = id  and  subject_id = str2;


end  if;

return sort(int_1 + int_2);

end

PS:需要注意开始与结束的对应关系。即颜色部分的对应。


·删除函数语法格式 

drop  function  函数名;


·调用自定义函数 

select  函数名(参数*);

----------------------------------------------------------------------------------------------------------------------------------------------------------

四、存储过程 

·创建存储过程语法格式 

create  procedure  存储过程名([in | out | inout  参数*]) 

body

EX1:

create  procedure  'test'.'student_pro'(in  param  int)

begin

if(param  is  not  null) then

update  student  set  id = 1  where  name = '小明';

end  if;

end

//因为是in参数,所以执行存储过程是要传入具体值10,如下

call  student_pro(10);


EX2:

create  procedure  'test'.'student_pro'(out  param  int)

begin

select  count(*)  into  param  from  student;

end

//因为是out参数,所以要传入无值参数@x,如下

call  student_pro(@x);

//查询out参数最终的结果

select  @x;


EX3:

//当参数为inout参数时,既要在call存储过程之前,执行set  @x = 1;

又可以在call完之后select @x;来看看这个变量的修改结果是什么。


·修改存储过程

EX:

drop  procedure  if  exists  'student_pro'

create  procedure  'student_pro'(out  param  int)

begin

...  ... 

end


·删除存储过程语法格式 

drop  procedure if  exists  存储过程名;

----------------------------------------------------------------------------------------------------------------------------------------------------------

五、触发器 

·创建触发器语法 

create  trigger  触发器名

before | after  触发事件  on  表名

for  each  now //对于该表的每一行

begin

...  ... //与写存储过程一样

end


·删除触发器 

drop  trigger  触发器名;


·修改触发器(先删除在创建)

drop  trigger  旧触发器名

和创建触发器一样

----------------------------------------------------------------------------------------------------------------------------------------------------------

六、数据库权限与备份 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值