Board index » delphi » Modify Key Field ( when it's foreign key in another table)

Modify Key Field ( when it's foreign key in another table)

I need to change the value of a key field in a parent table, which is a
foreign key in a child table, how to do it? Can I do it with changing the
values of the foerign key in the child table simutanously?

Thanks a lot.

 

Re:Modify Key Field ( when it's foreign key in another table)


You need to do the following:

1.  Create a new parent record with all the same data but a new key.
2.  Rename the key fields in the detail table to match the new parent record.

3.  Delete the old parent record.

Alternatively, you could delete the foreign key relationships, change your
keys on the existing records, and then put the foreign key back on.  The
other way is probably safer, however.

HTH,

Mike Rodriguez

Quote
Jingyi Peng wrote:
> I need to change the value of a key field in a parent table, which is a
> foreign key in a child table, how to do it? Can I do it with changing the
> values of the foerign key in the child table simutanously?

> Thanks a lot.

Re:Modify Key Field ( when it's foreign key in another table)


Foreign keys have this option. Let the database do this job.

This is sql anywhere syntax, but for your server (whichever) should be very
similar:
alter table Detail1 add foreign key fk_master(id_master)
references master(ID)
on update cascade on delete cascade //<-action !!

or when creating table:

create table detail1(
id integer not null references master(ID) on update cascade on delete
cascade,
....)

But you must assure that ALL foreign key that refer to a particular master
have the same "action" (=on update|delete cascade|restrict|set null|set
default), otherwise it won't work.

--
----------------------
Regards
Robert Cerny
Remove both qwe when replying
email: robert.qwe.ce...@neosys.xrs.qwe.si

No questions via email, unless explicitly invited.

Quote
Jingyi Peng wrote in message <393bf885@dnews>...
>I need to change the value of a key field in a parent table, which is a
>foreign key in a child table, how to do it? Can I do it with changing the
>values of the foerign key in the child table simutanously?

>Thanks a lot.

Re:Modify Key Field ( when it's foreign key in another table)


Quote
Jingyi Peng wrote in message <393bf885@dnews>...
>I need to change the value of a key field in a parent table, which is a
>foreign key in a child table, how to do it? Can I do it with changing the
>values of the foerign key in the child table simutanously?

>Thanks a lot.

Both responses you've received so far will work.  Robert's suggestion is
superior (with syntax modifications) IF your database schema is set up
appropriately OR if you have the rights to modify database schema and can
make it so.

I hope this is helpful.

Naren

Other Threads