数据库六 视图和索引

一 、视图 view

创建

create view v_noname as select s_no,s_name from student;

使用

select * from v_noname;

select s_name from v_noname;

修改

alter view v_noname as select s_name ,s_classno from student;

删除

drop view v_noname;

二、索引 index

1、普通索引       允许定义索引的字段重复和空值

create index 索引名 on 表明(属性1,....属性n);

     修改:

alter table 表名 add index 索引名 (属性1, .... 属性n);

    删除

drop index  索引名 on 表名;

查看表中的索引:

show index from 表名;

show keys from 表名;

2、主键索引  创建主键时会自动创建主键索引 无法手动创建

                       创建外键约束自动创建外键索引 无法手动创建

3、唯一索引 

      创建唯一约束自动创建唯一索引 

      create unique index ui_name on student(s_name);

4、全文索引  用于查询文本中的关键字 只有MyISAM引擎支持

     只支持char varchar text类型

alter table student add fulltext(s_name);

存储过程和函数

1、存储过程 procedure

无参存储过程

创建:

create procedure 存储过程名()

begin

函数体;

end;

调用:

call 存储过程名();

删除:

drop procedure 存储过程名;

查看存储过程

show create procedure 存储过程名;

修改

alter procedure 存储过程名;

1、代参存储过程

create procedure 存储过程名(

in[out,inout]  参数1 类型,

in[out,inout]  参数2 类型,

...

in[out,inout]  参数n 类型

)

 create procedure getstu(in info int)
    -> begin
    -> select * from student where s_no=info;
    -> end;
create procedure mmfun(in s_name varchar(20))
    -> begin
    -> select * from student where s_name=s_name;
    -> end;

 create procedure putinfo(in stu_no int, in stu_name varchar(20))
    -> begin
    -> insert into student values(stu_no, stu_name);
    -> end;
Query OK, 0 rows affected

mysql> call putinfo(5,"张高");
Query OK, 1 row affected

函数

1、局部变量 declare a int [default 0]

mysql> create procedure getsum()
    -> begin
    -> declare a int;
    -> declare b int;
    -> declare c int;
    -> set a = 1;
    -> set b = 2;
    -> set c = a + b;
    -> select c as "a+b的和";
    -> end;

Query OK, 0 rows affected

mysql> call getsum();
+---------+
| a+b的和 |
+---------+
|       3 |
+---------+

2、用户变量 @变量名    作用域相当于全局变量

set @变量名=变量值

set @x=10;    复值决定类型
Query OK, 0 rows affected

mysql> select @x;
+-----+
| @x |
+-----+
|  10 |
+-----+

2、全局变量\会话变量  是系统自定义的,用于存储数据库中的一些系统参数  我们一般不用

查看全局变量\会话变量

show global variables;

show session variables;

自定义函数

create funcation 函数名(参数1 类型,...)

returns 返回值类型;

begin

return(....)

end;

create function mmm(a int,b int)
    -> returns int
    -> begin 
    -> return(a+b);
    -> end;

报1418错误
执行:SET GLOBAL log_bin_trust_function_creators = 1;

create function mmm(a int,b int)
    -> returns int
    -> return(a+b);
Query OK, 0 rows affected

遇到reuturn时及结束

查询:

select mmm(10,20);

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值