I have two tables with a one-to-one relationship. Table1 has a composite primary key consisting of about 4 columns. Table2’s foreign key is set to Table1’s primary key.
When I try the following UPDATE clause, I am getting an error:
UPDATE Table2
SET column1 = fakeTable.c1
FROM Table2 INNER JOIN
(
SELECT Table1.primaryKey
, (Table1.column3 + Table1.column4) AS c1
FROM Table1
) AS c1
ON Table2.foreignKey = fakeTable.primaryKey
Am I not allowed to reference keys as if they are columns?
No, you need to list all the fields individually. But you can avoid the sub-query that you have…
EDIT
Response to comment:
–
I thought the whole point of keys was to avoid having to concatenate columns!Keys aren’t a time saving device, they’re data integrity devices.
A primary key is a unique identifier. I can be a composite or not, but the important thing is that it is unique and not nullable.
A foreign key is also a data integrity device. It ensure that if data refers to something in another table, it actually must exist in that other table.