/*********************字符集(character)与校对规则(collation)****************************/
说明:字符集用来定义MySQL存储字符串的方式,校对规则用来定义比较字符串的方式;MySQL支持不同服务器,不同数据库,不同的表,不同的字段指定不同的字符集;一种字符集可以对应多种校对规则
创建对象时指定字符集,校对规则可以查看help craete object;
查看所有可用字符集:
show character set;
desc information_schema.character_sets
查看系统的校对规则:
show collation like ‘****‘
desc information_schema.collations;
服务器字符集,校对规则(没有指定字符集则使用默认字符集,没有指定校对规则则使用字符集默认的校对规则)
查询服务器字符集:show variables like ‘character_set_server‘;
查询服务器校对规则:show variables like ‘collation_server‘;
数据库字符集,校对规则
查询数据库字符集:show variables like ‘character_set_database‘;
查询数据库校对规则:show variables like ‘collation_database‘;
表字符集,校对规则
show create table tablename \G;
/**********************索引*******************************/
创建索引:create [unique|fulltext|spatial]index index_name
[using index_type]
on table_name(index_col_name)
index_col_name:
col_name[(length)][ASC|DESC]
删除索引:drop index index_name on table_name;
/************************视图*******************************/
视图:一种虚拟存在的表
创建视图:
create [or replace] [algorithm={undefined|merge|temptable}]
view view_name[(column_list)]
as select_statement
[with[cascaded|local]check option]
修改示图:
alter [algorithm={undefined|merge|temptable}]
view view_name[(column_list)]
as select_statement
[with[cascaded|local]check option]
删除视图:
drop view[if exit] view_name[,view_name]...[restrict|caseade]
查看视图:
show tables;
show table status like ‘****‘;
show create view view_name;
desc information_schema.views;
/************************存储过程和函数****************************/
存储过程和函数:一段SQL语句的集合,区别在于函数必须有返回值,参数只能为IN类型;而存储过程没有返回值,参数可以是IN,OUT,INOUT
创建:
create procedure sp_name([proc_parameter][....])
[characteristic...]routine_body
create function sp_name([func_parameter][....])
returns type
[characteristic...]routine_body
proc_parameter:
[in|out|inout]param_name type
func_parameter:
para_name type
type:
any valid mysql data type
修改:
alter {procedure|function} sp_name [characteristic...]
调用:
call sp_name([parameter][....])
删除:
drop {procedure|function}[if exit] sp_name
查看:
show {procedure|function} status [like ‘***‘]
show create {procedure|function} sp_name
/******************变量的使用***********************/
定义:
declare var_name[,...] type [default value];
赋值:
set var_name=exp[,var_name=epx]...
select col_name[,...] into var_name[,...]table_expr //查询赋值
/********************定义条件和处理**********************/
定义条件:
declare condition_name condition for condition_value;
condition_value:
sqlstate[value]sqlstate_value|mysql_error_code
定义条件处理:
declare handler_type handler for condition_value[,...] sp_statement;
hadler_type:
continue|EXIT|UNDO
condition_value:
/*****************光标的使用*************************/
申明光标:
declare cursor_name cursor for select_name
打开光标:
open cuesor_name
操作光标:
fetch cuesor_name into var_value [,var_name]...
关闭光标:
close cursor_name
/******************流程控制***********************/
if search_condition then statement_list
[elseif search_condition then statement_list]...
[else statement_list]
endif
/*****************/
case case_value
when when_value then statement_list
[when search_condition then statement_list]
[else statement_list]
end case
/*****************/
case
when search_condition then statement_list
[when search_condition then statement_list]
[else sattement_list]
end case
/*****************/
[begin_label:]loop
statement_list
end loop[end_label]
/*****************/
退出循环leave/iterate
/*****************/
[begin_label:]repeat
statement_list
until search_condition
end repeat[end_label]
/*****************/
[begin_label:]while search_condition do
statement_list
end while[end_label]
/******************事件调度器*****************/
说明:按自定义的时间周期触发某种操作
create event event_name
on schedule at current_timestamp+interval 1 hour
do
/**************触发器**********************/
创建:
create trigger trigger_name trigger_event
on table_name for each trigger_stmt
删除:
drop trigger[schema_name]trigger_name
查看:
show triggers \G;