1. 程式人生 > >《SQL語句測試》

《SQL語句測試》

score 姓名 subject 成績 建議 記錄 not null 內部 key

新建一張學員信息表(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語句測試》