創(chuàng)建數(shù)據(jù)庫(kù),指定數(shù)據(jù)庫(kù)的默認(rèn)字符集為utf8。
create database schoolDB default character set utf8;
連接數(shù)據(jù)庫(kù),客戶端必須選擇UTF8字符集。
數(shù)據(jù)庫(kù)中的三張表分別為學(xué)生表(student)、課程表(TSubject)、分?jǐn)?shù)表(TScore)。
創(chuàng)新互聯(lián)公司專注于布爾津企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城系統(tǒng)網(wǎng)站開發(fā)。布爾津網(wǎng)站建設(shè)公司,為布爾津等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
CREATE TABLE `TStudent` (
`StudentID` varchar(15) NOT NULL,
`Sname` varchar(10) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`cardID` varchar(20) DEFAULT NULL,
`Birthday` date 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','計(jì)算機(jī)網(wǎng)絡(luò)','奠基計(jì)算機(jī)網(wǎng)絡(luò)','清華出版社');
insert into TSubject values ('0002','數(shù)據(jù)結(jié)構(gòu)','大話數(shù)據(jù)結(jié)構(gòu)','人郵出版社');
insert into TSubject values ('0003','JAVA開發(fā)','JAVA企業(yè)級(jí)開發(fā)','人郵出版社');
create function CreateName()
RETURNS varchar(3)
begin
DECLARE LN VARCHAR(300);
DECLARE MN VARCHAR(500);
DECLARE FN VARCHAR(500);
DECLARE LN_N INT;
DECLARE MN_N INT;
DECLARE FN_N INT;
SET LN='李王張劉陳楊黃趙周吳徐孫朱馬胡郭林何高梁鄭羅宋謝唐韓曹許鄧蕭?cǎi)T曾程蔡彭潘袁于董余蘇葉呂魏蔣田杜丁沈姜范江傅鐘盧汪戴崔任陸廖姚方金邱夏譚韋賈鄒石熊孟秦閻薛侯雷白龍段郝孔邵史毛常萬顧賴武康賀嚴(yán)尹錢施牛洪龔';
SET MN='偉剛勇春菊毅俊峰強(qiáng)軍平保東文輝力明永健世廣志瑗琰韻融園藝詠卿聰瀾純毓悅昭冰爽琬茗羽希寧欣飄育瀅馥新利筠柔竹靄凝曉歡霄楓蕓菲寒伊亞宜可姬舒義興良海山仁波寧貴福生龍?jiān)珖?guó)勝學(xué)祥亮政謙亨奇固之嵐苑富順信子杰濤昌成康星光天達(dá)安巖中茂進(jìn)林有堅(jiān)和彪博誠(chéng)先敬震振壯會(huì)思群豪清飛彬娜靜淑惠珠翠雅芝妍茜秋珊莎錦黛青倩婷姣婉嫻瑾穎露瑤怡嬋雁蓓紈儀荷丹蓉眉君琴蕊薇菁?jí)羲貍傆乱憧》鍙?qiáng)軍平保東文輝力明永健世廣志義興良海山仁波寧貴福生龍?jiān)珖?guó)勝學(xué)祥才發(fā)武新利清飛彬富順信子杰濤昌成康星光天達(dá)安巖中茂進(jìn)林有堅(jiān)和彪博誠(chéng)先敬震振壯會(huì)思群豪心邦承樂紹功松善厚慶磊民友裕河哲江超浩亮政謙亨奇固之輪翰朗伯宏言若鳴朋斌梁棟維啟克倫翔旭鵬澤晨辰士以建家致樹炎德行時(shí)泰盛雄琛鈞冠策騰楠榕風(fēng)航弘';
SET FN='偉剛勇毅俊云蓮真環(huán)雪榮愛妹霞香月鶯媛艷瑞凡佳嘉瓊勤珍貞莉桂娣葉璧才發(fā)武麗琳輪翰朗伯宏言若鳴朋斌梁棟維啟克倫翔旭鵬澤晨辰士以建家致樹炎德河哲江超浩璐婭琦晶裕華慧巧美婕馨影荔枝思心邦承樂紹功松善厚慶磊民友玉萍紅娥玲芬芳燕彩蘭鳳潔梅秀娟英行時(shí)泰盛雄琛鈞冠策騰楠榕風(fēng)航弘峰強(qiáng)軍平保東文輝力明永健世廣志義興良海山仁波寧貴福生龍?jiān)珖?guó)勝學(xué)祥才發(fā)武新利清飛彬富順信子杰濤昌成康星光天達(dá)安巖中茂進(jìn)林有堅(jiān)和彪博誠(chéng)先敬震振壯會(huì)思群豪心邦承樂紹功松善厚慶磊民友裕河哲江超浩亮政謙亨奇固之輪翰朗伯宏言若鳴朋斌梁棟維啟克倫翔旭鵬澤晨辰士以建家致樹炎德行時(shí)泰盛雄琛鈞冠策騰楠榕風(fēng)航弘';
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'),
CreateName(),
if(ceil(rand()*10)%2=0,'男','女'),
RPAD(convert(ceil(rand()*1000000000000000000),char(18)),18,'0'),
Concat(convert(ceil(rand()*10)+1980,char(4)),'-',LPAD(convert(ceil(rand()*12),
char(2)),2,'0'),'-',LPAD(convert(ceil(rand()*28),char(2)),2,'0')),
Concat(PINYIN(sname),'@hotmail.com'),
case ceil(rand()*3) when 1 then '網(wǎng)絡(luò)與網(wǎng)站開發(fā)' when 2 then 'JAVA' ELSE 'NET' END,
NOW()
);
set i=i+1;
end while;
select * from TStudent;
End
插入1000個(gè)學(xué)生記錄。
call addStudent(1000);
--創(chuàng)建漢字轉(zhuǎn)拼音的函數(shù)使用的表
CREATE TABLE `pinyin` (
`letter` char(1) NOT NULL,
`chinese` char(1) NOT NULL,
PRIMARY KEY (`letter`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
--插入數(shù)據(jù)
INSERT INTO `pinyin` VALUES ('A','驁'),('B','簿'),('C','錯(cuò)'),('D','鵽'),('E','樲'),('F','鰒'),('G','腂'),('H','夻'),('J','攈'),('K','穒'),('L','鱳'),('M','旀'),('N','桛'),('O','漚'),('P','曝'),('Q','囕'),('R','鶸'),('S','蜶'),('T','籜'),('W','鶩'),('X','鑂'),('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
create procedure fillScore()
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
插入學(xué)生成績(jī)
call fillScore();
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;
本文標(biāo)題:MySQL數(shù)據(jù)庫(kù)基礎(chǔ)(四)——MySQL數(shù)據(jù)庫(kù)創(chuàng)建實(shí)例
URL分享:http://www.rwnh.cn/article6/gcggog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、微信小程序、關(guān)鍵詞優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、ChatGPT、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)