During the database migration, I encountered a limitation of the form database table:
ALTER TABLE [dbo].[myTable] ADD CONSTRAINT [someName] FOREIGN KEY ([id]) REFERENCES [dbo].[myTable] ([id]) ON DELETE NO ACTION ON UPDATE NO ACTION
Why do this? This was originally done in the Sybase database, and we are converting to SQL Server 2008 R2.
UPDATE : Yes, a foreign key constraint is a field that references the same table AND SUCH A FIELD.
I ran this query in the original Sybase database and found 42 of these crazy keys, so it doesn't look like a typo.
SELECT sr.constrid as [Constraint ID], so.name as [Table], sc.name as [Column] FROM sysreferences sr INNER JOIN sysobjects so ON (so.id = sr.tableid) INNER JOIN syscolumns sc ON (sc.id = sr.tableid AND sc.colid = sr.fokey1) WHERE sr.tableid = sr.reftabid AND sr.fokey1 = sr.refkey1 AND sr.fokey2 = 0 AND sr.refkey2 = 0
Matt hamsmith
source share