-- 索引 index
create index idx_sname on student( sname(4));
alter table teacher add index idx_tname(tname);
drop index idx_sname on student;
-- 檢視 view
create view v_stu
asselect sname,degree from student s, score sc
where s.sno=sc.sno;
select * from v_stu;
-- 儲存過程 procedure
-- 建立 insert procedure
drop procedure if exists `p_insert_student`;
create procedure p_insert_student(
in xueh varchar(3),
in xingm varchar(4),
in xingb varchar(2),
in shengr datetime,
in banj varchar(5)
begin
set @xueh = xueh;
set @xingm = xingm;
set @xingb = xingb;
set @shengr = shengr;
set @banj = banj;
set @insertsql = concat('insert into student values(?,?,?,?,?)');
prepare stmtinsert from @insertsql;
execute stmtinsert using @xueh,@xingm,@xingb,@shengr,@banj;
deallocate prepare stmtinsert;
end;
-- 儲存過程呼叫
call p_insert_student(412,'黃一','女','1990-12-01 10:20:01','304');
call p_insert_student(413,'黃二','男','1992-12-01 10:20:01','304');
call p_insert_student(414,'黃三','男','1993-12-01 10:20:01','304');
call p_insert_student(416,'黃五','女','1992-02-01 10:20:01','304');
-- 結果驗證
select * from student;
-- 建立儲存過程 查詢
drop procedure if exists `pro_get_info`;
create procedure pro_get_info(
in ckh varchar(5)
begin
set @ckh=ckh;
set @selectsql = concat('select s.*,cno,degree from student s,score sc
where s.sno=sc.sno and cno=?');
prepare stmtselect from @selectsql;
execute stmtselect using @ckh;
deallocate prepare stmtselect;
end;
call pro_get_info('3-105');
call pro_get_info('3-245');
-- 觸發器 trigger
-- 建立 insert trigger
drop trigger if exists `t_insert_score`;
create trigger t_insert_score after insert on student for each row
begin
declare sno varchar(3) ;
declare cno varchar(35) ;
declare degree decimal(10) ;
insert into score values(111,'3-105','99');
end;
-- 結果驗證
select * from score;
# 批量造資料,插入資料庫
#建立測試表
drop table if exists test.test;
create table test.test(
id int(10) not null auto_increment,
a int(10) not null,
b int(10) not null,
c int(10) not null,
primary key (`id`)
)engine innodb default charset utf8 comment '測試表';
#清空資料
truncate table test.test;
#定義儲存過程
delimiter //
drop procedure if exists insert_test_val;
##num_limit 要插入資料的數量,rand_limit 最大隨機的數值
create procedure insert_test_val(
in num_limit int,
in rand_limit int)
begin
declare i int default 1;
declare a int default 1;
declare b int default 1;
declare c int default 1;
while i<=num_limit do
set a = floor(rand()*rand_limit);
set b = floor(rand()*rand_limit);
set c = floor(rand()*rand_limit);
insert into test.test values (null,a,b,c);
set i = i + 1;
end while;
end#呼叫儲存過程
call insert_test_val(1000,10);
drop procedure if exists mytest; --判斷程序是否存在,存在則刪除
create procedure mytest() --建立儲存過程
begin
declare i int;
declare j varchar(200);
set i = 2;
while i < 11 do --迴圈體
set j=concat('wwq',i); --拼接字串
insert into pub_user(`id`, `name`, `account`, `password`, `user_code`)
values (i, '王馨', j, 'e10adc3949ba59abbe56e057f20f883e', '');
insert into pub_user_post (`post_code`, `user_code`)
values (i, 'a685187d29af4ad793f2753dc17c1435');
values ('95ac32c8b9874b4085a01187c341067b', i, 'inspur-dzzw-tysp');
set i=i+1;
end while;
end --結束定義語句
call mytest(); --呼叫儲存過程
Mysq 儲存過程
drop procedure if exists xl 建立儲存過程 delimiter 轉譯字元 使用 代替 create procedure xl begin select from fd car report end delimiter 轉譯字元 使用 代替 call xl 呼叫儲存過程 de...
mysql 儲存過程例子
size large b 1 迴圈遍歷值 b size drop procedure if exists doit delimiter表示以 結束編譯 delimiter create procedure doit in parameter integer begin declare v1 int ...
mysql儲存過程小例子
drop procedure if exists customview logs pro delimiter create procedure customview logs pro in counts bigint begin declare i bigint default 1 declare ...