Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
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 18-Dec-12 22:40pm
Comments
DaveP62 at 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 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

/****** 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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100