简体   繁体   中英

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

So I am working on AS400 , DB2 System . 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.

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.

On the IBM i, a logical file can act as either an SQL index, and SQL View or both at the same time. As Mustaccio mentions, there isn't any actual data in the object.

You best bet, might be to query the SYSTABLEINDEXS catalog view looking for primary key or unique indexes over a given table.

You could also take a look at the getIndexes() method.

I found a solution by selecting the "DBKFLD" field from "QSYS.QADBKATR"
The SQL Query:
SELECT DBKFLD FROM QSYS.QADBKATR WHERE DBKLIB = "Your lib" AND DBKFIL = "Your table"

The Java Code:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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