SQL 实验详细源码参考

  1 -- ----------------------------------------------------------------------------------
  2 -- 数据库初始化 (实验二 数据描述、定义实验)
  3 -- ----------------------------------------------------------------------------------
  4 create  database s_13420228;
  5 use s_13420228;
  6
  7 create table student(
  8     sno varchar(8),
  9     sname varchar(20) not null,  -- 姓名
 10     sage smallint,  -- 年龄
 11     ssex char(8),  -- 性别
 12     sdept varchar(20) ,  -- 所在系
 13     constraint s_pk primary key (sno),  -- 主码
 14     constraint s1 check(sage between 16 and 40),
 15     constraint s2 check(ssex in(‘male‘,‘female‘)),
 16     constraint s3 check(grade between 0 and 100)
 17  );
 18
 19 create table course(
 20     cno varchar(7),  -- 课程号
 21     cname varchar(50),  -- 课程名
 22     cpno varchar(50),  -- 先行课
 23     ccredit numeric(2,1),  -- 学分,精确存储3.5
 24     constraint c_pk primary key (cno,cname),  -- 主码
 25     constraint s4 foreign key (cpno) references course (cno)  -- 先行课必须要存在
 26 );
 27
 28 create table sc(  -- 学生选课表
 29     sno varchar(8),  -- 学号
 30     cno varchar(7),  -- 课程号
 31     grade smallint,  -- 成绩
 32     constraint sc_pk primary key (sno,cno),  -- 主码
 33     constraint s5 foreign key (sno) references student(sno) on delete cascade on update cascade,  -- sc中的学号必须存在,级联删除、更新
 34     constraint s6 foreign key (cno) references course(cno) on delete cascade on update cascade -- sc中课程号必须存在
 35 );
 36
 37 insert into course values(‘01‘, ‘Database‘, null, 4.0);  -- 插入数据 course
 38 insert into course values(‘02‘, ‘Operating System‘, null, 3.5);
 39 insert into course values(‘03‘, ‘Computer Networking‘, null, 4.0);
 40 insert into course values(‘04‘, ‘Data Structures‘, null, 4.0);
 41 insert into course values(‘05‘, ‘The C++ Programming Language‘, null, 4.0);
 42 insert into course values(‘06‘, ‘The C Programming Language‘, null, 4.0);
 43 insert into course values(‘07‘, ‘Physical Principles‘, null, 2.0);
 44 insert into course values(‘08‘, ‘Music Video Production‘, null, 3.0);
 45 insert into course values(‘09‘, ‘Computational Biology‘, null, 3.0);
 46 insert into course values(‘10‘, ‘Genetics‘, null, 3.0); --  Biology
 47 insert into course values(‘11‘, ‘Intro. to Biology‘, null, 3.0);
 48
 49 insert into student values(‘13420201‘, ‘Zhang‘, 18,‘male‘, ‘CS‘);  -- 插入数据 student
 50 insert into student values(‘13420202‘, ‘Shankar‘, 20,‘male‘, ‘CS‘);
 51 insert into student values(‘13420203‘, ‘Brandt‘, 18,‘male‘, ‘CS‘);
 52 insert into student values(‘13420204‘, ‘Chavez‘, 19,‘female‘, ‘CS‘);
 53 insert into student values(‘13312205‘, ‘Peltier‘, 21,‘female‘, ‘Music‘);
 54 insert into student values(‘13312213‘, ‘Levy‘, 20,‘female‘, ‘Music‘);
 55 insert into student values(‘13312137‘, ‘Williams‘,20, ‘male‘, ‘Music‘);
 56 insert into student values(‘13523111‘, ‘Bourikas‘, 19,‘male‘, ‘Biology‘);
 57 insert into student values(‘13523222‘, ‘Smith‘, 19,‘male‘, ‘Biology‘);
 58 insert into student values(‘13453334‘, ‘Snow‘, 19,‘male‘, ‘Physics‘);
 59
 60 insert into sc values(‘13420201‘, ‘01‘, 80);  -- 插入数据 sc  CS
 61 insert into sc values(‘13420201‘, ‘02‘, 70);
 62 insert into sc values(‘13420201‘, ‘03‘, 90);
 63 insert into sc values(‘13420201‘, ‘04‘, 40);
 64 insert into sc values(‘13420201‘, ‘05‘, 60);
 65 insert into sc values(‘13420201‘, ‘06‘, 90);
 66
 67 insert into sc values(‘13420202‘, ‘01‘, 80);
 68 insert into sc values(‘13420202‘, ‘03‘, 90);
 69 insert into sc values(‘13420202‘, ‘04‘, 30);
 70 insert into sc values(‘13420202‘, ‘05‘, 70);
 71 insert into sc values(‘13420202‘, ‘06‘, 90);
 72
 73 insert into sc values(‘13420203‘, ‘01‘, 80);
 74 insert into sc values(‘13420203‘, ‘02‘, 90);
 75 insert into sc values(‘13420203‘, ‘03‘, 90);
 76 insert into sc values(‘13420203‘, ‘04‘, 85);
 77 insert into sc values(‘13420203‘, ‘05‘, 75);
 78 insert into sc values(‘13420203‘, ‘06‘, 90);
 79
 80 insert into sc values(‘13420204‘, ‘01‘, 55);
 81 insert into sc values(‘13420204‘, ‘02‘, 55);
 82 insert into sc values(‘13420204‘, ‘03‘, 90);
 83 insert into sc values(‘13420204‘, ‘04‘, 55);
 84 insert into sc values(‘13420204‘, ‘05‘, 70);
 85 insert into sc values(‘13420204‘, ‘06‘, 90);
 86
 87 insert into sc values(‘13312205‘, ‘08‘, 90);  -- Music
 88 insert into sc values(‘13312213‘, ‘08‘, 60);
 89 insert into sc values(‘13312137‘, ‘08‘, 60);
 90
 91 insert into sc values(‘13523111‘, ‘09‘, 60);  -- Biology
 92 insert into sc values(‘13523111‘, ‘10‘, 59);
 93 insert into sc values(‘13523111‘, ‘11‘, 80);
 94
 95 insert into sc values(‘13453334‘, ‘07‘, 70);  -- Physics
 96
 97 update course set cpno = ‘04‘ where cno = ‘01‘;  -- 更新 course
 98 update course set cpno = ‘04‘ where cno = ‘02‘;
 99 update course set cpno = ‘05‘ where cno = ‘03‘;
