数据库实验二

 

 

(1)查询性别为“男”的所有学生的名称并按学号升序排列。

select hsy_Sname,hsy_Sno
from Husy_Students
where hsy_Ssex='男'
order by hsy_Sno asc

 (2)查询学生的选课成绩合格的课程成绩,并把成绩换算为积分。积分的计算公式为:[1+(考试成绩-60)*0.1]*Ccredit考试成绩>=60 否则=0

 SELECT hsy_Sno,hsy_Cno,hsy_Score,(hsy_Score-60)*0.1+1
FROM Husy_Reports
WHERE hsy_Score>=60

(3)查询学分是3或4的课程的名称。

select hsy_Cname
from Husy_Courses
where hsy_Scredit=3 or hsy_Scredit=4

(4)查询所有课程名称中含有“算法”的课程编号。

select hsy_Cno
from Husy_Courses
where hsy_Cname like '%算法%'

(5)查询所有选课记录的课程号(不重复显示)。

select distinct hsy_Cno
from Husy_Reports

(6)统计所有老师的平均工资。

select avg(hsy_Tsalary)

from Husy_Teachers

7)查询所有教师的编号及选修其课程的学生的平均成绩,按平均成绩降序排列。

(8)统计各个课程的选课人数和平均成绩。

select hsy_Cno,count (hsy_Sno)人数,avg(hsy_Score)平均成绩

from Husy_Reports

group by hsy_Cno

(9)查询至少选修了三门课程的学生编号和姓名。

select hsy_Sno,hsy_Sname

from Husy_Students

where hsy_Sno in(

select hsy_Sno from Husy_Reports

group by hsy_Sno

having count(hsy_Cno)>=3 )

(10)查询编号S26的学生所选的全部课程的课程名和成绩。

select hsy_Cname,hsy_Score

from Husy_Reports,Husy_Courses

where hsy_Sno='S26' and Husy_Courses.hsy_Cno =Husy_Reports.hsy_Cno

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

11)查询所有选了“数据库原理及其应用”课程的学生编号和姓名。

12)求出选择了同一个课程的学生对

(13)求出至少被两名学生选修的课程编号。

select hsy_Cno

from Husy_Reports

group by hsy_Cno

having count(hsy_Sno)>=2

(14)查询选修了编号S26的学生所选的某个课程的学生编号。

select distinct hsy_Sno

FROM Husy_Reports

where hsy_Cno in(

        select hsy_Cno from Husy_Reports

              where hsy_Sno='S26')     

(15)查询学生的基本信息及选修课程编号和成绩。

select Husy_Students.hsy_Sno,hsy_Sname,hsy_Semail,hsy_Cno,hsy_Score

from Husy_Students,Husy_Reports            

where Husy_Reports.hsy_Sno=Husy_Students.hsy_Sno

         

(16)查询学号S52的学生的姓名和选修的课程名称及成绩。

(17)查询和学号S52的学生同性别的所有学生资料。

(18)查询所有选课的学生的详细信息。

select * from Husy_Students

where hsy_Sno in(select distinct hsy_Sno from Husy_Reports)

(19)查询没有学生选的课程的编号和名称。

(20)查询选修了课程名为C++的学生学号和姓名。

select Husy_Students.hsy_Sno,hsy_Sname

from Husy_Students,Husy_Reports,Husy_Courses

where hsy_Cname='C++' and Husy_Reports.hsy_Cno=Husy_Courses.hsy_Cno

   and Husy_Reports.hsy_Sno=Husy_Students.hsy_Sno

(21)找出选修课程UML或者课程C++的学生学号和姓名。

select hsy_Sno,hsy_Sname from Husy_Students

where hsy_Sno

in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='C++' or hsy_Cname='UML')  

      )

(22)找出和课程UML或课程C++的学分一样课程名称。

select hsy_Cname

from Husy_Courses

where hsy_Scredit in

(select hsy_Scredit from Husy_Courses

where hsy_Cname='UML'or hsy_Cname='C++')

(23)查询所有选修编号C01的课程的学生的姓名。

select hsy_Sname

from Husy_Students

where hsy_Sno

in(select distinct hsy_Sno from Husy_Reports

   where hsy_Cno='C01' )

24)查询选修了所有课程的学生姓名。

(25)利用集合查询方式,查询选修课程C++或选择课程JAVA的学生的编号、姓名和积分。

select hsy_Sno,hsy_Sname,hsy_Scredit from Husy_Students

