selectt.name as TableWithForeignKey,fk.constraint_column_id as FK_PartNo, c.name as ForeignKeyColumnfromsys.foreign_key_columns as fkinner joinsys.tables as t on fk.parent_object_id = t.object_idinner joinsys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_idwherefk.referenced_object_id = (select object_idfrom sys.tableswhere name = 'TableOthersForeignKeyInto')order byTableWithForeignKey, FK_PartNo
获取外键约束的名称
select distinct name from sys.objects where object_id in( select fk.constraint_object_id from sys.foreign_key_columns as fkwhere fk.referenced_object_id =(select object_id from sys.tables where name = 'TableOthersForeignKeyInto'))
SELECT t.name AS TableWithForeignKey, fk.constraint_column_id AS FK_PartNo,c.name AS ForeignKeyColumn, o.name AS FK_NameFROM sys.foreign_key_columns AS fkINNER JOIN sys.tables AS t ON fk.parent_object_id = t.object_idINNER JOIN sys.columns AS c ON fk.parent_object_id = c.object_idAND fk.parent_column_id = c.column_idINNER JOIN sys.objects AS o ON fk.constraint_object_id = o.object_idWHERE fk.referenced_object_id = (SELECT object_id FROM sys.tablesWHERE name = 'TableOthersForeignKeyInto')ORDER BY TableWithForeignKey, FK_PartNo;
SELECT OBJECT_NAME(fk.parent_object_id) as ReferencingTable,OBJECT_NAME(fk.constraint_object_id) as [FKContraint]FROM sys.foreign_key_columns as fkWHERE fk.referenced_object_id = OBJECT_ID('ReferencedTable', 'U')
select O.name as [Object_Name], C.text as [Object_Definition]from sys.syscomments Cinner join sys.all_objects O ON C.id = O.object_idwhere C.text like '%table_name%'
/* CAST IS DONE , SO THAT OUTPUT INTEXT FILE REMAINS WITH SCREEN LIMIT*/WITH ALL_KEYS_IN_TABLE (CONSTRAINT_NAME,CONSTRAINT_TYPE,PARENT_TABLE_NAME,PARENT_COL_NAME,PARENT_COL_NAME_DATA_TYPE,REFERENCE_TABLE_NAME,REFERENCE_COL_NAME)AS(SELECT CONSTRAINT_NAME= CAST (PKnUKEY.name AS VARCHAR(30)) ,CONSTRAINT_TYPE=CAST (PKnUKEY.type_desc AS VARCHAR(30)) ,PARENT_TABLE_NAME=CAST (PKnUTable.name AS VARCHAR(30)) ,PARENT_COL_NAME=CAST ( PKnUKEYCol.name AS VARCHAR(30)) ,PARENT_COL_NAME_DATA_TYPE= oParentColDtl.DATA_TYPE,REFERENCE_TABLE_NAME='' ,REFERENCE_COL_NAME=''
FROM sys.key_constraints as PKnUKEYINNER JOIN sys.tables as PKnUTableON PKnUTable.object_id = PKnUKEY.parent_object_idINNER JOIN sys.index_columns as PKnUColIdxON PKnUColIdx.object_id = PKnUTable.object_idAND PKnUColIdx.index_id = PKnUKEY.unique_index_idINNER JOIN sys.columns as PKnUKEYColON PKnUKEYCol.object_id = PKnUTable.object_idAND PKnUKEYCol.column_id = PKnUColIdx.column_idINNER JOIN INFORMATION_SCHEMA.COLUMNS oParentColDtlON oParentColDtl.TABLE_NAME=PKnUTable.nameAND oParentColDtl.COLUMN_NAME=PKnUKEYCol.nameUNION ALLSELECT CONSTRAINT_NAME= CAST (oConstraint.name AS VARCHAR(30)) ,CONSTRAINT_TYPE='FK',PARENT_TABLE_NAME=CAST (oParent.name AS VARCHAR(30)) ,PARENT_COL_NAME=CAST ( oParentCol.name AS VARCHAR(30)) ,PARENT_COL_NAME_DATA_TYPE= oParentColDtl.DATA_TYPE,REFERENCE_TABLE_NAME=CAST ( oReference.name AS VARCHAR(30)) ,REFERENCE_COL_NAME=CAST (oReferenceCol.name AS VARCHAR(30))FROM sys.foreign_key_columns FKCINNER JOIN sys.sysobjects oConstraintON FKC.constraint_object_id=oConstraint.idINNER JOIN sys.sysobjects oParentON FKC.parent_object_id=oParent.idINNER JOIN sys.all_columns oParentColON FKC.parent_object_id=oParentCol.object_id /* ID of the object to which this column belongs.*/AND FKC.parent_column_id=oParentCol.column_id/* ID of the column. Is unique within the object.Column IDs might not be sequential.*/INNER JOIN sys.sysobjects oReferenceON FKC.referenced_object_id=oReference.idINNER JOIN INFORMATION_SCHEMA.COLUMNS oParentColDtlON oParentColDtl.TABLE_NAME=oParent.nameAND oParentColDtl.COLUMN_NAME=oParentCol.nameINNER JOIN sys.all_columns oReferenceColON FKC.referenced_object_id=oReferenceCol.object_id /* ID of the object to which this column belongs.*/AND FKC.referenced_column_id=oReferenceCol.column_id/* ID of the column. Is unique within the object.Column IDs might not be sequential.*/
)
select * from ALL_KEYS_IN_TABLEwherePARENT_TABLE_NAME in ('YOUR_TABLE_NAME')or REFERENCE_TABLE_NAME in ('YOUR_TABLE_NAME')ORDER BY PARENT_TABLE_NAME,CONSTRAINT_NAME;
declare @tablename as varchar(MAX)SET @tablename = 'yourtablename'SELECT nameFROM YOURDATABASE.sys.objectsWHERE type_desc = 'FOREIGN_KEY_CONSTRAINT' and (name LIKE '%_' + @tablename + 'empdb_%' or name LIKE '%_' + @tablename )
这是一个更一般的形式:
SELECT nameFROM YOURDATABASE_PROD.sys.objectsWHERE type_desc = 'FOREIGN_KEY_CONSTRAINT' and name LIKE '%' + @tablename + '%' andname NOT LIKE '[a-zA-Z0-9]' + @tablename + '%' and name NOT LIKE '%' + @tablename + '[a-zA-Z0-9]'
-->>SPECIFY THE DESIRED DBUSE ???GO
/*********************************************************************************************
LIST OUT ALL PRIMARY AND FOREIGN KEY CONSTRAINTS IN A DB OR FOR A SPECIFIED TABLE
*********************************************************************************************/DECLARE @tblName VARCHAR(255)
/*******************/
SET @tblName = NULL-->NULL will return all PK/FK constraints for every table in the database
/*******************/
SELECT PKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()),PKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O1.schema_id)),PKTABLE_NAME = CONVERT(SYSNAME,O1.name),PKCOLUMN_NAME = CONVERT(SYSNAME,C1.name),FKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()),FKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O2.schema_id)),FKTABLE_NAME = CONVERT(SYSNAME,O2.name),FKCOLUMN_NAME = CONVERT(SYSNAME,C2.name),-- Force the column to be non-nullable (see SQL BU 325751)KEY_SEQ = isnull(convert(smallint,K.constraint_column_id),0),UPDATE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.object_id,'CnstIsUpdateCascade')WHEN 1 THEN 0ELSE 1END),DELETE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.object_id,'CnstIsDeleteCascade')WHEN 1 THEN 0ELSE 1END),FK_NAME = CONVERT(SYSNAME,OBJECT_NAME(F.object_id)),PK_NAME = CONVERT(SYSNAME,I.name),DEFERRABILITY = CONVERT(SMALLINT,7) -- SQL_NOT_DEFERRABLEFROM sys.all_objects O1,sys.all_objects O2,sys.all_columns C1,sys.all_columns C2,sys.foreign_keys FINNER JOIN sys.foreign_key_columns KON (K.constraint_object_id = F.object_id)INNER JOIN sys.indexes ION (F.referenced_object_id = I.object_idAND F.key_index_id = I.index_id)WHERE O1.object_id = F.referenced_object_idAND O2.object_id = F.parent_object_idAND C1.object_id = F.referenced_object_idAND C2.object_id = F.parent_object_idAND C1.column_id = K.referenced_column_idAND C2.column_id = K.parent_column_idAND ( O1.name = @tblNameOR O2.name = @tblNameOR @tblName IS null)ORDER BY PKTABLE_NAME,FKTABLE_NAME
SELECTccu.table_name AS SourceTable,ccu.constraint_name AS SourceConstraint,ccu.column_name AS SourceColumn,kcu.table_name AS TargetTable,kcu.column_name AS TargetColumnFROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccuINNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rcON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAMEINNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcuON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAMEORDER BY ccu.table_name
--Eliminar las llaves foraneasdeclare @query varchar(8000)declare cursorRecorrerTabla cursor for
SELECT 'ALTER TABLE [PoaComFinH].['+sch.name+'].['+referencingTable.Name+'] DROP CONSTRAINT ['+foreignKey.name+']' 'query'FROM PoaComFinH.sys.foreign_key_columns fkJOIN PoaComFinH.sys.tables referencingTable ON fk.parent_object_id = referencingTable.object_idJOIN PoaComFinH.sys.schemas sch ON referencingTable.schema_id = sch.schema_idJOIN PoaComFinH.sys.objects foreignKey ON foreignKey.object_id = fk.constraint_object_idJOIN PoaComFinH.sys.tables referencedTable ON fk.referenced_object_id = referencedTable.object_id
--3ro. abrir el cursor.open cursorRecorrerTablafetch next from cursorRecorrerTablainto @querywhile @@fetch_status = 0begin--inicio cuerpo del cursorprint @queryexec(@query)--fin cuerpo del cursorfetch next from cursorRecorrerTablainto @queryend--cerrar cursorclose cursorRecorrerTabladeallocate cursorRecorrerTabla
SELECTOBJECT_NAME(f.parent_object_id) TableName,COL_NAME(fc.parent_object_id,fc.parent_column_id) ColNameFROMsys.foreign_keys AS fINNER JOINsys.foreign_key_columns AS fcON f.OBJECT_ID = fc.constraint_object_idINNER JOINsys.tables tON t.OBJECT_ID = fc.referenced_object_idWHEREOBJECT_NAME (f.referenced_object_id) = 'TableName'
-- EXEC sp_fkeys 'TableName'
-- DROP CONSTRAINTS: I drop one, here drop every constraint you desire.BEGIN TRANSACTIONGOALTER TABLE dbo.TableNameDROP CONSTRAINT IF EXISTS FK_TableName_OtherTableGOALTER TABLE dbo.TableName SET (LOCK_ESCALATION = TABLE)GOCOMMIT
-- TRUNCATEBEGIN TRANSACTIONTRUNCATE TABLE TableNameGOCOMMIT
-- RECREATE CONSTRAINTS: I recreate 1, here recreate every fk you desireBEGIN TRANSACTIONGOALTER TABLE dbo.TableName SET (LOCK_ESCALATION = TABLE)GO
ALTER TABLE dbo.TableName ADD CONSTRAINTFK_TableName_OtherTable FOREIGN KEY(Id_FK) REFERENCES dbo.OtherTable(Id) ON UPDATE NO ACTIONON DELETE NO ACTIONGOCOMMIT