⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datatype.sgml

📁 PostgreSQL7.4.6 for Linux
💻 SGML
📖 第 1 页 / 共 5 页
字号:
    </indexterm>    <indexterm>     <primary>int4</primary>     <see>integer</see>    </indexterm>    <indexterm>     <primary>int2</primary>     <see>smallint</see>    </indexterm>    <indexterm>     <primary>int8</primary>     <see>bigint</see>    </indexterm>    <para>     The types <type>smallint</type>, <type>integer</type>, and     <type>bigint</type> store whole numbers, that is, numbers without     fractional components, of various ranges.  Attempts to store     values outside of the allowed range will result in an error.    </para>    <para>     The type <type>integer</type> is the usual choice, as it offers     the best balance between range, storage size, and performance.     The <type>smallint</type> type is generally only used if disk     space is at a premium.  The <type>bigint</type> type should only     be used if the <type>integer</type> range is not sufficient,     because the latter is definitely faster.    </para>    <para>     The <type>bigint</type> type may not function correctly on all     platforms, since it relies on compiler support for eight-byte     integers.  On a machine without such support, <type>bigint</type>     acts the same as <type>integer</type> (but still takes up eight     bytes of storage).  However, we are not aware of any reasonable     platform where this is actually the case.    </para>    <para>     <acronym>SQL</acronym> only specifies the integer types     <type>integer</type> (or <type>int</type>) and     <type>smallint</type>.  The type <type>bigint</type>, and the     type names <type>int2</type>, <type>int4</type>, and     <type>int8</type> are extensions, which are shared with various     other <acronym>SQL</acronym> database systems.    </para>    <note>     <para>      If you have a column of type <type>smallint</type> or      <type>bigint</type> with an index, you may encounter problems      getting the system to use that index.  For instance, a clause of      the form<programlisting>... WHERE smallint_column = 42</programlisting>      will not use an index, because the system assigns type      <type>integer</type> to the constant 42, and      <productname>PostgreSQL</productname> currently      cannot use an index when two different data types are involved.  A      workaround is to single-quote the constant, thus:<programlisting>... WHERE smallint_column = '42'</programlisting>      This will cause the system to delay type resolution and will      assign the right type to the constant.     </para>    </note>   </sect2>   <sect2 id="datatype-numeric-decimal">    <title>Arbitrary Precision Numbers</title>    <indexterm zone="datatype-numeric-decimal">     <primary>numeric (data type)</primary>    </indexterm>    <indexterm>     <primary>decimal</primary>     <see>numeric</see>    </indexterm>    <para>     The type <type>numeric</type> can store numbers with up to 1000     digits of precision and perform calculations exactly. It is     especially recommended for storing monetary amounts and other     quantities where exactness is required. However, the     <type>numeric</type> type is very slow compared to the     floating-point types described in the next section.    </para>    <para>     In what follows we use these terms:  The     <firstterm>scale</firstterm> of a <type>numeric</type> is the     count of decimal digits in the fractional part, to the right of     the decimal point.  The <firstterm>precision</firstterm> of a     <type>numeric</type> is the total count of significant digits in     the whole number, that is, the number of digits to both sides of     the decimal point.  So the number 23.5141 has a precision of 6     and a scale of 4.  Integers can be considered to have a scale of     zero.    </para>    <para>     Both the precision and the scale of the numeric type can be     configured.  To declare a column of type <type>numeric</type> use     the syntax<programlisting>NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)</programlisting>     The precision must be positive, the scale zero or positive.     Alternatively,<programlisting>NUMERIC(<replaceable>precision</replaceable>)</programlisting>     selects a scale of 0.  Specifying<programlisting>NUMERIC</programlisting>     without any precision or scale creates a column in which numeric     values of any precision and scale can be stored, up to the     implementation limit on precision.  A column of this kind will     not coerce input values to any particular scale, whereas     <type>numeric</type> columns with a declared scale will coerce     input values to that scale.  (The <acronym>SQL</acronym> standard     requires a default scale of 0, i.e., coercion to integer     precision.  We find this a bit useless.  If you're concerned     about portability, always specify the precision and scale     explicitly.)    </para>    <para>     If the precision or scale of a value is greater than the declared     precision or scale of a column, the system will attempt to round     the value.  If the value cannot be rounded so as to satisfy the     declared limits, an error is raised.    </para>    <para>     The types <type>decimal</type> and <type>numeric</type> are     equivalent.  Both types are part of the <acronym>SQL</acronym>     standard.    </para>   </sect2>   <sect2 id="datatype-float">    <title>Floating-Point Types</title>    <indexterm zone="datatype-float">     <primary>real</primary>    </indexterm>    <indexterm zone="datatype-float">     <primary>double precision</primary>    </indexterm>    <indexterm>     <primary>float4</primary>     <see>real</see>    </indexterm>    <indexterm>     <primary>float8</primary>     <see>double precision</see>    </indexterm>    <indexterm zone="datatype-float">     <primary>floating point</primary>    </indexterm>    <para>     The data types <type>real</type> and <type>double     precision</type> are inexact, variable-precision numeric types.     In practice, these types are usually implementations of     <acronym>IEEE</acronym> Standard 754 for Binary Floating-Point     Arithmetic (single and double precision, respectively), to the     extent that the underlying processor, operating system, and     compiler support it.    </para>    <para>     Inexact means that some values cannot be converted exactly to the     internal format and are stored as approximations, so that storing     and printing back out a value may show slight discrepancies.     Managing these errors and how they propagate through calculations     is the subject of an entire branch of mathematics and computer     science and will not be discussed further here, except for the     following points:     <itemizedlist>      <listitem>       <para>        If you require exact storage and calculations (such as for        monetary amounts), use the <type>numeric</type> type instead.       </para>      </listitem>      <listitem>       <para>        If you want to do complicated calculations with these types        for anything important, especially if you rely on certain        behavior in boundary cases (infinity, underflow), you should        evaluate the implementation carefully.       </para>      </listitem>      <listitem>       <para>        Comparing two floating-point values for equality may or may        not work as expected.       </para>      </listitem>     </itemizedlist>    </para>    <para>     On most platforms, the <type>real</type> type has a range of at least     1E-37 to 1E+37 with a precision of at least 6 decimal digits.  The     <type>double precision</type> type typically has a range of around     1E-307 to 1E+308 with a precision of at least 15 digits.  Values that     are too large or too small will cause an error.  Rounding may     take place if the precision of an input number is too high.     Numbers too close to zero that are not representable as distinct     from zero will cause an underflow error.    </para>    <para>     <productname>PostgreSQL</productname> also supports the SQL-standard     notations <type>float</type> and     <type>float(<replaceable>p</replaceable>)</type> for specifying     inexact numeric types.  Here, <replaceable>p</replaceable> specifies     the minimum acceptable precision in binary digits.     <productname>PostgreSQL</productname> accepts      <type>float(1)</type> to <type>float(24)</type> as selecting the     <type>real</type> type, while      <type>float(25)</type> to <type>float(53)</type> select     <type>double precision</type>.  Values of <replaceable>p</replaceable>     outside the allowed range draw an error.     <type>float</type> with no precision specified is taken to mean     <type>double precision</type>.    </para>    <note>     <para>      Prior to <productname>PostgreSQL</productname> 7.4, the precision in      <type>float(<replaceable>p</replaceable>)</type> was taken to mean      so many decimal digits.  This has been corrected to match the SQL      standard, which specifies that the precision is measured in binary      digits.  The assumption that <type>real</type> and      <type>double precision</type> have exactly 24 and 53 bits in the      mantissa respectively is correct for IEEE-standard floating point      implementations.  On non-IEEE platforms it may be off a little, but      for simplicity the same ranges of <replaceable>p</replaceable> are used      on all platforms.     </para>    </note>   </sect2>   <sect2 id="datatype-serial">    <title>Serial Types</title>    <indexterm zone="datatype-serial">     <primary>serial</primary>    </indexterm>    <indexterm zone="datatype-serial">     <primary>bigserial</primary>    </indexterm>    <indexterm zone="datatype-serial">     <primary>serial4</primary>    </indexterm>    <indexterm zone="datatype-serial">     <primary>serial8</primary>    </indexterm>    <indexterm>     <primary>auto-increment</primary>     <see>serial</see>    </indexterm>    <indexterm>     <primary>sequence</primary>     <secondary>and serial type</secondary>    </indexterm>    <para>     The data types <type>serial</type> and <type>bigserial</type>     are not true types, but merely     a notational convenience for setting up unique identifier columns     (similar to the <literal>AUTO_INCREMENT</literal> property     supported by some other databases). In the current     implementation, specifying<programlisting>CREATE TABLE <replaceable class="parameter">tablename</replaceable> (    <replaceable class="parameter">colname</replaceable> SERIAL);</programlisting>     is equivalent to specifying:<programlisting>CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;CREATE TABLE <replaceable class="parameter">tablename</replaceable> (    <replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') NOT NULL);</programlisting>     Thus, we have created an integer column and arranged for its default     values to be assigned from a sequence generator.  A <literal>NOT NULL</>     constraint is applied to ensure that a null value cannot be explicitly     inserted, either.  In most cases you would also want to attach a     <literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent     duplicate values from being inserted by accident, but this is     not automatic.    </para>    <note>     <para>      Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>      implied <literal>UNIQUE</literal>.  This is no longer automatic.  If      you wish a serial column to be in a unique constraint or a       primary key, it must now be specified, same as with      any other data type.     </para>    </note>    <para>     To insert the next value of the sequence into the <type>serial</type>     column, specify that the <type>serial</type>     column should be assigned its default value. This can be done     either by excluding the column from the list of columns in     the <command>INSERT</command> statement, or through the use of     the <literal>DEFAULT</literal> key word.    </para>    <para>     The type names <type>serial</type> and <type>serial4</type> are     equivalent: both create <type>integer</type> columns.  The type     names <type>bigserial</type> and <type>serial8</type> work just     the same way, except that they create a <type>bigint</type>     column.  <type>bigserial</type> should be used if you anticipate     the use of more than 2<superscript>31</> identifiers over the     lifetime of the table.    </para>    <para>     The sequence created for a <type>serial</type> column is     automatically dropped when the owning column is dropped, and     cannot be dropped otherwise.  (This was not true in     <productname>PostgreSQL</productname> releases before 7.3.  Note     that this automatic drop linkage will not occur for a sequence     created by reloading a dump from a pre-7.3 database; the dump     file does not contain the information needed to establish the     dependency link.) Furthermore, this dependency between sequence     and column is made only for the <type>serial</> column itself; if     any other columns reference the sequence (perhaps by manually     calling the <function>nextval</> function), they will be broken     if the sequence is removed. Using a <type>serial</> column's sequence     in such a fashion is considered bad form; if you wish to feed several     columns from the same sequence generator, create the sequence as an     independent object.    </para>   </sect2>  </sect1>  <sect1 id="datatype-money">   <title>Monetary Types</title>   <note>    <para>     The <type>money</type> type is deprecated. Use     <type>numeric</type> or <type>decimal</type> instead, in     combination with the <function>to_char</function> function.    </para>   </note>   <para>    The <type>money</type> type stores a currency amount with a fixed    fractional precision; see <xref

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -