<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jiramot.info &#187; plsql</title>
	<atom:link href="http://www.jiramot.info/tag/plsql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jiramot.info</link>
	<description>Developer&#039;s Life</description>
	<lastBuildDate>Mon, 06 Feb 2012 08:49:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Oracle Naming Conventions</title>
		<link>http://www.jiramot.info/oracle-naming-conventions</link>
		<comments>http://www.jiramot.info/oracle-naming-conventions#comments</comments>
		<pubDate>Wed, 21 Jul 2010 12:24:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[plsql]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=531</guid>
		<description><![CDATA[When designing a database it&#8217;s a good idea to follow some sort of naming convention. This will involve a little thought in the early design stages but will save significant time when maintaining the finished system. <p>It&#8217;s less important which exact conventions you choose to follow &#8211; but this page has a few suggestions.</p> [...]]]></description>
			<content:encoded><![CDATA[<h1><span style="font-weight: normal; font-size: 13px;">When designing a database it&#8217;s a good idea to follow some sort of naming convention. This will involve a little thought in the early design stages but will save significant time when maintaining the finished system.</span></h1>
<p>It&#8217;s less important which exact conventions you choose to follow &#8211; but this page has a few suggestions.</p>
<p>The benefits of using a naming convention are more to do with human factors than any system limitations &#8211; but this does not make them any less important.</p>
<p><span id="more-531"></span></p>
<p><strong>Table Names</strong></p>
<blockquote>
<blockquote><p>Table names are <em>plural</em>, field name is <em>singular</em></p>
<p>Table names should not contain spaces, words should be split_up_with_underscores.</p>
<p>The table name is limited to 30 bytes which should equal a 30 character name (try a DESC ALL_TABLES and note the size of the Table_Name column)</p>
<p>If the table name contains serveral words, only the last one should be plural:<br />
APPLICATIONS<br />
APPLICATION_FUNCTIONS<br />
APPLICATION_FUNCTION_ROLES</p>
<p>There are pros and cons to adding a prefix or suffix to identify tables-</p>
<p>Pros: If most access will be made via VIEWS then prefixing all the tables with T_ and all the views with V_ keeps things organised neatly, you will never accidentally query the wrong one.</p>
<p>Cons: Suppose, your naming convention is to have the &#8216;_TAB&#8217; suffix for all tables. According to that naming convention, the APPLICATIONS table would be called APPLICATIONS_TAB. If as time goes by, your application gets a second login, perhaps for auditing, or for security reasons. To avoid code changes, you will then have to create a View or Synonym that points at the original tables and is confusingly called APPLICATIONS_TAB.</p>
<p>Of course if all your code was written against Views in the first place then you skirt right around that issue.</p></blockquote>
</blockquote>
<p><strong>Field Names</strong></p>
<blockquote>
<blockquote><p>Ideally each field name should be unique within the database schema. This makes it easy to search through a large set of code (or documentation) and find all occurences of the field name.</p>
<p>The convention is to prefix the fieldname with a 2 or 3 character contraction of the table name e.g.</p>
<blockquote><p>PATIENT_OPTIONS would have a field called po_patient_option</p>
<p>PATIENT_RELATIVES would have a field called pr_relative_name</p>
<p>ABSENCES would have a field called ab_start_date</p></blockquote>
<p>In a large schema you will often find two tables having similar names which could result in the same prefix. You can avoid this by thinking carefully about the name you give each table &#8211; and documenting the prefixes to be used.</p>
<p>For very complex systems (thousands of tables) consider alternatives e.g. a prefix/suffix to identify the Application module.</p>
<p>Keeping names short: Oracle places no limit on the number of columns in a GROUP BY clause or the number of sort specifications in an ORDER BY clause. However, the sum of the sizes of all such expressions is limited to the size of an Oracle data block (specified by the initialization parameter DB_BLOCK_SIZE) minus some overhead.</p></blockquote>
</blockquote>
<p><strong>Primary Key Fields </strong>- indicate by appending <strong>_pk</strong></p>
<blockquote>
<blockquote><p>e.g.</p>
<p>PATIENTS would have a primary key called pa_patient_id_pk</p>
<p>REGIONS would have a primary key called re_region_id_pk</p>
<p>And so on&#8230;the name of the primary key field being a singular version of the table name. Other tables containing this as a foreign key would omit the _PK</p>
<p>so<br />
CLINIC_ATTENDANCE might then have a foreign key called ca_patient_id<br />
or alternatively: ca_patient_id_fk</p>
<p>Tables with Compound PK&#8217;s, use <strong>_ck</strong> in place of _pk</p>
<p>Notice that where several tables use the same PK as part of a compound foreign key then the only unique part of the FK fieldname will be the table prefix.</p></blockquote>
</blockquote>
<p><strong>View Names</strong></p>
<blockquote>
<blockquote><p>View names are <em>plural</em>, field name is <em>singular</em></p>
<p>View names should not contain spaces, words should be split_up_with_underscores.</p>
<p>While it is common to prefix (or suffix) all views with V_ or VW_, a strong argument can be made that neither are really needed.</p>
<p>One of the easiest ways to boost the performance of an application is to provide, at an early stage in the design, a carefully tuned set of views, then write all the application code against those Views.<br />
Giving your Views friendly easy names will promote their use by developers and end-users, this in turn will mean fewer badly written queries and more use of &#8216;shared SQL&#8217; which will improve the cache hit ratio.</p>
<p>For very large systems, it can make sense to prefix tables/views with the application module name, so a database holding data for both Widget Production and Human Resources data might prefix everything with either HR_ or WP_</p></blockquote>
</blockquote>
<p><strong>Index names</strong></p>
<blockquote>
<blockquote><p>Name the Primary Key index as <strong>idx_</strong>&lt;TableName&gt;<strong>_pk</strong></p>
<blockquote><p>e.g.<br />
PATIENTS would have a primary key index called idx_patients_pk</p></blockquote>
<p>Name a Unique Index as <strong>idx_</strong>&lt;TableName&gt;<strong>_uk</strong></p>
<blockquote><p>e.g.<br />
PATIENTS would have a unique index called idx_patients_uk</p></blockquote>
<p>Where more indexes are added to the same table, simply append a numeric:</p>
<blockquote>
<blockquote><p><strong>idx_</strong>&lt;TableName&gt;<strong>_##</strong></p>
<p>Where ## is a simple number</p></blockquote>
<p>e.g.<br />
PATIENTS would have additional indexes called idx_patients_01, idx_patients_02,&#8230;</p></blockquote>
<p><em>Note &#8211; Conventions that attempt to use the column name as part of the index name become unmanageable as soon as you have multiple columns appearing in multiple indexes.</em></p></blockquote>
</blockquote>
<p><strong>Constraints</strong></p>
<blockquote>
<blockquote><p>Primary and Unique constraints will be explicitly named.</p>
<p>Name the Primary Key Constraint as <strong>pk_</strong>&lt;TableName&gt;<strong></strong></p>
<blockquote><p>e.g.<br />
PATIENTS would have a primary key index called pk_patients</p></blockquote>
<p>Name a Foreign Key Constraint as <strong>fk_</strong>&lt;TableName&gt;</p>
<blockquote><p>e.g.<br />
PATIENTS would have a Foreign Key constraint called fk_patients</p></blockquote>
<p>Note &#8211; in general each constraint should have a similar name to the index used to support the constraint.</p></blockquote>
</blockquote>
<p><strong>Other Fields</strong></p>
<blockquote>
<blockquote><p>Without getting carried away, you can also apply a suffix to non key fields where this is helpful in describing the type of data being stored.<br />
e.g.<br />
A column used to store boolean (Yes/No) values can be given a _yn suffix: retired<strong>_yn</strong>, superuser <strong>_yn</strong>, driver<strong><strong>_yn</strong></strong></p>
<p>In lookup tables an easy way to identify the main text field is to name it as a singular version of the tablename<br />
e.g.</p>
<pre>asset_types.at_asset_type_id_pk   (Primary Key)
asset_types.at_asset_type         (Text field)
asset_types.at_network_yn         (boolean)</pre>
</blockquote>
</blockquote>
<p><strong>SQL</strong></p>
<blockquote>
<blockquote><p>Type all SQL statements in lowercase, being consistent with capitalisation improves the caching of SQL statements. A common variant is to put only SQL keywords in capitals.</p>
<pre>SELECT
   em_employee_id_pk,
   em_employee,
   ab_start_date
FROM
   employees em,
   absences ab
WHERE
   absences.ab_employee_id=employees.em_employee_id_pk;</pre>
<p>You already have a unique prefix worked out for every table, so use the same thing when an ALIAS is required &#8211; this makes the SQL much easier to read.</p>
<p>Always list tables in the FROM / WHERE clause in desired join order &#8211; even with CBO you are giving the Query Optimiser less work to do.</p></blockquote>
</blockquote>
<p><strong>PL/SQL</strong></p>
<blockquote>
<blockquote><p>Prefix scalar variable names with <strong>v_ </strong><br />
Prefix global variables (including host or bind variables) with <strong>g_ </strong><br />
Prefix constants with<strong> c_</strong><br />
Prefix procedure or function call parameters (including sql*plus substitution parameters) with <strong>p_</strong></p>
<p>Prefix record collections with <strong>r_</strong> (alternatively suffix with <strong>_record</strong>)<br />
Prefix %rowtype% collections with <strong>rt_</strong> (alternatively suffix with <strong>_record_type</strong>)</p>
<p>Prefix pl/sql tables with <strong>t_ </strong>(alternatively suffix with <strong>_table</strong>)<br />
Prefix table types with <strong>tt_ </strong>(alternatively suffix with <strong>_table_type</strong>)</p>
<p>Suffix cursors with <strong>_cursor</strong><br />
Prefix exceptions with <strong>e_</strong></p>
<p>If a pl/sql variable is identical to the name of a column in the table Oracle will interpret the name as a column name.</p>
<p><strong>Packages</strong><br />
Prefix package names with PKG_</p>
<p>Write one package for each table &#8211; named PKG_TABLENAME, put all other code that logically belongs to the schema, but not to any particular table in a single Schema package PKG_SCHEMANAME.<br />
If, as is likely, more complex grants are required for different groups of users then create an additional package for each workgroup &#8211; these should contain no code just wrappers that call procedures/functions in the other packages.<br />
This gives a level of separation between the basic code and the user security/grants and makes it easier to change one without breaking the other.</p>
<p>A pl/sql function name like PAYROLL.TAX_RATE the word PAYROLL could refer to either a schema or a package name.</p></blockquote>
</blockquote>
<p><strong>Edit Replace</strong></p>
<blockquote>
<blockquote><p>If you apply a naming convention and then decide to rename something it may be possible to use Edit-Replace to update the associated code. But consider these two fieldnames:</p>
<pre>area_codes.<strong>ac_code</strong>
region_area_codes.r<strong>ac_code</strong></pre>
<p>The columns may be unique but one is a substring of the other!</p></blockquote>
</blockquote>
<p><strong>Instance</strong></p>
<blockquote>
<blockquote><p>Oracle database instance names are limited to eight characters. The first two or three characters of the name should reflect the Application, with the remainder indicating the nature of the instance.<br />
e.g.</p>
<pre>Live instance   SSLive
Test instance   SSTest
Train instance  SSTrain
Data Warehouse  SSdw
Staging Area    SSsa</pre>
</blockquote>
</blockquote>
<p><strong>Data Files</strong></p>
<blockquote>
<blockquote><p>Name Data files so that they identify the instance and the tablespace.</p>
<p>Each filename should end with a two digit numeric value starting with 01, that is incremented by 1 for each new datafile added to the tablespace.<br />
Use the extension &#8220;.dbf&#8221;<br />
e.g.</p>
<pre>SSLive_temp01.dbf
SSLive_rbs01.dbf
SSLive_clinical01.dbf
SSLive_clinical02.dbf</pre>
</blockquote>
</blockquote>
<p><strong>Tablespaces</strong></p>
<blockquote>
<blockquote><p>Avoid naming tablespaces according to time periods.<br />
(Oracle never forgets a tablespace and SMON will scan the list of tablespaces in TS$ every 5 minutes) For a partitioned datawarehouse, try to adopt a strategy of recycling the tablespace names.</p></blockquote>
</blockquote>
<p><strong>Redo Logs</strong></p>
<blockquote>
<blockquote><p>The redo log is a separate file (not in the tablespace)<br />
Name Redo Logs so that they identify the instance, group and member number of the log. Use the extension &#8220;.log&#8221;<br />
e.g.<br />
SSLive_redo_01.log</p>
<p>For more detail on the physical placement of files see Oracle <a href="syntax-ofa.html">Optimal Flexible Architecture</a> (OFA)</p></blockquote>
</blockquote>
<p><strong>Documentation</strong></p>
<blockquote>
<blockquote><p>Lastly &#8211; write and maintain a data dictionary for all data elements &#8211; rather than just dumping the Oracle data dictionary into a text document or an <a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://en.wikipedia.org/wiki/Entity-relationship_model" >Entity relationship diagram</a> &#8211; you should also be defining the <em>business</em> meaning of each data item.</p></blockquote>
</blockquote>
<p><strong>Summary</strong></p>
<blockquote>
<blockquote><p>RDBMS naming conventions can become the subject of endless debate &#8211; here are a few last things to consider:</p>
<p>Does your naming convention make names longer or shorter?</p>
<p>PURCHASE_ORDER_DATE versus PO_DATE</p>
<p>Will you have novice users writing SQL against the database?<br />
If so will they understand the meaning of things like PO_DATE</p>
<p>Is the naming convention documented somewhere that everyone can find? If you arent planning to change it very often, drop the text right into a tableSELECT * from Naming_Conventions;</p></blockquote>
</blockquote>
<p><strong>Related:</strong></p>
<p><a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:6729304326802" >Ask Tom</a> &#8211; Argues against the column prefix<br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://www.oracle-base.com/articles/misc/NamingConventions.php" >Oracle-Base.com Naming conventions</a><br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://philip.greenspun.com/sql/style.html" >SQL convention</a> from Philip Greenspun<br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://www.xoc.net/standards/default.asp" >Reddick Naming convention</a> (hungarian notation)<br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://www.intelligententerprise.com/001205/celko1_1.shtml" >Joe Celko</a> &#8211; 10 Bad Habits<br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://vyaskn.tripod.com/coding_conventions.htm" >SQL Server Naming conventions</a><br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://www.oriolecorp.com/dbcr.html" >Oriolecorp</a> &#8211; File Naming, password conventions<br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://www.wangz.net/pp/sqlformat.htm" >SQL formatter</a> &#8211; Online SQL beautifier<br />
<a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://support.microsoft.com/?kbid=189220" >Q189220</a> &#8211; Microsoft support for spaces in fieldnames<br />
Standard ISO-11179 &#8211; Rules for defining data elements<br />
<a href="syntax-ofa.html">Optimal Flexible Architecture</a> (OFA)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/oracle-naming-conventions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

