Home Forums SQL Server 2008 SQL Server 2008 - General how ro create a table with multiple coloumns defined to a single primary key RE: how ro create a table with multiple coloumns defined to a single primary key

  • It is very important for you to know how to read tsql documentation. Even better, download it and install locally. Effort on that will give you many benefits and faster advancement in knowledge.

    Here is example of multicolumn primary key, along with two foreign keys:

    CREATE TABLE MyTable

    (

    ForeignId1 int,

    ForeignId2 int,

    CONSTRAINT MyTable_PK

    PRIMARY KEY ( ForeignID1, ForeignID2 ),

    CONSTRAINT MyTable_FK_Table1

    FOREIGN KEY ( ForeignID1 ) REFERENCES Table1( ID ),

    CONSTRAINT MyTable_FK_Table2

    FOREIGN KEY ( ForeignID2 ) REFERENCES Table2( ID )

    )

    Primary key = not null constraint + unique index

    Is is recommended to give table a PK that is artificial, e.g. "int identity".