<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 'http://www.w3.org/TR/html4/loose.dtd"'> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Constraint naming standard</title><link rel="stylesheet" type="text/css" href="openacs.css"><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="OpenACS Core Documentation"><link rel="up" href="eng-standards.html" title="Chapter 12. Engineering Standards"><link rel="previous" href="eng-standards-versioning.html" title="Release Version Numbering"><link rel="next" href="eng-standards-filenaming.html" title="ACS File Naming and Formatting Standards"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><a href="http://openacs.org"><img src="/doc/images/alex.jpg" style="border:0" alt="Alex logo"></a><table width="100%" summary="Navigation header" border="0"><tr><td width="20%" align="left"><a accesskey="p" href="eng-standards-versioning.html">Prev</a> </td><th width="60%" align="center">Chapter 12. Engineering Standards</th><td width="20%" align="right"> <a accesskey="n" href="eng-standards-filenaming.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="eng-standards-constraint-naming"></a>Constraint naming standard</h2></div></div></div><div class="authorblurb"><p>By Michael Bryzek</p> OpenACS docs are written by the named authors, and may be edited by OpenACS documentation staff. </div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="eng-standards-constraint-naming-big-picture"></a>The Big Picture</h3></div></div></div><p> Constraint naming standard is important for one reason: The SYS_* name oracle assigns to unnamed constraints is not very understandable. By correctly naming all constraints, we can quickly associate a particular constraint with our data model. This gives us two real advantages: </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p> We can quickly identify and fix any errors. </p></li><li class="listitem"><p> We can reliability modify or drop constraints </p></li></ul></div><p> </p><div>Why do we need a naming convention? </div><p> <a class="ulink" href="https://docs.oracle.com/database/121/SQLRF/sql_elements008.htm#SQLRF00223" target="_top">Oracle limits names</a>, in general, to 30 characters, which is hardly enough for a human readable constraint name. </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="eng-standards-constraint-naming-abbr"></a>Abbreviations</h3></div></div></div><p> We propose the following naming convention for all constraints, with the following abbreviations taken from Oracle Docs. Note that we shortened all of the constraint abbreviations to two characters to save room. </p><div class="informaltable"><table class="informaltable" cellspacing="0" border="1"><colgroup><col><col></colgroup><thead><tr><th>Constraint type</th><th>Abbreviation</th></tr></thead><tbody><tr><td>references (foreign key)</td><td>fk</td></tr><tr><td>unique</td><td>un</td></tr><tr><td>primary key</td><td>pk</td></tr><tr><td>check</td><td>ck</td></tr><tr><td>not null</td><td>nn</td></tr><tr><td>index</td><td>idx</td></tr></tbody></table></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="eng-standards-constraint-naming-format"></a>Format of constraint name</h3></div></div></div><p> <table name>_<column_name>_<constraint abbreviation> </p><p> In reality, this won't be possible because of the character limitation on names inside oracle. When the name is too long, we will follow these two steps in order: </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p> Abbreviate the table name with the table's initials (e.g. users -> u and users_contact -> uc). </p></li><li class="listitem"><p> Truncate the column name until it fits.</p></li></ol></div><p> If the constraint name is still too long, you should consider rewriting your entire data model :) </p><p><span class="strong"><strong>Notes:</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p> If you have to abbreviate the table name for one of the constraints, abbreviate it for all the constraints</p></li><li class="listitem"><p> If you are defining a multi column constraint, try to truncate the two column names evenly </p></li></ul></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="eng-standards-constraint-naming-example"></a>Example</h3></div></div></div><pre class="programlisting"> create table example_topics ( topic_id integer constraint example_topics_topic_id_pk primary key ); create table constraint_naming_example ( example_id integer constraint cne_example_id_pk primary key, one_line_description varchar(100) constraint cne_one_line_desc_nn not null, body clob, up_to_date_p char(1) default('t') constraint cne_up_to_date_p_check check(up_to_date_p in ('t','f')), topic_id constraint cne_topic_id_nn not null constraint cne_topic_id_fk references example_topics, -- Define table level constraint constraint cne_example_id_one_line_unq unique(example_id, one_line_description) ); </pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="eng-standards-constraint-naming-pk"></a>Why it's good to name primary keys</h3></div></div></div><p> Naming primary keys might not have any obvious advantages. However, here's an example where naming the primary key really helps (and this is by no means a rare case! </p><pre class="programlisting"> SQL> set autotrace traceonly explain; SQL> select * from constraint_naming_example, example_topics where constraint_naming_example.topic_id = example_topics.topic_id; Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE 1 0 NESTED LOOPS 2 1 TABLE ACCESS (FULL) OF 'CONSTRAINT_NAMING_EXAMPLE' 3 1 INDEX (UNIQUE SCAN) OF 'EXAMPLE_TOPICS_TOPIC_ID_PK' (UNIQUE) </pre><p> Isn't it nice to see "EXAMPLE_TOPICS_TOPIC_ID_PK" in the trace and know exactly which table oracle is using at each step? </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="eng-standards-constraint-naming-nn"></a>Naming not null constraints is optional...</h3></div></div></div><p> People disagree on whether or not we should be naming not null constraints. So, if you want to name them, please do so and follow the above naming standard. But, naming not null constraints is not a requirement. </p><p> </p><div>About Naming the not null constraints</div><p> </p><p> Though naming "not null" constraints doesn't help immediately in error debugging (e.g. the error will say something like "Cannot insert null value into column"), we recommend naming not null constraints to be consistent in our naming of all constraints. </p><div class="cvstag">($Id: eng-standards-constraint-naming.html,v 1.55 2024/09/11 06:15:47 gustafn Exp $)</div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="eng-standards-versioning.html">Prev</a> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right"> <a accesskey="n" href="eng-standards-filenaming.html">Next</a></td></tr><tr><td width="40%" align="left">Release Version Numbering </td><td width="20%" align="center"><a accesskey="u" href="eng-standards.html">Up</a></td><td width="40%" align="right"> ACS File Naming and Formatting Standards</td></tr></table><hr><address><a href="mailto:docs@openacs.org">docs@openacs.org</a></address></div><a name="comments"></a></body></html>