oracle習題SQL語句練習


表(一)Student (學生表)                        

屬性名

數據類型

可否為空

含 義

Sno

Varchar2(3)

學號(主碼)

Sname

Varchar2(8)

學生姓名

Ssex

Varchar2(2)

學生性別

Sbirthday

Date

學生出生年月

Class

Varchar2(5)

學生所在班級

 

create table student

(

  Sno   varchar2(3) not null,

  Sname   varchar2(8) not null,

  Ssex   varchar2(2) not null,

  Sbirthday   Date,

  Class  varchar2(5)

 

)

;

comment on column student.Sno

  is '學號';

comment on column student.Sname

  is '學生姓名';

comment on column student.Ssex

  is '學生性別';

comment on column student.Sbirthday

  is '學生出生年月';

comment on column student.Class

  is '學生所在班級';

 

表(二)Course(課程表)

屬性名

數據類型

可否為空

含 義

Cno

Varchar2(5)

課程號(主碼)

Cname

Varchar(10)

課程名稱

Tno

Varchar2(3)

教工編號(外碼)

 

create table Course

(

  Cno   varchar2(5) not null,

  Cname   varchar(10) not null,

  Tno   varchar2(3) not null

 

)

;

comment on column Course.Cno

  is '課程號';

comment on column Course.Cname

  is '課程名稱';

comment on column Course.Tno

  is '教工編號';

 

alter table Course

  add constraint pk_Course primary key (Cno);

alter table Course

  add constraint fk_Tno foreign key (Tno)

  references Teacher (Tno);

 

表(三)Score(成績表)

屬性名

數據類型

可否為空

含 義

Sno

Varchar2(3)

學號(外碼)

Cno

Varchar2(5)

課程號(外碼)

Degree

Number(4,1)

成績

主碼:Sno+ Cno

 

create table Score

(

  Sno   varchar2(3) not null,

  Cno   varchar2(5) not null,

  Degree Number(4,1)

)

;

comment on column Score.Sno

  is '學號';

comment on column Score.Cno

  is '課程號';

comment on column Score.Degree

  is '成績';

alter table Score

  add constraint fk_Sno foreign key (Sno)

   references  student (SNO);

  alter table Score

  add constraint fk_Cno foreign key (Cno)

   references  Course (Cno);

 

表(四)Teacher(教師表)

屬性名

數據類型

可否為空

含 義

Tno

Varchar2(3)

教工編號(主碼)

Tname

Varchar2(4)

教工姓名

Tsex

Varchar2(2)

教工性別

Tbirthday

Date

教工出生年月

Prof

Varchar2(6)

職稱

Depart

Varchar(10)

教工所在部門

 

create table TEACHER

(

  tno       VARCHAR2(3) not null,

  tname     VARCHAR2(4) not null,

  tsex      VARCHAR2(2) not null,

  tbirthday DATE,

  prof      VARCHAR2(6),

  depart    VARCHAR2(10) not null

)

comment on column TEACHER.tno

  is '教工編號';

comment on column TEACHER.tname

  is '教工姓名';

comment on column TEACHER.tsex

  is '教工性別';

comment on column TEACHER.tbirthday

  is '教工出生年月';

comment on column TEACHER.prof

  is '職稱';

comment on column TEACHER.depart

  is '教工所在部門';

alter table TEACHER

  add constraint PK_Tno primary key (TNO)

 

1-2數據庫中的數據

表(一)Student

Sno

Sname

Ssex

Sbirthday

class

108

曾華

1977-09-01

95033

105

匡明

1975-10-02

95031

107

王麗

1976-01-23

95033

101

李軍

1976-02-20

95033

109

王芳

1975-02-10

95031

103

陸君

1974-06-03

95031

 

 

insert into Student values('108','曾華','男',to_date ('1977-09-01','yyyy-mm-dd'),'95033');

insert into Student values('105','匡明','男',to_date ('1975-10-02','yyyy-mm-dd'),'95031');

insert into Student values('107','王麗','女',to_date ('1976-01-23','yyyy-mm-dd'),'95033');

insert into Student values('101','李軍','男',to_date ('1976-02-20','yyyy-mm-dd'),'95033');

insert into Student values('109','王芳','女',to_date ('1975-02-10','yyyy-mm-dd'),'95031');

