Hi,
I need to disable all the foreign key and primary key constraints in a table
and reenable them at the end of my commands. I can use ALTER TABLE to do
this but I was wondering if it is a good practise. It is not maintenance
code but production code. Everything is done in a transaction but can I be
sure there is no incidence with concurent accesses ? What about the rights
for the db user ?
Thanks.
FredFrdric Mayot wrote:
> Hi,
> I need to disable all the foreign key and primary key constraints in
> a table and reenable them at the end of my commands. I can use ALTER
> TABLE to do this but I was wondering if it is a good practise. It is
> not maintenance code but production code. Everything is done in a
> transaction but can I be sure there is no incidence with concurent
> accesses ? What about the rights for the db user ?
> Thanks.
> Fred
Why would you need to disable constraints in production code?
David Gugick
Quest Software
www.imceda.com
www.quest.com|||The reason is quite simple.
Assume we have the tables
A(pkA <PK> )
B(pkfkA <PK,FK>, pkfkC <PK,FK> )
C(pkC <PK> )
with the data
A = {1}
C = {11, 12}
B = {(1, 11), (1, 12)}
Now, I want to update the two rows in B in the following manner :
(1, 11) -> (1, 12) and (1, 12) -> (1, 11).
This operation is supposed to be atomic (that's to say in a transaction)
"David Gugick" <david.gugick-nospam@.quest.com> a crit dans le message de
news: %23$J$Y0pbFHA.3040@.TK2MSFTNGP14.phx.gbl...
> Frdric Mayot wrote:
> Why would you need to disable constraints in production code?
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com|||Have you tried this?
UPDATE B
SET pkfkC = CASE pkfkC
WHEN 11 THEN 12
WHEN 12 THEN 11
END
WHERE pkfkA = 1 AND pkfkC IN (11, 12)
This update is atomic and as such doesn't cause a constraint violation error
.
"Frédéric Mayot" wrote:
> The reason is quite simple.
> Assume we have the tables
> A(pkA <PK> )
> B(pkfkA <PK,FK>, pkfkC <PK,FK> )
> C(pkC <PK> )
> with the data
> A = {1}
> C = {11, 12}
> B = {(1, 11), (1, 12)}
> Now, I want to update the two rows in B in the following manner :
> (1, 11) -> (1, 12) and (1, 12) -> (1, 11).
> This operation is supposed to be atomic (that's to say in a transaction)
>
> "David Gugick" <david.gugick-nospam@.quest.com> a écrit dans le message de
> news: %23$J$Y0pbFHA.3040@.TK2MSFTNGP14.phx.gbl...
>
>
没有评论:
发表评论