I am using java on sql server and I have a DB problem of duplicate customers
(many customers are duplicated in the following way).
My tables are :
CUSTOMER(person_id,customer_id)
ORDER (order_id,person_id )
and a duplicated customer looks like this :
CUSTOMER(333,040535743)
CUSTOMER(334,040535743)
CUSTOMER(335,040535743)
ORDER (1,333 )
ORDER (2,333 )
ORDER (4,334 )
ORDER (5,334 )
ORDER (6,334 )
ORDER (7,335 )
ORDER (8,335 )
ORDER (9,335 )
CUSTOMER(336,009530650)
CUSTOMER(337,009530650)
ORDER (10,336 )
ORDER (11,336 )
ORDER (12,336 )
ORDER (13,337 )
ORDER (14,337 )
ORDER (15,337 )
333,334,335 are three rows for the same customer because they have the same customer_id, I want to keep only the last customer ( 335), and make all the orders of 334 and 333 refer to 333.
my final data should be :
CUSTOMER(335,040535743)
ORDER (1,335)
ORDER (2,335)
ORDER (4,335)
ORDER (5,335)
ORDER (6,335)
ORDER (7,335 )
ORDER (8,335 )
ORDER (9,335 )
CUSTOMER(337,009530650)
ORDER (10,337)
ORDER (11,337)
ORDER (12,337)
ORDER (13,337 )
ORDER (14,337 )
ORDER (15,337 )
how do I do a query that does that for all my duplicated customers? (I have a query returning the list of the customer_id’s that have duplications)
Update the
Ordertable:Then, update the
Customertable:After that, it would be good to have
Customer(person_id)defined asPRIMARY KEYor with aUNIQUEconstraint.And a
FOREIGN KEYconstraint fromOrder(person_id)toCustomer(person_id)