1

我需要在 GUI 应用程序中执行 DDL 语句。对于删除列语句,我需要删除之前的所有约束和依赖项。到目前为止,我知道:

  1. 主键约束
  2. 外键约束
  3. 独特的约束
  4. 检查约束
  5. 默认约束
  6. 索引依赖

DROP TABLE COLUMN在执行该语句之前,我是否必须删除任何其他约束或依赖项?

4

1 回答 1

0

这些现在是最终的工作 SQL 语句:

protected final String ASSEMBLE_DROP_DEFAULT_CONSTRAINT_STATEMENT_FOR_TABLE_COLUMN = "SELECT "
        + "'ALTER TABLE '+t.name+' DROP CONSTRAINT '+df.name 'DropDefaultConstraintStatement' "
        + ", c.name 'ColName' "
        + "FROM sys.default_constraints df "
        + "INNER JOIN sys.tables t ON df.parent_object_id = t.object_id "
        + "INNER JOIN sys.columns c ON df.parent_object_id = c.object_id AND df.parent_column_id = c.column_id "
        + "WHERE t.name = ? AND c.name = ?";

protected final String ASSEMBLE_DROP_CONSTRAINT_STATEMENTS_FOR_TABLE_COLUMN = "WITH CTE AS "
        + "( "
        + "SELECT OBJECT_NAME(parent_object_id) AS TABLE_NAME, name AS CONSTRAINT_NAME, Type "
        + "FROM sys.key_constraints "
        + "WHERE parent_object_id = object_id( ? ) "
        + "UNION "
        + "SELECT OBJECT_NAME(parent_object_id) AS TABLE_NAME, name AS CONSTRAINT_NAME, Type "
        + "FROM sys.foreign_keys "
        + "WHERE parent_object_id = object_id( ? ) "
        + "UNION "
        + "SELECT OBJECT_NAME(parent_object_id) AS TABLE_NAME, name AS CONSTRAINT_NAME, Type "
        + "FROM sys.check_constraints "
        + "WHERE parent_object_id = object_id( ? ) "
        + ") "
        + "SELECT DISTINCT "
        + "'ALTER TABLE '+cte.TABLE_NAME+' DROP CONSTRAINT '+cte.CONSTRAINT_NAME AS 'DropConstraintStatement', "
        + "CASE WHEN Type = 'PK' THEN 1 ELSE 0 END 'Is PK', ccu.COLUMN_NAME "
        + "FROM CTE cte "
        + "INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu "
        + "ON cte.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME "
        + "WHERE ccu.COLUMN_NAME = ? "
        + "ORDER BY CASE WHEN Type = 'PK' THEN 1 ELSE 0 END DESC;";

protected final String ASSEMBLE_DROP_ALL_FK_CONSTRAINTS_FOR_REFERENCED_TABLE_COLUMN = "SELECT "
        + "'ALTER TABLE ' + OBJECT_NAME(fk.parent_object_id) + ' DROP CONSTRAINT ' + fk.name 'DropFKStatement', "
        + "fk.name, "
        + "OBJECT_NAME(fk.parent_object_id) 'Parent table', "
        + "c1.name 'Parent column', "
        + "OBJECT_NAME(fk.referenced_object_id) 'Referenced table', "
        + "c2.name 'Referenced column' "
        + "FROM sys.foreign_keys fk "
        + "INNER JOIN sys.foreign_key_columns fkc "
        + "ON fkc.constraint_object_id = fk.object_id "
        + "INNER JOIN sys.columns c1 "
        + "ON fkc.parent_column_id = c1.column_id AND fkc.parent_object_id = c1.object_id "
        + "INNER JOIN sys.columns c2 ON "
        + "fkc.referenced_column_id = c2.column_id AND fkc.referenced_object_id = c2.object_id "
        + "WHERE OBJECT_NAME(fk.referenced_object_id) = ? AND c2.name = ?";

protected final String ASSEMBLE_DROP_INDEX_CONSTRAINT_STATEMENTS_FOR_TABLE_COLUMN = "SELECT "
        + "'DROP INDEX '+ind.name+' ON ' + t.name "
        + "FROM sys.indexes ind "
        + "INNER JOIN sys.index_columns ic "
        + "ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id "
        + "INNER JOIN sys.columns col "
        + "ON ic.object_id = col.object_id and ic.column_id = col.column_id "
        + "INNER JOIN sys.tables t "
        + "ON ind.object_id = t.object_id "
        + "WHERE "
        + "ind.is_primary_key = 0 "
        + "AND ind.is_unique = 0 "
        + "AND ind.is_unique_constraint = 0 "
        + "AND t.name = ? AND col.name = ?";
于 2013-06-24T14:26:09.737 回答