1
我需要在GUI應用程序中執行DDL語句。對於刪除列語句,我需要先刪除所有約束和依賴關係。到目前爲止,我知道:SQL Server列約束和依賴關係
- 主鍵約束
- 外鍵約束
- 唯一約束
- 檢查約束
- 默認約束
- 指數依賴
是否有任何我在c之前必須刪除的其他約束或依賴關係執行DROP TABLE COLUMN
聲明?
我需要在GUI應用程序中執行DDL語句。對於刪除列語句,我需要先刪除所有約束和依賴關係。到目前爲止,我知道:SQL Server列約束和依賴關係
是否有任何我在c之前必須刪除的其他約束或依賴關係執行DROP TABLE COLUMN
聲明?
這些現在最後的工作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 = ?";
這是在回答[文件](http://msdn.microsoft.com/en-us/library/ms190273.aspx),其正好列出了你上面的要點。 – Pondlife
感謝您的回覆!我很抱歉,但在本文檔中,我無法弄清楚是否必須刪除任何進一步的限制。這個問題的原因是,因爲我沒有使用SQL Server的經驗,今天我第一次聽說了'Default Constraints'。所以我絕對想避免由於未知的約束類型而導致「DROP COLUMN」聲明失敗。 –
您是否閱讀過關於約束的[documentation](http://msdn.microsoft.com/zh-cn/library/ms189862(v = sql.105).aspx)?我並不完全確定,如果擔心自己對SQL Server不夠了解(在這種情況下,文檔是很好的參考資料),或者擔心文檔不完整和準確? – Pondlife