gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

156 lines
13KB

  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>5.6. Modifying Tables</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="ddl-system-columns.html" title="5.5. System Columns" /><link rel="next" href="ddl-priv.html" title="5.7. Privileges" /></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">5.6. Modifying Tables</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ddl-system-columns.html" title="5.5. System Columns">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><th width="60%" align="center">Chapter 5. Data Definition</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="ddl-priv.html" title="5.7. Privileges">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="DDL-ALTER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.6. Modifying Tables</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ddl-alter.html#DDL-ALTER-ADDING-A-COLUMN">5.6.1. Adding a Column</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#DDL-ALTER-REMOVING-A-COLUMN">5.6.2. Removing a Column</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#DDL-ALTER-ADDING-A-CONSTRAINT">5.6.3. Adding a Constraint</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#DDL-ALTER-REMOVING-A-CONSTRAINT">5.6.4. Removing a Constraint</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#id-1.5.4.8.9">5.6.5. Changing a Column's Default Value</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#id-1.5.4.8.10">5.6.6. Changing a Column's Data Type</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#id-1.5.4.8.11">5.6.7. Renaming a Column</a></span></dt><dt><span class="sect2"><a href="ddl-alter.html#id-1.5.4.8.12">5.6.8. Renaming a Table</a></span></dt></dl></div><a id="id-1.5.4.8.2" class="indexterm"></a><p>
  3. When you create a table and you realize that you made a mistake, or
  4. the requirements of the application change, you can drop the
  5. table and create it again. But this is not a convenient option if
  6. the table is already filled with data, or if the table is
  7. referenced by other database objects (for instance a foreign key
  8. constraint). Therefore <span class="productname">PostgreSQL</span>
  9. provides a family of commands to make modifications to existing
  10. tables. Note that this is conceptually distinct from altering
  11. the data contained in the table: here we are interested in altering
  12. the definition, or structure, of the table.
  13. </p><p>
  14. You can:
  15. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>Add columns</p></li><li class="listitem"><p>Remove columns</p></li><li class="listitem"><p>Add constraints</p></li><li class="listitem"><p>Remove constraints</p></li><li class="listitem"><p>Change default values</p></li><li class="listitem"><p>Change column data types</p></li><li class="listitem"><p>Rename columns</p></li><li class="listitem"><p>Rename tables</p></li></ul></div><p>
  16. All these actions are performed using the
  17. <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>
  18. command, whose reference page contains details beyond those given
  19. here.
  20. </p><div class="sect2" id="DDL-ALTER-ADDING-A-COLUMN"><div class="titlepage"><div><div><h3 class="title">5.6.1. Adding a Column</h3></div></div></div><a id="id-1.5.4.8.5.2" class="indexterm"></a><p>
  21. To add a column, use a command like:
  22. </p><pre class="programlisting">
  23. ALTER TABLE products ADD COLUMN description text;
  24. </pre><p>
  25. The new column is initially filled with whatever default
  26. value is given (null if you don't specify a <code class="literal">DEFAULT</code> clause).
  27. </p><div class="tip"><h3 class="title">Tip</h3><p>
  28. From <span class="productname">PostgreSQL</span> 11, adding a column with
  29. a constant default value no longer means that each row of the table
  30. needs to be updated when the <code class="command">ALTER TABLE</code> statement
  31. is executed. Instead, the default value will be returned the next time
  32. the row is accessed, and applied when the table is rewritten, making
  33. the <code class="command">ALTER TABLE</code> very fast even on large tables.
  34. </p><p>
  35. However, if the default value is volatile (e.g.
  36. <code class="function">clock_timestamp()</code>)
  37. each row will need to be updated with the value calculated at the time
  38. <code class="command">ALTER TABLE</code> is executed. To avoid a potentially
  39. lengthy update operation, particularly if you intend to fill the column
  40. with mostly nondefault values anyway, it may be preferable to add the
  41. column with no default, insert the correct values using
  42. <code class="command">UPDATE</code>, and then add any desired default as described
  43. below.
  44. </p></div><p>
  45. You can also define constraints on the column at the same time,
  46. using the usual syntax:
  47. </p><pre class="programlisting">
  48. ALTER TABLE products ADD COLUMN description text CHECK (description &lt;&gt; '');
  49. </pre><p>
  50. In fact all the options that can be applied to a column description
  51. in <code class="command">CREATE TABLE</code> can be used here. Keep in mind however
  52. that the default value must satisfy the given constraints, or the
  53. <code class="literal">ADD</code> will fail. Alternatively, you can add
  54. constraints later (see below) after you've filled in the new column
  55. correctly.
  56. </p></div><div class="sect2" id="DDL-ALTER-REMOVING-A-COLUMN"><div class="titlepage"><div><div><h3 class="title">5.6.2. Removing a Column</h3></div></div></div><a id="id-1.5.4.8.6.2" class="indexterm"></a><p>
  57. To remove a column, use a command like:
  58. </p><pre class="programlisting">
  59. ALTER TABLE products DROP COLUMN description;
  60. </pre><p>
  61. Whatever data was in the column disappears. Table constraints involving
  62. the column are dropped, too. However, if the column is referenced by a
  63. foreign key constraint of another table,
  64. <span class="productname">PostgreSQL</span> will not silently drop that
  65. constraint. You can authorize dropping everything that depends on
  66. the column by adding <code class="literal">CASCADE</code>:
  67. </p><pre class="programlisting">
  68. ALTER TABLE products DROP COLUMN description CASCADE;
  69. </pre><p>
  70. See <a class="xref" href="ddl-depend.html" title="5.14. Dependency Tracking">Section 5.14</a> for a description of the general
  71. mechanism behind this.
  72. </p></div><div class="sect2" id="DDL-ALTER-ADDING-A-CONSTRAINT"><div class="titlepage"><div><div><h3 class="title">5.6.3. Adding a Constraint</h3></div></div></div><a id="id-1.5.4.8.7.2" class="indexterm"></a><p>
  73. To add a constraint, the table constraint syntax is used. For example:
  74. </p><pre class="programlisting">
  75. ALTER TABLE products ADD CHECK (name &lt;&gt; '');
  76. ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
  77. ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
  78. </pre><p>
  79. To add a not-null constraint, which cannot be written as a table
  80. constraint, use this syntax:
  81. </p><pre class="programlisting">
  82. ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
  83. </pre><p>
  84. </p><p>
  85. The constraint will be checked immediately, so the table data must
  86. satisfy the constraint before it can be added.
  87. </p></div><div class="sect2" id="DDL-ALTER-REMOVING-A-CONSTRAINT"><div class="titlepage"><div><div><h3 class="title">5.6.4. Removing a Constraint</h3></div></div></div><a id="id-1.5.4.8.8.2" class="indexterm"></a><p>
  88. To remove a constraint you need to know its name. If you gave it
  89. a name then that's easy. Otherwise the system assigned a
  90. generated name, which you need to find out. The
  91. <span class="application">psql</span> command <code class="literal">\d
  92. <em class="replaceable"><code>tablename</code></em></code> can be helpful
  93. here; other interfaces might also provide a way to inspect table
  94. details. Then the command is:
  95. </p><pre class="programlisting">
  96. ALTER TABLE products DROP CONSTRAINT some_name;
  97. </pre><p>
  98. (If you are dealing with a generated constraint name like <code class="literal">$2</code>,
  99. don't forget that you'll need to double-quote it to make it a valid
  100. identifier.)
  101. </p><p>
  102. As with dropping a column, you need to add <code class="literal">CASCADE</code> if you
  103. want to drop a constraint that something else depends on. An example
  104. is that a foreign key constraint depends on a unique or primary key
  105. constraint on the referenced column(s).
  106. </p><p>
  107. This works the same for all constraint types except not-null
  108. constraints. To drop a not null constraint use:
  109. </p><pre class="programlisting">
  110. ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
  111. </pre><p>
  112. (Recall that not-null constraints do not have names.)
  113. </p></div><div class="sect2" id="id-1.5.4.8.9"><div class="titlepage"><div><div><h3 class="title">5.6.5. Changing a Column's Default Value</h3></div></div></div><a id="id-1.5.4.8.9.2" class="indexterm"></a><p>
  114. To set a new default for a column, use a command like:
  115. </p><pre class="programlisting">
  116. ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;
  117. </pre><p>
  118. Note that this doesn't affect any existing rows in the table, it
  119. just changes the default for future <code class="command">INSERT</code> commands.
  120. </p><p>
  121. To remove any default value, use:
  122. </p><pre class="programlisting">
  123. ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
  124. </pre><p>
  125. This is effectively the same as setting the default to null.
  126. As a consequence, it is not an error
  127. to drop a default where one hadn't been defined, because the
  128. default is implicitly the null value.
  129. </p></div><div class="sect2" id="id-1.5.4.8.10"><div class="titlepage"><div><div><h3 class="title">5.6.6. Changing a Column's Data Type</h3></div></div></div><a id="id-1.5.4.8.10.2" class="indexterm"></a><p>
  130. To convert a column to a different data type, use a command like:
  131. </p><pre class="programlisting">
  132. ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
  133. </pre><p>
  134. This will succeed only if each existing entry in the column can be
  135. converted to the new type by an implicit cast. If a more complex
  136. conversion is needed, you can add a <code class="literal">USING</code> clause that
  137. specifies how to compute the new values from the old.
  138. </p><p>
  139. <span class="productname">PostgreSQL</span> will attempt to convert the column's
  140. default value (if any) to the new type, as well as any constraints
  141. that involve the column. But these conversions might fail, or might
  142. produce surprising results. It's often best to drop any constraints
  143. on the column before altering its type, and then add back suitably
  144. modified constraints afterwards.
  145. </p></div><div class="sect2" id="id-1.5.4.8.11"><div class="titlepage"><div><div><h3 class="title">5.6.7. Renaming a Column</h3></div></div></div><a id="id-1.5.4.8.11.2" class="indexterm"></a><p>
  146. To rename a column:
  147. </p><pre class="programlisting">
  148. ALTER TABLE products RENAME COLUMN product_no TO product_number;
  149. </pre><p>
  150. </p></div><div class="sect2" id="id-1.5.4.8.12"><div class="titlepage"><div><div><h3 class="title">5.6.8. Renaming a Table</h3></div></div></div><a id="id-1.5.4.8.12.2" class="indexterm"></a><p>
  151. To rename a table:
  152. </p><pre class="programlisting">
  153. ALTER TABLE products RENAME TO items;
  154. </pre><p>
  155. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ddl-system-columns.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ddl-priv.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.5. System Columns </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.7. Privileges</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1