rules repository

G-9204

🆓
Warning

Always follow naming conventions for primary key constraints.

Reason

All table constraints share the same namespace. Furthermore the name of the primary key constraint is used by default as index name. Follow naming conventions to prevent naming conflicts, improve readability, and clearly indicate the scope without forcing the use of qualified names. A common practice is to use a prefix and/or suffix to distinguish the identifier types.

Recommendations

Table name or table abbreviation followed by the suffix _pk.

Examples

  • employees_pk
  • departments_pk
  • sct_contracts_pk

Example

Non-Compliant Example

create table emp (
   empno number(4,0) not null,
   -- ...
   constraint emp#primary#key primary key (empno)
);
Issues
LineColumnMessage
415Primary key emp#primary#key does not match '(?i)^[a-z][a-z0-9$#_]*_pk$'.

Compliant Solution - ★★★★★

create table emp (
   empno number(4,0) not null,
   -- ...
   constraint emp_pk primary key (empno)
);

Tests

Test SQL query

select owner || '.' || constraint_name as identifier,
       'Primary key constraint ' || constraint_name || ' on ' || table_name || ' does not match '''
       || lower(#PrimaryKeyConstraintPattern#) || '''.' as message
  from dba_constraints pk
 where owner in (#SchemaNames#)
   and constraint_type = 'P'
   and table_name not like 'AQ$%'
   and not regexp_like(constraint_name, replace(lower(#PrimaryKeyConstraintPattern#), '(?i)', null), 'i')
   and not exists (
          select 1
            from dba_queue_tables qt
           where qt.owner = pk.owner
             and qt.queue_table = pk.table_name
       )
   and not exists (
          select 1
            from dba_tables t
           where t.iot_type is not null
             and t.owner = pk.owner
             and t.table_name = pk.table_name
       )
 order by identifier

Test results

IdentifierMessageMigration
DBL_OWNER.EMP#PRIMARY#KEYPrimary key constraint EMP#PRIMARY#KEY on EMP does not match '(?i)^[a-z][a-z0-9$#_]*_pk$'.-

Parameters

Use parameters to customize the rule to your needs.

ParameterDescriptionDefault Value
PrimaryKeyConstraintPatternRegular expression pattern for SQL primary key constraints.(?i)^[a-z][a-z0-9$#_]*_pk$
SchemaNamesComma-separated list of database schemas owning the database objects of an application.dbl_owner

References