Skip to content
  • Java And Other Languages
  • jOOQ and jOOλ
  • jOOQ Tuesdays and Guest Posts
  • SQL Tricks And Tuning
  • Thoughts on Programming

Java, SQL and jOOQ.

Best Practices and Lessons Learned from Writing Awesome Java and SQL Code. Get some hands-on insight on what's behind developing jOOQ.

Tag: schema information

jOOQ Tip of the Day: Discover all Primary Keys

Posted on November 14, 2014November 1, 2014 by lukaseder

We’ve recently encountered this interesting use-case on the jOOQ user group. How do you discover all primary keys of your schema via the jOOQ API? There are two ways:

  • Using the generated meta data
  • Using runtime meta data

Let’s see how it works:

Using the generated meta data

This is straightforward. If you’re using Java 8, you can run the following program

GENERATED_SCHEMA
    .getTables()
    .stream()
    .map(t -> t.getPrimaryKey())
    .filter(Objects::nonNull)
    .forEach(System.out::println);

Whereas with Java 7 or less, you’d write

for (Table<?> t : GENERATED_SCHEMA.getTables()) {
    UniqueKey<?> key = t.getPrimaryKey();

    if (key != null)
        System.out.println(key);
}

Using runtime meta data

If you’re not using the code generator, the same can be achieved with:

DSL.using(configuration)
   .meta()
   .getTables()
   .[ same as above ]

That’s it! Another interesting use case of querying the meta information via the jOOQ API can be seen here.

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • More
  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)
Tagged information_schema, jooq, meta data, query, schema information4 Comments

Like this blog? Check out our product:

jOOQ: Get Back in Control of Your SQL

Don’t forget to follow us on Twitter

Jump to:

  • Home
  • SQL Tricks and Tuning
  • Thoughts on Programming
  • jOOQ Tuesdays and Guest Posts
  • Java and Other Languages
  • jOOQ and jOOλ

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2,081 other followers

Top Posts & Pages

  • SQL IN Predicate: With IN List or With Array? Which is Faster?
  • How to Create a Range From 1 to 10 in SQL
  • 3 Reasons why You Shouldn't Replace Your for-loops by Stream.forEach()
  • How to Write a Multiplication Aggregate Function in SQL
  • How to Calculate Multiple Aggregate Functions in a Single Query
  • The Difference Between ROW_NUMBER(), RANK(), and DENSE_RANK()
  • Say NO to Venn Diagrams When Explaining JOINs
  • How to Fill Sparse Data With the Previous Non-Empty Value in SQL
  • Selecting all Columns Except One in PostgreSQL
  • A Beginner's Guide to the True Order of SQL Operations

Recent Posts

  • Never Again Forget to Call .execute() in jOOQ March 30, 2021
  • Calculating Pagination Metadata Without Extra Roundtrips in SQL March 11, 2021
  • Simulating Latency with SQL / JDBC February 15, 2021
  • Translating Stored Procedures Between Dialects February 10, 2021
  • Implementing a generic REDUCE aggregate function with SQL February 8, 2021
  • jOOQ Internals: Pushing up SQL fragments February 4, 2021
  • Automatically Transform Oracle Style Implicit Joins to ANSI JOIN using jOOQ November 17, 2020
  • jOOQ 3.14 Released With SQL/XML and SQL/JSON Support October 20, 2020
  • Using jOOQ 3.14 Synthetic Foreign Keys to Write Implicit Joins on Views October 13, 2020
  • Nesting Collections With jOOQ 3.14’s SQL/XML or SQL/JSON support October 9, 2020
jOOQ: Get Back in Control of Your SQL

Archives

  • March 2021 (2)
  • February 2021 (4)
  • November 2020 (1)
  • October 2020 (3)
  • September 2020 (1)
  • August 2020 (1)
  • July 2020 (1)
  • May 2020 (1)
  • April 2020 (2)
  • March 2020 (5)
  • February 2020 (2)
  • November 2019 (1)
  • October 2019 (3)
  • September 2019 (6)
  • August 2019 (1)
  • July 2019 (1)
  • June 2019 (2)
  • April 2019 (3)
  • March 2019 (3)
  • February 2019 (2)
  • January 2019 (3)
  • December 2018 (1)
  • November 2018 (3)
  • October 2018 (2)
  • September 2018 (5)
  • August 2018 (2)
  • July 2018 (2)
  • June 2018 (5)
  • May 2018 (3)
  • April 2018 (6)
  • March 2018 (2)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (5)
  • November 2017 (4)
  • October 2017 (2)
  • September 2017 (3)
  • August 2017 (3)
  • July 2017 (7)
  • June 2017 (6)
  • May 2017 (7)
  • April 2017 (2)
  • March 2017 (9)
  • February 2017 (5)
  • January 2017 (6)
  • December 2016 (6)
  • November 2016 (4)
  • October 2016 (7)
  • September 2016 (3)
  • August 2016 (5)
  • July 2016 (8)
  • June 2016 (4)
  • May 2016 (3)
  • April 2016 (7)
  • March 2016 (10)
  • February 2016 (8)
  • January 2016 (8)
  • December 2015 (10)
  • November 2015 (2)
  • October 2015 (8)
  • September 2015 (5)
  • August 2015 (7)
  • July 2015 (3)
  • June 2015 (7)
  • May 2015 (9)
  • April 2015 (11)
  • March 2015 (11)
  • February 2015 (10)
  • January 2015 (9)
  • December 2014 (10)
  • November 2014 (8)
  • October 2014 (5)
  • September 2014 (13)
  • August 2014 (11)
  • July 2014 (10)
  • June 2014 (14)
  • May 2014 (15)
  • April 2014 (11)
  • March 2014 (15)
  • February 2014 (17)
  • January 2014 (14)
  • December 2013 (23)
  • November 2013 (24)
  • October 2013 (19)
  • September 2013 (15)
  • August 2013 (24)
  • July 2013 (17)
  • June 2013 (3)
  • May 2013 (5)
  • April 2013 (6)
  • March 2013 (8)
  • February 2013 (11)
  • January 2013 (3)
  • December 2012 (5)
  • November 2012 (2)
  • October 2012 (5)
  • September 2012 (7)
  • August 2012 (7)
  • July 2012 (7)
  • June 2012 (1)
  • May 2012 (6)
  • April 2012 (12)
  • March 2012 (3)
  • February 2012 (5)
  • January 2012 (12)
  • December 2011 (8)
  • November 2011 (15)
  • October 2011 (16)
  • September 2011 (12)
  • August 2011 (13)
  • July 2011 (4)
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.