数据库查询1

CREATE TABLE Student

(Sno char(9) not null unique,

 Sname char(20) unique,

 Ssexchar(2),

 Sagesmallint check(Sage>16),

 Sdept char(20)

);

 

CREATE TABLE Course

(Cno char(4) primary key,

 Cname char(40) unique,

 Cpno char(4),

 Ccredit smallint,

);

 

CREATE TABLE Teacher

(

Tno char(9) primary key,

Tname char(20),

Department char(20),

Email char(25),

Salary smallint

);

 

CREATE TABLE  SC

(Sno char(9) not null,

 Cnochar(4) not null,

 Grade int

);

 

ALTER TABLE SC Alter column Grade float;

ALTER TABLE Student ADD Scome DATETIME;

ALTER TABLE Student drop column Scome;

ALTER TABLE Teacher drop column Email;

ALTER TABLE Student drop UQ__Student__7D78A4E7;

 

INSERT INTO Student VALUES ('200215121','李勇','',20,'CS');

INSERT INTO Student VALUES ('200215122','刘晨','',19,'CS');

INSERT INTO Student VALUES ('200215123','王敏','',18,'MA');

INSERT INTO Student VALUES ('200215125','张立','',19,'IS');

 

INSERT INTO Course VALUES (1,'数据库',5,4);

INSERT INTO Course(Cno,Cname,Ccredit)VALUES (2,'数学',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(Cno,Cname,Ccredit)VALUES(6,'数据处理',2);

INSERT INTO Course VALUES (7,'PASCAL语言',6,4);

 

INSERT INTO SC VALUES('200215121',1,92);

INSERT INTO SC VALUES('200215121',2,85);

INSERT INTO SC VALUES('200215121',3,88);

INSERT INTO SC VALUES('200215122',2,90);

INSERT INTO SC VALUES('200215122',3,80);

 

CREATE NONCLUSTERED INDEX Stusage ON student(SageDESC);

CREATE CLUSTERED INDEX CLU_Stusno ONstudent(Sno);

CREATE CLUSTERED INDEX CLU_Stusname ONstudent(Sname);

--无法对表 'student' 创建多个聚集索引。请在创建新聚集索引前删除现有的聚集索引'CLU_Stusno'--

CREATE UNIQUE INDEX ASC_CC ON Course(Ccredit);

CREATE UNIQUE INDEX UN_SC ON SC(Sno ASC,CnoDESC);

 

DROP INDEX Student.CLU_Stusno;

DROP INDEX Sc.UN_SC;

DROP TABLE Teacher;

 

--1、简单查询

-- (1)      查询全体学生的姓名、学号、所在系。

select Sname,Sno,Sdept from Student;

-- (2)      查询全体学生的详细记录。

select * from Student;

-- (3)      查询全体学生的姓名、出生年份和所有系,要求用小写字母表示所有系名,并使用列别名改变查询结果的列标题。

selectSname NAME,'Year of Birth:' BIRTH ,2012-Sage BIRTHDAY,LOWER(Sdept)DEPARTMENTfrom Student;

-- (4)      查询选修了课程的学生学号。

select Sno from SC;

-- (5)      查询所有年龄在20岁以下的学生姓名及其年龄。

select Sname,Sage from Student whereSage<20;

-- (6)      查询年龄不在20~23岁之间的学生姓名、系别和年龄。

select Sname,Sdept,Sage from Student WhereSage not between 20 and 23;

-- (7)      查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。

select Sname,Ssex from Student where Sdeptnot in ('CS','MA','IS');

-- (8)      查询学号为20080711的学生的详细情况。(具体的学号值根据表中数据确定)

select * from Student where Sno='20080711';

-- (9)      查询姓“刘”且全名为三个汉字的学生姓名。

select Sname from Student where Sname like'__';

-- (10)查询名字中第3个字为“阳”字的学生的姓名和学号。

select Sname,Sno from Student where Snamelike '__';

-- (11)查询所有不姓刘的学生姓名。

select Sname from Student where Sname notlike '%';

-- (12)查询以"DB_"开头,且倒数第3个字符为 i的课程的详细情况。

select * from Course where Cnamelike'DB\_%i__'escape'\';

-- (13)查所有有成绩的学生学号和课程号。

select Sno,Cno from SC where Grade is notnull;

-- (14)查询计算机系年龄在20岁以下的学生姓名。

select Sname from Student where Sdept='CS'and Sage<20;

-- (15)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。

select Sno,Grade from SC where Cno='3'order by Grade desc;

-- (16)查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

select * from Student order by Sdept,Sagedesc;

-- (17)查询年龄最大的前三个学生的姓名。

select top 3 Sname from Student order bySage desc;

-- (18)查询学生总人数。

select count(*) from Student;

-- (19)查询选修了课程的学生人数。

select count(distinct Sno) from SC;

-- (20)查询选修2号课程的学生平均成绩。

select avg(Grade) from SC where Cno='2';

-- (21)查询选修1号课程的学生最高分数。

select max(Grade) from SC where Cno='1';

-- (22)求各个课程号及相应的选课人数。

select Cno,count (Sno) from SC group byCno;

-- (23)查询选修了3门以上课程的学生学号。

select Sno from SC group by Sno havingcount(*)>3;

-- (24)查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。

select Sno,count(*) from SC whereGrade>=90 group by Sno having count(*)>=3;

-- (25)查询学生20060711选修课程的总学分。

select sum(Ccredit) from SC,Course whereSno='20060711' and SC.Cno=Course.Cno;

-- (26)查询每个学生选修课程的总学分。

select sum(Ccredit) from SC,Course whereSC.Cno=Course.Cno group by Sno;

 

--2、连接查询

-- (1)查询每一门课的间接先修课(即先修课的先修课)

select first.Cno,second.Cpno from Coursefirst,course second where first.Cpno=second.Cno;

-- (2)查询每个学生及其选修课程的情况包括没有选修课程的学生(用外连接)

selectStudent.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from Student left outer join SCon(Student.Sno=SC.Sno);

-- (3)查询选修2号课程且成绩在90分以上的所有学生的学号、姓名

select Student.Sno,Sname from Student,SCwhere Student.Sno=SC.Sno and SC.Cno='2' and SC.Grade>90;

-- (4)查询每个学生的学号、姓名、选修的课程名及成绩。

select Student.Sno,Sname,Cname,Grade fromStudent,SC,Course where Student.Sno=SC.Sno and SC.Cno=Course.Cno;

--3、嵌套查询

-- (5)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查询)

select Sno,Sname,Sdept from Student whereSdept in (select Sdept from Student where Sname='刘晨');

-- (6)查询选修了课程名为“信息系统”的学生学号和姓名

select Sno,Sname from Student where Sno in(select Sno from SC where Cno in(select Cno from Course where Cname='信息系统'));

-- (7)查询其他系中比信息系任意一个(其中某一个)学生年龄小的学生姓名和年龄

select Sname,Sage from Student whereSage<ANY (select Sage from Student where Sdept='CS') and Sdept<>'CS';

-- (8)查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。分别用ALL谓词和集函数

select Sname,Sage from Student whereSage<ALL(select Sage from Student where Sdept='CS') and Sdept<>'CS';

-- (9)查询所有选修了1号课程的学生姓名。(分别用嵌套查询和连接查询)

select Sname from Student where  exists (select * from SC whereSno=Student.Sno and Cno='1');

-- (10)查询没有选修1号课程的学生姓名。

select Sname from Student where not exists(select * from SC where Sno=Student.Sno and Cno='1');

-- (11)查询选修了全部课程的学生姓名。

select Sname from Student where not exists(select * from Course where not exists (select * from SC where Sno=Student.Snoand Cno=Course.Cno));

-- (12)查询至少选修了学生95002选修的全部课程的学生号码。

select distinct Sno from SC SCX where notexists (select * from SC SCY where SCY.Sno='95002' and not exists (select *from SC SCZ where SCZ.Sno=SCX.Sno and SCZ.Cno=SCY.Cno));

--4、集合查询

-- (13)查询选修了课程1或者选修了课程2的学生的信息。

SELECT Sno FROM SC WHERE Cno='1' UNION SELECT Sno FROM SC WHERE Cno='2';

