Dealing with FK constraints - Emptying and refilling sql server 2008 db -


i have sql db in ms sql server 2008 r2

for development purposes, trying clear data - add in dummy data.

after struggles fk constraints, , using

alter table ? nocheck constraint delete ? alter table ? check constraint 

i managed clear data.

now want add dummy data.

lets have 3 tables, country, address , country_address (linking address country).

i have added data country , address

but when try add country_address:


no row updated.

the data in row 1 not committed. error source: .net sqlclient data provider. error message: insert statement conflicted foreign key constraint


i not quite sure why happening, because doing linking newly added country , addresses - both exist - why conflicting fk constraint?

from googling has hinted reseeding tables may required fix this. firstly i'm not 100% sure reseeding means, assuming talking resetting autogenerated id column.

i did notice when adding new records country or address use int incremented last record (which gone), e.g. start id of 400

how can add data in?

it sounds have id column in country , address tables, , values in country_address id values reference other 2 tables. and, appears have data country_address has original values referenced data.

so, reseeding may solution. assume tables this.

country ------- countryid int identity(1, 1) country varchar(100)  address ------- addressid int identity(1, 1) address varchar(100) city varchar(100) 

it doesn't matter columns exist reseeding, except countryid , addressid columns in each table. identity(1, 1) property indicates autonumbering starting @ 1 , incrementing 1. so, reseed country , address tables, can following before adding data country , address tables:

dbcc checkident ('country', reseed, 1) dbcc checkident ('address', reseed, 1) 

it isn't necessary run command country_address, unless has column identity property.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -