Skip to Main Content
  • Questions
  • Is it a good design practice to have ID column in Oracle Tables

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Swarup.

Asked: June 02, 2024 - 4:37 pm UTC

Last updated: June 04, 2024 - 1:57 pm UTC

Version: 19c

Viewed 1000+ times

You Asked

Hi Tom,

I have a question about having an ID column in all tables of an OLTP system.

In my application, all tables have ID (Not sure why it was designed that way) column which gets populated by Sequence and this ID column value is not used as a referential key in other tables. So in that case is it a good practice to have ID column?

I know that we need to have either a single or composite columns to uniquely identify a row and that is possible as we have for ex. Code column in most of the tables and that generally contains Unique value.

Please let me know if my question is not clear to you.

Thanks in advance,
Swarup

and Chris said...

It's good practice for every table to have a primary key. In most cases, this will be an ID column populated by a sequence or an identity column. This is the "safe" choice, though often unnecessary.

Code columns can make for good primary keys in limited cases. In my opinion, this is best when the codes are part of an external, widely used standard. ISO country & currency codes, and IATA airport codes are good examples.

Composite primary keys are often good enough for many-to-many junction tables, which only store the primary keys of the parent tables & perhaps some auditing metadata columns. They can also work well for child tables in master-detail relationships (orders -> order items, invoices -> invoice items, etc.).

That said, some frameworks expect all tables to have an ID column. If this is missing they can be tricky to work with. If using such a tool, mandating ID columns across the board is often the path of least resistance.

Is this answer out of date? If it is, please let us know via a Comment