我有一个单调乏味的情况,我正在寻找一种“不那么乏味”的方式。
目前,我被要求从数据库中删除一些数据 - 没什么大不了的,这很容易吗?错。
这个数据库我没有参与过,所以我不熟悉主/外键关系,这是一个单调乏味的部分,因为大约有15-20个表非常错综复杂地连接在一起。
在不知道PK / FK关系的情况下删除索引数据的最快方法是什么,并且必须逐个DELETE FROM ... WHERE [something] = [somethingElse]
并根据另一个表的键找出它?
答案 0 :(得分:0)
您可以查询sys.foreign_keys系统视图以获取有关所有FK关系的信息。从那里创建DROP和CREATE语句并不过分。
我有一个函数可以为partiuclar表的所有FK返回各种语句(例如DROP,CREATE,CHECK)。在Management Studio中运行此函数并将结果返回到Text可以为您提供格式良好的语句,以便轻松删除所有FK,删除数据,然后再将FK添加回来。
希望您可以根据自己的需要量身定制。
希望它有所帮助,
灰
CREATE FUNCTION [utils].[uf_ForeignKeyScripts]
(
@PrimaryKeyTable varchar(128)
, @PrimaryKeyTableSchema varchar(32)
)
RETURNS @Scripts TABLE
(
ForeignKeyName varchar(64)
, IfExistsStatement varchar(1000)
, DropStatement varchar(1000)
, IfNotExistsStatement varchar(1000)
, CreateStatement varchar(1000)
, CheckStatement varchar(1000)
, NoCheckStatement varchar(1000)
)
AS
/*
This function returns statements used to create, drop, and check all Foreign Key constraints that reference a given table.
These statements can be then added to T-SQL scripts.
Example usage (ensure selection of the Results to Text option in SSMS) :
1) To create statements to check all foreign keys
SELECT
IfExistsStatement + CHAR(13) +
CHAR(9) + CheckStatement + CHAR(13)
FROM
utils.uf_ForeignKeyScripts('t_Dim_Date','dbo')
;
This will return a formatted statement to check the existence of a foreign key and if it exists, check that data does not violate the key.
*/
BEGIN
INSERT INTO
@Scripts
(
ForeignKeyName
, IfExistsStatement
, DropStatement
, IfNotExistsStatement
, CreateStatement
, CheckStatement
, NoCheckStatement
)
SELECT
FK.name AS ForeignKeyName
, 'IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N''' + SFK.name + '.' + FK.name + ''') ' +
'AND parent_object_id = OBJECT_ID(N''' + SFK.name + '.' + OBJECT_NAME(FK.parent_object_id) + '''))'
AS IfExistsStatement
, 'ALTER TABLE ' + SFK.name + '.' + OBJECT_NAME(FK.parent_object_id) + ' ' +
'DROP CONSTRAINT ' + FK.name + CHAR(13) + ';'
AS DropStatement
, 'IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N''' + SFK.name + '.' + FK.name + ''') ' +
'AND parent_object_id = OBJECT_ID(N''' + SFK.name + '.' + OBJECT_NAME(FK.parent_object_id) + '''))'
AS IfNotExistsStatement
, 'ALTER TABLE ' + SFK.name + '.' + OBJECT_NAME(FK.parent_object_id) + ' ' +
'WITH CHECK ADD CONSTRAINT ' + FK.name + ' ' +
'FOREIGN KEY (' + C.FKColumns + ') ' +
'REFERENCES ' + ST.name + '.' + OBJECT_NAME(fk.referenced_object_id) + ' ' +
'(' + C.FKColumns + ')' + CHAR(13) + ';'
AS CreateStatement
, 'ALTER TABLE ' + SFK.name + '.' + OBJECT_NAME(FK.parent_object_id) + ' ' +
'CHECK CONSTRAINT ' + FK.name + CHAR(13) + ';'
AS CheckStatement
, 'ALTER TABLE ' + SFK.name + '.' + OBJECT_NAME(FK.parent_object_id) + ' ' +
'NOCHECK CONSTRAINT ' + FK.name + CHAR(13) + ';'
AS NoCheckStatement
FROM
sys.foreign_keys AS FK
INNER JOIN
sys.schemas AS SFK -- schema of foreign key table
ON
FK.schema_id = SFK.schema_id
INNER JOIN
sys.tables AS T -- primary key table
ON
FK.referenced_object_id = T.object_id
INNER JOIN
sys.schemas AS ST -- schema of primary key table
ON
T.schema_id = ST.schema_id
CROSS APPLY
(
/* Get all columns to handle composite keys */
SELECT
SFKC.constraint_object_id
, utils.uf_ConcatanateStringWithDelimiter(COL_NAME(SFKC.referenced_object_id, SFKC.referenced_column_id),', ') AS FKColumns
FROM
sys.foreign_key_columns AS SFKC
WHERE
SFKC.constraint_object_id = FK.object_id
GROUP BY
SFKC.constraint_object_id
)
AS C
WHERE
OBJECT_NAME(T.object_id) = @PrimaryKeyTable
AND ST.name = @PrimaryKeyTableSchema
;
RETURN
END