views:

64

answers:

2

This question is pretty much similar to this one, but for SQL Server 2005 :

I have 2 tables in my database:

--'#' denotes the primary key
[Libraries]
#ID   #Application  Name
 1     MyApp        Title 1
 2     MyApp        Title 2


[Content]
#ID   Application  LibraryID  Content
 10    MyApp       1          xxx
 11    MyApp       1          yyy

(the database is obviously much more complex and having this double key makes sense)

Each library is identified by its unique ID and Application name. I'm trying to ensure that each content is properly referencing an existing library.

When creating the constraint (using the Wizard) as

Primary key table            Foreign key table
[Libraries]                  [Content]
ID                  --->     LibraryID
Application         --->     Application

I have the following error:

The columns in table 'Libraries' do not match an existing primary key or UNIQUE constraint

Do you have any idea of what is going on? and if it's possible at all using SQL Server? (I can't modify the [Library] table at all)

Thanks a lot for your help!

+1  A: 

Of course it's possible to create a foreign key relationship to a compound (more than one column) primary key. You didn't show us the statement you're using to try and create that relationship - it should be something like:

ALTER TABLE dbo.Content
   ADD CONSTRAINT FK_Content_Libraries
   FOREIGN KEY(LibraryID, Application)
   REFERENCES dbo.Libraries(ID, Application)

Is that what you're using?? If (ID, Application) is indeed the primary key on dbo.Libraries, this statement should definitely work.

Luk: just to check - can you run this statement in your database and report back what the output is??

SELECT
    tc.TABLE_NAME,
    tc.CONSTRAINT_NAME, 
    ccu.COLUMN_NAME
FROM 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN 
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu 
      ON ccu.TABLE_NAME = tc.TABLE_NAME AND ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
WHERE
    tc.TABLE_NAME IN ('Libraries', 'Content')
marc_s
I was using the UI directly at that point, but using your syntax raises the following error: "There are no primary or candidate keys in the referenced table 'dbo.Libraries' that match the referencing column list in the foreign key...
Luk
Well, in that case, the pair (Id, Application) is **not** the primary key on the Libraries table. You can only reference the primary key on a parent table - or a column (or set of columns) that are part of a unique index. Check your `Libraries` table!
marc_s
That was my first thought too, but the SQL script specifies`CREATE TABLE [Libraries] (`...` CONSTRAINT [PK_sf_Libraries] PRIMARY KEY CLUSTERED ( [Application] ASC, [ID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]`... (sorry for dumping all the SQL here)
Luk
@Luk: something must be off here - either that script wasn't executed, or the table Libraries was modified since, or you have a problem with table names, or you're in the wrong database or something.... if `Libraries` really has that primary key, then the FK reference ought to work.
marc_s
@Luk: added a statement you could run for me on your database - just to check.....
marc_s
Sorry, I was sure I had replied. I have checked and the primary keys seem correct. However, I have created a new database with only these two tables (in a clean, empty state) and I can create the key without any trouble. I guess there's something in the state of the database, unrelated to the matter at hand, that prevents me from creating the key. I'm gonna drop the matter from now, the problem is deeper than expected. Thanks!
Luk
+1  A: 

The Content table likely to have multiple duplicate Application values that can't be mapped to Libraries. Is it possible to drop the Application column from the Libraries Primary Key Index and add it as a Unique Key Index instead?

Adrian Godong
Yes, maybe duplicates for `Application` but that's never required to be unique. The pair of `(LibraryID, Application)` can even have multiple values - that's not the PK on the `Content` table - it's the PK on the `Libraries` table!
marc_s
OK, so what happens when you have (LibraryID, Application) from Content that does not correspond to (ID, Application) on Libraries? As of now, there's no constraint to enforce that.If OP don't want to change the current structure, I would suggest him to try creating a Unique Key index on (LibraryID, Application) and see if it can be successfully created. Only then, try creating the relationship again.
Adrian Godong
Unfortunately, I can't touch `Libraries` at all. The two tables are even supposed have a 1-1 relationship because I can't add new columns to `Libraries`. You are right, a Unique index is required there (but that didn't fix the problem)
Luk
Am I wrong thinking I can only reference columns in a Foreign Key?
Luk
Luk: you can reference two columns in an FK. Have you checked whether all row in Contents can be mapped to Libraries? (a simple LEFT JOIN will determine whether you can do this)
Adrian Godong
Content is empty for the moment, `ID` and `LibraryID` are a `uniqueidentifier NOT NULL` and `Application` is a `nvarchar(50) NOT NULL` on both sides... This is getting weird, I'll probably come back with a fresher head later.
Luk