c#图书馆管理系统

guo9805 2013-10-26 09:04:38
create table book
(
book_ip int identity(1,1) primary key,
book_name nvarchar(20),
book_author nvarchar(20),
book_publisher nvarchar(20),
book_Type nvarchar(20),
foreign key (book_Type) references book_Type(book_Type)
)

消息 1776,级别 16,状态 0,第 1 行
在被引用表 'book_Type' 中没有与外键 'FK__book__book_Type__5070F446' 中的引用列列表匹配的主键或候选键。
消息 1750,级别 16,状态 0,第 1 行
无法创建约束。请参阅前面的错误消息。


我的book_Type表是这样的:
create table book_Type
(
book_id int identity(1,1),
book_Type nvarchar(20),
primary key(book_id,book_Type)
)
求大神指教!!!急急!很急!!
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
全栈极简 2013-10-26
  • 打赏
  • 举报
回复
或者这样:
create table book_Type
(
       book_id int identity(1,1),
       book_Type nvarchar(20),
       primary key(book_id,book_Type)
)
create table book
(
       book_ip int identity(1,1) primary key,
       book_name nvarchar(20),
       book_author nvarchar(20),
       book_publisher nvarchar(20),
	   book_id int,
       book_Type nvarchar(20),
       foreign key (book_id,book_Type) references book_Type(book_id,book_Type)
)
全栈极简 2013-10-26
  • 打赏
  • 举报
回复
引用 2 楼 u012456925 的回复:
恩恩!好了!!我想问一下,为什么book_id和book_Type同时设为主键就出问题??你能帮我解答一下吗??
错误提示很明确了,但是我想说的是,我们一般用id作为唯一主键,而不是id+name作为联合主键。 应该这样来做:
create table book_Type
(
       book_id int identity(1,1),
       book_Type nvarchar(20),
       primary key(book_id)
)
create table book
(
       book_ip int identity(1,1) primary key,
       book_name nvarchar(20),
       book_author nvarchar(20),
       book_publisher nvarchar(20),
       book_id int,
       foreign key (book_id) references book_Type(book_id)
)
按照这样做就好了
  • 打赏
  • 举报
回复
引用 2 楼 u012456925 的回复:
恩恩!好了!!我想问一下,为什么book_id和book_Type同时设为主键就出问题??你能帮我解答一下吗??
因为你写了 foreign key (book_Type) references book_Type(book_Type) 但是在你的 book_Type 表中不存在这样的“主键或候选键”。 你的表 book_Type 的主键不需要修改,但需要增加一个候选键(唯一索引),它仅仅使用 book_Type 这一个字段。
lkr2380869 2013-10-26
  • 打赏
  • 举报
回复
foreign key (book_Type) references book_Type(book_Type)
guo9805 2013-10-26
  • 打赏
  • 举报
回复
恩恩!好了!!我想问一下,为什么book_id和book_Type同时设为主键就出问题??你能帮我解答一下吗??
全栈极简 2013-10-26
  • 打赏
  • 举报
回复
create table book_Type ( book_id int identity(1,1), book_Type nvarchar(20), primary key(book_Type) )

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