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. SQL Compound Primary Keys

SQL Compound Primary Keys

Scheduled Pinned Locked Moved Database
databasesysadminhelptutorialquestion
10 Posts 5 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.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have an application where the client is working against a local DB, and each night will upload its data to the server, so I need Id's that are unique in the server's DB. The server and client DB's are identical. Here's the tables:

    CREATE TABLE tblOperators
    (OperatorId INT IDENTITY NOT NULL,
    AccountNo VARCHAR(10) NULL,
    [Name] VARCHAR(100) NOT NULL,
    Address1 VARCHAR(100) NOT NULL,
    Address2 VARCHAR(100) NULL,
    City VARCHAR(100) NOT NULL,
    [State] VARCHAR(2) NOT NULL,
    ZipCode VARCHAR(10) NOT NULL,
    Phone VARCHAR(10) NOT NULL,
    Email VARCHAR(100) NULL,
    IsActive BIT NULL)

    CREATE TABLE tblFacilities
    (FacilityId INT IDENTITY NOT NULL,
    OperatorId INT NOT NULL,
    FormOfPaymentId INT NULL,
    SaleTypeId INT NULL,
    FacilityName VARCHAR(100) NULL,
    FacilityAddress1 VARCHAR(100) NULL,
    FacilityAddress2 VARCHAR(100) NULL,
    FacilityCity VARCHAR(100) NULL,
    FacilityState VARCHAR(02) NULL,
    FacilityZip VARCHAR(10) NULL,
    FacilityPhone VARCHAR(10) NULL,
    BankName VARCHAR(100) NULL,
    ManagerName VARCHAR(100) NULL,
    ManagerPhone VARCHAR(10) NULL,
    ManagerEmail VARCHAR(100) NULL,
    PickupDays VARCHAR(100) NULL,
    IsActive BIT NULL)

    You can see the Operator and Facility Id's. They won't work once sent to the server. I'm not sure how to define compound keys. Can someone point me in the right direction? Thanks

    If it's not broken, fix it until it is

    P J L D 4 Replies Last reply
    0
    • K Kevin Marois

      I have an application where the client is working against a local DB, and each night will upload its data to the server, so I need Id's that are unique in the server's DB. The server and client DB's are identical. Here's the tables:

      CREATE TABLE tblOperators
      (OperatorId INT IDENTITY NOT NULL,
      AccountNo VARCHAR(10) NULL,
      [Name] VARCHAR(100) NOT NULL,
      Address1 VARCHAR(100) NOT NULL,
      Address2 VARCHAR(100) NULL,
      City VARCHAR(100) NOT NULL,
      [State] VARCHAR(2) NOT NULL,
      ZipCode VARCHAR(10) NOT NULL,
      Phone VARCHAR(10) NOT NULL,
      Email VARCHAR(100) NULL,
      IsActive BIT NULL)

      CREATE TABLE tblFacilities
      (FacilityId INT IDENTITY NOT NULL,
      OperatorId INT NOT NULL,
      FormOfPaymentId INT NULL,
      SaleTypeId INT NULL,
      FacilityName VARCHAR(100) NULL,
      FacilityAddress1 VARCHAR(100) NULL,
      FacilityAddress2 VARCHAR(100) NULL,
      FacilityCity VARCHAR(100) NULL,
      FacilityState VARCHAR(02) NULL,
      FacilityZip VARCHAR(10) NULL,
      FacilityPhone VARCHAR(10) NULL,
      BankName VARCHAR(100) NULL,
      ManagerName VARCHAR(100) NULL,
      ManagerPhone VARCHAR(10) NULL,
      ManagerEmail VARCHAR(100) NULL,
      PickupDays VARCHAR(100) NULL,
      IsActive BIT NULL)

      You can see the Operator and Facility Id's. They won't work once sent to the server. I'm not sure how to define compound keys. Can someone point me in the right direction? Thanks

      If it's not broken, fix it until it is

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      That is a big reason not to use integers as keys. You may need to rethink your whole application. I mainly wonder why the clients would be adding records to these tables. However, in cases like that each client should probably have its own ID and therefore you could make the key be both the ClientID and the other Id.

      K L 2 Replies Last reply
      0
      • P PIEBALDconsult

        That is a big reason not to use integers as keys. You may need to rethink your whole application. I mainly wonder why the clients would be adding records to these tables. However, in cases like that each client should probably have its own ID and therefore you could make the key be both the ClientID and the other Id.

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        PIEBALDconsult wrote:

        You may need to rethink your whole application.
        I mainly wonder why the clients would be adding records to these tables.

        What if the client doesn't have a network connection? Where would the store the data. This application cannot have down time in the event of a lost connection. So the app will have a local data store and update the server periodically. So, since new records can be added in the client's DB, I need to make sure the PK's are unique. Thanks

        If it's not broken, fix it until it is

        P 1 Reply Last reply
        0
        • K Kevin Marois

          I have an application where the client is working against a local DB, and each night will upload its data to the server, so I need Id's that are unique in the server's DB. The server and client DB's are identical. Here's the tables:

          CREATE TABLE tblOperators
          (OperatorId INT IDENTITY NOT NULL,
          AccountNo VARCHAR(10) NULL,
          [Name] VARCHAR(100) NOT NULL,
          Address1 VARCHAR(100) NOT NULL,
          Address2 VARCHAR(100) NULL,
          City VARCHAR(100) NOT NULL,
          [State] VARCHAR(2) NOT NULL,
          ZipCode VARCHAR(10) NOT NULL,
          Phone VARCHAR(10) NOT NULL,
          Email VARCHAR(100) NULL,
          IsActive BIT NULL)

          CREATE TABLE tblFacilities
          (FacilityId INT IDENTITY NOT NULL,
          OperatorId INT NOT NULL,
          FormOfPaymentId INT NULL,
          SaleTypeId INT NULL,
          FacilityName VARCHAR(100) NULL,
          FacilityAddress1 VARCHAR(100) NULL,
          FacilityAddress2 VARCHAR(100) NULL,
          FacilityCity VARCHAR(100) NULL,
          FacilityState VARCHAR(02) NULL,
          FacilityZip VARCHAR(10) NULL,
          FacilityPhone VARCHAR(10) NULL,
          BankName VARCHAR(100) NULL,
          ManagerName VARCHAR(100) NULL,
          ManagerPhone VARCHAR(10) NULL,
          ManagerEmail VARCHAR(100) NULL,
          PickupDays VARCHAR(100) NULL,
          IsActive BIT NULL)

          You can see the Operator and Facility Id's. They won't work once sent to the server. I'm not sure how to define compound keys. Can someone point me in the right direction? Thanks

          If it's not broken, fix it until it is

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          Kevin Marois wrote:

          and each night will upload its data to the server

          Is this a oneway communication where the data is only uploaded, or will it synchronise the data with the server? If it's just an upload you don't need to have global keys, you can add new keys at the server when uploading.

          Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

          K 1 Reply Last reply
          0
          • K Kevin Marois

            I have an application where the client is working against a local DB, and each night will upload its data to the server, so I need Id's that are unique in the server's DB. The server and client DB's are identical. Here's the tables:

            CREATE TABLE tblOperators
            (OperatorId INT IDENTITY NOT NULL,
            AccountNo VARCHAR(10) NULL,
            [Name] VARCHAR(100) NOT NULL,
            Address1 VARCHAR(100) NOT NULL,
            Address2 VARCHAR(100) NULL,
            City VARCHAR(100) NOT NULL,
            [State] VARCHAR(2) NOT NULL,
            ZipCode VARCHAR(10) NOT NULL,
            Phone VARCHAR(10) NOT NULL,
            Email VARCHAR(100) NULL,
            IsActive BIT NULL)

            CREATE TABLE tblFacilities
            (FacilityId INT IDENTITY NOT NULL,
            OperatorId INT NOT NULL,
            FormOfPaymentId INT NULL,
            SaleTypeId INT NULL,
            FacilityName VARCHAR(100) NULL,
            FacilityAddress1 VARCHAR(100) NULL,
            FacilityAddress2 VARCHAR(100) NULL,
            FacilityCity VARCHAR(100) NULL,
            FacilityState VARCHAR(02) NULL,
            FacilityZip VARCHAR(10) NULL,
            FacilityPhone VARCHAR(10) NULL,
            BankName VARCHAR(100) NULL,
            ManagerName VARCHAR(100) NULL,
            ManagerPhone VARCHAR(10) NULL,
            ManagerEmail VARCHAR(100) NULL,
            PickupDays VARCHAR(100) NULL,
            IsActive BIT NULL)

            You can see the Operator and Facility Id's. They won't work once sent to the server. I'm not sure how to define compound keys. Can someone point me in the right direction? Thanks

            If it's not broken, fix it until it is

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

            I don't see a key, only an identity-field. If that identity is the key, and you need a quick solution, add a second column with a GUID and promote that to be the primary key. I'd recommend to have a "real" key; take those columns that uniquely identify a record, and use those as a key, and the identity merely as an alternative shortcut to that.

            Bastard Programmer from Hell :suss:

            1 Reply Last reply
            0
            • P PIEBALDconsult

              That is a big reason not to use integers as keys. You may need to rethink your whole application. I mainly wonder why the clients would be adding records to these tables. However, in cases like that each client should probably have its own ID and therefore you could make the key be both the ClientID and the other Id.

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

              PIEBALDconsult wrote:

              That is a big reason not to use integers as keys.

              Yeah, people being too lazy. The problem goes away with GUIDs, since that allows for laziness.

              Bastard Programmer from Hell :suss:

              1 Reply Last reply
              0
              • K Kevin Marois

                I have an application where the client is working against a local DB, and each night will upload its data to the server, so I need Id's that are unique in the server's DB. The server and client DB's are identical. Here's the tables:

                CREATE TABLE tblOperators
                (OperatorId INT IDENTITY NOT NULL,
                AccountNo VARCHAR(10) NULL,
                [Name] VARCHAR(100) NOT NULL,
                Address1 VARCHAR(100) NOT NULL,
                Address2 VARCHAR(100) NULL,
                City VARCHAR(100) NOT NULL,
                [State] VARCHAR(2) NOT NULL,
                ZipCode VARCHAR(10) NOT NULL,
                Phone VARCHAR(10) NOT NULL,
                Email VARCHAR(100) NULL,
                IsActive BIT NULL)

                CREATE TABLE tblFacilities
                (FacilityId INT IDENTITY NOT NULL,
                OperatorId INT NOT NULL,
                FormOfPaymentId INT NULL,
                SaleTypeId INT NULL,
                FacilityName VARCHAR(100) NULL,
                FacilityAddress1 VARCHAR(100) NULL,
                FacilityAddress2 VARCHAR(100) NULL,
                FacilityCity VARCHAR(100) NULL,
                FacilityState VARCHAR(02) NULL,
                FacilityZip VARCHAR(10) NULL,
                FacilityPhone VARCHAR(10) NULL,
                BankName VARCHAR(100) NULL,
                ManagerName VARCHAR(100) NULL,
                ManagerPhone VARCHAR(10) NULL,
                ManagerEmail VARCHAR(100) NULL,
                PickupDays VARCHAR(100) NULL,
                IsActive BIT NULL)

                You can see the Operator and Facility Id's. They won't work once sent to the server. I'm not sure how to define compound keys. Can someone point me in the right direction? Thanks

                If it's not broken, fix it until it is

                D Offline
                D Offline
                David Mujica
                wrote on last edited by
                #7

                You may have to change the way you are bringing data from the client DB to your server DB. Check this article out ... http://www.sqlteam.com/article/how-to-insert-values-into-an-identity-column-in-sql-server[^] Remember to vote if this helps. :thumbsup:

                1 Reply Last reply
                0
                • K Kevin Marois

                  PIEBALDconsult wrote:

                  You may need to rethink your whole application.
                  I mainly wonder why the clients would be adding records to these tables.

                  What if the client doesn't have a network connection? Where would the store the data. This application cannot have down time in the event of a lost connection. So the app will have a local data store and update the server periodically. So, since new records can be added in the client's DB, I need to make sure the PK's are unique. Thanks

                  If it's not broken, fix it until it is

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Yes, but why would the client be adding records to those tables?

                  Kevin Marois wrote:

                  PK's are unique

                  Globally unique?

                  K 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Yes, but why would the client be adding records to those tables?

                    Kevin Marois wrote:

                    PK's are unique

                    Globally unique?

                    K Offline
                    K Offline
                    Kevin Marois
                    wrote on last edited by
                    #9

                    I don't understand why you're questioning this. The software is installed locally on a user's PC, along with a local copy of the DB. The software does CRUD operations on the local copy of the DB. Then, at night, or at least once per week, the software uploads all new/changed/deleted records to the server.

                    If it's not broken, fix it until it is

                    1 Reply Last reply
                    0
                    • J Jorgen Andersson

                      Kevin Marois wrote:

                      and each night will upload its data to the server

                      Is this a oneway communication where the data is only uploaded, or will it synchronise the data with the server? If it's just an upload you don't need to have global keys, you can add new keys at the server when uploading.

                      Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                      K Offline
                      K Offline
                      Kevin Marois
                      wrote on last edited by
                      #10

                      There will be two-way communicaation

                      If it's not broken, fix it until it is

                      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