Naming Conventions in Oracle Projects: Why They Matter More Than You'd Think
It's easy to treat naming as an afterthought. You're focused on getting the table created and the app working, so a column ends up called name or date1, and you move on. Six months later, nobody, including you, remembers what that column was actually for. A consistent naming convention fixes that before it becomes a problem.
1. Why bother with a convention at all?
A few reasons this pays off, even on a project you think you're the only one who'll ever touch:
- Joins get less confusing. If both your employees table and your departments table have a column called
id, every query needs table aliases just to stay readable. Prefix your columns and the ambiguity disappears on its own. - Other people can guess correctly. A teammate who's never seen your schema should be able to tell what
fk_orders_customersdoes just from the name. - Debugging is faster. When an error message points at a constraint or trigger, a name like
trg_employees_bitells you where to look immediately, instead of sending you hunting through the whole schema. - Generated code stays sane. APEX and other tools often name things automatically based on your existing objects. Messy input names produce messy generated names.
2. Naming tables and columns
Pick a stance on singular versus plural table names and stick with it everywhere. Both employee and employees are fine choices. What matters is that you don't end up with a mix of both across your schema.
For columns, prefixing with a short abbreviation of the table name is one of the most useful habits you can build. It's the pattern we used when we set up the employees table earlier:
The prefix costs you a few extra keystrokes, but it pays off the moment you join this table against another one that also has a first_name or an id column. There's never any doubt about which table a column belongs to.
3. Naming everything else in your schema
Tables and columns get most of the attention, but constraints, indexes, sequences, and triggers all need names too. Oracle will happily generate names like SYS_C007821 if you don't supply your own, which is exactly as useless as it sounds. Here's a set of prefixes that covers most of what you'll need:
| Object | Prefix | Example |
|---|---|---|
| Primary key | pk_ | pk_employees |
| Foreign key | fk_ | fk_employees_departments |
| Unique key | uq_ | uq_employees_email |
| Check constraint | ck_ | ck_employees_salary_positive |
| Index | idx_ | idx_employees_last_name |
| Sequence | seq_ | seq_employees |
| Trigger | trg_ | trg_employees_bi |
| View | vw_ | vw_active_employees |
Naming a trigger trg_employees_bi is also a small habit worth picking up on its own: the suffix tells you when it fires. bi means before insert, au means after update, and so on. You can tell what a trigger does before you even open it.
4. Putting it together
Here's the employees table again, this time with a named primary key constraint and an index added on top:
Notice that spelling out the constraint separately, instead of inlining it on the column, is what lets you name it yourself. If you skip that step, Oracle names it for you, and you're back to guessing what SYS_C007821 was supposed to protect.
5. A few mistakes worth avoiding
|
DO
|
DON'T
|
- Prefixing columns with a short table abbreviation removes ambiguity the moment you start joining tables.
- Constraints, indexes, sequences, and triggers all deserve real names, not the auto-generated ones Oracle assigns by default.
- The specific convention matters less than picking one and applying it consistently across the whole project.
Comments
Post a Comment