Is it possible to update a primary key column? - sql-server-2008

I was asked to update a SQL Server table's primary key column, the column already has values. On inner joining with few other tables, I had to update the PK column values from another table. It's failing to do so due to duplicates, inserting value 435 to 2 or more PK column.
Any suggestion how it can be done?
UPDATE t
SET t.ID = c.NewID
FROM Table1 t
INNER JOIN Table2 p ON p.ID = t.ID
LEFT OUTER JOIN Table3 c ON c.ID = t.ID
WHERE c.status = 'Y'

Let's say your PK is called A, so:
create a new column called B
update table set B = A
do the changes you need to B
go through all the tables you have FK to your table; drop them, and make sure to reflect the data changes;
drop the PK on A
recreate it on B
recreate all the FKs you dropped 3 steps ago on B
Simple :)
I'm joking, as you can see it is very complicated and error prone. You shouldn't have to worry about your PKs values - if you are there is something wrong in your design

Related

MYSQL Maria DB - Compare 2 tables A and B and update records

I have 2 tables A and B. I wanted to compare table A with table B and for any mismatched record I wanted to update the entire record from table A to Table B. We have 2 primary keys and have around 40 million records.
Can we achieve this in one SQL or Script? Or can I create temporary table(Table c) where I can write table A records which are mismatching and then update Table B with temporary Table C. This I would be doing in MY SQL workbench and using mariaDB DB.
Added additional information - I have 2 primary keys and 15 columns. So, Ideally have to match 13 columns and find mismatches assuming the primary key matches in both tables.
Please assist and appreciate for any feedback.
One approach is to do an update left join of the B table to the A table, and update all columns in the former table with values from the latter. Note that the WHERE clause checks to make sure that a record in B did not match to any record in A.
UPDATE TABLE_B b
LEFT JOIN TABLE_A a
ON a.col1 = b.col1 AND
a.col2 = b.col2 -- AND all other columns
SET b.col1 = a.col1,
b.col2 = a.col2 -- AND set all other columns
WHERE a.col1 IS NULL

Mysql function to update foreign key

I have a table A with duplicate data and I would like to normalize it. But there is another table B which refers to table A ids. There is no real foreign key. It is only the fact. I would like to create temp. table C and fill it by use ON DUPLICATE KEY UPDATE to remove duplicities from table A. But so I lose some ids which make references to from table B to table A.
I would like to write a function which will be called like
ON DUPLICATE KEY UPDATE id = setNewId(`id`, VALUES(`id`))
Is it possible to make function setNewId() which will update table C.id and B.id to new value on dulicate key update?
This will create C with all the duplicate values of unique_field removed.
CREATE TABLE C LIKE A;
ALTER TABLE C ADD UNIQUE INDEX (unique_field);
INSERT IGNORE INTO C
SELECT * FROM A;
After you create C, you can use a join between A and C to find the corresponding ID, and use that to update B.
UPDATE B
JOIN A ON B.foreign_key = A.id
JOIN C ON C.unique_field = A.unique_field AND C.id != A.id
SET B.foreign_key = C.id;
After that's done, drop A and rename C to A.

MySQL: Insert or update if row is newer

I have table A and B, identical, with a timestamp column. I need to update A with B, adding all rows from B that don't exist in A, and updating any rows that already exist with the same pk. That's trivial to do with the INSERT ... ON DUPLICATE KEY UPDATE clause. However, A might have rows that are actually newer than the same row in B, therefore in those cases where A.last_modified > B.last_modified I don't want to do anything.
Is there any simple solution to that?
You can use left join to get the rows you want
insert into B
select a.*
from A a
left join B b on a.id = b.id
where b.last_modified < a.last_modified or b.last_modified is null
on duplicate key update ...;

Join table on earlier joined table

Say I have got three tables: A, B and C.
A has primary key a
B has primary key a and also a non-primary key c.
C has primary key c.
I want to begin selecting from table A.
So I got a query like this:
Select * from A join B on A.a=B.a join C on B.c=C.c
It returns
Unknown column 'B.c' in 'on clause''
Is this impossible in mysql, joining a table on a joined table? Or am I just doing something wrong? BTW table and column names a made up.
It is possible, and your syntax should be correct.
please refer to this link for syntax:
http://dev.mysql.com/doc/refman/5.0/en/join.html
example to interesting things you can do in MySQL (from the documentation):
SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
Also note the error claims that column C does not exist - so you should check the structure of B..

Problem in importing records from one table to another table

i m trying to import records from one table to another table.
i m having tables suppose A,B,C,D.
i m importing records from table A to table B.
Table B has two foreign keys which are primary keys in table C and D.
I m using query as below::
INSERT INTO B(userid,behaviorid,userNid,behaviorNid,timestamp)SELECT userid,behaviorid,userNid,behaviorNid,timestamp FROM A where userNid = ANY (select Nid from C);
but i m getting error as foreign key constraints fails.
How can i solve this.
Thanks in advance.
Well this should check Nid in C for every foreign key in C and append to B.
INSERT INTO B(userid,behaviorid,userNid,behaviorNid,timestamp) SELECT
userid,behaviorid,userNid,behaviorNid,timestamp FROM A LEFT JOIN C on A.foreignkey = C.Nid;
Make sure you try it and comment, if not working