where hsy_Sno

in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='C++' )        

         )

union

select hsy_Sno,hsy_Sname,hsy_Scredit from Husy_Students

where hsy_Sno in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='UML' ) 

 )  

(26)实现集合交运算,查询既选修课程C++又选修课程JAVA的学生的编号、姓名和积分。

select hsy_Sno,hsy_Sname,hsy_Scredit from Husy_Students

where hsy_Sno

in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='C++' )        

         )

Intersect

select hsy_Sno,hsy_Sname,hsy_Scredit from Husy_Students

where hsy_Sno in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='UML' ) 

 )  

(27)实现集合减运算,查询选修课程C++而没有选修课程JAVA的学生的编号。

select hsy_Sno,hsy_Sname,hsy_Scredit from Husy_Students

where hsy_Sno

in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='C++')         

         )

except

select hsy_Sno,hsy_Sname,hsy_Scredit from Husy_Students

where hsy_Sno in(select hsy_Sno from Husy_Reports

   where hsy_Cno

   in(select hsy_Cno from Husy_Courses

      where hsy_Cname='JAVA' ) 

 )       

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验目的: 1.了解opengauss数据库的数据类型和数据结构 2.掌握opengauss数据库的表的创建、修改和删除 3.掌握opengauss数据库的数据的插入、更新和删除 实验环境: 操作系统:CentOS 7.7 数据库版本:opengauss-2.0.0 实验步骤: 1.创建数据库 登录opengauss数据库,创建一个名为testdb的数据库。 $ gsql -d postgres -p 5432 -U gauss -W Password: psql (2.0.0) Type "help" for help. postgres=# create database testdb; CREATE DATABASE postgres=# \q 2.创建表 使用testdb数据库,创建一个名为students的表,包含学生的姓名、年龄、性别和所在城市等信息。 $ gsql -d testdb -p 5432 -U gauss -W Password: psql (2.0.0) Type "help" for help. testdb=# CREATE TABLE students ( id SERIAL PRIMARY KEY, name VARCHAR(20), age INT, gender VARCHAR(10), city VARCHAR(20) ); CREATE TABLE testdb=# \d students Table "public.students" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+---------------------------------- id | integer | | not null | nextval('students_id_seq'::regclass) name | character varying(20) | | | age | integer | | | gender | character varying(10) | | | city | character varying(20) | | | Indexes: "students_pkey" PRIMARY KEY, btree (id) 3.插入数据 向students表中插入一些数据。 testdb=# INSERT INTO students (name, age, gender, city) VALUES ('张三', 20, '男', '北京'); INSERT 0 1 testdb=# INSERT INTO students (name, age, gender, city) VALUES ('李四', 22, '女', '上海'); INSERT 0 1 testdb=# INSERT INTO students (name, age, gender, city) VALUES ('王五', 18, '男', '广州'); INSERT 0 1 testdb=# INSERT INTO students (name, age, gender, city) VALUES ('赵六', 21, '女', '深圳'); INSERT 0 1 4.查询数据 查询students表中的所有数据。 testdb=# SELECT * FROM students; id | name | age | gender | city ----+------+------+--------+-------- 1 | 张三 | 20 | 男 | 北京 2 | 李四 | 22 | 女 | 上海 3 | 王五 | 18 | 男 | 广州 4 | 赵六 | 21 | 女 | 深圳 (4 rows) 5.更新数据 将students表中张三的年龄修改为25。 testdb=# UPDATE students SET age=25 WHERE name='张三'; UPDATE 1 testdb=# SELECT * FROM students WHERE name='张三'; id | name | age | gender | city ----+------+------+--------+-------- 1 | 张三 | 25 | 男 | 北京 (1 row) 6.删除数据 将students表中年龄大于等于20岁的学生记录删除。 testdb=# DELETE FROM students WHERE age>=20; DELETE 2 testdb=# SELECT * FROM students; id | name | age | gender | city ----+------+------+--------+------ 3 | 王五 | 18 | 男 | 广州 (1 row) 7.删除表 删除students表。 testdb=# DROP TABLE students; DROP TABLE testdb=# \q 实验总结: 本次实验通过创建表、插入数据、查询数据、更新数据和删除数据等操作,掌握了opengauss数据库的表的创建、修改和删除,以及数据的插入、更新和删除等基本操作。同时,也了解了opengauss数据库的数据类型和数据结构,为后续的实验打下了基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值