JDBC Metada retrieve Constraint information

社会主义新天地 提交于 2019-12-06 03:32:27

问题


I need information about table and column name from the name of the constraint.

exists someone similar to connection.getMetadata().getX to retrieve the constraint information?

my test case is in Oracle Database, but my best solution I just want to solve with jdbc


回答1:


If you are talking about foreign key and primary key constraints. The DatabaseMetaData does provide methods for retrieving this information: you can use getImportedKeys(..) and getCrossReference(..) for foreign keys, and getPrimaryKeys(..) and getExportedKeys(..) for primary keys.

Just be careful how you use them: getCrossReference(..) and getExportedKeys are a bit counter-intuitive in my opinion.

If you also need unique constraints, then you should be able to use getIndexInfo(..) with passing true for the parameter unique.




回答2:


You can get defined constraints information from USER_CONSTRAINTS and ALL_CONSTRAINTS table.
You need to pass required where clause fields.

Example :

select 
    CONSTRAINT_NAME,
    CONSTRAINT_TYPE,
    TABLE_NAME,
    ...
from
    USER_CONSTRAINTS
where
    CONSTRAINT_NAME like concat(?, '%');


来源:https://stackoverflow.com/questions/11494549/jdbc-metada-retrieve-constraint-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!