1:select ((select count(*) from borrow)/(select count(*) from cards)) as AverageNumber
2:等高手了
3:select title from books where bno not in(select distinct(bno) from borrow) or bno in(select bno from borrow where datediff(year,borrow_date,getdate())>=2)
4:with t as (select bno,count(*) as cou from borrow group by bno)select bno from t where cou in(select max(cou) from t)
看似簡單的 其實蠻費勁的。。。
② 3.21考慮圖3-21中的圖書館資料庫。用SQL寫出如下查詢:
select name from member,book,borrowed where member.memb_no=borrowed.memb_no and book.isbn=borrowed.isbn and borrowed.publisher="xxx"
select name from member where memb_no in (select memb_no from borrowed group by memb_no having count(*) = (select count(*) from book where publisher="xxx"))
select name,publisher from member,borrowed,book group by name,publisher where member.memb_no=borrowed.memb_no
and
book.isbn=borrowed.isbn
having
count(*)>5
select(select count(*) from borrowed)/ (select count(*)from member)
我沒有數據, 所以憑空寫的, 思路差不多這樣吧。有問題私信我吧. 你的列有關鍵字name,如果是mysql的話加``,sqlserver加[]。
③ 有關圖書館借閱系統的SQL語句
修改列名sp_rename 'mytable.name','新的列名'
但是次功能會破壞資料庫結構請慎用.
exec sp_rename 『table.oldfield』,』newfield』,』column』
Select的基本查詢
1. 選擇所有列
Select *from student
2. 選擇指定的若干列
Select s_avgrade , s_name
From student
3. 構造計算列
Select s_name 姓名,year(getdate())-year(s_birthday) 年齡
From student;
帶DISTINCT的查詢
SELECT DISTINCT s_dept 所在的系
FROM student;
比較上下的區別
select s_dept 所在的系
from student;
帶WHERE子句的條件查詢
select s_no 學號,s_name姓名,s_sex性別,s_avgrade平均成績
from student
where s_avgrade>=90;
帶BETWEEN的范圍查詢
select s_name 姓名,s_sex 性別,s_dept 系別,s_avgrade 平均成績,s_brithday 出生年月
from student
where s_brithday ("not between") BETWEEN '1986-00-00' and '1987-00-00';(這里的日期從 char 數據類型到 smalldatetime 數據類型的轉換導致 smalldatetime 值越界,全部寫0好像不對,具體一點到是可以運行)
-----------------------------帶IN的范圍查詢
select s_no 學號,s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,s_avgrade 平均成績,s_brithday 出生年月
from student
where s_speciality in('電子信息工程','生物工程');(查詢所選定s_speciality的student的信息)
select s_no 學號,s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,s_avgrade 平均成績,s_brithday 出生年月
from student
where s_speciality='電子信息工程'or s_speciality='生物工程');
-------------------帶GROUP的分組查詢
1.select s_dept 系別,count(*) 人數
from student
group by s_dept;(統計各個s_dept的人數)
2.select s_dept 系別,count(*) 人數
from student
group by s_dept
having count(*)>=3; (統計各個s_dept的人數,但是只輸出人數大於3的)
帶like的匹配查詢和帶is的空值查詢
--------------------------帶like的匹配查詢
select s_no 學號,s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,s_avgrade 平均成績,s_brithday 出生年月
from student
where s_name like 'y%';(只能查開頭為y的名字且為第一個字,如果要具體查出"yd",就應寫"y%d%") )
空值null的查詢
Select *
from student
where s_avgrade=null;——錯誤
Select *
from student
where s_avgrade is null;——正確
------------使用Order排序查詢結果
如果ASC和DESC都沒有選擇,則按依據列進行升序排列,即ASC為默認值
1.select s_no 學號,s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,s_avgrade 平均成績,s_brithday 出生年月
from student
where s_sex='男'
order by s_avgrade DESC;
2.select s_no 學號,s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,s_avgrade 平均成績,s_brithday 出生年月
from student
where s_sex='男'
order by s_avgrade DESC,s_no;(先按s_avgrade的要求(DESC)排序,之後按s_no(默認的是升序排列),如果後面仍然有要求,依前面類推)
-------------連接查詢
同時涉及兩個或兩個以上數據表的查詢稱為連接查詢
1.等值連接和自然連接查詢
select student.s_no 學號,s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,c_name 課程名稱,c_grade 課程成績
from student,sc
where student.s_no =sc.s_no;
---------------------------------
2.自連接查詢(創建同義詞student2)
Sql server 2000貌似還沒有創建同義詞的功能,Sql server 2005才有,不知道這說法對不對!!!
select student2.s_no 學號,student2.s_name 姓名,student2.s_sex 性別,student2.s_speciality 專業,student2.s_avgrade 平均成績
from student,student2
where student,s_name='yx'
and student.s_speciality=student2.s_speciality
-----------------------------------
3.外連接查詢
select s_name 姓名,s_sex 性別,s_speciality 專業,s_dept 系別,
sc.c_name 課程姓名, sc.c_grade 課程成績
from student left join sc on (student.s_no=sc.s_no);
以上是左外連接
把最後一句改為
from sc right join student on (student.s_no=sc.s_no);
-------------嵌套查詢
使用謂詞IN的嵌套查詢
查出an和yo所在專業的所有學生
方法一.
第1步select student.s_speciality
from student
where student.s_name='an'or student.s_name='yo'
第2步 將第一步得到的結果放到下面
select *
from student
where student.s_speciality in ('電子信息工程','生物工程');
方法二.
select *
from student
where student.s_speciality in (
select student.s_speciality
from student
where student.s_name='an' or student.s_name='yo');
-----使用比較運算符的嵌套查詢
查詢所有成績比yo低的學生
select s_no 學號,s_name 姓名,s_speciality 專業,s_avgrade平均成績
from student
where s_avgrade<(
select s_avgrade
from student
where s_name='yo');
-----------------使用謂詞EXISTS的嵌套查詢
select s_no 學號,s_name 姓名,s_speciality 專業
from student
where exists(
select *
from sc
where student.s_no=s_no and c_name='演算法設計與分析');
-----------------查詢的集合運算
對任意兩個SELECT語句進行集合運算
Union--並
Intersect--交
Except--差
select s_no 學號,s_name 姓名,s_speciality 專業,s_avgrade 平均成績
from student
where s_speciality='計算機軟體與理論'
Union------換成intersect和except都會出問題,網上查了一下說是Sql Server 2000 不支持。
select s_no 學號,s_name 姓名,s_speciality 專業,s_avgrade 平均成績
from student
where s_avgrade>=85;
------------------SQL的數據操縱功能
將學號為『50』,姓名『lo』,性別『男』,出生日期『1987-1-1』,專業為『計算機應用技術』,
平均成績『92.5』,系別『計算機系』的學生記錄插入student
insert into student(s_no,s_name,s_sex,s_brithday,s_speciality,s_avgrade,s_dept)
values('50','lo','男','1987-1-1','計算機應用技術',92.5,'計算機系')
將查詢到的數據輸入到另一個數據表中
1.當然首先要自己creat一個表,例如student1
2.insert into student1(s_no,s_name,s_speciality,s_avgrade,s_dept)
(select s_no,s_name,s_speciality,s_avgrade,s_dept
from student);
---SQLserver 2000 可以在企業管理器上視圖建表
也可以在SQL查詢分析器上寫create建表
-------------------使用Update語句更新數據
將所有學生的平均成績減5分
update student
set s_avgrade=s_avgrade-5;
將所有女生的成績加上原來分數的1%
update student
set s_avgrade=s_avgrade+s_avgrade*1%
where s_sex='女';
如果表student中平均成績s_avgrade是表sc中的課程成績c_grade的平均值。
1.---創建一個用於存放中間結果的數據表tmp_table
create table tmp_table(
s_no char (8)
s_avgrade numberic(3,1)
);
2.---通過按學號分組的方法求各個學生的平均成績,並將其學號和平均成績存放到表tmp_table
insert into tmp_table(s_no,s_avgrade)
(select s_no,AVG(c_grade)
from sc
group by s_no);
3.---用表tmp_table中各個學生的平均成績(tmp_table.s_avgrade)
更新student中的平均成績(student.s_avgrade).對於沒有選修的學生,其成績為空值NULL.
Update student
Set s_avgrade=(select s_avgrade from tmp_table where s_no=student.s_no);
DROP table tmp_table;
-----使用DELETE語句刪除數據
刪除表student中的所有數據
DELETE
from student;
刪除表中沒有選修任何課程的學生
delete
from student
where s_no not in (
select s_no
from sc);
------------SQL的數據控制功能
1.授予許可權
SQL語句中操作全限是由GRANT語句來完成的。
把對表student的修改,查詢許可權給user1,user2
grant update,select
on student
to user1,user2;
把表sc中c_no和c_name的插入許可權授權給user1,並同時給他授予許可權的轉授權。
grant insert(c_no,c_name)
on sc
to user1
with grant option;
把對表sc和表student的全部操作權授給user1和user2
grant all privileges[給予所有的許可權]
on student,sc
to user1,user2;
把對表student的查詢許可權授權給所有資料庫用戶
grant select
on student
to public;
對象 對象類型 操作許可權
------------------------
基本表,列 TABLE select,insert,update,delete,alter,index,all privileges
視圖 TABLE select,insert,update,delete,all privileges
資料庫 Database create table
-----------------------
2.收回許可權
revoke update,select
on student
to user1,user2;
收回user1,user2對student查看和修改許可權
------
revoke insert(c_no,c_name)
on sc
from user1;
收回user1對sc表c_no,c_name的插入許可權和許可權的授予權
------
revoke select
on student
from public;
收回所有用戶對student查詢許可權
④ 資料庫的查詢語句作業二
1、select * from 讀者信息表
2、select * from 借書表 where 編號='2002060328'
3、select * from 圖書信息表 where 書名 like '程序設計'
4、select * from 圖書信息表 where 出版社='清華大學出版社' order by 單價
希望對你有幫助
⑤ 圖書館管理員利用SQL怎樣用索引查詢圖書信息
你首要先建一個表,然後將數據錄入
表中可以有多個欄位,比如圖書編號、圖書名稱、出版社等
在資料庫中可以使用查詢語句進行數據檢索
例如
SELECT
*
FROM
圖書表
WHERE
圖書編號='0001'
⑥ 鈴SQL語句統計圖書館一共有多少本圖書
這兩句都是基礎SQL
select count(1) from 圖書表
第二句需要幾個表的連接,不清楚表結構,不能寫,建議你把表結構發出來,一般應該有圖書表,學生表,借閱流水表
⑦ 資料庫SQL語句實現下列問題
create proc t1
@dzno int,
@num int output
as
select @num=count(書號) from 借閱 where 讀者號=@dzno
return @num
go
create proc t2
@bookname varchar(50),
@author varchar(20)
as
select 出版社,單價 from 圖書 a where 書名 =@bookname and 作者=@author
go
create proc t3
@readername varchar(10),
@bookno int
as
declare @num int,@readerno int
select @readerno=讀者號 from 讀者 where 姓名=@readername
exec t1 @readerno,@num output
if @num>=(select 允許借閱冊數 from 讀者類別 a, 讀者 b where a.姓名=@readername and a.讀者類別編號=b.讀者類別編號)
begin
print '超出允許借閱冊數,不能再借了'
return
end
declare @days int
select @days=允許借閱天數 from 讀者類別 a, 讀者 b where a.姓名=@readername and a.讀者類別編號=b.讀者類別編號
print '借閱成功!'
insert into 借閱 values( @readerno,@bookno,getdate(),dateadd(dd,@days,getdate())
⑧ 有「圖書管理」資料庫,包含如下的表結構,根據要求寫出相應的SQL語句。
1.select 書號,單價 from 圖書 where 出版單位='清華大學出版社' order by 單價
2.select count(*) from 讀者
3.insert into 借閱 values ('JSJ001','CJ005','2010-11-11')
4.update 圖書 set 單價=單價*0.1
5.select 出版單位,sum(單價)/count(單價) from 圖書 group by 出版單位
⑨ SQL 圖書管理系統的查詢語句
1. 求總藏書量、藏書總金額,總庫存冊數、最高價、最低價。
select count(圖書編號) as 總藏書量,
sum(定價) as 藏書總金額,
sum(實際數量) as 總庫存冊數,
max(定價) as 最高價,
min(定價) as 最低價
from 圖書卡片
go
2. 列出藏書在10本以上的書(書名、作者、出版社、年份)。
select 圖書名稱,作者姓名,出版社,出版日期
from 圖書卡片
group by 圖書編號 having(coung(1)>10)
order by 圖書名稱
go
3. 哪些出版社的藏書種類數超過100種。
select 出版社 as '藏書種類數超過100種的出版社'
from 圖書卡片
group by 出版社 having(count(類別)>100)
order by 出版社
go
4. 目前實際已借出多少冊書?
select sum(借出數量) as '借出數量'
from 圖書卡片
go
5. 年份最久遠的書。
select top 1 with ties 圖書名稱 from 圖書卡片
order by 出版日期
go
6. 「資料庫系統原理教程,王珊編,清華大學出版社,1998年出版」還有幾本?
select count(1) from 圖書卡片
where concaints(摘要,'"資料庫系統原理教程,王珊編,清華大學出版社,1998年出版"')
go
7. 哪一年的圖書最多?
select top 1 with ties convert(substring(出版日期,1,4)) as 年份,count(1) as '圖書數量'
from 圖書卡片
group by 出版日期
order by 圖書數量 desc
go
8. 哪本借書證未歸還的圖書最多?
select top 1 with ties A.讀者編號,count(1) as '借書數量'
from 圖書卡片 A,借閱 B
where A.圖書編號=B.圖書編號
group by A.讀者編號
order by 借書數量 desc
go
9、平均每本借書證的借書冊數。
select avg(借閱數量) as '平均每本借書證的借書冊數'
from 借閱
go
10.哪個系的同學平均借書冊數最多?
select top 1 with ties A.工作單位,avg(借閱數量) as '平均借閱數量'
from 讀者 A,借閱 B
where A.讀者編號=B.讀者編號
group by A.工作單位
order by 平均借閱數量' desc
go
11. 最近兩年都未被借過的書。
select 圖書名稱
from 圖書卡片
where 圖書編號 in(select 圖書編號 from 借閱 where datediff(year,借閱日期,getdate())>2)
go
12. 列出那些借了圖書逾期未歸還的借書證號和圖書名。
select A.讀者編號 as '借書證號',B.圖書名稱
from 讀者 as A inner join 圖書卡片 as B on A.圖書編號=B.圖書編號
where A.應歸還日期<getdate() and A.實際歸還日期 is null
go
13.今年未借過書的借書證。
select 讀者編號
from 讀者
where 讀者編號 not in(select 讀者編號
from 讀者
where datediff(year,借閱日期,getdate())=0)
go
14. 今年那種書出借最多?
select top 1 with ties A.類別,count(1) as '借出數量'
from 圖書卡片 A,借閱 B
where datediff(year,B.借閱日期,getdate())=0
group by A.類別
order by 借出數量' desc
go