优化MySchool 第二章课后

来源:互联网 发布:忘不了前男友 知乎 编辑:程序博客网 时间:2024/06/04 19:52
--创建图书管理系统数据库  use master  go  if exists (select * from sysdatabases where name='Library')  drop database Library  create database Library  on primary   (  name='Library_data',  filename='D:\Project_S2\Library_data.mdf',  size=5mb,  filegrowth=15%  )  log on   (  name='Library_log',  filename='D:\Project_S2\Libraary_log.ldf',  size=1mb,  filegrowth=15%  )  go  --创建Book表  use Library   go  create table  Book  (  BID int not null,  BName nvarchar(20) not null,  Author nvarchar(20) not null,  PubComp nvarchar(50) null,  PubDate datetime not  null,  BCount int not null,  Price money not null   )  go    --创建Reader表  use Library  go  create table Reader  (  RID int not null,  RName nvarchar(20) not null,  LendNum int null,  RAddress nvarchar(50) null   )   go      --创建Borrow表  use Library  go   create table Borrow  (  RID int not null,  BID int not null,  LendDate datetime  not null,  WillDate datetime null,  ReturnDate datetime null  )  go      --创建Penalty表  use Library   go   create table Penalty  (  RID int not null,  BID int not null,  PDate datetime not null,  PType int null,  Amount money null  )  go      --添加约束    --1.Book表中       --添加主键  alter table Book  add constraint PK_BID primary key (BID)          --检查约束      --1.(必须以ISBN 开头)  alter table Book  add constraint CK_BID check (BID like 'ISDN%')        --2.(必须小于当前日期)  alter table Book   add constraint CK_PubDate check (PubDate<getdate())       --3.(必须大于等于1)  alter table Book  add constraint CK_BCount check (BCount>='1')       --4.(必须大于0)  alter table Book   add constraint CK_Price check (Price>'0')    --2.Reader表中      --添加主键  alter table Reader  add constraint PK_RID primary key (RID)        --检查约束  alter table Reader  add constraint CK_LendNum check (LendNum>='0')    --3.Borrow表中          --1.添加主键  alter table Borrow  add constraint PK_RID1 primary key (RID)        --外键约束     --1.  alter table Reader  add constraint FK_RID  foreign key(RID)  references Borrow(RID)    --------删除约束  ------alter table Borrow  ------drop constraint PK_LendDate    ------alter table Reader  ------drop constraint FK_RID    --2.  alter table Borrow  add constraint FK_BID  foreign key (BID) references Book (BID)        --默认约束  alter table Borrow  add constraint DB_LendDate default (getDate()) for LendDate      --(必须大于等于借阅日期,默认为借阅日+1)    alter table Borrow  add constraint CK_WillDate check (WillDate>=LendDate)    alter table Borrow  add constraint DB_WillDate default (dateAdd(mm,1,getdate())) for WillDate  --删除约束  alter table Borrow  drop constraint DB_WillDate          --默认null  alter table Borrow  add constraint DB_ReturnDate default (null) for ReturnDate    --4.Penalty表中     --1.添加主键  alter table Penalty  add constraint PK_RID2 primary key (RID)     --2.添加主键(有错误!)  alter table Penalty  add constraint PK_BID2 primary key (BID)  ---------------删除约束  --------------alter table Penalty  --------------drop constraint PK_RID2   --外键约束     --1.  alter table Reader  add constraint FK_RID1  foreign key(RID)  references Penalty(RID)        --2.  alter table Penalty  add constraint FK_BID1  foreign key (BID) references Book (BID)      --默认约束  alter table Penalty  add constraint DB_PDate default (getDate()) for PDate          --检查约束  alter table Penalty  add constraint CK_Amount check (Amount>0)      --Book中添加BTotal列     --先删除表 再添加数据  drop table Book    use Library   go  create table  Book  (  BID int not null,  BName nvarchar(20) not null,  Author nvarchar(20) not null,  PubComp nvarchar(50) null,  PubDate datetime not  null,  BCount int not null,  Price money not null,  BTotal int null   )  go  --像各表中添加至少2条数据      --Book  insert into Book(BID,BName,Author,PubComp,PubDate,BCount,Price,BTotal)  select 1,'','','','',1,100,1000000  union  select 1,'','','','',1,100,1000000  union  select 1,'','','','',1,100,1000000       --Reader  insert into Reader(RID,RName,LendNum,RAddress)  select 1,'',2,'' union  select 1,'',2,'' union  select 1,'',2,''      --Borrow  insert into Borrow (RID,BID,LendDate,WillDate,ReturnDate)  select 1,1,'','','' union  select 1,1,'','','' union  select 1,1,'','',''      --Penalty  insert into Penalty(RID,BID,PDate,PType,Amount)  select 1,1,'',1,20 union  select 1,1,'',3,20 union  select 1,1,'',2,20   

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在上海动漫城游戏厅里输了钱怎么办 电缝纫机扎快了上线有套那是怎么办 老式缝纫机底线总夹梭子上怎么办 两个月宝宝脸一边大一边小怎么办 炒股入金10万亏3万怎么办 如果一支股票有好几个庄家怎么办 浦发信用卡查询密码被锁定怎么办 浦发信用卡输错密码被锁定怎么办 浦发手机银行查询密码锁定怎么办 宝宝吃母乳没吃饱就睡着了怎么办 量柱画线不是方屏电脑怎么办 夏天卧室开空调太干了怎么办 健身房锻炼手臂抻的肌肉疼怎么办 人累了可以休息 心累了怎么办 上来两天班感觉不想做了怎么办 休产假的时候公司解散了怎么办 上三天班老板不给结工资怎么办 老师说孩子上课纪律不好我该怎么办 大班的孩子记不住拼音怎么办呢 家长跟孩子沟通出现问题该怎么办? 如果孩子入学分配出现问题该怎么办 2岁多的宝宝喜欢动手打人怎么办 儿童新长出的大门牙像两边撇怎么办 被烫伤了怎么办的活动反思怎么写 生完孩子脸上起蝴蝶斑了怎么办 鼻子部位突然长了晒斑怎么办 我脸上长有日晒斑.该怎么办 做为小领导同事不听你的怎么办 二年级的小孩叫写作业不听怎么办 苹果手机微信出现黑框怎么办 百度网盘下载原画视频会闪退怎么办 已发布的公众号推文段落重复怎么办 谷歌商店找不到方舟手游怎么办 染头发的颜色弄到衣服上怎么办 橡皮把桌面油漆弄掉了怎么办 手机被调成静音不知道放哪了怎么办 金丝熊吃大米吃撑了怎么办 部落有可疑记录被暂时禁封怎么办 鼻子通向嘴那里痒得难受怎么办 小孩上嘴唇中间的连线碰掉了怎么办 秋田犬夏天退毛严重么 怎么办