mySQL教程 第2章 准备学习用到的表和数据

第2章 准备学习用到的表和数据

将以下语句拷贝到SQL manager,分部执行

--创建学生表

CREATE TABLE `TStudent` (

`StudentID` varchar(15) NOT NULL,

`Sname` varchar(10) DEFAULT NULL,

`sex` char(1) DEFAULT NULL,

`cardID` varchar(20) DEFAULT NULL,

`Birthday` datetime DEFAULT NULL,

`Email` varchar(40) DEFAULT NULL,

`Class` varchar(20) DEFAULT NULL,

`enterTime` datetime DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--创建课程表

create table TSubject

(

subJectID nvarchar(10),

subJectName nvarchar(30),

BookName nvarchar(30),

Publisher nvarchar(20)

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

--创建分数表

create table TScore

(

StudentID nvarchar(15),

subJectID nvarchar(10),

mark decimal

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

--插入课程信息

insert into TSubject values ('0001','计算机网络','奠基计算机网络','清华出版社');

insert into TSubject values ('0002','数据结构','大话数据结构','人邮出版社');

insert into TSubject values ('0003','JAVA开发','JAVA企业级开发','人邮出版社')

--创建函数 该函数能够产生学生姓名

create function create_name()

RETURNS varchar(3)

begin

DECLARE LN VARCHAR(300);

DECLARE MN VARCHAR(200);

DECLARE FN VARCHAR(200);

DECLARE LN_N INT;

DECLARE MN_N INT;

DECLARE FN_N INT;

SET LN='李王张刘陈杨黄赵周吴徐孙朱马胡郭林何高梁郑罗宋谢唐韩曹许邓萧冯曾程蔡彭潘袁于董余苏叶吕魏蒋田杜丁沈姜范江傅钟卢汪戴崔任陆廖姚方金邱夏谭韦贾邹石熊孟秦阎薛侯雷白龙段郝孔邵史毛常万顾赖武康贺严尹钱施牛洪龚';

SET MN='伟刚勇春菊毅俊峰强军平保东文辉力明永健世广志瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥新利筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒义兴良海山仁波宁贵福生龙元全国胜学祥亮政谦亨奇固之岚苑富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪清飞彬娜静淑惠珠翠雅芝妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦素伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘';

SET FN='伟刚勇毅俊云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧才发武丽琳轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德河哲江超浩璐娅琦晶裕华慧巧美婕馨影荔枝思心邦承乐绍功松善厚庆磊民友玉萍红娥玲芬芳燕彩兰凤洁梅秀娟英行时泰盛雄琛钧冠策腾楠榕风航弘峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘';

SET LN_N=CHAR_LENGTH(LN);

SET MN_N=CHAR_LENGTH(MN);

SET FN_N=CHAR_LENGTH(FN);

return Concat(substring(LN,ceil(rand()*LN_N),1),substring(MN,ceil(rand()*MN_N),1),substring(FN,ceil(rand()*FN_N),1));

end

--创建添加学生的存储过程

create procedure addStudent(in num int)

begin

declare i int;

set i=1;

delete from TStudent;

while num>=i do

insert TStudent values (

LPAD(convert(i,char(5)),5,'0'),

create_name(),

if(ceil(rand()*10)%2=0,'男','女'),

RPAD(convert(ceil(rand()*1000000000000000000),char(18)),18,'0'),

Concat('198',convert(ceil(rand()*10),char(1)),'-',LPAD(convert(ceil(rand()*12),char(2)),2,'0'),'-',LPAD(convert(ceil(rand()*30),char(2)),2,'0')),

Concat(PINYIN(sname),'@hotmail.com'),

case ceil(rand()*3) when 1 then '网络与网站开发' when 2 then 'JAVA' ELSE 'NET' END,

NOW());

set i=i+1;

end while;

select * from TStudent;

end

--创建汉字转拼音的函数,用来产生用户的邮箱

--创建汉字转拼音的函数使用的表

CREATE TABLE `pinyin` (

`letter` char(1) NOT NULL,

`chinese` char(1) NOT NULL,

PRIMARY KEY (`letter`)

) ENGINE=MyISAM DEFAULT CHARSET=gbk;

--插入数据

INSERT INTO `pinyin` VALUES ('A','�'),('B','簿'),('C','�e'),('D','�z'),('E','��'),('F','�v'),('G','�B'),

('H','��'),('J','�h'),('K','�i'),('L','�w'),('M','��'),('N','��'),('O','�a'),('P','曝'),('Q','��'),('R','�U'),

('S','�R'),('T','�X'),('W','�F'),('X','�R'),('Y','�'),('Z','��');

--创建汉字转拼音的函数

CREATE FUNCTION PINYIN(str CHAR(255))

RETURNS char(255)

BEGIN

DECLARE hexCode char(4);

DECLARE pinyin varchar(255);

DECLARE firstChar char(1);

DECLARE aChar char(1);

DECLARE pos int;

DECLARE strLength int;

SET pinyin = '';

SET strLength = CHAR_LENGTH(LTRIM(RTRIM(str)));

SET pos = 1;

SET @str = (CONVERT(str USING gbk));

WHILE pos <= strLength DO

SET @aChar = SUBSTRING(@str,pos,1);

SET hexCode = HEX(@aChar);

IF hexCode >= "8140" AND hexCode <= "FEA0" THEN

SELECT letter into firstChar

FROM pinyin

WHERE chinese >= @aChar

LIMIT 1;

ELSE

SET firstChar = @aChar;

END IF;

SET pinyin = CONCAT(pinyin,firstChar);

SET pos = pos + 1;

END WHILE;

RETURN UPPER(pinyin);

END

--创建插入学生成绩的存储过程 学生成绩在50-100分之间

create procedure fillSore()

begin

DECLARE St_Num INT;

DECLARE Sb_Num INT;

DECLARE i1 INT;

DECLARE i2 INT;

set i1=1;

set i2=1;

delete from TScore;

select count(*) into St_Num from TStudent;

select count(*) into Sb_Num from TSubject;

while St_Num>=i1 do

set i2=1;

while Sb_Num>=i2 do

insert TScore values

(LPAD(convert(i1,char(5)),5,'0'),LPAD(convert(i2,char(4)),4,'0'),ceil(50+rand()*50));

set i2=i2+1;

END WHILE;

set i1=i1+1;

END WHILE;

end

--插入1000个学生

call addStudent(1000)

select * from TStudent

--插入成绩

call fillSore()

--查看三张表连接的结果

select a.*,b.*,c.* from TStudent a join TScore b on a.studentid=b.studentid join TSubject c on b.subjectid=c.subjectid limit 50

广告

titel263

12353

system63

system373

你可能感兴趣的:(manager,学习,email,表)