Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get primary keys for all tables in JDBC?

Tags:

java

mysql

jdbc

I have a database with at least 500 tables. What is the exact code to get all the primary keys and foreign keys of each table?

//Primary Key
DatabaseMetaData meta=conn.getMetaData();
ResultSet rs1= meta.getTables(null, null, "TableName" , new String[]{"TABLE"});
rs1=meta.getPrimaryKeys(null, null, "TableName");
while(rs1.next())
    System.out.println("Primary Key :"+rs1.getString(4));

//Foreign Key
rs1=meta.getExportedKeys(null, null, "TableName");
while(rs1.next())
    System.out.println("Foreign Key :"+rs1.getString(4));

I have used this code and it gives me accurate keys but for 500 tables, I have to change my code 500 times. Is there any way to minimize this effort?

like image 798
Kaushal Daga Avatar asked Nov 28 '16 09:11

Kaushal Daga


People also ask

How can you retrieve a list of all tables in a database from JDBC?

You can get the list of tables in the current database in MySQL using the SHOW TABLES query. Show tables; Following JDBC program retrieves the list of tables in the database by executing the show tables query.

How do you find primary keys in a table?

Columns that have a high percentage of uniqueness, as well as at least one foreign key candidate, make the best primary key candidates. Identify the columns that have the highest uniqueness percentage and assign one of the columns as the primary key. You might only have one primary key per table.

Do you need a primary key for every table?

Every table can have (but does not have to have) a primary key. The column or columns defined as the primary key ensure uniqueness in the table; no two rows can have the same key. The primary key of one table may also help to identify records in other tables, and be part of the second table's primary key.


2 Answers

You don't need to modify your code 500 times, you can retrieve all table names using meta.getTables(null, null, "%", new String[]{"TABLE"}).

The method getTables takes a like-pattern for the tableNamePattern parameter, so "%" matches all table names.

The method getPrimaryKeys and getExportedKeys do not take a pattern, so you will need to loop over the result of getTables and execute those methods for each row of the getTables result set.

So you will need to do something like:

try (ResultSet tables = meta.getTables(null, null, "%", new String[] { "TABLE" })) {
    while (tables.next()) {
        String catalog = tables.getString("TABLE_CAT");
        String schema = tables.getString("TABLE_SCHEM");
        String tableName = tables.getString("TABLE_NAME");
        System.out.println("Table: " + tableName);
        try (ResultSet primaryKeys = meta.getPrimaryKeys(catalog, schema, tableName)) {
            while (primaryKeys.next()) {
                System.out.println("Primary key: " + primaryKeys.getString("COLUMN_NAME"));
            }
        }
        // similar for exportedKeys
    }
}

I have included retrieval of catalog and schema, because that might influence how things work.

like image 102
Mark Rotteveel Avatar answered Oct 25 '22 18:10

Mark Rotteveel


You can refer to this code snippet that shows you how to get all the tables from a database.

You need to iterate over your ResultSet calling next().

I have tried this for MS SQL Server 2012

public class RetrieveAllTables {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HubDB", "sa", "password");
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            ResultSet resultSet = databaseMetaData.getTables(null, null, "%", new String[] {"TABLE"});

            while(resultSet.next()) {
                System.out.print("Table Catalog: " + resultSet.getString("TABLE_CAT"));
                System.out.print("\tTable Schema: " + resultSet.getString("TABLE_SCHEM"));
                System.out.print("\tTable Name: " + resultSet.getString("TABLE_NAME"));
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Returns the following as output:

Table Catalog: HubDB    Table Schema: dbo   Table Name: films
Table Catalog: HubDB    Table Schema: dbo   Table Name: FILMS_AUDIT
Table Catalog: HubDB    Table Schema: sys   Table Name: trace_xe_action_map
Table Catalog: HubDB    Table Schema: sys   Table Name: trace_xe_event_map

Hence you need to change the following from

ResultSet resultSet = databaseMetaData.getTables(null, null, "%", new String[] {"TABLE"});

to

ResultSet resultSet = databaseMetaData.getTables(null, "dbo", "%", new String[] {"TABLE"});

You can refer the getTables() method documentation here

Using this information, you can dynamically pass the individual TableName to retrieve the Primary Key and the Foreign Key details. Your existing code should work just fine in retrieving these details by passing just one parameter, that's the TableName from the above code.

like image 41
N00b Pr0grammer Avatar answered Oct 25 '22 17:10

N00b Pr0grammer