insert into Student values('103','陸君','男',to_date ('1974-06-03','yyyy-mm-dd'),'95031');

 

 

 

表(二)Course

Cno

Cname

Tno

3-105

計算機導論

825

3-245

操作系統

804

6-166

數字電路

856

9-888

高等數學

831

 

insert into Course values('3-105','計算機導論','825');

insert into Course values('3-245'   ,'操作系統','804');

insert into Course values('6-166'   ,'數字電路', '856');

insert into Course values('9-888',  '高等數學',  '831');

 

 

表(三)Score

Sno

Cno

Degree

103

3-245

86

105

3-245

75

109

3-245

68

103

3-105

92

105

3-105

88

109

3-105

76

101

3-105

64

107

3-105

91

108

3-105

78

101

6-166

85

107

6-166

79

108

6-166

81

insert into score values('108', '6-166',  '81');

表(四)Teacher

Tno

Tname

Tsex

Tbirthday

Prof

Depart

804

李誠

1958-12-02

副教授

計算機系

856

張旭

1969-03-12

講師

電子工程系

825

王萍

1972-05-05

助教

計算機系

831

劉冰

1977-08-14

助教

電子工程系

 

insert into Teacher values('804',   '李誠', '男',   to_date('1958-12-02','yyyy-mm-dd'), '副教授', '計算機系');

insert into Teacher values('856',   '張旭', '男',   to_date('1969-03-12','yyyy-mm-dd'), '講師', '電子工程系');

insert into Teacher values('825',   '王萍', '女',   to_date('1972-05-05','yyyy-mm-dd'), '助教', '計算機系');

insert into Teacher values('831',   '劉冰', '女', to_date('1977-08-14','yyyy-mm-dd'), '助教', '電子工程系');

 

1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。

select sname,ssex,class from student;

2、 查詢教師所有的單位即不重復的Depart列。

select distinct * from student;

3、 查詢Student表的所有記錄。

select * from Student;

4、 查詢Score表中成績在60到80之間的所有記錄。

select * from Score where DEGREE > 60 and DEGREE < 80;

5、 查詢Score表中成績為85,86或88的記錄。

select * from Score where DEGREE = 85 or DEGREE = 86 or DEGREE = 88;

6、 查詢Student表中“95031”班或性別為“女”的同學記錄。

select * from Student where CLASS = 95031 or SSEX = '女';

7、 以Class降序查詢Student表的所有記錄。

select * from student order by class desc;

8、 以Cno升序、Degree降序查詢Score表的所有記錄。

select * from score order by cno asc , degree desc;

9、 查詢“95031”班的學生人數。

select class,count(1) as 數量 from student where class ='95031' group by class;

10、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)

select sno || cno as 學號和課程號 from score order by DEGREE desc;

11、 查詢每門課的平均成績。

select Cno,count(1) as 數量, avg(Degree) as 平均值 from Score group by Cno;

12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。

select Cno,count(1) as 數量, avg(Degree) as 平均值 from Score where cno like'3%' group by Cno having count(cno)>5;

13、查詢分數大於70,小於90的Sno列。

select * from Score where Degree between 70 and 90;

14、查詢所有學生的Sname、Cno和Degree列。

Select s.sname,c.cno,c.degree from student s , score c where s.sno=c.sno;

15、查詢所有學生的Sno、Cname和Degree列。

Select c.sno,q.cname,c.degree from student s , score c,course q where s.sno=c.sno and q.cno=c.cno;

 

16、查詢所有學生的Sname、Cname和Degree列。

Select s.sname,q.cname,c.degree from student s , score c,course q where s.sno=c.sno and q.cno=c.cno;

 

17、 查詢“95033”班學生的平均分。

Select class,avg(degree) as 平均值 from student s,score c where s.sno=c.sno and class = '95033' group by class ;

 

18、 假設使用如下命令建立了一個grade表:

create table grade(low  number(3),upp  number (3),rank  char(1))

insert into grade values(90,100,'A');

insert into grade values(80,89,'B');

insert into grade values(70,79,'C');

insert into grade values(60,69,'D');

insert into grade values(0,59,'E');

現查詢所有同學的Sno、Cno和rank列。

 

select s.sno,s.cno,g.rank from score s , grade g where s.Degree between g.low and g.upp;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM