具有多个主键的单个引用键。 [英] Having Single Reference Keys to multiple primary keys.

查看:50
本文介绍了具有多个主键的单个引用键。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的编码员,



Iam有两张桌子名为E1和E2,



在E1 I已经在两个不同的列中设置了两个主键,但问题是现在我正在尝试将其引用到表E2中具有单个主键的单个列。显示主键不存在表E1并且它不为空。请帮我解决问题。

Dear Coders,

Iam Having two tables name E1 and E2,

In E1 I have two Primary Keys already set in two different columns but the problem is now I am trying to have a reference of it to a single column in the table E2 which is having a single primary key. It is Showing that "Primary key does not exist the the table E1 and it is not null". Please Help me solving the Problem.

推荐答案

/****** Object:  Table [dbo].[PrimaryTable]    Script Date: 12/19/2012 17:31:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PrimaryTable](
	[Id1] [int] NOT NULL,
	[id2] [int] NOT NULL,
	[data] [nchar](10) NULL,
 CONSTRAINT [PK_PrimaryTable] PRIMARY KEY CLUSTERED 
(
	[Id1] ASC,
	[id2] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[ForeignTable]    Script Date: 12/19/2012 17:31:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ForeignTable](
	[id1] [int] NOT NULL,
	[id2] [int] NOT NULL,
	[Refernencedata] [nchar](10) NULL
) ON [PRIMARY]
GO
/****** Object:  ForeignKey [FK_ForeignTable_PrimaryTable]    Script Date: 12/19/2012 17:31:35 ******/
ALTER TABLE [dbo].[ForeignTable]  WITH CHECK ADD  CONSTRAINT [FK_ForeignTable_PrimaryTable] FOREIGN KEY([id1], [id2])
REFERENCES [dbo].[PrimaryTable] ([Id1], [id2])
GO
ALTER TABLE [dbo].[ForeignTable] CHECK CONSTRAINT [FK_ForeignTable_PrimaryTable]
GO





我为你创建了粗略的脚本请检查您是否正在寻找此项。我建议首先尝试在表中没有数据,这样就不会得到与数据相关的问题,然后检查是否存在不允许您创建外键的现有记录。



I created rough script for you please check if you are looking for this. I would suggest try first without data in the tables so that you do not get the data related issues and then check if there is existing records which are not allowing you to create foreign keys.


这篇关于具有多个主键的单个引用键。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