1 /*==============================================================*/ 2 /* DBMS name: Microsoft SQL Server 2012 */ 3 /* Created on: 2018/11/25 13:42:24 */ 4 /*==============================================================*/ 5 6 use "zuoye" 7 if exists (select 1 8 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') 9 where r.fkeyid = object_id('借书者') and o.name = 'FK_借书者_FK_2_借书记录') 10 alter table 借书者 11 drop constraint FK_借书者_FK_2_借书记录 12 go 13 14 if exists (select 1 15 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') 16 where r.fkeyid = object_id('图书') and o.name = 'FK_图书_FK_1_借书记录') 17 alter table 图书 18 drop constraint FK_图书_FK_1_借书记录 19 go 20 21 if exists (select 1 22 from sysobjects 23 where id = object_id('借书者') 24 and type = 'U') 25 drop table 借书者 26 go 27 28 if exists (select 1 29 from sysobjects 30 where id = object_id('借书记录') 31 and type = 'U') 32 drop table 借书记录 33 go 34 35 if exists (select 1 36 from sysobjects 37 where id = object_id('图书') 38 and type = 'U') 39 drop table 图书 40 go 41 42 /*==============================================================*/ 43 /* Table: 借书者 */ 44 /*==============================================================*/ 45 create table 借书者 ( 46 借书者编号 int not null, 47 图书编号 int null, 48 借书者姓名 varchar(32) not null, 49 借书数量 int null, 50 constraint PK_借书者 primary key (借书者编号) 51 ) 52 go 53 54 /*==============================================================*/ 55 /* Table: 借书记录 */ 56 /*==============================================================*/ 57 create table 借书记录 ( 58 图书编号 int not null, 59 借书者编号 int null, 60 期望归还时间 datetime null, 61 借书时间 datetime null, 62 还书时间 datetime null, 63 constraint PK_借书记录 primary key (图书编号) 64 ) 65 go 66 67 /*==============================================================*/ 68 /* Table: 图书 */ 69 /*==============================================================*/ 70 create table 图书 ( 71 图书编号 int not null, 72 书名 varchar(32) null, 73 作者 varchar(32) null, 74 出版社 varchar(50) null, 75 出版日期 datetime null, 76 库存 int null, 77 价格 money null, 78 constraint PK_图书 primary key (图书编号) 79 ) 80 go 81 82 alter table 借书者 83 add constraint FK_借书者_FK_2_借书记录 foreign key (图书编号) 84 references 借书记录 (图书编号) 85 go 86 87 alter table 图书 88 add constraint FK_图书_FK_1_借书记录 foreign key (图书编号) 89 references 借书记录 (图书编号) 90 go
1 /*==============================================================*/ 2 /* DBMS name: Microsoft SQL Server 2012 */ 3 /* Created on: 2018/11/26 7:17:16 */ 4 /*==============================================================*/ 5 6 7 if exists (select 1 8 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') 9 where r.fkeyid = object_id('交易记录') and o.name = 'FK_交易记录_REFERENCE_商品') 10 alter table 交易记录 11 drop constraint FK_交易记录_REFERENCE_商品 12 go 13 14 if exists (select 1 15 from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F') 16 where r.fkeyid = object_id('购物者') and o.name = 'FK_购物者_REFERENCE_交易记录') 17 alter table 购物者 18 drop constraint FK_购物者_REFERENCE_交易记录 19 go 20 21 if exists (select 1 22 from sysobjects 23 where id = object_id('交易记录') 24 and type = 'U') 25 drop table 交易记录 26 go 27 28 if exists (select 1 29 from sysobjects 30 where id = object_id('商品') 31 and type = 'U') 32 drop table 商品 33 go 34 35 if exists (select 1 36 from sysobjects 37 where id = object_id('购物者') 38 and type = 'U') 39 drop table 购物者 40 go 41 42 /*==============================================================*/ 43 /* Table: 交易记录 */ 44 /*==============================================================*/ 45 create table 交易记录 ( 46 交易记录ID int not null, 47 交易物品ID varchar(32) not null, 48 交易物品数量 int not null, 49 交易商家ID varchar(32) not null, 50 constraint PK_交易记录 primary key (交易记录ID) 51 ) 52 go 53 54 /*==============================================================*/ 55 /* Table: 商品 */ 56 /*==============================================================*/ 57 create table 商品 ( 58 交易物品ID varchar(32) not null, 59 交易物品名称 varchar(32) not null, 60 库存 int not null, 61 单价 money not null, 62 constraint PK_商品 primary key (交易物品ID) 63 ) 64 go 65 66 /*==============================================================*/ 67 /* Table: 购物者 */ 68 /*==============================================================*/ 69 create table 购物者 ( 70 账户ID varchar(32) not null, 71 用户名 varchar(32) not null, 72 交易记录ID int not null, 73 交易时间 datetime not null, 74 constraint PK_购物者 primary key (账户ID) 75 ) 76 go 77 78 alter table 交易记录 79 add constraint FK_交易记录_REFERENCE_商品 foreign key (交易物品ID) 80 references 商品 (交易物品ID) 81 go 82 83 alter table 购物者 84 add constraint FK_购物者_REFERENCE_交易记录 foreign key (交易记录ID) 85 references 交易记录 (交易记录ID) 86 go