自己看的sql -3

--card(cno,cname,class)借书证(卡号,姓名,班级);
create table card
(
 cno varchar(20) not null primary key,
 cname varchar(20) not null,
 class varchar(20) not null
)
select *from card
delete from card

insert into card values('1001','李东','C01')
insert into card values('1002','王欣','C01')
insert into card values('1003','张东','C02')
insert into card values('1004','蔡琴','C02')
insert into card values('1005','吴晰','C03')
insert into card values('1006','郭涛','C03')


--books(bno,bname,author,price,quantity)书(书号,书名,作者,单价,库存数量);
create table books
(
bno varchar(20) not null primary key,
bname varchar(20) not null,
author varchar(20) null,
price varchar(20) null,
quantity varchar(20) null
)
select * from books
delete from books
drop table books

insert into books values('0001','水浒','施耐庵','20','30')
insert into books values('0002','网络组建','李建','25','20')
insert into books values('0003','计算机网络','洪为','21','15')
insert into books values('0004','计算方法','马俊','26','25')
insert into books values('0005','组合数学','李来','20','18')
insert into books values('0006','数据库技术及应用','王博','32','23')
insert into books values('0007','计算方法习题集','李杜','18','32')

--borrow(cno,bno,rdate)借阅记录(卡号,书号,归还日期);
create table borrow
(
cno varchar(20) foreign key references card(cno) not null ,
bno varchar(20) foreign key references books(bno) not null,
rdate datetime not null,
primary key(cno,bno)
)
select * from borrow
delete from borrow
drop table borrow

insert into borrow values('1001','0002','2009.12.30')
insert into borrow values('1003','0005','2010.1.15')
insert into borrow values('1005','0004','2010.1.20')
insert into borrow values('1002','0002','2010.1.10')
insert into borrow values('1005','0001','2010.1.10')
insert into borrow values('1005','0002','2010.1.10')
insert into borrow values('1002','0004','2010.1.10')
insert into borrow values('1002','0001','2010.1.10')
insert into borrow values('1002','0003','2010.1.10')
insert into borrow values('1002','0005','2010.1.10')
insert into borrow values('1002','0006','2010.1.10')


--备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。
  --1. 写出建立BORROW表的SQL语句,要求定义主码完整性约束和引用完整性约束。
  --2. 找出借书超过5本的读者,输出借书卡号及所借图书册数。
select cno as'卡号',count(distinct bno)as'所借图书册数' from borrow
group by cno

 

  --3. 查询借阅了"水浒"一书的读者,输出姓名及班级。
select cname as'姓名',class as'班级'
from card,books,borrow
where card.cno=borrow.cno and borrow.bno=books.bno and bname='水浒'


  --4. 查询过期未还图书,输出借阅者(卡号)、书号及还书日期。
select cno as'卡号',bno as'书号',rdate as'还书日期'
from borrow
where rdate<getdate()


  --5. 查询书名包括"网络"关键词的图书,输出书号、书名、作者。
select bno as'书号',bname as'书名',author as'作者'
from books
where bname like'%网络%'


  --6. 查询现有图书中价格最高的图书,输出书名及作者。
select top 1 bname as'输出书名',author as'作者'
from books
order by price


  --7. 查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出。
select cno as'借书卡号'
from borrow,books
where borrow.bno=books.bno and bname='计算方法'
 and cno not in(select cno from borrow
 where  borrow.bno=books.bno and bname!='计算方法习题集')
order by cno desc

  --8. 将"C01"班同学所借图书的还期都延长一周。
update borrow
set rdate=rdate+7
where cno in(select cno from card where class='C01')
select * from borrow

  --9. 从BOOKS表中删除当前无人借阅的图书记录。
delete from books
where bno not in(select bno from borrow)


  --10.如果经常按书名查询图书信息,请建立合适的索引。
create index bname_index
on books(bname)

  --11.在BORROW表上建立一个触发器,完成如下功能:如果读者借阅的书名是"
        /*数据库技术及应用",就将该读者的借阅记录保存在BORROW_SAVE表中(注
        ORROW_SAVE表结构同BORROW表)。*/
create trigger triggered on borrow
for insert ,update
as
select * into borrow_save from borrow where 1=2
insert into borrow_save select inserted.* from books,inserted
where books.bno=inserted.bno and bname='数据库技术及应用'

 

  --12.建立一个视图,显示"力01"班学生的借书信息(只要求显示姓名和书名)。
create view borrow_view
as
select * from borrow
where cno in(select cno from card where class='C01')

select * from borrow_view

  --13.查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书
       /*卡号,并按卡号升序排序输出。*/
select cno as'卡号' from borrow,books
where borrow.bno=books.bno and bname='计算方法'
 and cno in(select cno from borrow
 where  borrow.bno=books.bno and bname!='组合数学')
order by cno desc

  --14.假定在建BOOKS表时没有定义主码,写出为BOOKS表追加定义主码的语句。
alter table books
add
constraint PF_key primary key(bno)

  --15.对CARD表做如下修改:
        /*a. 将NAME最大列宽增加到10个字符(假定原为6个字符)。
        b. 为该表增加1列NAME(系名),可变长,最大20个字符。*/
alter table card
alter column cname varchar(10)

alter table card
add name varchar(20)

select * from card

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趙大叔

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值