[SQL]求查询语句T-SQL代码!关于图书管理系统的!

背景:

图书管理系统
提供几个主要的表:

create table userIn --用户信息表
(
userid char(6) primary key not null,
username varchar(8) not null,
usergender bit not null default 1,----规定男为1,女为0
admins varchar(10) not null,
catename varchar(10)not null,
userunit varchar(4) not null,
dorm varchar(30),
Addr nvarchar(30),
Tel varchar(20) not null,
userreg date not null,
userbknum smallint not null)

create table lending---图书借阅表
(
id int not null primary key,
userid char(6) not null,
bookid char(10) not null,
LendDate date not null,
RtnDate date not null)

create table book---图书信息表
(
bookid char(10) primary key not null,
bookname varchar(100) not null,
bkcateid varchar(6) not null,
author varchar(20) not null,
pubid varchar(6) not null,
pubtime date not null,
checkin date not null,
price money not null,
bookstatus bit not null)

create table publisher ----出版社信息表
(
pubid varchar(6) not null primary key,
pubname nvarchar(20) not null,
pubtel varchar(20),
pubtax varchar(20),
pubadd varchar(40),
https varchar(40)
)

一些外键:
--读者信息表(userin)上的外键
alter table userin
add constraint userin1 foreign key (catename) references usercate(catename)
alter table userin
add constraint userin2 foreign key (userunit) references unit(userunit)
alter table userin
add constraint userin3 foreign key (Admins) references usertype(admins)

--图书信息表(book)上的外键
alter table book
add constraint book1 foreign key (bkcateid) references bookcate(bkcateid)
alter table book
add constraint book2 foreign key (pubid) references publisher(pubid)

--图书借阅表(Lending)上的外键
alter table lending
add constraint lending1 foreign key (userid)references userin(userid)
alter table lending
add constraint lending2 foreign key (bookid)references book(bookid)

问题:
1、通过触发器定义超过5次超期未还的学生不能继续借阅图书;
我的代码如下:
create trigger LEND_FORBIDDEN2
on userin
instead of update
as
begin
declare @b char(6)
declare @t int
declare @c int
declare @count int
select @count=1
select @c=0
declare @rnttime date
declare @borrtime date
select @t=max(id) from lending
select @b=userid from updated
select @rnttime=rtntime from lending where userid=@b
select @borrtime=borrtime from lending where userid=@b
while @c<@t
if @rnttime>@borrtime
@count=@count+1
@c=@c+1
end
if @count>5
print'不能借书!'
end

报错:
消息 102,级别 15,状态 1,过程 LEND_FORBIDDEN2,第 20 行
'@count' 附近有语法错误。
消息 102,级别 15,状态 1,过程 LEND_FORBIDDEN2,第 25 行
'end' 附近有语法错误。

2、统计查询:统计出各个出版社出版的图书的平均定价。.
疑惑:怎么用select语句查询出版社名称及其书本的平均价格??这个链接怎么链接?

3、统计查询:统计出今年年度下半年借书次数超过10次并且没有过期未还的学生信息。

疑惑:也是用select语句完成的。

求T-SQL代码!!T T!

1.
while @c<@t
begin
if @rnttime>@borrtime
@count=@count+1
@c=@c+1
end
if @count>5
print'不能借书!'

2.
select p.pubname, avg(b.price) average_price
from publisher p, book b
where p.pubid = b.pubid

3.
你的表里面没有定义过期日期,只有借出日期和返还日期,怎么判断过期未还?
温馨提示:答案为网友推荐,仅供参考