创建三个表格,然后添加相关的约束,修改、删除、查询…
drop table Books
create table Books
(BookID char(20) primary key,
Bname char(50) unique,
Author varchar(30),
Press varchar(30),
Price money
)
select *from Books
drop table Readers
create table Readers
(ReaderID char(10) primary key,
Rname char(20) not null,
Rsex char(2) check(rsex = '男' or rsex = '女'),
Department varchar(30),
Phone char(20)
)
select *from Readers
drop table Lending
create table Lending
(ReaderID char(10) not null,
BookID char(20) not null,
Borrowdate date default getdate() not null,
Returndate date check(Returndate>Borrowdate)
)
select *from Lending
alter table Books add ISBN char(10)
select *from Books
alter table Books alter column ISBN char(10)
alter table Books add constraint df_ISBN default '7111085949'for ISBN
alter table Books drop constraint df_ISBN
select *from Books
alter table Books drop column ISBN
insert into Books values('TP311.138DFG','网络数据库实用教程','石大鑫','机械工业出版社',23)
insert into Books values('TP311.SQ','SQL Server2008实用教程','董建斌','机械工业出版社',25)
insert into Books values('TP311.138QZA','SQL Server实训','郑启芬','清华大学出版社',18)
insert into Books values('TP311.138WJ','数据库程序设计','刘韵华','电子工业出版社',38)
insert into Books values('TP311.138XZQ','SQL Server2008数据库系统管理','赵志清','人民邮电出版社',45)
insert into Books values('TP393.41ZX','ASP.NET案例开发','陈正熙','机械工业出版社',45)
select *from Books
insert into Readers values('R20101001','郭xx','男','计算机系',1008611),('R20101002','姚期智','男','计算机系',6688993),
('R20101003','张大彪','男','计算机系',1234566),('R20101004','毕淑敏','女','信息系',9898765),
('R20101005','董明珠','女','信息系',4008365315),('R20101006','撒贝宁','男','信息系',3816666)
SELECT *FROM Readers
delete Readers
insert into Lending values('R20101001','TP311.138SQ','2014-9-6','2014-10-20'),('R20101001','TP311.138SQZA','2014-9-15','2014-11-9'),
('R20101001','TP393.41ZX','2014-9-6','2014-12-3'),('R20101002','TP311.138WJ','2014-7-5','2014-9-20'),('R20101005','TP311.138SQ','2014-7-8','2014-9-8'),
('R20101005','TP311.138XZQ','2015-3-18',NULL)
select *from Lending
select BookID,Bname from Books where Bname like '%__数%'
select BookID,Bname from Books where price > '38'
select ReaderID from Lending where Returndate = ' '
select *from Books,Readers,Lending where Bname = '%数据库程序设计%'
select distinct ReaderID,Rname,Rsex,Department,Phone
from Readers,Lending
where ReaderID in (select BookID from Lending where BookID = 'TP311.138WJ')
select Rname
from Readers,Lending
group by Readers.ReaderID
having COUNT(Lending.ReaderID)>2
select *from Readers
select Rname
from Readers
where ReaderID in(
select ReaderID from Lending
group by Lending.ReaderID
having count(Lending.ReaderID)>2
)
未完,待续…
|