gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

34 lines
4.1KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.18. Domain Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="rangetypes.html" title="8.17. Range Types" /><link rel="next" href="datatype-oid.html" title="8.19. Object Identifier Types" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.18. Domain Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="rangetypes.html" title="8.17. Range Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-oid.html" title="8.19. Object Identifier Types">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="DOMAINS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.18. Domain Types</h2></div></div></div><a id="id-1.5.7.26.2" class="indexterm"></a><a id="id-1.5.7.26.3" class="indexterm"></a><p>
  3. A <em class="firstterm">domain</em> is a user-defined data type that is
  4. based on another <em class="firstterm">underlying type</em>. Optionally,
  5. it can have constraints that restrict its valid values to a subset of
  6. what the underlying type would allow. Otherwise it behaves like the
  7. underlying type — for example, any operator or function that
  8. can be applied to the underlying type will work on the domain type.
  9. The underlying type can be any built-in or user-defined base type,
  10. enum type, array type, composite type, range type, or another domain.
  11. </p><p>
  12. For example, we could create a domain over integers that accepts only
  13. positive integers:
  14. </p><pre class="programlisting">
  15. CREATE DOMAIN posint AS integer CHECK (VALUE &gt; 0);
  16. CREATE TABLE mytable (id posint);
  17. INSERT INTO mytable VALUES(1); -- works
  18. INSERT INTO mytable VALUES(-1); -- fails
  19. </pre><p>
  20. </p><p>
  21. When an operator or function of the underlying type is applied to a
  22. domain value, the domain is automatically down-cast to the underlying
  23. type. Thus, for example, the result of <code class="literal">mytable.id - 1</code>
  24. is considered to be of type <code class="type">integer</code> not <code class="type">posint</code>.
  25. We could write <code class="literal">(mytable.id - 1)::posint</code> to cast the
  26. result back to <code class="type">posint</code>, causing the domain's constraints
  27. to be rechecked. In this case, that would result in an error if the
  28. expression had been applied to an <code class="structfield">id</code> value of
  29. 1. Assigning a value of the underlying type to a field or variable of
  30. the domain type is allowed without writing an explicit cast, but the
  31. domain's constraints will be checked.
  32. </p><p>
  33. For additional information see <a class="xref" href="sql-createdomain.html" title="CREATE DOMAIN"><span class="refentrytitle">CREATE DOMAIN</span></a>.
  34. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="rangetypes.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-oid.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.17. Range Types </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 8.19. Object Identifier Types</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1