![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello! Another day, another problem... :-) I've got something like this: CREATE TABLE A ( pk_A INT CONSTRAINT primarykey_A PRIMARY KEY ); CREATE TABLE B ( pk_B INT CONSTRAINT primarykey_B PRIMARY KEY, fk_B_A INT CONSTRAINT foreginkey_B REFERENCES A ); and I want to force connection 1 to 1..n between tables A and B. Inserting into B and than into A is impossible because of reference constraint in table B. So I insert into A and then into B. To enforce the relation (1-1..n) I'll probably need a trigger. I wrote it like this: GO CREATE TRIGGER trig ON A AFTER INSERT AS BEGIN IF (SELECT COUNT(*) FROM A WHERE pk_A NOT IN ( SELECT fk_B_A FROM B)) > 0 BEGIN ROLLBACK PRINT 'STOP!' END END but now I can't put any data in the tables. Do You know how should I write the trigger? Maybe there is a better solution? Regards, Wojtek P.S. I thought about DISABLE/ENABLE TRIGGER but i couldn't find how to enforce check before triggering-event appears... |
#3
| |||
| |||
|
|
So let's get this straight. The constraint on fk_B_A will not let you enter data into that field unless it already exists in pk_A in A. Your trigger that you have created will ensure that no data will get into pk_a that does not already exist in fk_B_A, so it seems to be working fine. What exactly do you want to do? Do you want to ensure that after you insert a record into A, a matching record gets inserted into B? If so then your trigger should insert a record if one didn't exist. |
#4
| |||
| |||
|
|
Well.. This tables are only an example... They are much more complex in fact, so I can't simply put data in the B table. I need to do it in "normal" way. But after puting something into table A the trigger fires and removes the invalid record (rollback), so I can't put anything into the tables. So if the trigger is correct how can I put something into both tables without doing it in the body of the trigger. :-) Thanx for help So let's get this straight. The constraint on fk_B_A will not let you enter data into that field unless it already exists in pk_A in A. Your trigger that you have created will ensure that no data will get into pk_a that does not already exist in fk_B_A, so it seems to be working fine. What exactly do you want to do? Do you want to ensure that after you insert a record into A, a matching record gets inserted into B? If so then your trigger should insert a record if one didn't exist.- Hide quoted text - - Show quoted text - |
#5
| |||
| |||
|
|
Well.. This tables are only an example... They are much more complex in fact, so I can't simply put data in the B table. I need to do it in "normal" way. But after puting something into table A the trigger fires and removes the invalid record (rollback), so I can't put anything into the tables. So if the trigger is correct how can I put something into both tables without doing it in the body of the trigger. :-) Thanx for help So let's get this straight. The constraint on fk_B_A will not let you enter data into that field unless it already exists in pk_A in A. Your trigger that you have created will ensure that no data will get into pk_a that does not already exist in fk_B_A, so it seems to be working fine. What exactly do you want to do? Do you want to ensure that after you insert a record into A, a matching record gets inserted into B? If so then your trigger should insert a record if one didn't exist.- Hide quoted text - - Show quoted text - |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
Well.. to be honest I need to solve this problem as academic exercise... It is rather didactic than business problem. So I need to enforce the relationship and it must be done with trigger. It will be checked by trial of puting wrong data in the tables (rollback) and than correct data (normal commit). But with my declaration I can't put anything in the tables... The trigger is fired just after the insert while I need it to be fired, lets say, on commit. So this is the "real" prblem :-) Thanx and sorry for this mess :-) |
#8
| |||
| |||
|
|
Another day, another problem... :-) I've got something like this: CREATE TABLE A ( pk_A INT CONSTRAINT primarykey_A PRIMARY KEY ); CREATE TABLE B ( pk_B INT CONSTRAINT primarykey_B PRIMARY KEY, fk_B_A INT CONSTRAINT foreginkey_B REFERENCES A ); and I want to force connection 1 to 1..n between tables A and B. Inserting into B and than into A is impossible because of reference constraint in table B. So I insert into A and then into B. To enforce the relation (1-1..n) I'll probably need a trigger. I wrote it like this: |
![]() |
| Thread Tools | |
| Display Modes | |
| |