Composite foreign Key in SQL Server

5 pts.
Tags:
Composite Foreign Keys
Foreign keys
SQL Server 2008
SQL Server error messages
I have a composite foreign key that has to be part of the primary key of the new table... but an error appears in SQL server...

CREATE TABLE Se_paga_con

(IDpel INT NOT NULL,

IDvta INT NOT NULL,

IDnota INT NOT NULL,

CantidadPeliculas INT,

PRIMARY KEY (IDpel,IDvta,IDnota),

FOREIGN KEY (IDpel, IDvta) REFERENCES PeliculaEnVenta(IDpel, IDvta));

IDpel AND IDvta ARE the primary key from PeliculaEnVenta

There are no primary or candidate keys in the referenced table 'PeliculaEnVenta' that match the referencing column list in the foreign key 'FK__Se_paga_con__51851410'.



Software/Hardware used:
SQL server 2008
ASKED: November 17, 2009  1:56 AM
UPDATED: November 17, 2009  3:01 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    That should work if IDpel and IDvta are the primary key of PeliculaEnVenta. Can you run the following query, to check the pk of the table ?
    SELECT kc.name, c.name
    FROM sys.key_constraints kc
    JOIN sys.index_columns ic
    ON kc.parent_object_id = ic.object_id
    JOIN sys.columns c 
    ON ic.object_id = c.object_id 
    AND ic.column_id = c.column_id
    WHERE kc.type = 'PK'
    AND OBJECT_NAME(kc.parent_object_id) = 'PeliculaEnVenta';
    65,110 pointsBadges:
    report
  • carlosdl
    The first line should be: SELECT kc dot name, c dot name
    65,110 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following