最佳答案
您可以查询系统表SYS.SYSCONSTRAINTS,SYS.SYSTABLES和SYS.SYSSCHEMAS以获取给定模式的所有约束名称和相关表.
首先,关于这些表的几句话(从Re: System tables in Derby开始):
SYSTABLES has one row for each table in the database. Its primary key is TABLEID,which contains system-generated values. The SCHEMAID is a foreign key column,which references SYSSCHEMAS.
SYSSCHEMAS has one row for each schema in the database. Its primary key is SCHEMAID.
…
SYSCONSTRAINTS has one row for each constraint in the database (primary,unique,foreign and check constraints). Its primary key is CONSTRAINTID,which is a system-generated value. The TABLEID column is a foreign key referring to SYSTABLES.TABLEID. The SCHEMAID column is a foreign key referring to SYSSCHEMAS.SCHEMAID.
所以你可以使用以下查询:
SELECT
C.CONSTRAINTNAME,T.TABLENAME
FROM
SYS.SYSCONSTRAINTS C,SYS.SYSSCHEMAS S,SYS.SYSTABLES T
WHERE
C.SCHEMAID = S.SCHEMAID
AND
C.TABLEID = T.TABLEID
AND
S.SCHEMANAME = 'MYSCHEMA';
然后循环并构建相应的ALTER TABLE DROP CONSTRAINT语句.
参考