Discussions

Performance and scalability: Group Primary Keys

  1. Group Primary Keys (1 messages)

    Hello All

    Can anyone tell me how to combine primary keys into one single key

    Previously, my database has just one primary key (PK). But the database has gone a change with 3 new fields acting as PK.

    I am using SQL like –

    SELECT primary_id as pk,
                   secondary_field1,
                   secondary_field2,
    FROM schema.table
             WHERE primary_id = {0};

    Where the primary_id is dynamically filled.

    But Now I have following SQL –

    SELECT secondary_field1,
      secondary_field2,
            secondary_field3 as pk,
    secondary_field4,
    secondary_field5
    FROM schema.table WHERE secondary_field1= {0} and secondary_field2= {0} secondary_field3= {0};

    How can I combine the above 3 PK’s (Char Datatype) into one PK (Integer Datatype). There is no EJB, it's simple MVC
    Your suggestion is highly appreciated

    Threaded Messages (1)

  2. Group Primary Keys[ Go to top ]

    Well, if you really want to, you can build a 32-bit integer out of 3 8-bit chars by having the lowest 8 bits as the first char, the next 8 bits as the second char, and the next 8 bits as the third char. The last 8 bits should always be zero. This will be an integer equivalent of your three chars; you can use the shift operators (<
    However, I would seriously question why you'd need to do this. I think a better solution would be to make a class that contains the three chars and that acts as the PK.

    Also, using composite PKs in the database is generally believed to be a bad design practice, so if you can influence the DB design, it might be a good idea to use the integer as a PK in the DB in the first place.

    Pietari Laurila