views:

29

answers:

2

Let's say I hava a table

object:

id, primary key auto-increment
obj_id, foreign key (object.id)
val, integer

Is it possible to insert a record with obj_id = id. Not having an error with foreign key constraint?

Insert into object (val) values (123)

A: 

Yes, that's totally ok.

halfdan
No, it doesn't work obj_id is not null. I want to have id set automatically (auto-incement) and obj_id = id in one insert.
liysd
A: 

If obj_id allows null then it is ok.

EDIT:

What are you trying to achieve? You said it is a foreign key then it should be the value from that table. If you want another column having same data as your PK value then this column should not be the foreign key and you need to write trigger to update that column or some other approach.

Muhammad Kashif Nadeem
If I know id I may write "Insert into tab (10,10,123)" and it is ok. I now I want id to be auto-incerement value and obj_id to point on it.
liysd