建立数据库结构,完成数据库中的基本表的建立,同时将表中数据进行添加。
实验代码及注释:
实验代码及注释:
Student表增加新的属性s_entrance
Sc表grade属性的类型改为int
删除student表中s_entrance属性
实验代码及注释:
Student表插入数据(中间错误是因为忘更改学号了)
Course表插入数据
Sc表插入数据
mysql> insert sc(sno,cno,grade) values('201215121','7',50);
mysql> insert sc(sno,cno,grade) values('201215121','4',45);
mysql> insert sc(sno,cno,grade) values('201215121','6',55);
对于已完成数据库中数据进行修改、删除操作。
实验代码及注释:
mysql> update sc
-> set grade=grade+5
-> where 'cs'=
-> (select sdept
-> from student
-> where student.sno=sc.sno and sc.grade<60 );
实验代码及注释:
mysql> delete
-> from sc
-> where 'CS'=
-> (select sdept
-> from student
-> where student.sno=sc.sno);
实验代码及注释:
mysql> delete
-> from sc
-> where cno=2;
实验代码及注释:
mysql> update sc
-> set grade=grade-10
-> where grade>90 and cno=6;
采用查询语句对于数据库中数据进行查询或统计功能的操作。
实验代码及注释:
mysql> select sname,cname,grade
-> from student,sc,course
-> where student.sno=sc.sno
-> and course.cno=sc.cno
-> and grade>
-> (select avg(grade)
-> from sc
-> where course.cno=cno);
实验代码及注释:
mysql> select sname
-> from student
-> where exists
-> (select *
-> from sc
-> where sno=student.sno and cno=1)
-> and exists
-> (select *
-> from sc
-> where sno=student.sno and cno=2);
实验代码及注释:
mysql> select sname
-> from student
-> where exists
-> (select * from sc where student.sno=sno and cno=1)
-> and not exists
-> (select * from sc where student.sno=sno and cno=2);
实验代码及注释:
mysql> select cno,count(sno)
-> from sc
-> where grade>80
-> group by cno;
实验代码及注释:
mysql> select avg(grade)
-> from sc
-> where sno in
-> (select sno
-> from student
-> where sdept='CS');
实验代码及注释:
mysql> select count(sname)
-> from student
-> where exists
-> (select *
-> from sc
-> where student.sno=sno
-> group by sno
-> having count(cno)>2);
实验代码及注释:
mysql> select sname
-> from student
-> where exists
-> (select *
-> from sc
-> where student.sno=sno
-> group by sno
-> having count(cno)>=2);
实验代码及注释:
mysql> select cno,cname,cpno,ccredit
-> from course
-> where not exists
-> (select *
-> from sc
-> where course.cno=cno);
实验代码及注释:
mysql> select *
-> from student
-> where not exists
-> (select *
-> from sc
-> where cno=1 and sno=student.sno);
实验代码及注释:
mysql> select count(sno)
-> from student
-> where not exists
-> (select *
-> from sc
-> where cno=1 and sno=student.sno);
实验代码及注释:
mysql> select *
-> from course
-> where cno=
-> (select cno
-> from sc
-> group by cno
-> having avg(grade)>= all
-> (select avg(grade)
-> from sc
-> group by cno));
实验代码及注释:
mysql> select sc.sno,sname,cname,grade
-> from sc,student,course
-> where sc.sno=student.sno
-> and course.cno=sc.cno
-> and sc.cno=7;
这个课程号是我上一问求出来的,大家根据自己的数据课程号可能不同。我只是懒得上面再重复一遍了。
实验代码及注释:
mysql> select sdept
-> from student
-> where sno=
-> (select sno
-> from sc
-> group by sno
-> having avg(grade)>= all
-> (select avg(grade)
-> from sc
-> group by sno));
实验代码及注释:
mysql> select count(*)/count(distinct sno) from sc;
实验代码及注释:
mysql> select sdept,count(*)/count(distinct student.sno)
-> from student,sc
-> where student.sno=sc.sno
-> group by sdept;
实验代码及注释:
mysql> select cno,count(*),max(grade),avg(grade),min(grade)
-> from sc
-> group by cno;
实验代码及注释:
mysql> select *
-> from student
-> where sno in
-> (select sno
-> from sc
-> group by sno
-> having avg(grade)>75
-> and min(grade)>60);
对于已有数据库进行建立相应视图,同时在视图上进行相应的查询、修改、删除等操作
实验代码及注释:
创建视图:
mysql> create view not_c1
-> as
-> select *
-> from student
-> where not exists
-> (select *
-> from sc
-> where cno=1 and sno=student.sno);
查询视图:
mysql> select sname
-> from not_c1
-> where sage=18;
修改视图:
mysql> update not_c1
-> set sage=sage+1
-> where sdept='MA';
删除视图:
mysql> drop view not_c1;
实验代码及注释:
创建视图
mysql> create view good_stu
-> as
-> select *
-> from student
-> where sno in
-> (select sno
-> from sc
-> group by sno
-> having avg(grade)>75
-> and min(grade)>60);
插入新的数据
mysql> insert sc(sno,cno,grade) values('201215127','7',100);
视图自动更新
mysql> select * from good_stu;
查询视图:
mysql> select *
-> from good_stu
-> where sage<20;
删除视图:
mysql> drop view good_stu;
实验代码及注释:
创建视图:
mysql> create view final(cno,countt,maxx,avgg,minn)
-> as
-> select cno,count(*),max(grade),avg(grade),min(grade)
-> from sc
-> group by cno;
该视图不可更改!
查询视图:
mysql> select *
-> from final
-> where countt>=4;
实验代码及注释:
创建视图:
mysql> create view c2_yes
-> as
-> select *
-> from student
-> where exists
-> (select * from sc where student.sno=sno and cno=1)
-> and not exists
-> (select * from sc where student.sno=sno and cno=2);
更新视图:
删除所有选修2的记录。
mysql> delete
-> from sc
-> where cno=2;
生成sc表插入数据,输出格式是可以直接用MySQL运行的。
生成一个在本cpp同目录下的sctxt.out文件,用记事本打开即可。
生成数据随机,可能不符合范式,可能不均匀,自行更改。
#include
#include
#include
using namespace std;
typedef long long ll;
int main()
{
freopen("sctxt.out","w",stdout);
srand((int)time(0));
for(int i=1;i<=20;i++)
{
int sno=rand()%6,cno=rand()%8,grade=rand()%100;//学号末尾1~6,课程号1~7,成绩百分制,可自行更改
if(sno==0) sno=1;
if(cno==0) cno=1;
cout<<"insert sc(sno,cno,grade) values(\'20121512"<