创建外键约束的问题!

skyboy0720 2004-05-05 11:12:57
create table dbo.a
(id int identity(1,1) primary key nonclustered,
name varchar(10) not null,
sortid int not null foreign key references dbo.b(sortid)
)


create table dbo.b
(sortid int not null,
color varchar(20) not null
)

服务器: 消息 1767,级别 16,状态 1,行 1
外键 'FK__a__sortid__0A9D95DB' 引用了无效的表 'dbo.b'。
服务器: 消息 1750,级别 16,状态 1,行 1
未能创建约束。请参阅前面的错误信息。

我就想a的sortid是b的外键,哪错了??!!!
...全文
69 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
结构与数据不同的啦。

建表是先有主键才有外键。

插入数据也是自上而下,
但删除数据是自下而上的啊。
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
楼上的,是可以了,但是,可能是我概念不清楚吧!我是想,如果没有a的sortid产生
b中的sortid也不会有,删除的时候也是,只能先删除b中的数据,
那怎么样写啊,我都糊涂了!
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
楼主再试试
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
--应该是先B后A

create table dbo.b
(sortid int not null PRIMARY KEY ,
color varchar(20) not null
)

create table dbo.a
(id int identity(1,1) primary key nonclustered,
name varchar(10) not null,
sortid int not null foreign key references dbo.b(sortid)
)


drop table a,b
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
楼上的,不行呀,还是同样的错误!!!
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
create table dbo.a
(id int identity(1,1) primary key nonclustered,
name varchar(10) not null,
sortid int not null foreign key references dbo.b(sortid)
)


create table dbo.b
(sortid int not null PRIMARY KEY ,
color varchar(20) not null
)
vileboy 2004-05-05
  • 打赏
  • 举报
回复
我也正为这个而伤脑筋呢。
vileboy 2004-05-05
  • 打赏
  • 举报
回复
b中的sortid要是主键或unique类型字段
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
好了,我明白了,谢谢你那么耐心!
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
术语不好,明白你意思。我不知怎样表达才好了。
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
意思是,如果sortid不是a的主键,在b中创建外键就不行么???!!!
可我只想sortid关联a中的id是必须要的啊!
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复


create table dbo.a
(id int identity(1,1) primary key nonclustered,
name varchar(10) not null--,
--sortid int not null primary key--foreign key references dbo.b(sortid)
)


create table dbo.b
(sortid int not null foreign key references dbo.a(id),
color varchar(20) not null
)



drop table b,a--应该是先B后A
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
try

create table dbo.a
(--id int identity(1,1) primary key nonclustered,
name varchar(10) not null,
sortid int not null primary key--foreign key references dbo.b(sortid)
)


create table dbo.b
(sortid int not null foreign key references dbo.a(sortid),
color varchar(20) not null
)



drop table a,b

--因为有主键,所以A表ID列不用也可
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
谢谢,我是明白了,但是我的要求不是这样的,我的要求是a是底,b是上层,那根据我那几个字段,该怎么建约束(sortid)
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
先插入B的数据,才能插入A-------------正确

但删除数据就要先删除A啦。


其实它们是上下楼层的关系,建楼时是先有底层B,再有上层A
拆楼时就先拆上层A,再拆底层B
不知这样说你明不明白
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
我的意思是,象
create table dbo.b
(sortid int not null PRIMARY KEY ,
color varchar(20) not null
)

create table dbo.a
(id int identity(1,1) primary key nonclustered,
name varchar(10) not null,
sortid int not null foreign key references dbo.b(sortid)
)

这样的话,我必须先插入B的数据,才能插入A,而且删除的话,如果先删除B,就会出错,我想反过来,我都糊涂了!不知道明白没有!
internetcsdn 2004-05-05
  • 打赏
  • 举报
回复
你的什么意思啊?

改指的是什么呢?

你先弄明白结构与数据这两个概念吧。
skyboy0720 2004-05-05
  • 打赏
  • 举报
回复
那你按我的意思,怎么改呢?!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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