locked
SQL 2008 R2 Project Relationships RRS feed

  • Question

  • Hi together,

    I have created my tables in VS 2010 with MS SQL 2008 R2, this has all worked fine, what I require now is to create relationships between the tables. I have been able to establish relationships, yet it seems to me that I just can not get it right. Is there an introduction avaliable as to how to do this ?

    Thank you,

    Wolfgang

    Monday, August 15, 2011 4:33 PM

Answers

  • Hello Wolfgang,

    Thanks for your post.

    Based on your description it seems that you want to add relationships between two tables in the database project. Am I right? If so, you should add two foreign keys in the database project. In your database project, go to Schema Objects->Schemas->dbo->Tables-Keys, add two foreign keys.

    For example, if I have two tables, [dbo].[Student] and [dbo].[Classtable]. I would like to add reference to the [dbo].[Classtable] in the [dbo].[Student]. I should add two foreign keys:

    ALTER TABLE [dbo].[Student]

        ADD CONSTRAINT [FK_Student_Classtable] FOREIGN KEY ([classId]) REFERENCES [dbo].[Classtable] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

     

    ALTER TABLE [dbo].[Classtable]

        ADD CONSTRAINT [FK_Classtable_Classtable] FOREIGN KEY ([Id]) REFERENCES [dbo].[Classtable] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

    Thanks,


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 16, 2011 7:57 AM
    Moderator