Azure sql database -some FK check constraints automatically change to nocheck

Puppala, Sreedhar 0 Reputation points
2026-09-09T20:44:48.4533333+00:00

Azure sql database -some FK check constraints automatically change to nocheck , no alter was applied , checked audit log as well.anyone faced similiar issue

Azure SQL Database

1 answer

Sort by: Oldest
  1. Erland Sommarskog 137.4K Reputation points MVP Volunteer Moderator
    2026-09-11T19:14:05.06+00:00

    yes i check using SELECT schema_name(f.schema_id)||'.'||name,name FROM sys.foreign_keys AS f Where Is_Not_Trusted = 1;

    Aha. Sorry, I should have realised that directly. Yes, that flag can be set to 1 without DDL. Here is one example how this can happen. We have this setup:

    CREATE TABLE master (id int NOT NULL,
                         somedata varchar(23) NOT NULL,
                         CONSTRAINT pk_master PRIMARY KEY (id)
    )
    INSERT master(id, somedata)
       VALUES(1, 'somedata')
    go
    CREATE TABLE detail (id int NOT NULL,
                         detailno int NOT NULL,
                         moredata varchar(24) NOT NULL,
                         CONSTRAINT pk_details PRIMARY KEY (id, detailno),
                         CONSTRAINT fk_details_master FOREIGN KEY (id) REFERENCES master(id)
    )
    go
    SELECT is_not_trusted FROM sys.foreign_keys WHERE object_id = object_id('fk_details_master')
    

    The SELECT returns 0 at this point.

    Then create a local file, slask.bcp, with this content:

    1;1;moredata
    

    Then run from the command line:

    bcp detail in slask.bcp -c -t; -S yourserver.database.windows.net -U username -P pwd -d yourdb
    

    Now run that SELECT again

    SELECT is_not_trusted FROM sys.foreign_keys WHERE object_id = object_id('fk_details_master')
    

    The value is now 1.

    This is because BCP, by default, does not check constraints. This makes it go faster. It also permits you to load data in parallel without considering dependency order. But since data is loaded without checking constraints, the constraints can no longer be trusted, but you need to run ALTER tbl WITH CHECK CHECK to make the constraint trusted again.

    Drop the tables and re-recreate them as above. Run the BCP this way this time:

    bcp detail in slask.bcp -c -t; -S yourserver.database.windows.net -U username -P pwd -d yourdb -hCHECK_CONSTRAINTS
    

    This time the SELECT still returns 0.There are other ways you can do bulk inserts with the same effect. For instance OPENROWSET(BULK). You mentioned SSIS. I don't use SSIS and I know very little about it, but I would expect it to be able to use bulk-load methods that bypasses constraints by default, because, again, that loads data faster. I would expect the same be true for Azure Data Factory.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.