Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
DaveP62 19-Dec-12 9:29am    
Hello, more information is needed to provide a solution.
What are the data types for the two Primary Key fields in E1?
Are you trying to use a single column in E2 as a Foreign Key to the two Primary Key fields in E1?
This may not be possible. You may have to have two fields in E2 of the same data types as the two Primary Key fields in E1 and then make two Foreign Key references.
armarzook 19-Dec-12 9:37am    
That is exactly the problem here. I don't have those two fields in the table E2. and about that datatypes, two tables are having int datatype only.
DaveP62 19-Dec-12 13:03pm    
Still not sure what you are trying to do. Can you provide the following scripts?

1 - Script to create table E1 as it is now

2 - Script to create table E2 as it is now

3 - Script that causes your problem "Primary key does not exist the the table E1 and it is not null".

1 solution

SQL
/****** 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900