-1

I need to use one primary key to many table (foreign key) for e.g

Table 1:

 id --P K

1 
2
3

Table 2:

 id -- F K 

Table 3:

 id -- F K 

If I insert value to table 2 by using a stored procedure. it showing error

constraint "FK_tbl_table2_tbl_table1_reg". The conflict occurred in database "BPMC", table "dbo.tbl_table1", column 'ID'.

where's error...thanks..

sorry i made error in sp datatype size....

4
  • what RDBMS you are working on ??? Does value 2 exist in tbl_Table1 and can you show definition of your stored procedure ??
    – M.Ali
    Jan 7, 2014 at 11:43
  • 2
    Well, show us the code that you've written in your stored procedure! What values are you passing into the stored procedure?
    – marc_s
    Jan 7, 2014 at 11:43
  • I think the error should tell you enough... something needs to exist in table1 before you can insert into table2 because you cannot reference something from table1 that does not exist Jan 7, 2014 at 11:44
  • sorry i made error in sp variable size thanks....
    – Happy
    Jan 7, 2014 at 11:57

2 Answers 2

2

Check this:

table2 ID Column should be same datatype of table1

I could not add as a comment so add it in answer section.

2
  • i made error in sp variable size thanks....
    – Happy
    Jan 7, 2014 at 11:58
  • @RadhamaniMuthusamy +1 Right on the spot ! Jan 7, 2014 at 12:02
1

To avoid this error from happening, make sure that the value you are inserting into a column that references another table exists in that table. If the value does not exist in the primary table, insert to that table first before doing the insert on the second table.

Referential Integrity

3
  • i made error in sp variable size thanks....
    – Happy
    Jan 7, 2014 at 11:57
  • 1
    @Happy We will be thankful if you can share your error and solution , Please so it may benefit others. Jan 7, 2014 at 11:59
  • @Happy You mean ForeignKey variable size ? Jan 7, 2014 at 12:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.