Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group the Primary Keys

Status
Not open for further replies.

DB2Problem

Programmer
Oct 17, 2002
53
US
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)

Your suggestion is highly appreciated


 
If you want to combine the keys only for using in the query :

you can use the concat operator to concatenate two strings
'||' (its not the pipe sign).
In my mainframe '|' is obtained by the <alt>+'`' combination.
Or you can use the concat function
concat(string1,string2)

so you can combine three primary keys as follows :

secondary_field1 || secondary_field2 || secondary_field3

or

concat(secondary_field1,
concat(secondary_field2,secondary_field3))


The result will be a char type.

you can convert an integer to char type using the following function CHAR(var).

So if you want to check the combined primary key with an integer value. you can convert the integer value to char format using char(variable_name) and compare the two char types.

Please let me know if you have any doubt in this.
 
Thanks a lot for your feedback.

However, still a code snippet or a small example will help me better understand.

Taking the example above, can you tell me how this will work.. I am using DB2-UDB (no mainframe)
 
Consider the following values for the table fields :

secondary_field1 char 4 = '1234' ==> primary key
secondary_field2 char 4 = '5678' ==> primary key
secondary_field3 char 4 = '9012' ==> primary key

then the following query can be used to select that row:

Select * from table_name where concat(concat(secondary_field1,secondary_field2),secondary_field3) =
char(123456789012);

I hope this is what you intended. Let me know if it isnt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top