HBU-數據庫第五週作業

第五週數據庫作業

注意

MySQL的數據庫名、表名、列名、別名大小寫規則是這樣的:

1、數據庫名與表名是嚴格區分大小寫的;

2、表的別名是嚴格區分大小寫的;

3、列名與列的別名在所有的情況下均是忽略大小寫的;

4、字段內容默認情況下是大小寫不敏感的。

#先建表,這是我們查詢的預備工作

Student表

創建表

create table student(
Sno int,
Sname varchar(30),
Ssex varchar(10),
Sage int,
Sdept varchar(20));

插入數據

insert into student values("2012151121","李勇","男",20,"CS");

insert into student values("2012151122","劉晨","女",19,"CS");

insert into student values("2012151123","王敏","女",18,"MA");

insert into student values("2012151125","張立","男",19,"IS");

Course表

創建表

create table course(
cno int,
cname  varchar(30),
cpno int,
ccredit int));

插入數據

insert into Course values("1","數據庫",5,4);

insert into Course values("2","數學",NULL,2);

insert into Course values("3","信息系統",1,4);

insert into Course values("4","操作系統",6,3);

insert into Course values("5","數據結構",7,4);

insert into Course values("6","數據處理",NULL,2);

insert into Course values("7","PASCAL語言",6,4);

SC表

建表

Create table Sc(
sno int,
cno int,
grade int);

插入數據

insert into SC values(201215121,1,92);

insert into SC values(201215121,2,85);

insert into SC values(201215121,3,88);

insert into SC values(201215122,2,90);

insert into SC values(201215122,3,80);

#查詢語句

1.查詢全體學生的詳細記錄

select * from student;

2.查詢全體學生的學號和姓名

select Sno,Sname from student;

3.查詢全體學生的學號和姓名,使用列別名改變查詢結果的列標題,把列名改爲漢字“學生編號”“學生姓名”。(選做)

select Sno as "學生編號",Sname as "學生姓名" from student;

4.查詢選修了課程的學生學號。(不去掉重複學號的和去重的都試一下)

select distinct Sno from SC;
select Sno from SC;

5.查詢‘CS’系全體學生的名單

select * from student where Sdept="CS";

6.查詢‘1’號課的選課情況

select * from SC where Cno=1;

7.查詢男同學的學號和姓名

select Sno,Sname from student where Ssex="男";

8.查詢考試成績有不及格的課程的課程號。(不去掉重複學號的和去重的都試一下)

select Cno from sc where grade<60;
select distinct Cno from sc where grade<60;

9.查詢成績在95~99分(包括95分和99分)之間的選課記錄的學號、課程號和成績.

select * from SC where grade>=95 and grade<=99;
select * from SC where grade between 95 and 99;

10.查詢成績不在95~99分之間的學號、課程號和成績。

select * from SC where grade not between 95 and 99;

11.查詢年齡是18歲、20歲或24歲的學生的姓名和性別。(幾種寫法?)

select Sname,Ssex from student where Sage=18 or Sage=20 or Sage=24;
select Sname,Ssex from student where Sage in(18,20,24);

12.查詢年齡既不是18歲、20歲,也不是24歲的學生的姓名和性別。

select Sname,Ssex from student where Sage not in(18,20,24);

13.查詢課程名中第2個字爲 “據” 字的課程的課程號、課程名和學分。

select Cno,Cname,Ccredit from course where substr(Cname,2,1)="據";

14.查詢課程名爲“A_ C”課程的課程號和學分

select Cno,Ccredit,Cname from course where Cname like "%A%C%";

15.查詢沒有先行課的課程號和課程名

select Cno,Cname from course where cpno is NULL;

16.查詢缺少了成績的學生的學號和課程號

select Sno,Cno from SC where grade is null;

17.查詢男同學的學號、姓名、年齡和所在系,將查詢結果按所在系的系號降序排列,同一系中的學生按年齡升序排列。

select Sno,Sname,Sage,Sdept from Student where Ssex="男" order by Sdept desc ,Sage asc;

18.查詢開設的課程總門數.

select count(*) from Course;

19.查詢有學生選的課程的門數

select count(distinct Cno) from SC;

20.查詢全體同學的最小年齡。

select min(Sage) from student;

21.查詢男同學的最小年齡。

select min(Sage) from student where Ssex="男";

22.查詢‘CS’系男同學的最小年齡。

select min(Sage) from student where Sdept="CS" and Ssex="男";

23.查詢‘95001’同學的選課平均成績。

select avg(grade) from SC where Sno="95001";

24.查詢‘95001’同學的選課最高成績

select max(grade) from SC where Sno="95001";

25.查詢有選課記錄的同學的學號和他相應的選課門數。

select Sno,count(Sno) from sc group by Sno;

26查詢‘CS’系或‘MA’系姓劉的學生的信息。

select * from student where substr(Sname,1,1)="劉" and Sdept in("CS","MA");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章