自己看的sql -3

来源:互联网 发布:威尔克姆绣花软件问题 编辑:程序博客网 时间:2024/05/14 11:49

--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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝头发少又黄怎么办 头旋附近头发少怎么办 25岁掉头发严重怎么办 2岁宝宝头发稀少怎么办 掉头发很厉害怎么办吧 头发点的很厉害怎么办 为什么掉头发很厉害怎么办 产后2年脱发严重怎么办 产妇掉头发很厉害怎么办 头发掉了怎么办小妙招 头顶头发掉的厉害怎么办 20岁头发有些少怎么办 头痒头发掉厉害怎么办 头油头痒掉头发怎么办 头发痒掉发严重怎么办 宝宝的脸皴了怎么办 宝宝冬天脸皴了怎么办 白衬衣领子变黄怎么办 白衬衣领子烂了怎么办 玩手机眼睛红了怎么办 吃了发芽的土豆怎么办 散尾竹叶子发黄怎么办 吃了一朵长春花怎么办 3d模型打开缓慢怎么办 多肉叶片发芽后怎么办 多肉种子发芽后怎么办 玫瑰金手机掉漆怎么办 18k金掉色了怎么办 6s玫瑰金掉漆了怎么办 书被水泡了皱了怎么办 长头发掉的厉害怎么办 长头发容易掉发怎么办 家人被传销洗脑怎么办 衣服上有荧光剂怎么办 有荧光剂的衣服怎么办 3个月了恶露还有怎么办 京东倒闭欠的钱怎么办 便利通卡过期了怎么办 京东e卡被绑定了怎么办 天猫预售不发货怎么办 面签失败首付款怎么办