SQL验证列的约束

当规则类型为唯一时:SELECT 字段名 FROM 表名 GROUP BY 字段名 HAVING COUNT(1) >0

当规则类型为非空时:SELECT * FROM 表名 WHERE 字段名 IS NULL

当规则类型为主键时:SELECT COUNT(1) FROM USER_CONSTRAINTS UC,USER_CONS_COLUMNS UCC WHERE UC.CONSTRAINT_NAME = UCC.CONSTRAINT_NAME AND UC.CONSTRAINT_TYPE = 'P' AND UCC.TABLE_NAME = 表名 AND UCC.COLUMN_NAME = 字段名

当规则类型为外键时:SELECT * FROM 表名 WHERE 关联字段 NOT IN (SELECT DISTINCT关联字段 FROM  关联表)

你可能感兴趣的:(sql,约束)