dual primary keys?

  • In Access it's possible to apply two primary keys to a field (so that both fields must be unique as a pair)--will SQL Server 2000 recognize such structures? How would this be accomplished?

  • In SQL this would be a compound PK. You could setup two fields as the PK with

    ALTER TABLE dbo.<xxx, char( 32), TblName> ADD CONSTRAINT

    Table_PK PRIMARY KEY NONCLUSTERED

    (

    FieldA Int

    , FieldB int)

    Steve Jones

    steve@dkranch.net

  • Composite keys (when multiple fields make up a unique identifier for a row) are supported in SQL Server. A way to do this in T-SQL is to use a table-level constraint. Instead of creating the constraint against the column like so:

    
    
    Table Example (
    ColumnName int NOT NULL CONSTRAINT PK_Example PRIMARY KEY,
    .
    .
    .
    )

    we would create a constraint separate from the column like so:

    
    
    CREATE TABLE Example (
    Column1 char(3) NOT NULL,
    Column2 int NOT NULL,
    .
    .
    .
    CONSTRAINT PK_Example PRIMARY KEY (Column1, Column2)
    )

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • Or in EM click the first items row selector (the grey box to the side) and then hold down shift or ctrl to select others the same way and finally press the PrimaryKey button or right click and choose Primary Key. Then save.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • thanks everybody!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply