Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Dual Primary Key?

Dual Primary Key?

Scheduled Pinned Locked Moved Database
question
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Hi Our ERP system has a stock master table. It consist of the fields 'warehouse', 'product' and a bunch of other fields relating to the stock item. A product can exist in various warehouses, so it is the warehouse +product combination that is unique. How would the primary key (if any) be set on this table?

    L 1 Reply Last reply
    0
    • R Richard Berry100

      Hi Our ERP system has a stock master table. It consist of the fields 'warehouse', 'product' and a bunch of other fields relating to the stock item. A product can exist in various warehouses, so it is the warehouse +product combination that is unique. How would the primary key (if any) be set on this table?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A primary key (often) consists out of multiple columns. I'd suggest putting the primary key on BOTH, and to add an autoincrement-field and make that unique. Use the autoincrement-column to make relations to other tables.

      ALTER TABLE dbo.MasterTable ADD CONSTRAINT PK_MasterTable
      PRIMARY KEY CLUSTERED (WareHouseId, ProductId);

      (And call the autoincrement "MasterTableId")

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      R M 2 Replies Last reply
      0
      • L Lost User

        A primary key (often) consists out of multiple columns. I'd suggest putting the primary key on BOTH, and to add an autoincrement-field and make that unique. Use the autoincrement-column to make relations to other tables.

        ALTER TABLE dbo.MasterTable ADD CONSTRAINT PK_MasterTable
        PRIMARY KEY CLUSTERED (WareHouseId, ProductId);

        (And call the autoincrement "MasterTableId")

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        R Offline
        R Offline
        Richard Berry100
        wrote on last edited by
        #3

        Thanks Eddie, but you have totally lost me? If I lok at the two lines:

        ADD CONSTRAINT PK_MasterTable

        Prevents any duplicates in MasterTable column and

        PRIMARY KEY CLUSTERED (WareHouseId, ProductId)

        Stores the data physically sorted by WarehouseId then ProductID? Or are there three Primary keys now? If you look at the table in MSQuery, the Product is bold, so is this the primary key?

        L 1 Reply Last reply
        0
        • R Richard Berry100

          Thanks Eddie, but you have totally lost me? If I lok at the two lines:

          ADD CONSTRAINT PK_MasterTable

          Prevents any duplicates in MasterTable column and

          PRIMARY KEY CLUSTERED (WareHouseId, ProductId)

          Stores the data physically sorted by WarehouseId then ProductID? Or are there three Primary keys now? If you look at the table in MSQuery, the Product is bold, so is this the primary key?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Richard.Berry100 wrote:

          Prevents any duplicates in MasterTable column

          No, it doesn't; it's the part behind that which says "which type" of constraint. It's a single statement, not two separate statements.

          Richard.Berry100 wrote:

          PRIMARY KEY CLUSTERED (WareHouseId, ProductId)

          Stores the data physically sorted by WarehouseId then ProductID?

          Yup.

          Richard.Berry100 wrote:

          Or are there three Primary keys now?

          One (compound) primary key, consisting of two columns, and the name of that constraint is "PK_MasterTable".

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • L Lost User

            A primary key (often) consists out of multiple columns. I'd suggest putting the primary key on BOTH, and to add an autoincrement-field and make that unique. Use the autoincrement-column to make relations to other tables.

            ALTER TABLE dbo.MasterTable ADD CONSTRAINT PK_MasterTable
            PRIMARY KEY CLUSTERED (WareHouseId, ProductId);

            (And call the autoincrement "MasterTableId")

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Why would you not use WharehouseID-ProductID in a concatenated field as a primary key.

            Never underestimate the power of human stupidity RAH

            P L 2 Replies Last reply
            0
            • M Mycroft Holmes

              Why would you not use WharehouseID-ProductID in a concatenated field as a primary key.

              Never underestimate the power of human stupidity RAH

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Mycroft Holmes wrote:

              WharehouseID

              I see what you did there. :laugh:

              1 Reply Last reply
              0
              • M Mycroft Holmes

                Why would you not use WharehouseID-ProductID in a concatenated field as a primary key.

                Never underestimate the power of human stupidity RAH

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Mycroft Holmes wrote:

                Why would you not use WharehouseID-ProductID in a concatenated field as a primary key.

                That's a possibility; then again, it introduces a concatenation-action, and we'd be storing redundant information. It'd also affect performance; having a large varchar-based key (as two bigints as Id's or Guids would be concatenated to a varchar) would be not-nice for your indexes. Or, in the words of my teacher; it would no longer be an atomic value.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups