constraint expression for unique keys

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

    constraint expression for unique keys

    if i have a table which defines a rule as "combinatio n of two field
    must be unique", how can I write this in a constraint expression
    section?
    i started learning more about ms sql side to handle all the necessary
    rules in back-end instead of front-end.
    also any good learning links, references, or book recommandations ?
    thanks

  • Alexander Kuznetsov

    #2
    Re: constraint expression for unique keys

    an excerpt from BOL:

    "C. Using UNIQUE constraints
    UNIQUE constraints are used to enforce uniqueness on nonprimary key
    columns. The following example enforces a restriction that the Name
    column of the Product table must be unique.

    Copy Code
    Name nvarchar(100) NOT NULL
    UNIQUE NONCLUSTERED

    "

    Comment

    • HandersonVA

      #3
      Re: constraint expression for unique keys

      can you explain what "NONCLUSTER ED" is doing there? is that for
      non-relation to a field in other table?

      Alexander Kuznetsov wrote:[color=blue]
      > an excerpt from BOL:
      >
      > "C. Using UNIQUE constraints
      > UNIQUE constraints are used to enforce uniqueness on nonprimary key
      > columns. The following example enforces a restriction that the Name
      > column of the Product table must be unique.
      >
      > Copy Code
      > Name nvarchar(100) NOT NULL
      > UNIQUE NONCLUSTERED
      >
      > "[/color]

      Comment

      • Alexander Kuznetsov

        #4
        Re: constraint expression for unique keys

        SQL Server implicitly creates an index to implement a uinque
        constraint. In this case NONCLUSTERED means the index will be
        non-clustered.

        Comment

        • Erland Sommarskog

          #5
          Re: constraint expression for unique keys

          HandersonVA (handersonva@ho tmail.com) writes:[color=blue]
          > if i have a table which defines a rule as "combinatio n of two field
          > must be unique", how can I write this in a constraint expression
          > section?[/color]

          CONSTRAINT u_tbl UNIQUE (col1, col2)




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

          Books Online for SQL Server 2005 at

          Books Online for SQL Server 2000 at

          Comment

          • --CELKO--

            #6
            Re: constraint expression for unique keys

            >> if i have a table which defines a rule as "combinatio n of two field must be unique", how can I write this in a constraint expression section? <<

            CONSTRAINT unique_location UNIQUE (x, y)[color=blue][color=green]
            >> any good learning links, references, or book recommandations ? <<[/color][/color]

            I recommend buying all of my books :)

            Comment

            Working...