PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > moving a foreign key
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
moving a foreign key

Réponse
 
LinkBack Outils de la discussion
Vieux 29/08/2008, 17h27   #1
Arne Garvander
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut moving a foreign key

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
(I program VB.Net for fun and C# to get paid.)
  Réponse avec citation
Vieux 29/08/2008, 19h46   #2
Tibor Karaszi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: moving a foreign key

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...

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


"Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
news:BE298AA9-1307-40B7-9AFE-D44FD8966AF8@microsoft.com...
> 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
> (I program VB.Net for fun and C# to get paid.)


  Réponse avec citation
Vieux 29/08/2008, 20h01   #3
Arne Garvander
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: moving a foreign key

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.
--
Arne Garvander
(I program VB.Net for fun and C# to get paid.)


"Tibor Karaszi" wrote:

> 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...
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
>
> "Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
> news:BE298AA9-1307-40B7-9AFE-D44FD8966AF8@microsoft.com...
> > 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
> > (I program VB.Net for fun and C# to get paid.)

>
>

  Réponse avec citation
Vieux 29/08/2008, 20h51   #4
Tibor Karaszi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: moving a foreign key

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;


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


"Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
news:A0803648-D64E-45AD-A064-6B5EEA527041@microsoft.com...
>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.
> --
> Arne Garvander
> (I program VB.Net for fun and C# to get paid.)
>
>
> "Tibor Karaszi" wrote:
>
>> 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...
>>
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://sqlblog.com/blogs/tibor_karaszi
>>
>>
>> "Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
>> news:BE298AA9-1307-40B7-9AFE-D44FD8966AF8@microsoft.com...
>> > 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
>> > (I program VB.Net for fun and C# to get paid.)

>>
>>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11399 seconds with 12 queries