分类:
My SQL
语法:delimiter// create procedure 存储过程名称(参数) begin 相关代码....... end// delimiter; 调用: call 存储过程名称(参数) 注意:My SQL5.0以前并不支持存储过程. My SQL存储过程特点: (1).不能有select语句 (2).不能使用return(return只能在函数中调用). 示例: drop database if exists BookDB; create database BookDB; -- 获取权限 alter database BookDB CHARACTER set utf8; -- 连接库 use BookDB; -- 图书类型表 drop table if exists BookType; create table BookType ( type_id int not null primary key, type_name varchar(20) not null, type_remark VARCHAR(50) null DEFAULT'略' ); insert into BookType values(1001,'历史经典',DEFAULT); insert into BookType values(1002,'教育类文学',DEFAULT); insert into BookType values(1003,'小说类',DEFAULT); insert into BookType values(1004,'儿童文学',DEFAULT); insert into BookType values(1005,'自然科学',DEFAULT); -- 图书信息表 drop table if exists Book; create table Book ( book_id int not null primary key, book_name varchar(20) not null, book_author VARCHAR(20) not null, book_date VARCHAR(20) not null, book_type int not null, book_remark VARCHAR(50) null default'略', price decimal(6,2) not null, book_num int not null, foreign key(book_type) references BookType(type_id) ); insert into Book values(2001,'西游记','吴承恩','1986-1-2',1001,default,55,10); insert into Book values(2002,'红楼梦','曹雪芹','1986-1-2',1001,default,77,12); insert into Book values(2003,'三国演义','罗贯中','1986-1-2',1001,default,60,20); insert into Book values(2004,'水浒传','施耐庵','1986-1-2',1001,default,40,30); insert into Book values(2005,'满分作文','不详','2016-1-2',1002,default,20,20); insert into Book values(2006,'单词速记','不详','2017-1-2',1002,default,10,10); insert into Book values(2007,'王后雄文学','王后雄','2016-5-3',1002,default,25,20); insert into Book values(2008,'全文解析','不详','2018-1-2',1002,default,30,25); insert into Book values(2009,'斗破苍穹','唐家三少','2005-12-2',1003,default,50,50); insert into Book values(2010,'斗罗大陆','唐家三少','2006-10-2',1003,default,30,10); insert into Book values(2011,'圣墟','唐家三少','2007-1-2',1003,default,10,20); insert into Book values(2012,'星辰变','辰东','2008-1-2',1003,default,12,15); insert into Book values(2013,'安徒生童话','安徒生','2003-1-2',1004,default,10,20); insert into Book values(2014,'格林童话','格林','2004-1-2',1004,default,15,30); insert into Book values(2015,'海的女儿','安徒生','2001-6-26',1004,default,20,10); insert into Book values(2016,'睡美人','川端康成','2002-7-8',1004,default,30,10); insert into Book values(2017,'皇帝的新装','安徒生','1999-1-2',1004,default,50,40); insert into Book values(2018,'地理大百科','不详','2005-1-2',1005,default,20,30); insert into Book values(2019,'动物大百科','不详','2006-6-21',1005,default,30,20); insert into Book values(2020,'人与自然','不详','1986-4-2',1004,default,20,20); SELECT*FROM BookType; SELECT*FROM Book; desc book; /*************************************************************************************/ #创建数据库 drop database if exists studb; create database studb; #调协编号 alter database studb CHARACTER set utf8; #切换 use studb; #创建表一:学生信息表 drop table if exists stuinfo; create table stuinfo ( stuno varchar(10) not null, stuname varchar(10) not null, stusex enum('男','女') not null, stuphone char(11) not null, stuaddrss varchar(200) null ); #创建表二: drop table if exists courseinfo; create table courseinfo ( cid int primary key auto_increment, cname varchar(20) not null ); #创建表三:成绩表 drop table if exists scoreinfo; create table scoreinfo ( sid int primary key auto_increment, stuno varchar(10) not NULL, -- 外键,引用stuinfo(stuno) cid int not null, -- 外键,引用courseinfo(cid) bishi int not null, jishi int not null ); # 为stuinfo 表添加主键列stuno /* 主键特点: 一个表中可以没有主键,但如果有最多一个 联合主键:由多个列构成的主键 */ alter table stuinfo add constraint PK_stuinfo_stuno primary key(stuno); # 为stuinfo表添加默认约束stuaddress alter table stuinfo alter column stuaddrss set default '地址不详'; #为course表添加唯一约束cname alter table courseinfo add constraint UQ_courseinfo_cname unique(cname); #为scoreinfo表添加外键 #1.stuno去引用stuinfo表的stuno alter table scoreinfo add constraint FK1 foreign key(stuno) references stuinfo(stuno); #2.cid去引用courseinfo表的cid alter table scoreinfo add constraint Fk2 foreign key(cid) references courseinfo(cid); desc stuinfo; desc courseinfo; desc scoreinfo; select * from stuinfo; select * from courseinfo; select * from scoreinfo; insert into stuinfo values('T468001','小黑黑','男','13112345678',default); insert into stuinfo values('T468002','小花花',2,'15112345678',default); insert into stuinfo values('T468003','小黑黑',1,'15245869365','北京'); insert into stuinfo values('T468004','张秋丽',1,'13657859654','上海'); insert into stuinfo values('T468005','韦小宝',2,'15145263542','河南'); insert into stuinfo values('T468006','张三个',2,'18745263596','成都'); insert into courseinfo values(null,'C#'); insert into courseinfo values(null,'SQLSERVER'); insert into courseinfo values(null,'JAVASCRIPT'); insert into courseinfo values(null,'ASP.NET'); insert into scoreinfo values(null,'T468001',1, 86,82 ); insert into scoreinfo values(null,'T468001',2,77,67); insert into scoreinfo values(null,'T468001',3,85,99); insert into scoreinfo values(null,'T468002',2,36,52); insert into scoreinfo values(null,'T468002',3,45,22); insert into scoreinfo values(null,'T468002',4,98,90); insert into scoreinfo values(null,'T468005',1,66,60); insert into scoreinfo values(null,'T468005',2,78,52); insert into scoreinfo values(null,'T468005',3,99,56); insert into scoreinfo values(null,'T468005',4,25,52); select * from stuinfo; select * from courseinfo; select * from scoreinfo; #定义存储过程,根据给入学号,获取该学员及格的科目数量(带输入和输出参)studb drop procedure if exists proc_getsum; delimiter // create procedure proc_getsum(in sno varchar(10),out count int) BEGIN set count = (select count(*) from scoreinfo where stuno = (select stuno from stuinfo where stuno=sno) and bishi >=60 and jishi >=60); end// delimiter ; -- 调用 call proc_getsum('T468001',@sum1); select @sum1 as '及格科数';
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术