Tech Off Post

Single Post Permalink

View Thread: How do you set up a foreign key in SQL Azure?
  • User profile image

    , complete wrote

    is that done on T-SQL?  I think it has to be done with the Management Studio.

    Generic Forum Image

    The youtube video instructs to use this syntax.  But what is the constraint?  Which table should be altered -- the main one or the index table?

    This instructional video does not explain how it is to be done.  It shows how to create the required three constraints but nothing beyond that.  So, what else do I do?

    Generic Forum Image

    Generic Forum Image

    Generic Forum Image



    I don't use management studio, except to run SQL Wink

    So a constraint is, errr, a constraint on what data can go into a table. A primary key is a constraint because it tells SQL to constrain the values in the column to be unique, no duplicates.

    A foreign key tells SQL to constrain values in a column to only allow values which exist in another column in another table.

    So you first set up a primary key on the master column, to take one of yours, age classification. Let's assume it has an AgeId and a Description field; you'd do


    Then you need to setup the foreign key. I will assume that your primary table is called People, and it also has an AgeID field, which you want to be the link between the two.

    ALTER TABLE PeopleADD CONSTRAINT FK_People_AgeClassification_AgeIdFOREIGN KEY (AgeId)REFERENCES AgeClassification (AgeId)

    So the line FOREIGN KEY (AgeId) specifies the field you are going to link to another table. REFERENCES gives the other table name and then, in brackets, the field in the other table.

    Does that help?