简体   繁体   English

如何从 DB2 中的逻辑表中获取主键?

[英]How can I get the Primary Keys from logical Tables in DB2?

So I am working on AS400 , DB2 System .所以我正在研究AS400DB2 系统 I wrote a method which provides me the Primary Keys of every physical table.我写了一个方法,它为我提供了每个物理表的主键 But on some tables the primary keys are only set on the logical table.但是在某些表上,主键仅在逻辑表上设置。 There my method does not work.在那里我的方法不起作用。

@Override
    public ArrayList<Field> getPKS(String lib) {
        ArrayList<Field> pkList = new ArrayList<>();
        try (Connection connection = DriverManager.getConnection("jdbc:as400://" + ConnectionData.SYSTEM + ";naming=system;libraries=*" + lib + ";",
                ConnectionData.USER, ConnectionData.PASSWORD);
                            
                ResultSet rs = connection.getMetaData().getPrimaryKeys(null, connection.getSchema(), "LSAVSLA")){
                while (rs.next()) {
                    pkList.add(new Field(rs.getString("COLUMN_NAME")));
                }
           
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return pkList;
    
    }

For a pysical table it's working, but for a logical Table it is not.对于物理表它可以工作,但对于逻辑表它不是。 Do you have any idea how to get the primary keys from the logical table .您知道如何从逻辑表中获取主键吗?

Logical files do not contain data.逻辑文件不包含数据。 They contain a description of records that are found in one or more physical files.它们包含在一个或多个物理文件中找到的记录的描述。 A logical file is a view or representation of one or more physical files逻辑文件是一个或多个物理文件的视图或表示

says the manual . 手册上说 Similarly to a view in a conventional RDBMS, you cannot define a primary key for a logical file.与传统 RDBMS 中的视图类似,您不能为逻辑文件定义主键。

So basically what you have are physical files (or SQL tables) defined without a primary key and logical files or (SQL indexes) defined with a unique key.所以基本上你拥有的是没有主键定义的物理文件(或 SQL 表)和使用唯一键定义的逻辑文件或(SQL 索引)。

On the IBM i, a logical file can act as either an SQL index, and SQL View or both at the same time.在 IBM i 上,逻辑文件可以充当 SQL 索引和 SQL 视图,或者同时充当两者。 As Mustaccio mentions, there isn't any actual data in the object.正如 Mustaccio 所提到的,对象中没有任何实际数据。

You best bet, might be to query the SYSTABLEINDEXS catalog view looking for primary key or unique indexes over a given table.您最好的选择是查询SYSTABLEINDEXS 目录视图,以查找给定表的主键或唯一索引。

You could also take a look at the getIndexes() method.您还可以查看getIndexes()方法。

I found a solution by selecting the "DBKFLD" field from "QSYS.QADBKATR"我通过从“QSYS.QADBKATR”中选择“DBKFLD”字段找到了一个解决方案
The SQL Query: SQL 查询:
SELECT DBKFLD FROM QSYS.QADBKATR WHERE DBKLIB = "Your lib" AND DBKFIL = "Your table" SELECT DBKFLD FROM QSYS.QADBKATR WHERE DBKLIB = "Your lib" AND DBKFIL = "Your table"

The Java Code: Java代码:

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet resultSetQuery = statement.executeQuery("select DBKFLD from QSYS.QADBKATR where DBKLIB = '" + lib + "' and DBKFIL = '" + tablename + "'")) {
    
    ResultSetMetaData metadata = resultSetQuery.getMetaData();
                    int columnCount = metadata.getColumnCount();
        
                    while (resultSetQuery.next()) {
        
                        
                        for (int i = 1; i <= columnCount; i++) {
        
                            String pk = resultSetQuery.getString(i);
                            pk = pk.replaceAll("\\s+", "");
        
                            pkList.add(new Feld(pk));
        
                        }
                        
                    }
        
                    return pkList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM