Compound keys, anyone?

55 pts.
Tags:
primary key
SQL Server
In many cases, you have to join individually non unique keys that together, are unique and can be used as primary keys.
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

A good example of this would be an order header file.

All of these can be key field and may still not be unique.
customer#
warehouse
division
order date
but add the order# and now using all keys you have a unique key you can use.

Discuss This Question: 10  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000
    That happens all the time in some coding languages. Is there a specific database, file or coding language you need more info on?
    129,825 pointsBadges:
    report
  • amissor
    order# is supposed to be unique, no need to append or affix any other column.
    55 pointsBadges:
    report
  • ToddN2000
    In most cases yes, order# may be unique. In our case its not. We also have a backorder level for each order that can go from 00 to 99 which add further uniqueness. We also have other company/divisions on their own computer systems that transmit a daily order file to us that we incorporate into our sales files. So we can have the same order number overlap but for multiple divisions and locations. This can make it tough on a developer having to write code. Not my choice to handle it this way and let the remote locations keep using their software and sending us a file we have to incorporate. They should be on the corporate software in my opinion.
    129,825 pointsBadges:
    report
  • amissor
    OK in this case. I have seen order#s broken down in case of partial shipment go know why!
    55 pointsBadges:
    report
  • TheRealRaven
    Regardles of usage, what exactly is the question? Are you needing to know how to create a compound primary key for SQL Server?
    33,690 pointsBadges:
    report
  • amissor
    Was not a question but a comment
    Best.
    55 pointsBadges:
    report
  • Subhendu Sen
    Please link here for more help: https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_0535.htm
    134,840 pointsBadges:
    report
  • TheRealRaven
    @amissor: Since this isn't a good forum for just "a comment", is there something about it that we can help with?
    33,690 pointsBadges:
    report
  • amissor
    Thx, nope.
    55 pointsBadges:
    report
  • amissor
    An example of redundant constraint in the (IBM) example Subhendu pointed to:
    The first line is unneeded because if the second is verified, then the third one (acc1 > acc2) makes the first one automatically verified implicitly. Now of course you have to take into account how specific DBMS processes null values and comparing with valid values.
       CHECK (0 < acct1 AND acct1 < 99999),
       CHECK (0 < acct2 AND acct2 < 99999),
       CHECK (acct1 > acct2)
    55 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: