Udostępnij za pośrednictwem


How to: Create Matching Foreign and Primary Keys (Visual Database Tools)

When you copy a column from one table to another table, the column name and many of the other properties you defined for that column in the original table are copied to the new location. You can copy column properties from a primary key column to a foreign key column if you want to relate the two columns. Related columns must have the same data type (or data types that can be related) and length.

To copy primary key columns to foreign key columns

  1. In Object Explorer, right-click the table with primary key columns you want to copy and click Design (Modify in SP1 or earlier). Do the same for the table into which you want to copy the columns as foreign keys.

    The tables open in Table Designer.

  2. Click the tab for the table with the columns you want to copy.

  3. Select the primary key columns that you want to copy.

    Primary key columns are indicated by a key symbol in their row selector.

  4. From the Edit menu, click Copy.

  5. Click the tab for the table into which you want to copy the columns.

  6. Select the column you want to follow the inserted columns and from the Edit menu, click Paste.

  7. Right-click the foreign key table and click Relationships on the shortcut menu.

    The Foreign Key Relationships dialog box opens.

  8. Create a relationship between the primary key and foreign key columns.

See Also

Other Resources

Working with Keys (Visual Database Tools)
Working with Constraints (Visual Database Tools)
Working with Relationships (Visual Database Tools)

Help and Information

Getting SQL Server 2005 Assistance