数据库实验三答案

(1)对应的程序如下:

USE Library  
SELECT COUNT(DISTINCT(图书名)) AS '图书品种数目'  
FROM book  

(2)对应的程序如下:

USE Library  
SELECT 图书名 AS '书名',COUNT(*) AS '数目'  
FROM book  
GROUP BY 图书名  

(3)对应的程序如下:

USE Library  
SELECT 班号 AS '班号',COUNT(*) AS '人数'  
FROM student  
GROUP BY 班号  

(4)对应的程序如下:

USE Library  
SELECT sc.系名 AS '系名',COUNT(*) AS '人数'  
FROM student s,depart sc  
WHERE s.班号=sc.班号  
GROUP BY sc.系名  

(5)对应的程序如下:

USE Library  
SELECT s.学号,s.姓名,b.图书名,bor.借书日期  
FROM student s,book b,borrow bor  
WHERE s.学号=bor.学号 AND b.图书编号=bor.图书编号  
ORDER BY s.学号  

(6)对应的程序如下:

USE Library  
SELECT s.学号,s.姓名  
FROM student s  
WHERE s.学号 IN (SELECT DISTINCT 学号 FROM borrow)  

(7)对应的程序如下:

USE Library  
SELECT s.学号,s.姓名,COUNT(bor.学号) As '数目'  
FROM student s,borrow bor  
WHERE s.学号=bor.学号  
GROUP BY s.学号,s.姓名  

(8)对应的程序如下:

USE Library  
SELECT s.学号 '学号',s.姓名 '姓名',COUNT(bor.学号) As '借图书册数'  
FROM student s,borrow bor  
WHERE s.学号=bor.学号  
GROUP BY s.学号,s.姓名  
HAVING COUNT(bor.学号)>=2  

(9)对应的程序如下:

USE Library  
SELECT s.学号,s.姓名,s.班号  
FROM student s,book b,borrow bor  
WHERE s.学号=bor.学号 AND b.图书编号=bor.图书编号  
     AND b.图书名='操作系统'
ORDER BY s.班号  

(10)对应的程序如下:

USE Library  
SELECT s.班号,COUNT(bor.学号) AS '总数'  
FROM student s,borrow bor  
WHERE s.学号=bor.学号  
GROUP BY s.班号  

(11)对应的程序如下:

USE Library  
SELECT SUBSTRING(图书编号,1,3) As '图书类别',CAST(AVG(定价) AS decimal(4,1)) AS '平均价'  
FROM book  
GROUP BY SUBSTRING(图书编号,1,3)  

(12)对应的程序如下:

USE Library  
SELECT SUBSTRING(图书编号,1,3) As '图书类别',CAST(AVG(定价) AS decimal(4,1)) AS '平均价'  
FROM book  
GROUP BY SUBSTRING(图书编号,1,3)  
HAVING AVG(定价)>30  

(13)对应的程序如下:

USE Library  
SELECT MAX(平均价) AS '最高平均价'  
FROM (SELECT SUBSTRING(图书编号,1,3) As '图书类别',CAST(AVG(定价) AS decimal(4,1)) AS '平均价'  
FROM book  
GROUP BY SUBSTRING(图书编号,1,3)) a  

(14)对应的程序如下:

USE Library  
PRINT '当前日期:'+CONVERT(char(10),GETDATE(),102)  
SELECT s.学号,s.姓名,bor.图书编号,b.图书名  
FROM student s,book b,borrow bor  
WHERE s.学号=bor.学号 AND b.图书编号=bor.图书编号  
AND DATEDIFF(day,借书日期,GETDATE())>45  

(15)对应的程序如下:

USE Library  
SELECT 图书编号,图书名,作者  
FROM book  
WHERE 图书名 LIKE '%工程%'  

(16)对应的程序如下:

USE Library  
SELECT 图书编号,图书名,作者  
FROM book  
WHERE 定价=(  
    SELECT MAX(定价) FROM book)

(17)对应的程序如下:

USE Library  
GO  
PRINT '借<C程序设计>的学生:'  
SELECT s.学号,s.姓名  
FROM student s,book b,borrow bor  
WHERE s.学号=bor.学号 AND b.图书编号=bor.图书编号 AND b.图书名='C程序设计'  
GO  
PRINT '借<C程序设计>的但没借<C习题解答>的学生:'  
SELECT s.学号,s.姓名  
FROM student s,borrow bor,book b  
WHERE s.学号=bor.学号 AND bor.图书编号=b.图书编号 AND b.图书名='C程序设计'  
    AND NOT EXISTS(
        SELECT * 
        FROM borrow bor1,book b1
        WHERE bor1.图书编号=b1.图书编号 AND b1.图书名='C习题解答' AND bor1.学号=bor.学号)

(18)对应的程序如下:

USE Library  
SELECT 学号,姓名  
FROM student  
WHERE 学号 NOT IN  
   (SELECT DISTINCT 学号 FROM borrow)

(19)对应的程序如下:

USE Library  
SELECT sc.系名 AS '系名',COUNT(bor.学号) AS '借书总数'  
FROM student s,depart sc,borrow bor  
WHERE s.学号=bor.学号 AND s.班号=sc.班号  
GROUP BY sc.系名  

(20)对应的程序如下:

USE Library  
SELECT 出版社 AS '出版社',COUNT(*) AS '图书总数'  
FROM book  
GROUP BY 出版社  

(21)对应的程序如下:

USE Library  
DECLARE @num int  
SELECT @num=(SELECT COUNT(*) FROM book)  
SELECT 出版社 AS '出版社',  
CAST(ROUND(COUNT(*)*100.0/@num,1) AS decimal(5,1)) AS '图书比例(%)'  
FROM book  
GROUP BY 出版社  

(22)对应的程序如下:

USE Library  
SELECT 出版社 AS '出版社',COUNT(*) AS '被借图书数目'  
FROM book b,borrow bor  
WHERE b.图书编号=bor.图书编号  
GROUP BY 出版社