gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
9.7KB

  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>TRUNCATE</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="sql-start-transaction.html" title="START TRANSACTION" /><link rel="next" href="sql-unlisten.html" title="UNLISTEN" /></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">TRUNCATE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-start-transaction.html" title="START TRANSACTION">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</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="sql-unlisten.html" title="UNLISTEN">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-TRUNCATE"><div class="titlepage"></div><a id="id-1.9.3.180.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">TRUNCATE</span></h2><p>TRUNCATE — empty a table or set of tables</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. TRUNCATE [ TABLE ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ] [, ... ]
  4. [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
  5. </pre></div><div class="refsect1" id="id-1.9.3.180.5"><h2>Description</h2><p>
  6. <code class="command">TRUNCATE</code> quickly removes all rows from a set of
  7. tables. It has the same effect as an unqualified
  8. <code class="command">DELETE</code> on each table, but since it does not actually
  9. scan the tables it is faster. Furthermore, it reclaims disk space
  10. immediately, rather than requiring a subsequent <code class="command">VACUUM</code>
  11. operation. This is most useful on large tables.
  12. </p></div><div class="refsect1" id="id-1.9.3.180.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  13. The name (optionally schema-qualified) of a table to truncate.
  14. If <code class="literal">ONLY</code> is specified before the table name, only that table
  15. is truncated. If <code class="literal">ONLY</code> is not specified, the table and all
  16. its descendant tables (if any) are truncated. Optionally, <code class="literal">*</code>
  17. can be specified after the table name to explicitly indicate that
  18. descendant tables are included.
  19. </p></dd><dt><span class="term"><code class="literal">RESTART IDENTITY</code></span></dt><dd><p>
  20. Automatically restart sequences owned by columns of
  21. the truncated table(s).
  22. </p></dd><dt><span class="term"><code class="literal">CONTINUE IDENTITY</code></span></dt><dd><p>
  23. Do not change the values of sequences. This is the default.
  24. </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
  25. Automatically truncate all tables that have foreign-key references
  26. to any of the named tables, or to any tables added to the group
  27. due to <code class="literal">CASCADE</code>.
  28. </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
  29. Refuse to truncate if any of the tables have foreign-key references
  30. from tables that are not listed in the command. This is the default.
  31. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.180.7"><h2>Notes</h2><p>
  32. You must have the <code class="literal">TRUNCATE</code> privilege on a table
  33. to truncate it.
  34. </p><p>
  35. <code class="command">TRUNCATE</code> acquires an <code class="literal">ACCESS EXCLUSIVE</code> lock on each
  36. table it operates on, which blocks all other concurrent operations
  37. on the table. When <code class="literal">RESTART IDENTITY</code> is specified, any
  38. sequences that are to be restarted are likewise locked exclusively.
  39. If concurrent access to a table is required, then
  40. the <code class="command">DELETE</code> command should be used instead.
  41. </p><p>
  42. <code class="command">TRUNCATE</code> cannot be used on a table that has foreign-key
  43. references from other tables, unless all such tables are also truncated
  44. in the same command. Checking validity in such cases would require table
  45. scans, and the whole point is not to do one. The <code class="literal">CASCADE</code>
  46. option can be used to automatically include all dependent tables —
  47. but be very careful when using this option, or else you might lose data you
  48. did not intend to!
  49. Note in particular that when the table to be truncated is a partition,
  50. siblings partitions are left untouched, but cascading occurs to all
  51. referencing tables and all their partitions with no distinction.
  52. </p><p>
  53. <code class="command">TRUNCATE</code> will not fire any <code class="literal">ON DELETE</code>
  54. triggers that might exist for the tables. But it will fire
  55. <code class="literal">ON TRUNCATE</code> triggers.
  56. If <code class="literal">ON TRUNCATE</code> triggers are defined for any of
  57. the tables, then all <code class="literal">BEFORE TRUNCATE</code> triggers are
  58. fired before any truncation happens, and all <code class="literal">AFTER
  59. TRUNCATE</code> triggers are fired after the last truncation is
  60. performed and any sequences are reset.
  61. The triggers will fire in the order that the tables are
  62. to be processed (first those listed in the command, and then any
  63. that were added due to cascading).
  64. </p><p>
  65. <code class="command">TRUNCATE</code> is not MVCC-safe. After truncation, the table will
  66. appear empty to concurrent transactions, if they are using a snapshot
  67. taken before the truncation occurred.
  68. See <a class="xref" href="mvcc-caveats.html" title="13.5. Caveats">Section 13.5</a> for more details.
  69. </p><p>
  70. <code class="command">TRUNCATE</code> is transaction-safe with respect to the data
  71. in the tables: the truncation will be safely rolled back if the surrounding
  72. transaction does not commit.
  73. </p><p>
  74. When <code class="literal">RESTART IDENTITY</code> is specified, the implied
  75. <code class="command">ALTER SEQUENCE RESTART</code> operations are also done
  76. transactionally; that is, they will be rolled back if the surrounding
  77. transaction does not commit. This is unlike the normal behavior of
  78. <code class="command">ALTER SEQUENCE RESTART</code>. Be aware that if any additional
  79. sequence operations are done on the restarted sequences before the
  80. transaction rolls back, the effects of these operations on the sequences
  81. will be rolled back, but not their effects on <code class="function">currval()</code>;
  82. that is, after the transaction <code class="function">currval()</code> will continue to
  83. reflect the last sequence value obtained inside the failed transaction,
  84. even though the sequence itself may no longer be consistent with that.
  85. This is similar to the usual behavior of <code class="function">currval()</code> after
  86. a failed transaction.
  87. </p><p>
  88. <code class="command">TRUNCATE</code> is not currently supported for foreign tables.
  89. This implies that if a specified table has any descendant tables that are
  90. foreign, the command will fail.
  91. </p></div><div class="refsect1" id="id-1.9.3.180.8"><h2>Examples</h2><p>
  92. Truncate the tables <code class="literal">bigtable</code> and
  93. <code class="literal">fattable</code>:
  94. </p><pre class="programlisting">
  95. TRUNCATE bigtable, fattable;
  96. </pre><p>
  97. </p><p>
  98. The same, and also reset any associated sequence generators:
  99. </p><pre class="programlisting">
  100. TRUNCATE bigtable, fattable RESTART IDENTITY;
  101. </pre><p>
  102. </p><p>
  103. Truncate the table <code class="literal">othertable</code>, and cascade to any tables
  104. that reference <code class="literal">othertable</code> via foreign-key
  105. constraints:
  106. </p><pre class="programlisting">
  107. TRUNCATE othertable CASCADE;
  108. </pre></div><div class="refsect1" id="id-1.9.3.180.9"><h2>Compatibility</h2><p>
  109. The SQL:2008 standard includes a <code class="command">TRUNCATE</code> command
  110. with the syntax <code class="literal">TRUNCATE TABLE
  111. <em class="replaceable"><code>tablename</code></em></code>. The clauses
  112. <code class="literal">CONTINUE IDENTITY</code>/<code class="literal">RESTART IDENTITY</code>
  113. also appear in that standard, but have slightly different though related
  114. meanings. Some of the concurrency behavior of this command is left
  115. implementation-defined by the standard, so the above notes should be
  116. considered and compared with other implementations if necessary.
  117. </p></div><div class="refsect1" id="id-1.9.3.180.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-delete.html" title="DELETE"><span class="refentrytitle">DELETE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-start-transaction.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-unlisten.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">START TRANSACTION </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> UNLISTEN</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1