SQL数据库数据更新、视图操作

以下题目基于xsgl数据库实现,该数据库包括:
kc(课程表):课程号,课程名,学分,学时数,先行课号
xs(学生表):学号,姓名,性别,出生时间,专业,总学分
cj(选课表):学号,课程表,成绩

--1.将被全部学生都选修了的课程的总学分改为4学分。
update kc
set kc.学分=4
where kc.课程号 in 
(select kc.课程号 
from kc where not exists (
select *from xs where not exists 
(select * from cj c1 where c1.课程号=kc.课程号 and c1.学号=xs.学号))) 

update kc
set kc.学分=4
where kc.课程号 in 
(select distinct cj.课程号
from cj group by cj.课程号
having count(cj.课程号)=(select count(xs.学号)from xs))

--2.从学生表删除没有选课的学生。
delete 
from xs
where xs.学号 not in
    (select cj.学号 
	 from cj)

--3.将每个学生的平均分,总分和选课门数插入到数据库中(学号,姓名,平均分,总分,选课门数)
create table New_data(学号 char(10),
姓名 nchar(10),平均分 numeric(10,2),总分 numeric(10,2),选课门数 smallint) 
insert 
into New_data
select xs.学号,xs.姓名,avg(cj.成绩),sum(cj.成绩),count(cj.课程号)
from xs left join cj on xs.学号=cj.学号
group by xs.学号,xs.姓名

select xs.学号,xs.姓名,avg(cj.成绩)平均分,sum(cj.成绩) 总分,count(cj.课程号) 选课门数
into xs1
from xs left join cj on xs.学号=cj.学号
group by xs.学号,xs.姓名
--4.创建每门课程的平均分和选课人数的视图(课程号,课程名,平均分,人数)
create view Course(课程号,课程名,平均分,人数)
as
select kc.课程号,kc.课程名,avg(cj.成绩) 平均分,count(cj.学号) 人数
from kc left join cj on kc.课程号=cj.课程号
group by kc.课程号,kc.课程名

--5.将李强同学从学生表删除(提示应该先删除李强同学的选课记录)
delete 
from cj
where cj.学号 =(
select xs.学号 from xs where xs.姓名='李强')
delete 
from xs
where xs.姓名='李强'

--6.插入一条选课记录(具体内容自己选)。
insert 
into xs
values('201700800','李四',null,null,null,null,null,null,null)
insert 
into cj
values('201700800','A001',NULL)

--7.创建网络工程专业的学生的选课信息的视图,要求视图包含,学号,姓名,专业,课程号,课程名,成绩
create view Ima(学号,姓名,专业,课程号,课程名,成绩)
as
select xs.学号,xs.姓名,cj.课程号,kc.课程号,kc.课程名,cj.成绩
from xs left join cj on xs.学号=cj.学号 
 right join  kc on cj.课程号=kc.课程号
where xs.专业='网络工程'
 
--8.查询网络工程专业的各科的平均成绩,要求使用第7题创建的视图进行查询
select Ima.课程号,avg(Ima.成绩) 平均成绩
from Ima
group by Ima.课程号

--9.查询被信息管理专业的学生都选修了的课程的课程号,课程名
select kc.课程号,kc.课程名
from kc
where  not exists
(select *from xs
where xs.专业='信息管理' and not exists
(select *from cj c1
 where c1.学号=xs.学号 and c1.课程号=kc.课程号))


--10.显示选修课程数最多的学号及选修课程数最少的学号,姓名(使用派生表实现)
select xs.学号,姓名,count(*) 门数
from  xs,cj,(select top 1 count(*)
			from cj
			group by cj.学号
			order by count(*)desc
			)a(门数),
			(select top 1 count(*)
			from cj
			group by cj.学号
			order by count(*)
			)b(门数)
where cj.学号=xs.学号
group by xs.学号,xs.姓名,a.门数,b.门数
having count(*)=a.门数 or count(*)=b.门数

select xs.学号 ,c1.门数 
from xs ,(select cj.学号,count(*) from cj 
          group by cj.学号) as c1(学号,门数)