-- (14)查询计算机科学系中年龄不大于19岁的学生的信息。

select * from student where Sdept='CS'intersect select * from student where Sage<=19;

-- (15)查询既选修了课程1又选修了课程2的学生的信息。

select * from SC where Cno='1' intersectselect Sno from SC where Cno='2';

-- (16)查询计算机科学系的学生与年龄不大于19岁的学生的差集。

select * from student where Sdept='CS'ecxept select * from student where Sage<=19;

--5、综合查询

--1、查询所有学生都选修的课程的课程号和课程名

select Cno,Cname from course where notexists(select * from student where not exists(select * from sc wherecno=course.cno and sno=student.sno));

--2、查询与课程名“数据库”先行课相同的课程号和课程名

select Cno,Cname from course where Cpno in(select Cpno from course where Cname like '数据库');

--3、查询成绩在85分以上的学生选课情况,要求显示学生姓名、课程名及成绩

select sname,cname,grade from student,course,scwhere student.sno=sc.sno and course.cno=sc.cno and grade>85;

--4、查询总成绩大于500、总学分大于30的学生学号、总成绩和总学分

select student.sno,sum(grade),sum(ccredit)from student,course,sc where student.sno=sc.sno and sc.cno=course.cno

group by student.sno having sum(grade)>500and sum(ccredit)>30;

--5、查询每门课程及其被选修的情况

select course.cno,course.cname,sc.gradefrom course full join sc on (sc.cno=course.cno);

--6、查询至少选修了“刘晨”所选全部课程的学生学号和姓名

select distinct sname,student.sno fromstudent,sc scx where not exists

(select * from sc scy wherestudent.sno=scx.sno and student.sname='刘晨'and not exists

(select * from sc scz where scz.sno=scx.snoand scz.cno=scy.cno));

--7、查询没有选修“数据库”这门课程的学生学号和姓名

select sname,student.sno fromcourse,student,sc where

student.sno=sc.sno and sc.cno=course.cnoand not exists

(select * from course where cname='数据库');

--8、查询只选修了“数据库”这门课程的学生学号、姓名和成绩

select student.sno,sname,grade fromstudent,sc where student.sno=sc.sno and not exists

(select * from sc scx,course wherecourse.cno=scx.cno and course.cname!='数据库');

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django是一个流行的Python Web框架,它提供了许多功能来简化数据库操作和查询。在Django中,你可以使用ORM(对象关系映射)来查询数据库。ORM允许你将数据模型与数据库表分开,从而使得数据库操作更加灵活和易于管理。 Django提供了许多内置的数据库查询工具,包括以下几种: 1. 查询构建器:Django提供了强大的查询构建器,允许你使用SQL语句来构建查询。你可以使用`QuerySet`对象来构建查询,它提供了许多方法来过滤、排序、分组和连接数据。 2. 过滤器:Django的查询构建器支持各种过滤器,例如`filter()`、`exclude()`、`order_by()`等,这些方法允许你根据条件对数据进行筛选和排序。 3. 关联查询:Django支持关联查询,可以方便地查询多个表之间的关联数据。你可以使用`select_related()`、`prefetch_related()`等方法来执行关联查询,以提高性能和减少数据库交互次数。 4. 聚合查询:Django的查询构建器还支持聚合查询,可以用于计算总和、平均值、计数等统计信息。你可以使用`annotate()`、`aggregate()`等方法来进行聚合查询。 5. 数据库迁移:Django提供了数据库迁移工具,可以帮助你在开发过程中轻松地更新数据库结构。你可以使用`makemigrations`和`migrate`命令来创建和执行迁移文件,以更新数据库结构。 下面是一个简单的示例,展示了如何在Django中使用查询构建器和过滤器进行数据库查询: ```python from myapp.models import MyModel # 获取所有满足条件的模型实例 queryset = MyModel.objects.filter(status=1) # 按照指定字段排序 queryset = queryset.order_by('created_at') # 获取指定字段的总数 total = queryset.count() ``` 这只是一些基本的介绍,Django还提供了更多高级的查询功能和工具,可以根据具体需求进行学习和使用。你可以参考Django官方文档和教程,以获取更多关于数据库查询的详细信息和示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值