1 利用sql建立图书管理数据库,并定义以下基本表:
图书(书号,书名,价格,出版社)
读者(卡号,姓名,年龄,所属单位)
借阅(书号,卡号,借阅日期)
定义主码、外码、和年龄、价格的取值范围。
2 在三个表中输入若干记录,注意如果输入违反完整性约束条件的记录系统有何反应。
下面展示一些 内联代码片
。
create database MyDb
on
(
name='MyDb_data', --主数据文件的逻辑名称
filename='D:\MyDb_data.mdf', --主数据文件的物理名称
size=5mb, --主数据文件的初始大小(最小为3mb)
maxsize=10mb, --主数据文件增长的最大值
filegrowth=10% --主数据文件增长率
)
log on --日志文件
(
name='MyDb_log',
filename='D:\MyDb_log.ldf',
size=5mb,
filegrowth=10%
)
use MyDb
create table book
(
bno varchar(20) primary key,
bname varchar(20) not null,
bprice float check(0<bprice and bprice<10000),
bpublish varchar(20)
)
create table reader
(
card varchar(20) primary key,
rname varchar(20) not null,
age int check(0<age and age<100),
rwork varchar(15)
)
create table b_r
(
card varchar(20),
time varchar(15),
bno varchar(20) ,
CONSTRAINT c_b primary key(card,bno),
foreign key(card) references reader(card) ,
foreign key(bno) references book(bno)
)
insert into book(
bno,bname,bprice,bpublish
)
select'tp001','离散',99,'高教出版社' union all
select'tp002','离散',79,'人民出版社'union all
select'tp003','高数',79,'人民出版社'union all
select'tp004','数据库',179,'上海出版社'union all
select'tp005','C++',19,'上海出版社'union all
select'tp0103','C',119,'上海出版社'union all
select'tp006','数据库',55,'高教出版社'
insert into reader(
card,rname,age,rwork
)
select'22112','张三',20,'计算机系'union all
select'22113','李四',53,'计算机系'union all
select'22114','王五',29,'英语系'union all
select'22115','马六',50,'数学系'union all
select'22116','张七',30,'数学系'union all
select'22117','赵八',10,'计算机系'
insert into b_r(
card,bno,time
)
select'22112','tp001',getdate()union
select'22113','tp003',getdate()union
select'22116','tp002',getdate()union
select'22112','tp006',getdate()union
select'22112','tp0103',getdate()union
select'22113','tp0103',getdate()
完整实验1-8链接
实验数据库1-8