获取所有表与表之间的外键约束SELECT b.name,a.name,c.name FROM sys.foreign_keys a INNER JOIN sys.tables b ON a.referenced_object_id=b.object_id INNER JOIN sys.tables c ON a.parent_object_id=c.object_id--所有约束创建语句SELECT CONCAT('ALTER TABLE [',d.name,'] ADD CONSTRAINT [',b.name,'] FOREIGN KEY(',e.name,') REFERENCES [',c.name,'](',f.name,')')FROM sys.sysforeignkeys aINNER JOIN sys.foreign_keys b ON a.constid=b.object_idINNER JOIN sys.tables c ON b.referenced_object_id=c.object_idINNER JOIN sys.tables d ON b.parent_object_id=d.object_idINNER JOIN sys.syscolumns e ON a.fkey=e.colid AND e.id=a.fkeyidINNER JOIN sys.syscolumns f ON a.rkey=f.colid AND f.id=a.rkeyidWHERE a.constid=2078630448--所有约束删除语句SELECT CONCAT('ALTER TABLE [',d.name,'] DROP CONSTRAINT [',b.name,']')FROM sys.sysforeignkeys aINNER JOIN sys.foreign_keys b ON a.constid=b.object_idINNER JOIN sys.tables c ON b.referenced_object_id=c.object_idINNER JOIN sys.tables d ON b.parent_object_id=d.object_id