• The logic behind the question suggest that the `backuplist` is subset of `databases`, and that FK should be created on `backuplist` to reference `databases`. Check query seems to support that, because it checks whatever all rows from `backuplist` have a match in `databases`

    select * from backuplist d left join databases b on d.name = b.name where b.name is null

    Following query tries to create FK in opposite direction, on object `databases` which previous check query has nothing to do with.

    ALTER TABLE [dbo].[databases] WITH CHECK ADD CONSTRAINT [FK_backlist_backuplist] FOREIGN KEY([name]) REFERENCES [dbo].[backuplist] ([name])

    So my vote was for wrong alter command because it should be

    ALTER TABLE [dbo].[backuplist] WITH CHECK ADD CONSTRAINT [FK_backlist_backuplist] FOREIGN KEY([name]) REFERENCES [dbo].[databases] ([name])

    which could in fact fail because there is no PK on `databases (name)`..

    If original alter was correct and FK should be on `databases`, then the check query is not correct. There could be a rows in `backuplist` not existing in `databases` which would cause a fail and that was not checked.

    Also other quite possible reason for either fail is that column data types does not match between the two tables. Varchar(10) and Varchar(20) can join but FK creating would fail.

    _______________________________________________
    www.sql-kefalo.net (SQL Server saveti, ideje, fazoni i fore)