100 update course set cpno = ‘05‘ where cno = ‘04‘;
101 update course set cpno = ‘06‘ where cno = ‘05‘;
102
103 -- --------------------初始化完成----------------------------------------------------------
104
105 -- 建立索引可以加快查询速度
106 create index grade_index on sc(grade);  -- 多值索引
107 create unique index sno_index on student(sno);  -- 唯一索引,每一个索引值只对应一个唯一的数据记录
108 drop index grade_index on sc;  -- 删除索引
109 drop index sno_index on student;
110
111 alter table student add birthday date;  -- 添加属性列
112 alter table student drop sage;  -- 删除属性列
113
114 alter table course drop foreign key s3;  -- 删除约束s3
115 alter table course add constraint s3 foreign key (cpno) references course (cno)
116 on delete cascade on update cascade; -- 先行课必须要存在,添加级联删除、更新
117
118 delete from course where cno = ‘04‘;  -- 删除特定元组
119
120 delete from student;  -- 删除关系student的所有元组
121 delete from sc;
122 delete from course;
123 drop table student;  -- 完全删除student,包括student这个模式及其所有元组
124 drop table sc;
125 drop table course;
126
127 -- ----------------------------------------------------------------------------------
128 -- (实验三 数据更新实验)
129 -- ----------------------------------------------------------------------------------
130
131 -- 插入某个学生的选课信息(外码约束,拒绝的)
132 insert into sc values(‘13436214‘,‘01‘,0);
133
134 -- 求各系学生的平均成绩,并把结果存入数据库
135 create table sdept_avg_grade (
136     sdept varchar(20),
137     avg_grade smallint,
138     constraint sag_pk primary key (sdept),
139     constraint s7 check(avg_grade between 0 and 100)
140     -- constraint s8 foreign key (sdept) references student(sdept)  -- 由于外码属性必须为莫关系的主码,此外码定义语句错误
141 );
142
143 insert into sdept_avg_grade
144 select sdept,avg(grade) from sc,student where sc.sno = student.sno group by sdept ;
145
146 drop tables sdept_avg_grade;
147
148 --   将“CS”系全体学生的成绩置零
149 update sc set grade = 0 where ‘CS‘ = (
150 select sdept from student where sc.sno = student.sno);
151
152 -- 删除“CS”系全体学生的选课记录
153 delete from sc where ‘CS‘ = (
154 select sdept from student where sc.sno = student.sno);
155
156 -- 删除学号为“13420201”的相关信息(级联删除cascade)
157 delete from student where sno = ‘13420201‘;
158
159 --  将学号为“13420202”的学生的学号修改为“12420202”(级联更新)
160 update student set sno = ‘12420202‘ where sno = ‘13420202‘;
161
162 -- 把平均成绩大于80分的男同学的学号和平均成绩存入另一个表S_GRADE(SNO,AVG_GRADE)
163 create table s_grade(
164     sno varchar(8),
165     avg_grade smallint,
166     constraint sg_pk primary key (sno),
167     constraint s9 foreign key (sno) references student(sno),
168     constraint s10 check(grade between 0 and 100)
169 );
170 insert into s_grade
171 select sc.sno,avg(grade) from sc,student where sc.sno = student.sno and ssex = ‘male‘
172 group by sc.sno having avg(grade) > 80;
173
174 select * from s_grade;
175 drop table s_grade;
176
177 -- 把选修了课程名为“Data Structures”的学生的成绩提高10%;
178 update sc set grade = grade * 1.1 where ‘Data Structures‘ = (
179 select cname from course where sc.cno = course.cno );
180
181 select sno,grade from sc,course where sc.cno = course.cno and cname = ‘Data Structures‘;
182 select avg(grade) from sc,course where sc.cno = course.cno and  cname = ‘Data Structures‘;
183
184
185 -- ---------------------------------------------------------------------------------------------
186 -- (实验四 数据查询实验)
187 -- ---------------------------------------------------------------------------------------------
188
189 -- 1.   查询学生的基本信息;
190 select * from student;
191
192 -- 2.   查询“CS”系学生的基本信息
193 select * from student where sdept = ‘CS‘;
194
195 -- 3.   查询“CS”系学生年龄不在19到21之间的学生的学号、姓名;
196 select sno,sname from student where sdept = ‘CS‘ and sage not between 19 and 21;
197
198 -- 4.   找出最大年龄;
199 select max(sage) from student;
200
201 -- 5.   找出“CS”系年龄最大的学生,显示其学号、姓名;
202 select sno,sname from student where sdept = ‘CS‘ and sage = (
203 select max(sage) from student where sdept = ‘CS‘);
204
205 -- 6.   找出各系年龄最大的学生,显示其学号、姓名;
206 select sno,sname from student as s1 where sage = (
207 select max(sage) from student as s2 where s1.sdept = s2.sdept);
208
209 -- 7.   统计“CS”系学生的人数;
210 select count(sno) as number_of_student from student where sdept = ‘CS‘;
211
212 -- 8.   统计各系学生的人数,结果按升序排列;
213 select sdept, count(sno) as number_of_student from student group by sdept order by number_of_student;
214
215 -- 9.   按系统计各系学生的平均年龄,结果按降序排列;
216 select sdept,avg(sage) as avg_age from student group by sdept order by avg_age desc;
217
218 -- 10.  查询每门课程的课程名;
219 select cname from course;
220
221 -- 11.  查询无先修课的课程的课程名和学分数;注意空值测试语句 ‘is null‘ or ‘is not null‘;
222 select cname,ccredit from course where cpno is null;
223
224 -- 12.  统计无先修课的课程的学分总数;
225 select sum(ccredit) as total_credit from course where cpno is null;
226
227 -- 13.  统计每位学生选修课程的门数、学分及其平均成绩;
228 select sc.sno, count(sc.sno) as number_of_course, sum(ccredit) as number_of_credit, avg(grade) as avg_grade
229 from sc, course where sc.cno = course.cno group by sc.sno;
230
231 -- 14.  统计选修每门课程的学生人数及各门课程的平均成绩;
232 select cno,count(sno) as number_of_student, avg(grade) as avg_grade from sc group by cno;
233
234 -- 15.  找出平均成绩在85分以上的学生,结果按系分组,并按平均成绩的升序排列;
235 select sc.sno, avg(grade) as avg_grade from sc,student where sc.sno = student.sno
236 group by sc.sno, sdept having avg_grade >= 85 order by avg_grade;
237
238 -- 16.  查询选修了“01”或“02”号课程的学生学号和姓名;
239 select  distinct sc.sno,sname from sc,student where sc.sno = student.sno and (cno = ‘01‘ or cno = ‘02‘);
240
241 -- 17.  查询选修了“1”和“2”号课程的学生学号和姓名;
242 select distinct s1.sno,sname from sc as s1,student where s1.sno = student.sno and cno = ‘01‘ and ‘02‘ in (
243 select cno from sc as s2 where s2.sno = s1.sno);
244
245 select distinct sc.sno,sname from sc,student where sc.sno = student.sno and cno = ‘01‘ and sc.sno in (
246 select sno from sc where cno = ‘02‘);
247
248 -- 18.  查询选修了课程名为“Database”且成绩在60分以下的学生的学号、姓名和成绩;
249 select sc.sno,sname,grade from sc,student,course where student.sno = sc.sno and sc.cno = course.cno
250 and cname = ‘Database‘ and grade < 60;
251
252 -- 19.  查询每位学生选修了课程的学生信息(显示:学号,姓名,课程号,课程名,成绩);
253 select sc.sno,sname,sc.cno,cname,grade from sc,student,course where student.sno = sc.sno
254 and sc.cno = course.cno;
255
256 -- 20.  查询没有选修课程的学生的基本信息;
257 select sno,sname,sage,ssex,sdept from student where sno not in (select sno from sc);
258
259 -- 21.  查询选修了3门以上课程的学生学号;
260 select sno from sc group by sno having count(sno) >= 3;
261
262 -- 22.  查询选修课程成绩至少有一门在80分以上的学生学号;
263 select sno from sc group by sno having max(grade) > 80;
264
265 -- 23.  查询选修课程成绩均在80分以上的学生学号;
266 select sno from sc group by sno having min(grade) > 80;
267
268 -- 24.  查询选修课程平均成绩在80分以上的学生学号;
269 select sno from sc group by sno having avg(grade) > 80;
270
271 -- -----------------------------------------------------
272 drop database s_13420228;
时间: 2024-10-07 20:06:44

SQL 实验详细源码参考的相关文章

淘宝数据库OceanBase SQL编译器部分 源码阅读--Schema模式

淘宝数据库OceanBase SQL编译器部分 源码阅读--Schema模式 什么是Database,什么是Schema,什么是Table,什么是列,什么是行,什么是User?我们可以可以把Database看作是一个大仓库,仓库分了很多很多的房间,Schema就是其中的房间,一个Schema代表一个房间,Table可以看作是每个Schema中的柜子,行和列就是柜子中的格子.User就是房间的主人.简单来说,Schema是包括表,列,索引,视图等数据库对象的集合. OceanBase中的强Sche

淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划

body, td { font-family: tahoma; font-size: 10pt; } 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划 SQL编译解析三部曲分为:构建语法树,生成逻辑计划,指定物理执行计划.第一步骤,在我的上一篇博客淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树里做了介绍,这篇博客主要研究第二步,生成逻辑计划. 一. 什么是逻辑计划?我们已经知道,语法树就是一个树状的结构组织,每个节点代表一种类型的语法含义.如

淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划

SQL编译解析三部曲分为:构建语法树,制定逻辑计划,生成物理执行计划.前两个步骤请参见我的博客<<淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树>>和<<淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划>>.这篇博客主要研究第三步,生成物理查询计划. 一. 什么是物理查询计划 与之前的阅读方法一致,这篇博客的两个主要问题是what 和how.那么什么是物理查询计划?物理查询计划能够直接执行并返回数据结果数

vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效

vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) #include<bits/stdc++.h> using namespace std; int main(){ vector<int> v(3,3); vector<int>::iterator it=v.begin(); cout<<v.size()<<" "<<v.capacity()<<endl;//3

淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树

OceanBase是 阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作.在阿里巴巴集团 下,OceanBase数据库支持了多个重要业务的数据存储,包括收藏夹.直通车报表.天猫评价等.截止到2013年4月份,OceanBase线上业务 的数据量已经超过一千亿条. 看起来挺厉害的,今天我们来研究下它的源代码.关于OceanBase的架构描述有很多文档,这篇笔记也不打算涉及这些东西,只讨论OceanBase的SQL编译部分的代码. Ocea

Spark之SQL解析(源码阅读十)

如何能更好的运用与监控sparkSQL?或许我们改更深层次的了解它深层次的原理是什么.之前总结的已经写了传统数据库与Spark的sql解析之间的差别.那么我们下来直切主题~ 如今的Spark已经支持多种多样的数据源的查询与加载,兼容了Hive,可用JDBC的方式或者ODBC来连接Spark SQL.下图为官网给出的架构.那么sparkSql呢可以重用Hive本身提供的元数据仓库(MetaStore).HiveQL.以及用户自定义函数(UDF)及序列化和反序列化的工具(SerDes). 下来我们来

yum实验--使用源码包安装httpd2.2(centos6)

1.下载源码 官网,教室的服务器 lftp 172.18.0.1         user thirty-three pass:thirty-three         get files/httpd-2.4.27.tar.bz2         exit         tar xvf httpd-2.4.27.tar.bz2 [[email protected] ~]#lftp 172.18.0.1 lftp 172.18.0.1:~> user thirty-three Password:

WPF物料替换源码参考(返回多个数据表)

using GalaSoft.MvvmLight.Command; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Windows.Input; namespace Midea.MES.Plugin.OnlineRepair.ViewModel {     class ma

Linux下Zabbix_Server的安装-源码安装

Centos下Zabbix_Server/agent/proxy的安装配置 --------源码编译 完整版请查看附件 一.环境说明 a)软件环境 由于是源码安装,官方的zabbix源码包内已经包含了Server,Proxy, Agent, GUI所有组件和必要的执行脚本,截至本文正式版本限于2.2.3: LAMP环境 n操作系统使用Centos6.5 X86_64 最小化安装,并yum update到最新状态,内核版本为2.6.32-431.17.1.el6.x86_64: nMysql 使用