sql - Creating a foreign key constraint with non-matching data -


greetings!

i have inserted data sql insert files ms sql database. apperantly data not complete. discovered when trying make erd , create key constraints between tables. when try connect article_review order1 order1 has primary key , article review has foreign key.

i have query selects records non-matching key values: see image: http://imgur.com/vdbcug8

so want now: insert new rows article_review missing id values. values of other columns not matter, can null or random generated.

a simple join won't cut because of other columns non-identical.

ps. above needed because ms sql 2016 wont let me create key constraint between 2 tables 1 of them contains value not in other 1 , throws error:

'order1' table saved 'article_review' table saved 'review' table - unable create relationship 'fk_review_order1'.   alter table statement conflicted foreign key constraint "fk_review_order1". conflict occurred in database "superdatabase", table "dbo.order1", column 'id'. 

do want add foreign key documentation only?

you add fk this:

alter table dbo.order1 nocheck  add constraint fk_review_order1 foreign key (id) references dbo.article_review (id) 

note with nocheck

this way foreign key created, not trusted , disabled, can seen query:

select schema_name(fk.schema_id) sch, t.name,         fk.name, is_disabled, is_not_trusted, fk.is_not_for_replication    sys.foreign_keys fk (nolock)         inner join sys.tables t (nolock) on t.object_id = fk.parent_object_id   fk.is_not_trusted = 1         , fk.is_disabled = 0 order schema_name(fk.schema_id), t.name, fk.name; 

idea https://www.brentozar.com/blitz/foreign-key-trusted/


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -