Solved

How to change my primary key from bigint (int64) to int (int32)?

Posted on 2010-08-12
7
1,322 Views
Last Modified: 2012-05-10
Experts,

I want to change my primary key from bigint (int64) to int (int32). My old table schema was: CREATE TABLE table_old (mykey_old bigint(20), PRIMARY KEY (mykey_old)); I want to convert it to CREATE TABLE table_new (mykey_new int(11), PRIMARY KEY (mykey_new));

The old table has several data. For example: 1437900001132237672, 1437900001132292609, 1437900001133420533, 1437900001134147674, etc.
The new table data should be: 32237672, 32292609, 33420533, 34147674, etc. Another problem is the new data may contain duplicate and I need to create random value to replace the duplicates.

Any step ideas to convert it smoothly?

Thank you.
0
Comment
Question by:tikusbalap
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33426391
which version of sql are u using?
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33426406
this will create new table (remove duplicates)

select * into table_new from (
select * from (
select row_number() over (partititon by my_key old order by my_key old) rn, *
from table_old
) x where rn=1
)
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33426419
then update  table_new

set my_key = my_key % 10000000

then

alter my_table
alter column my_key int;

then add PK to your table
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 11

Expert Comment

by:JoeNuvo
ID: 33426458
There are few ways for you to go, depend on how do you want your old key to become

if just by yr example, you may insert into new table (or update old table)
by set value of mykey_new with value of (mykey_old - 14379000011)

or you may find MIN(mykey_old) and set value of mykey_new with value of (mykey_old - [old key min value])

or if you create new table and your primary key is "auto number",  you may just insert data into it
to let primary key start from any seed value you defined (usually start from 1)
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33426471
before adding a pk

run this many times ;) hopefully once

update  table_new
set my_key=cast(10000000 * RAND(another_number_column_in_your_table) as int)
where m_key in (
  select m_key from table_new group by m_key having count(1) > 1
)
0
 
LVL 6

Author Comment

by:tikusbalap
ID: 33427171
@HainKurt
How to update with RAND() function in my old table? I counter duplicate errors many times.
0
 
LVL 53

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 33432094
first run the update until you do not get any duplicates

if you get emty result from this (testing duplicates)

select m_key from table_new group by m_key having count(1) > 1

run this if you get any duplicates

update  table_new
set my_key=cast(10000000 * RAND(another_number_column_in_your_table*RAND(another_number_column_in_your_table)) as int)
where m_key in (
  select m_key from table_new group by m_key having count(1) > 1
)

add PK when tehre is no duplicates...
sample code

select cast(10 + 90*rand(num*1000*rand(num)) as int) new_id, * from dupid
35	12	634	a
87	13	235	b
27	13	334	g
54	15	356	t
27	11	334	u
16	12	764	b
41	14	856	d
99	12	467	y

select cast(10 + 90*rand(num*1000*rand(num)) as int) new_id, * from dupid
where id in (select id from dupId group by id having COUNT(1) >1)
35	12	634	a
87	13	235	b
27	13	334	g
16	12	764	b
99	12	467	y

update dupId
set id = cast(10 + 90*rand(num*1000*rand(num)) as int)
where id in (select id from dupId group by id having COUNT(1) >1)

(null)

select * from dupid
35	634	a
87	235	b
27	334	g
15	356	t
11	334	u
16	764	b
14	856	d
99	467	y

done!

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question