自己看的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
- 自己看的sql -3
- 给自己看的sql
- 自己看的 sql -2
- 给自己看的。。。
- 給自己看的
- 写给自己看的
- 转载自己看的
- 给自己看的
- 自己看的``
- 转给自己看的
- 以后自己看的
- 自己看的
- 给自己看的,
- 自己看的
- 自己看的
- 自己看的2
- 自己看的20161216...
- 自己看的
- Android软键盘遮住界面解决办法:通过软键盘改变视图位置
- 滑动窗口协议
- 给自己看的
- 海量数据取中位数
- 自己看的 sql -2
- 自己看的sql -3
- COMPILE && LINK
- 创建线程的三种方法
- SQL_Server_2005_函数大全
- 第三届蓝桥杯java预赛第3题汉诺塔
- 字节流
- sqlserver 字段类型说明
- Ubuntu搭建SVN服务器
- C++ 对象的内存布局(上) .