《SQL语句测试》

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《SQL语句测试》相关的知识,希望对你有一定的参考价值。

新建一张学员信息表(student),要求:
1.字段如下:学号(sid),姓名(name),性别(sex),年龄(age),地址(address).
2.分别为字段添加约束:学号为主键,姓名为非空,性别为检查约束,年龄为检查约束,地址为默认约束.
SQL>create table student(
sid int constraint student_sid_pk primary key,
name varchar2(20) constraint student_name_nn not null,
sex varchar(20) constraint student_sex_ck check(sex in (‘man‘, ‘woman‘)),
age int constraint student_age_ck check(age>0),
address varchar2(20) default ‘sdzb‘);

3.创建序列插入学号(sid)列建议初始值从为1001,增量为1.
SQL> create sequence test_sid increment by 1 start with 1001;

4.插入记录.
SQL> insert into student values (test_sid.nextval, ‘Li‘, ‘man‘, 20, ‘sdwf‘);
SQL> insert into student values (test_sid.nextval, ‘Xu‘, ‘man‘, 23, ‘sdgm‘);
SQL> insert into student values (test_sid.nextval, ‘zhao‘, ‘woman‘, 25, ‘sdwf‘);
SQL> insert into student values (test_sid.nextval, ‘Si‘, ‘woman‘, 24, default);

 


新建一张课程表(course),要求:
1.字段如下:课程编号(cid),课程名称(subject).
2.分别为字段添加约束:课程编号为主键,课程名称为非空.
SQL> create table course (
cid int constraint course_cid_pk primary key,
subject varchar2(20) constraint course_subject_nn not null);

3.创建序列插入课程编号列(cid),建议初始值从为1,增量为1.
SQL> create sequence test_cid increment by 1 start with 1;

4.插入记录.
SQL> insert into course values (test_cid.nextval,‘oracle‘);
SQL> insert into course values (test_cid.nextval,‘English‘);
SQL> insert into course values (test_cid.nextval,‘Java‘);
SQL> insert into course values (test_cid.nextval,‘SQL‘);

 


新建一张学员考试成绩表(grade),要求:
1.字段如下:成绩编号(gid),学号(sid),课程编号(cid),考试成绩(score).
2.分别为字段添加约束:成绩编号为主键,学号为外键,课程编号为外键,考试成绩为非空.
SQL> create table grade (
gid int constraint grade_gid_pk primary key,
sid int constraint grade_sid_fk references student(sid),
cid int constraint grade_cid_fk references course(cid),
score int constraint grade_score_nn not null);


3.创建序列插入成绩编号列(gid),建议初始值从为101,增量为1.
SQL> create sequence test_gid increment by 1 start with 101;

4.插入记录.
SQL> insert into grade values (test_gid.nextval,1001,1,60);
SQL> insert into grade values (test_gid.nextval,1001,1,70);
SQL> insert into grade values (test_gid.nextval,1001,1,40);
SQL> insert into grade values (test_gid.nextval,1001,2,80);
SQL> insert into grade values (test_gid.nextval,1001,2,90);
SQL> insert into grade values (test_gid.nextval,1001,3,60);
SQL> insert into grade values (test_gid.nextval,1002,1,65);

 


针对以上三张表,要求完成如下:
1.按照课程编号分组并求出每一组的平均分数(每门课程的平均分)
SQL> select cid, avg(score) from grade group by cid;

2.按照课程编号分组并求出每一组及格人数(grade>=60)的平均分数(每门课程的及格人数的平均分)
SQL> select cid, avg(score) from grade where score>60 group by cid;

3.求每个学员所有的课程的平均分
SQL> select sid, avg(score) from grade group by sid;

4.求每个学员所有的(考试成绩)及格课程的平均分
SQL> select sid, avg(score) from grade where score>60;

5.每次内部测试不同学员的平均成绩(每个学员的每门课程的平均分)
SQL> select sid, cid, avg(score) from grade group by sid, cid;

6.查询补考过的学员的平均成绩(求出学员的课程编号(cid)在分组内出现过一次以上)(每个学员的每门课
程的课程编号出现过一次以上)
SQL> select sid, cid, avg(score) from grade group by sid,cid having count (cid)>1;

7.使用多表内连接查询,求出学员的学号,姓名,考号,科目和成绩
SQL> select s.sid, s.name, g.gid, c.subject, g.score
from student s, course c, grade g
where s.sid=g.sid
and g.cid=c.cid;

8.建立一张视图,包含学员的学号,姓名,考号,科目,成绩这些字段
SQL> create view vi_t1 as
select s.sid, s.name, g.gid, c.subject, g.score
from student s, course c, grade g
where s.sid=g.sid
and g.cid=c.cid;




























































以上是关于《SQL语句测试》的主要内容,如果未能解决你的问题,请参考以下文章

如何为 SQL 语句创建单元测试?

MyBatis测试mybatis执行sql语句步骤

sql 语句 急!!!! 数据将英文和数字去掉,只保留汉字的sql语句

两表关联查询SQL语句的,要怎么写?

如何在python文件中测试sql语句

Access sql语句运行时显示无效的sql语句是啥原因