一 、视图 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)) mysql> call putinfo(5,"张高"); |
函数
1、局部变量 declare a int [default 0] |
mysql> create procedure getsum() mysql> call getsum(); |
2、用户变量 @变量名 作用域相当于全局变量 set @变量名=变量值 |
set @x=10; 复值决定类型 mysql> select @x; |
2、全局变量\会话变量 是系统自定义的,用于存储数据库中的一些系统参数 我们一般不用 |
查看全局变量\会话变量 show global variables; show session variables; |
自定义函数
create funcation 函数名(参数1 类型,...) returns 返回值类型; begin return(....) end; |
create function mmm(a int,b int) |
报1418错误 |
执行:SET GLOBAL log_bin_trust_function_creators = 1; |
create function mmm(a int,b int) 遇到reuturn时及结束 |
查询: select mmm(10,20); |