Unique Constraint on Multiple columns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    #1

    Unique Constraint on Multiple columns

    Can you create a unique constraint on multiple columns, or does it have
    to be implemented as a unique index?

    If possible can someone please post some sample code?

    Thanks,

  • Hugo Kornelis

    #2
    Re: Unique Constraint on Multiple columns

    On 31 Oct 2005 14:42:28 -0800, Dave wrote:
    [color=blue]
    >Can you create a unique constraint on multiple columns, or does it have
    >to be implemented as a unique index?
    >
    >If possible can someone please post some sample code?
    >
    >Thanks,[/color]

    Hi Dave,

    CREATE TABLE Example
    (Col1 int NOT NULL,
    Col2 int NOT NULL,
    UNIQUE (Col1, Col2)
    )

    Best, Hugo
    --

    (Remove _NO_ and _SPAM_ to get my e-mail address)

    Comment

    • Erland Sommarskog

      #3
      Re: Unique Constraint on Multiple columns

      Dave (daveg.01@gmail .com) writes:[color=blue]
      > Can you create a unique constraint on multiple columns, or does it have
      > to be implemented as a unique index?[/color]

      A UNIQUE constraint is always implemented as a unique index. But an
      index can span more than one column.
      [color=blue]
      > If possible can someone please post some sample code?[/color]

      Here is a real-world table:

      CREATE TABLE officerefunds (
      orfid int NOT NULL,
      chtcode aba_chtcode NOT NULL
      CONSTRAINT ckc_orf_chtcode CHECK
      (chtcode NOT IN ('MIN', 'MAX', 'CTX', 'STX')),
      ofcid smallint NULL,
      ityid smallint NULL,
      insid aba_insid NULL,
      chgid smallint NULL,
      officerefund aba_fraction NOT NULL,
      deductfee bit NOT NULL,
      CONSTRAINT pk_orf PRIMARY KEY CLUSTERED (orfid),
      CONSTRAINT ak_orf UNIQUE NONCLUSTERED
      (chtcode, ofcid, ityid, insid, chgid),
      CONSTRAINT ckt_orf_instrum ent CHECK
      (NOT (ityid IS NOT NULL AND insid IS NOT NULL))
      )



      --
      Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

      Books Online for SQL Server SP3 at
      http://www.microsoft.com/sql/techinf...2000/books.asp

      Comment

      • Dave

        #4
        Re: Unique Constraint on Multiple columns

        That is kind of what i though. So it is always an index? I could not
        find that documentation anywhere, but I know I have read that some
        where too.

        Comment

        • Hugo Kornelis

          #5
          Re: Unique Constraint on Multiple columns

          On 2 Nov 2005 08:36:48 -0800, Dave wrote:
          [color=blue]
          >That is kind of what i though. So it is always an index? I could not
          >find that documentation anywhere, but I know I have read that some
          >where too.[/color]

          Hi Dave,

          In SQL Server, a unique index will always be created forn each PRIMARY
          KEY or UNIQUE constraint. This index is used to enforce the constraint.

          Best, Hugo
          --

          (Remove _NO_ and _SPAM_ to get my e-mail address)

          Comment

          Working...