Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / August 2008

Tip: Looking for answers? Try searching our database.

moving a foreign key

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arne Garvander - 29 Aug 2008 16:27 GMT
What is the proper syntax for moving a foreign key from one table to another.
My foreign key used to be a desciption. I would like it to be an integer
entity.
update Cat2 set Cat2.Cat1ID = (
select  cat1.cat1id from Cat1 inner join Cat2 on Cat1.Description =
Cat2.c2c1FKey)
Signature

Arne Garvander
(I program VB.Net for fun and C# to get paid.)

Tibor Karaszi - 29 Aug 2008 18:46 GMT
Is it the referencing or referenced table you want to change? Anyhow, removing a foreign key you use
ALTER TABLE ... DROP CONTRAINT to do. And to add a foreign key you do ALTER TABLE ... ADD
CONSTRAINT...

Your choice of words confuses me however, so you might want to describe it a bit more...

Signature

Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi

> What is the proper syntax for moving a foreign key from one table to another.
> My foreign key used to be a desciption. I would like it to be an integer
> entity.
> update Cat2 set Cat2.Cat1ID = (
> select  cat1.cat1id from Cat1 inner join Cat2 on Cat1.Description =
> Cat2.c2c1FKey)
Arne Garvander - 29 Aug 2008 19:01 GMT
I know how to create a foreign key. Now I want to populate my new foreign key.
I can make a join on the old foreign key. I wnat to use the old join to
poulate my new foreign key.
Signature

Arne Garvander
(I program VB.Net for fun and C# to get paid.)

> Is it the referencing or referenced table you want to change? Anyhow, removing a foreign key you use
> ALTER TABLE ... DROP CONTRAINT to do. And to add a foreign key you do ALTER TABLE ... ADD
[quoted text clipped - 8 lines]
> > select  cat1.cat1id from Cat1 inner join Cat2 on Cat1.Description =
> > Cat2.c2c1FKey)
Tibor Karaszi - 29 Aug 2008 19:51 GMT
Use an update based on a join. Or of you are on 2008, use MERGE. Examples:

USE tempdb
DROP TABLE p
DROP TABLE c
GO

CREATE TABLE p(oldkey int primary key, newkey char(1) unique)
insert into p (oldkey, newkey) values(1, 'a'), (2, 'b')
GO

CREATE TABLE c(oldkey int references p(oldkey), newkey char(1) NULL)
insert into c(oldkey) VALUES(1), (2)
GO

SELECT * FROM p
SELECT * FROM c
GO

UPDATE c
SET c.newkey = p.newkey
FROM p INNER JOIN c ON c.oldkey = p.oldkey

SELECT * FROM p
SELECT * FROM c

--2008 alternative:
MERGE c
USING p ON c.oldkey = p.oldkey
WHEN MATCHED THEN UPDATE SET c.newkey = p.newkey;

Signature

Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi

>I know how to create a foreign key. Now I want to populate my new foreign key.
> I can make a join on the old foreign key. I wnat to use the old join to
[quoted text clipped - 13 lines]
>> > select  cat1.cat1id from Cat1 inner join Cat2 on Cat1.Description =
>> > Cat2.c2c1FKey)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.