概述
小引
首先说明,这个笔者2年前学习SQL的遗漏下来的笔记,由于参加完腾讯的笔试,内容比较偏向数据机构和编译以及数据库,刚好要换台本本,心里不想把它弄死在硬盘里,觉得蛮好的,所以把它都分享了,主要第一部分简单介绍SQL语言,第二部分简单实践,纯属于简单类似的记录,通俗易懂,希望对学习数据库原理的同学有一定的帮助!
主要内容:
综述
简单实践
综述
大家都知道SQL是结构化查询语言,是关系数据库的标准语言,是一个综合的,功能极强的同时又简洁易学的,它集级数据查询(Data Quest),数据操纵(Data Manipulation),数据定义(Data Definition),数据控制(Data Control)于一体 即:
SQL语言包含4个部分:
※ 数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句
※ 数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句
※ 数据查询语言(DQL),例如:SELECT语句
※ 数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句
SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)
现在说说SQL的几个基本概念:
基本表:本身独立存在的一个表,一个关系就是对应一个基本表
内模式:存储文件的逻辑结构组成了关系数据库的内模式
视图:从一个或几个基本表导出的表,他是一个虚表
简单实践
建一个数据库,包含“学生表”,“教员表”,“任课表”,“专业表”,“课程表”,“学生个人情况表”,“成绩表”七张表
下面几个表是我们这次实践要用到的表: 用SQL语句建表:
建立一个数据库cc
create database cc;
use cc;
创建一个名为tb_student的学生表
tb_student的学生表
create table tb_student(
stunum char(7) primary key, --学号
stuname char(8) not null, --姓名
stusex char(2) check(stusex in('女','男')), --性别
stubirthday smalldatetime not null, --学生出生日期
stuspec char(6)not null, --专业
stuscore numeric(4,1), --入学成绩
stuloan char(2) check(stuloan in ('是','否'))not null, --是否贷款
)
创建一个名为tb_teacher的教师表
tb_teacher的教师表
create table tb_teacher(
教师代号 char(7) primary key, --教师代号
姓名 char(8) not null, --教师名字
性别char(2) check(teasex in('女','男')), --教师性别
出生年月 datetime not null, --教师出生日期
职称 char(6), --职称
)
创建一个名为tb_renke的任课表
tb_renke的任课表
create table tb_renke(
课程代号 char(5), --课程代号
教师代号 char(7), -- 教师代号
)
创建一个名为tb_major的专业表
tb_major的专业表
create tabletb_major(
专业名称 char(8) primary key, --专业名称
负责人 char(8) not null, --负责人
)
创建一个名为tb_course的课程表
tb_course的课程表
create table course(
课程代号 char(5)primary key, --课程代号
课程名 char(14) not null, --课程名
周学时 int, --周学时
学分 int, --学分
)
创建一个名为tb_studentinfo的学生个人情况表
tb_studentinfo的学生个人情况表
create table tb_studentinfo(
学号 char(7), --学号
身份证 char(15) unique, --身份证号码
籍贯 char(8), --籍贯
家庭住址 text, --家庭住址
电话 char(30), --电话
特长 text, --特长
奖励 text, --奖励
处分 char(100), --处分
)
创建一个名为tb_grade的成绩表
tb_grade的成绩表
create table tb_grade(
学号 char(7), --学号
课程代号 char(5), --课程代号
平时 numeric(3,1), --平时
期中 numeric(3,1), --期中
期末 numeric(3,1), --期末
)
现在向每一张表插入记录
给学生表tb_student插入数据
tb_student
insert into tb_student values('9607039','邓盈莹','女','1978-6-6','外贸',666.6,'是');
insert into tb_student values('9907002','倪雯娴','女','1981-4-1','外贸',641.4,'是');
insert into tb_student values('9801055','赵东','男','1979-11-9','中文',450,'否');
insert into tb_student values('9902006','和音','女','1982-6-19','数学',487.1,'否');
insert into tb_student values('9704001','克敏敏','女','1978-7-22','物理',463,'否');
insert into tb_student values('9603001','申强','男','1978-1-15','新闻',512,'是');
insert into tb_student values('9606005','迟大为','男','1976-9-3','化学',491.3,'否');
insert into tb_studentvalues('9803011','欧阳小娟','女','1981-8-11','新闻',526.5,'否');
insert into tb_student values('9908088','毛杰','男','1982-1-1','计算机',622.2,'否');
insert into tb_student values('9608066','康红','女','1979-9-7','计算机',596.8,'是');
insert into tb_student values('9805026','夏天','男','1980-5-7','历史',426.7,'否');
insert into tb_student values('9702033','李力','男','1979-7-7','数学',463.9,'否');
给教师表tb_teacher插入数据
tb_teacher
insert into tb_teacher values('20222','于朵','女','1962-6-19','副教授');
insert into tb_teacher values('20406','张建','女','1946-7-16','教授');
insert into tb_teacher values('10429','蒋成功','男','1959-3-12','副教授');
insert into tb_teacher values('10616','万年','男','1945-9-1','教授');
insert into tb_teacher values('20626','孙乐','女','1971-12-15','讲师');
insert into tb_teacher values('10803','李铁','男','1958-9-22','副教授');
insert into tb_teacher values('10812','米粟','男','1960-1-3','副教授');
insert into tb_teacher values('11015','柴淮','男','1973-8-26','讲师');
insert into tb_teacher values('11107','方华','女','197**-6','讲师');
insert into tb_teacher values('20836','张静','女','1974-11-15','讲师');
insert into tb_teacher values('10101','高树声','男','1940-12-5','教授');
insert into tb_teacher values('10312','巩文','男','1959-3-17','副教授');
insert into tb_teacher values('20506','吴燕','女','1947-10-6','教授');
insert into tb_teacher values('20701','沈菲菲','女','1960-6-18','副教授');
insert into tb_teacher values('10202','梁龙林','男','1948-6-18','教授');
insert into tb_teacher values('10428','李阳','男','1955-8-12','教授');
insert into tb_teacher values('10621','鲁师','男','1943-11-18','教授');
insert into tb_teachervalues('10809','邓为民','男','1957-1-26','副教授');
insert into tb_teacher values('20106','姜晓红','女','1961-6-5','副教授');
insert into tb_teacher values('10131','付林','男','1968-9-11','讲师');
insert into tb_teacher values('10802','杨亮红','男','1941-5-23','教授');
insert into tb_teacher values('10223','周毅','男','1970-3-8','讲师');
insert into tb_teacher values('20255','孙莉莉','女','1975-9-12','讲师');
insert into tb_teacher values('20705','夏雪','女','1969-10-28','讲师');
insert into tb_teacher values('10712','南方','男','1975-9-13','讲师');
insert into tb_teacher values('10201','代顺达','男','1940-12-17','讲师');
insert into tb_teacher values('20301','高珊','女','1965-6-19','副教授');
insert into tb_teacher values('20319','林妮','女','1973-4-1','讲师');
insert into tb_teacher values('21025','张旗','女','1972-6-6','讲师');
insert into tb_teacher values('11117','韩明','男','1976-2-14','助教');
insert into tb_teacher values('10503','孙建国','男','1949-10-1','教授');
insert into tb_teacher values('10509','黄宁','男','1956-12-23','副教授');
给任课表tb_renke插入数据
tb_renke
insert into tb_renke values('21003','21025');
insert into tb_renke values('30211','20255');
insert into tb_renke values('30232','10201');
insert into tb_renke values('40711','10712');
insert into tb_renke values('40722','20701');
insert into tb_renke values('10101','20106');
insert into tb_renke values('11101','11107');
insert into tb_renke values('20511','10509');
insert into tb_renke values('10101','10131');
insert intotb_renke values('20534','10503');
insert into tb_renke values('10712','20705');
insert into tb_renke values('20115','20106');
insert into tb_renke values('10222','10223');
insert into tb_renke values('30412','10429');
insert into tb_renke values('40316','20319');
insert into tb_renke values('40612','20626');
insert into tb_renke values('20328','20301');
insert into tb_renke values('10812','10429');
insert into tb_renke values('20801','10803');
insert into tb_renke values('30802','10812');
insert into tb_renke values('11001','11015');
insert into tb_renke values('20113','10131');
insert into tb_renke values('30416','10428');
insert into tb_renke values('20327','10312');
insert into tb_renke values('20521','20506');
insert into tb_renke values('30213','10201');
insert into tb_renke values('11101','11117');
insert into tb_renke values('10715','20222');
insert intotb_renke values('20111','10101');
insert into tb_renke values('10218','10202');
insert intotb_renke values('30423','20406');
insert into tb_renke values('40331','20319');
insert intotb_renke values('40625','10616');
insert into tb_renke values('20314','20301');
insert into tb_renke values('10811','20836');
insert into tb_renke values('30819','10802');
给专业表tb_major插入数据
tb_major
insert into tb_major values('化学','鲁师');
insert into tb_major values('计算机','邓为民');
insert into tb_major values('软件','李明');
insert into tb_major values('外贸','沈菲菲');
insert into tb_major values('数学','梁龙林');
insert into tb_major values('物理','李阳');
insert intotb_major values('物理学','王国玉');
insert into tb_major values('历史','吴燕');
insert into tb_major values('中文','高树声');
insert into tb_major values('新闻','巩文');
给课程表tb_course插入数据
tb_course
insert into tb_course values('20511','世界近代史',4,4);
insert into tb_course values('10101','大学语文',2,2);
insert into tb_course values('20801','计算机基础(一)',4,3);
insert into tb_course values('10218','高等代数',4,4);
insert into tb_course values('11001','英语(一)',6,6);
insert into tb_course values('20113','外国文学',4,4);
insert into tb_course values('30416','接口技术',4,3);
insert into tb_course values('20327','报刊编辑学',2,2);
insert into tb_course values('20521','中国民族史',3,2);
insert into tb_course values('30213','数论',4,4);
insert into tb_course values('11101','体育',2,2);
insert into tb_course values('21003','英语(二)',4,4);
insert into tb_course values('10715','高等数学',4,4);
insert into tb_course values('20111','古代汉语',3,3);
insert into tb_course values('30802','计算机基础(二)',3,3);
insert into tb_course values('30423','电磁场理论',3,3);
insert into tb_course values('40331','传播心理学',2,2);
insert into tb_course values('40625','色谱学',2,2);
insert into tb_course values('20314','新闻学概论',2,2);
insert into tb_course values('10811','离散数学',2,2);
insert into tb_course values('30819','编译技术',4,4);
insert into tb_course values('20534','中国近代史',4,4);
insert into tb_course values('10712','政治经济学',3,3);
insert into tb_course values('20115','近代汉语',4,4);
insert into tb_course values('30211','概论统计',3,3);
insert into tb_course values('30232','数学分析',2,2);
insert into tb_course values('40711','国际投资学',2,2);
insert into tb_course values('40722','国际商法',2,2);
insert into tb_course values('30832','算法设计',4,4);
insert into tb_course values('10812','数字电路',4,4);
insert into tb_course values('10222','解析几何',2,2);
insert into tb_course values('30412','近代物理实验',3,2);
insert into tb_course values('40316','当代新闻史',2,2);
insert into tb_course values('40612','配位化学',3,3);
insert into tb_course values('20328','现代新闻报道',4,4);
给学生个人情况表tb_studentinfo插入数据
tb_studentinfo
insert into tb_studentinfo values('9607039','530120169021101','安徽','江岸小区栋单元','5033228','唱歌,摔跤','被评为三好学生','');
insert into tb_studentinfo values('9907002','530120170060701','云南','江岸小区栋单元','5033226','跳舞,篮球','被评为三好学生','');
insert into tb_studentinfo values('9801055','530120171072501','湖北','白马小区栋单元','4133224','围棋','','');
insert into tb_studentinfo values('9902006','530120170122901','湖南','金星小区栋单元','3133218','象棋','被评为三好学生','');
insert into tb_studentinfo values('9704001','530120168121101','云南','静园小区栋单元','2133227','排球,足球','','');
insert into tb_studentinfo values('9603001','530120174050101','云南','江岸小区栋单元','5033219','唱歌,跳舞','被评为红花少年','');
insert into tb_studentinfo values('9606005','530120175040702','江苏','江岸小区栋单元','5033123','演讲','','');
insert into tb_studentinfo values('9803011','530120173021201','四川','白马小区栋单元','4133124','集邮','获集邮展三等奖','');
insert into tb_studentinfo values('9908088','530120172092801','四川','阳光小区栋单元','3133177','长跑足球','获省长跑第二名','作弊受处分');
insert into tb_studentinfo values('9608066','530120174092201','云南','阳光小区栋单元','3133222','摄影','获优秀作文奖','');
insert into tb_studentinfo values('9805026','530120174110901','贵州','阳光小区栋单元','3133189','围棋','获数学竞赛一等奖','');
insert into tb_studentinfo values('9702033','530120170080401','黑龙江','阳光小区栋单元','3148212','围棋','获数学竞赛三等奖','');
给成绩表tb_grade插入数据
tb_grade
insert into tb_grade values('9805026','20801',75,87,82);
insert into tb_grade values('9702033','30802',80,89,91);
insert into tb_grade values('9907002','11001',91,83,85);
insert into tb_grade values('9801055','20113',70,65,55);
insert into tb_grade values('9607039','40711',85,80,88);
insert into tb_grade values('9907002','10715',83,90,86);
insert into tb_grade values('9801055','20111',78,60,65);
insert into tb_grade values('9902006','10218',75,63,52);
insert into tb_grade values('9902006','11001',78,86,81);
insert into tb_grade values('9704001','30416',80,90,90);
insert into tb_grade values('9803011','20327',95,93,90);
insert into tb_grade values('9908088','11001',90,91,87);
insert into tb_grade values('9805026','20521',90,97,96);
insert into tb_grade values('9702033','30213',88,69,76);
insert into tb_grade values('9907002','11101',88,65,72);
insert into tb_grade values('9801055','21003',70,90,84);
insert into tb_grade values('9902006','11101',80,70,70);
insert into tb_grade values('9803011','21003',78,84,82);
insert into tb_grade values('9908088','11101',82,75,78);
insert into tb_grade values('9805026','21003',83,85,84);
insert into tb_grade values('9805026','20511',90,82,86);
insert into tb_grade values('9702033','30232',80,84,83);
insert into tb_grade values('9907002','10101',84,96,92);
insert into tb_grade values('9801055','20801',76,78,60);
insert into tb_grade values('9902006','10101',85,88,81);
insert into tb_grade values('9704001','30802',90,87,82);
insert into tb_grade values('9803011','20801',60,50,51);
insert into tb_grade values('9704001','30423',80,81,85);
insert into tb_grade values('9603001','40331',67,72,70);
insert into tb_grade values('9606005','40625',83,85,84);
insert into tb_grade values('9803011','20314',76,76,76);
insert into tb_grade values('9908088','10811',82,92,89);
insert into tb_grade values('9608066','30819',78,84,82);
insert into tb_grade values('9805026','20534',70,90,84);
insert into tb_grade values('9702033','30211',93,89,90);
insert into tb_grade values('9607039','40722',90,87,82);
insert into tb_grade values('9907002','10712',90,96,97);
insert into tb_grade values('9801055','20115',80,82,87);
insert into tb_grade values('9902006','10222',85,91,79);
insert into tb_grade values('9704001','30412',78,87,90);
insert into tb_grade values('9603001','30316',66,71,73);
insert into tb_grade values('9606005','40612',70,78,60);
insert into tb_grade values('9803011','20328',90,88,87);
insert into tb_grade values('9908088','10812',80,67,83);
insert into tb_grade values('9608066','30832',95,92,93);
insert into tb_grade values('9908088','10101',80,82,87);
下面进行数据查询:
(1)查询在78年12月31日之后出生的学生的学号和姓名
思考一下在展开
select学号,姓名from tb_student where 出生年月>'1979';
或是:
select学号,姓名from tb_student where 出生年月>cast('1978-12-31' as datetime);
(2)查询入学成绩大于500分的女同学和入学成绩大于600分的男同学的姓名
思考一下在展开
select姓名from tb_student where (入学成绩>500 and 性别='女')or(入学成绩>600 and 性别='男') ;
(3)查询家在江岸小区住的学生的姓名和家庭住址
思考一下在展开
select家庭住址,姓名from tb_studentinfo,tb_student where( 家庭住址like'江岸小区%')and (tb_studentinfo.学号=tb_student.学号) ;
(4)查询没有选修姜晓红老师所授课程的学生的学号
思考一下在展开
select学号from tb_student where 学号not in(select 学号from tb_grade,tb_teacher,tb_renke where tb_grade.课程代号=tb_renke.课程代号and tb_renke.教师代号=tb_teacher.教师代号and姓名='姜晓红' );
(5)查询各学生所选课的期末平均成绩,学生学号,姓名
思考一下在展开
select tb_student.姓名,tb_grade.学号,avg(期末) from tb_student,tb_grade where tb_student.学号=tb_grade.学号group by tb_student.姓名,tb_grade.学号;
(6)查询赵东所选修的课程的课程名及其学号、姓名
思考一下在展开
select tb_student.姓名,tb_grade.学号,课程名from tb_student,tb_grade ,tb_course where tb_grade.学号=tb_student.学号and tb_grade.课程代号=tb_Course.课程代号and姓名='赵东';
(7)查询由教授所任课的课程名
思考一下在展开
select课程名from tb_course,tb_teacher ,tb_renke where tb_course.课程代号=tb_renke.课程代号and tb_teacher.教师代号=tb_renke.教师代号and职称='教授'
或是:
select课程名from tb_course where 课程代号in (select 课程代号from tb_renke where 教师代号in(select教师代号from tb_teacher where 职称='教授'));
(8)统计没有贷款的学生的人数
思考一下在展开
select count(*)没贷款的人数from tb_student where 是否货款='false';
(9)查询每个科目平时、期中及期末三个成绩的平均成绩在80分以上的学生的学号
思考一下在展开
select学号成绩平均在分以上的学号from tb_grade group by 学号having avg(平时+期中+期末)>80;
(10)查询和毛杰同学所学专业一样的人的学号和姓名
思考一下在展开
select学号,姓名from tb_student where 专业in (select 专业from tb_student where 姓名='毛杰');
(11)查询期末成绩大于任何一个老师所教学生的期末成绩的学生姓名、选修课程号、成绩
思考一下在展开
select 姓名,课程代号,期末,tb_grade.学号from tb_grade,tb_student where tb_student.学号=tb_grade.学号and 期末>=all(select 期末from tb_grade);
(12)来了一位新同学,学号为9607001,省份证号为530120169021100
思考一下在展开
insert into tb_studentinfo (学号,身份证)values('9607001','530120169021100');
insert into tb_student(学号)values('9607001');
(13)将期末成绩在所有人的期末平均成绩以上的同学加10分
思考一下在展开
update tb_grade set 期末=期末+10 where 期末>(select avg(期末) from tb_grade);
(14)查询至少选修了付林老师所授所有课程的学生学号
思考一下在展开
Select学号from tb_grade x Where not exists (select * from tb_grade y Where 教师代号in( Select 教师代号from tb_teacher,tb_renke Where 姓名='付林'and tb_teacher.教师代号=tb_renke.教师代号) And not exists(select *from tb_grade z Where z.课程代号=y.课程代号and z.学号=x.学号))
(15)统计每个老师授课的种数
思考一下在展开
select教师代号,count(*)课程种数from tb_renke group by tb_renke.教师代号;
(16)查询选修2门以上课程的学生学号及平均成绩(指各课程的期末成绩的平均成绩,只统计及格的课程),并按其平均成绩降序排列输出
思考一下在展开
select 学号,avg(期末)平均分from tb_grade where 期末> 60 group by 学号having count(*)>2 order by avg( 期末)desc;
(17)查询没有贷款的学生的学号
思考一下在展开
select学号没贷款的学号from tb_student where 是否货款='false';
(18)查询年龄最大的学生的学号和姓名
思考一下在展开
select学号,姓名from tb_student where 出生年月in ( select min(出生年月) from tb_student );
希望这些资料,能够对你有所帮助!
最后
以上就是纯真鲜花为你收集整理的收藏的SQL知识以及SQL语句简单实践通俗易懂的全部内容,希望文章能够帮你解决收藏的SQL知识以及SQL语句简单实践通俗易懂所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复