<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/styles/XMLStylesDoc_xhtml11_en.xsl"?><!DOCTYPE document SYSTEM "/schemas/XMLStyles10.dtd">
<document xmlns="http://XMLStyles.com/namespaces/styles" xmlns:xst="http://XMLStyles.com/namespaces/styles" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:date="http://exslt.org/dates-and-times">
   <noxml>
      <p xmlns="http://www.w3.org/1999/xhtml"/>
      <!-- You are viewing the source.  The following message should be ignored if you did "View Source" in your browser. -->
      <p xmlns="http://www.w3.org/1999/xhtml">
ATTENTION: XML WEB PAGES NOT SUPPORTED
   If you see this message, your current browser does not support the
   1999 XSLT 1.0 (or later) standard for XML web pages such as this one.
   Please upgrade your browser to a newer version
   that supports 1999 or later standards such as:
      Mozilla Firefox version 1.0.2 or later (GetFirefox -&gt; http://www.GetFirefox.com/)
      Netscape version 8 or later
      Safari version 1.3 or later
      Opera version 9 or later
      Microsoft Internet Explorer (MSIE) version 5 or later
   For further assistance, contact the software vendor for your browser.
   To go to the X<!-- extended HTML -->HTML version of this page click the following link:
<a href="index.html">index.html</a>
      </p>
      <p xmlns="http://www.w3.org/1999/xhtml">&#160;</p>
   </noxml>
   <path>/computers/databases/</path>
   <site>How To Guides</site>
   <logo xlink:type="simple" xlink:href="/images/howtohome.jpg" media="screen" width="240" height="34">How To Guides</logo>
   <logo xlink:type="simple" xlink:href="/images/howtoguides.jpg" media="print" width="240" height="34">How To Guides</logo>
   <logo xlink:type="simple" xlink:href="/images/howtoguidesmobile.jpg" media="handheld" width="150" height="17">How To Guides</logo>
   <navigation where="sections">
      <label>How To Guides</label>
      <link xlink:type="simple" xlink:href="/business/index.xml">Business</link>
      <link xlink:type="simple" xlink:href="/computers/index.xml">Computers</link>
      <link xlink:type="simple" xlink:href="/computers/databases/index.xml">Databases</link>
      <link xlink:type="simple" xlink:href="/internet/index.xml">Internet</link>
      <link xlink:type="simple" xlink:href="/mobile/index.xml">Mobile</link>
      <link xlink:type="simple" xlink:href="/money/index.xml">Money</link>
      <link xlink:type="simple" xlink:href="/movies/index.xml">Movies</link>
      <link xlink:type="simple" xlink:href="/computers/os/index.xml">Operating Systems</link>
   </navigation>
   <navigation where="up">
      <link xlink:type="simple" xlink:href="../">Computers</link>
   </navigation>
   <navigation where="subsections">
      <label>How To Guides for Databases</label>
      <link xlink:type="simple" xlink:href="design.xml">Design</link>
      <link xlink:type="simple" xlink:href="tuning.xml">Tuning</link>
      <link xlink:type="simple" xlink:href="derby/">Apache Derby</link>
      <link xlink:type="simple" xlink:href="ibmdb2/">IBM DB2</link>
      <link xlink:type="simple" xlink:href="mysql/">MySQL</link>
      <link xlink:type="simple" xlink:href="oracle/">Oracle</link>
      <link xlink:type="simple" xlink:href="sqlserver/">SQL Server</link>
   </navigation>
   <section id="body" type="body">
      <pages id="design" name="design">
         <title>Database Design</title>
         <label>Design</label>
         <navigation where="pages">
            <label>Design</label>
            <link xlink:type="simple" xlink:href="design.xml">Summary</link>
            <link xlink:type="simple" xlink:href="charvarchar.xml">CHAR vs. VARCHAR</link>
         </navigation>
         <navigation where="up">
            <link xlink:type="simple" xlink:href="../">Databases</link>
         </navigation>
         <page id="N101E8" name="charvarchar">
            <title>CHAR vs. VARCHAR</title>
            <label>CHAR vs. VARCHAR</label>
            <subpage id="N101EE" name="charpadding">
               <label>Char. Padding</label>
               <h1>Automatic padding of character columns</h1>
               <p>When considering whether to use CHAR or VARCHAR, the decision
                  depends more on programming considerations than on the database.
                  The difference is that comparisons of two fixed character fields will be
                  automatically padded to equal lengths with spaces; comparisons with VARCHAR will not.
               </p>
               <h1>Why not just make everything VARCHAR?</h1>
               <p>In order to save time on the database design, you might be tempted to avoid
                  using CHAR columns and simply use VARCHAR for all character data in the database.
                  As a result, every time one of these columns is compared to a fixed character
                  column or host variable, the programmers will have to use an RPAD or RTRIM function,
                  since programming languages won't do automatic padding on VARCHAR columns.
                  Because the database cannot simply search the values in the index when a
                  <link xlink:type="simple" xlink:href="functions.xml#keycolfn">function is applied to a key column</link>
                  queries may unexpectedly take much longer to complete.
                  So the time that was saved in the design phase will probably be spent many
                  times over trying to determine why certain queries are running so poorly.
               </p>
            </subpage>
            <subpage id="N101FF" name="proglang">
               <label>Prog. Variables</label>
               <h1>Fixed vs. variable length program variables</h1>
               <p>One reason that the VARCHAR data type is often used is that some programmers by habit use variable
                  length character variables, which will also defeat the programming language's
                  automatic padding for fixed-length comparisons.
                  For example, in Java, programmers might simply use <code>String</code>,
                  which is variable length, for all character variables and avoid
                  <code>char[]</code>, which is the appropriate type for storing
                  data being transferred to or from a CHAR column in a database.
               </p>
            </subpage>
            <subpage id="usewhich" name="usewhich">
               <label>Use CHAR or VARCHAR</label>
               <h1>When to use CHAR vs. VARCHAR</h1>
               <p>In database design, we're taught how to determine whether to make a column CHAR or VARCHAR.
                  You ask yourself, "is the length of the value significant?"
                  For example, if the column has up to 6 characters representing a gender,
                  is a 6-character "<code>MALE  </code>" different from a 4-character "<code>MALE</code>"?
                  In this case the answer is obviously "no" and the column should therefore be
                  CHAR so that regardless of whether you compare it to a 4- or 6-character constant
                  or 6-character host variable, the comparison is automatically padded and
                  works properly regardless of whether the column contains "<code>MALE  </code>" or
                  "<code>FEMALE</code>" and whether the constant or host variable contains
                  "<code>MALE</code>", "<code>MALE  </code>" or "<code>FEMALE</code>".
                  If either side of the comparison is a variable length character type, then you will
                  need to RPAD and/or RTRIM one or both sides of the comparison to make it work properly.
               </p>
            </subpage>
            <updated local="2006-11-14">Tuesday November 14, 2006</updated>
         </page>
      </pages>
   </section>
   <copyright>Copyright © 2006 How To Guides .com. Alteration of content, including addition of any function such as hypertext links or pop-up advertising, or interference with the hypertext links or other functions of this site is expressly prohibited.</copyright>
   <disclaimer>All information, links, forms, applications and other items on this site or obtained from it are provided <b>AS IS</b>, WITHOUT WARRANTY OF ANY KIND EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.</disclaimer>
   <ids urchin="UA-779578-2"/>
</document>