where xs.学号 =c1.学号 and( c1.门数=
                              (select top 1 count(*) 
							   from cj
							   group by cj.学号 
							   order by count(*) desc)
	or c1.门数=(select top 1 count(*) 
				 from cj
			    group by cj.学号 
				order by count(*)) )

--11.查询每个学生成绩高于自己的平均成绩的学号,姓名,课程号和成绩(使用派生表实现)
select xs.学号,xs.姓名,cj.课程号,cj.成绩
from xs,cj,(select c1.学号,avg(c1.成绩) 平均成绩 from cj c1 group by c1.学号) as avg_score 
where xs.学号=cj.学号 and cj.学号=avg_score.学号 and cj.成绩>avg_score.平均成绩
--12.自己验证with check option的作用。
create view IS_Student
as
select xs.学号,xs.姓名,xs.性别,xs.专业
from xs
where xs.专业='网络工程'
with check option
 
 insert into IS_Student(学号,姓名)
 values('2001001','张三') 
--13.创建一个网络工程系的学生基本信息的视图MA_STUDENT,在此视图的基础上,
--再定义一个该专业女生信息的视图,然后再删除MA_STUDENT,观察执行情况。
--打不开女生信息的视图
create view MA_STUDENT
as
select *
from xs
where xs.专业='网络工程'
with check option
 
create view Woman
as
select *
from MA_STUDENT
where MA_STUDENT.性别='女'

drop view MA_STUDENT

使用Northwind数据库完成下列操作

--1. 将员工lastname是: Peacock处理的订单中购买数量超过50的商品折扣改为七折
update [Order Details]
set Discount=0.3
where [Order Details].OrderID in
(select o1.OrderID
from Employees,Orders,[Order Details] o1
where Employees.EmployeeID=Orders.EmployeeID and Orders.OrderID=o1.OrderID
  and Employees.LastName='Peacock' and o1.Quantity>50) and [Order Details].Quantity>50
--2. 删除lastname是: Peacock处理的所有订单
delete 
from [Order Details]
where [Order Details].OrderID in(select distinct o1.OrderID
from Employees,Orders,[Order Details] o1
where Employees.EmployeeID=Orders.EmployeeID and Orders.OrderID=o1.OrderID
  and Employees.LastName='Peacock')
delete
from Orders
where Orders.EmployeeID=(
select Employees.EmployeeID
from Employees
where Employees.LastName='Peacock')

--3. 将每个订单的订单编号,顾客编号,产品总数量,总金额插入到数据库中
create table Order_Information
(OrdersID int, CustomerID nchar(5),Number smallint ,Totalmoney money)
insert
into Order_Information
select Orders.OrderID,Orders.CustomerID,sum([Order Details].Quantity) 数量,
sum([Order Details].Quantity*[Order Details].UnitPrice*(1-[Order Details].Discount)) 金额
from Orders,[Order Details]
where Orders.OrderID=[Order Details].OrderID
group by Orders.OrderID,Orders.CustomerID 

--4. 插入一个新的订单,要求该订单购买了商品编号为5,7,9的商品。
--(5号商品买了10个,7号买了20个,9号买了15个,都没有折扣)
--报错:仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'Orders'中的标识列指定显式值
insert
into Orders(CustomerID,EmployeeID)
values('VINET',6)

declare @id int
set id=(select max(Orders.OrderID) from Orders)

insert 
into [Order Details]
values(id, '5', '100', '10', '0');
inset into [Order Details]
values(id, '7', '130', '20', '0');
insert into [Order Details]
values(id, '9', '160', '15', '0');
      
--5.将每年每个员工处理订单的数量和订单的总金额创建为视图
create view Employ
as
select Employees.EmployeeID,datename(yy,Orders.OrderDate) 年份,
	   count(distinct Orders.OrderID) 订单数量,sum([Order Details].Quantity) 数量,
       sum([Order Details].Quantity*[Order Details].UnitPrice*(1-[Order Details].Discount)) 订单总金额
from Employees left join Orders on Employees.EmployeeID=Orders.EmployeeID
			   left join [Order Details] on Orders.OrderID=[Order Details].OrderID
group by Employees.EmployeeID,datename(yy,Orders.OrderDate)


你可能感兴趣的:(数据库)