Files
doc-exports/docs/dws/dev/dws_06_0225.html
luhuayi 177cd61a57 DWS DEVG 910.211 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: luhuayi <luhuayi@huawei.com>
Co-committed-by: luhuayi <luhuayi@huawei.com>
2025-05-05 07:44:03 +00:00

45 lines
11 KiB
HTML

<a name="EN-US_TOPIC_0000001811634849"></a><a name="EN-US_TOPIC_0000001811634849"></a>
<h1 class="topictitle1">TRUNCATE</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001811634849__sbd36550b75f74804baacfa83e897796e"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001811634849__p9350410219"><strong id="EN-US_TOPIC_0000001811634849__b1777225165812">TRUNCATE</strong> quickly removes all rows from a database table.</p>
<p id="EN-US_TOPIC_0000001811634849__af57d7228990f4842beafa608dd6b125d">It has the same effect as an unqualified <strong id="EN-US_TOPIC_0000001811634849__b718613010586">DELETE</strong> on each table, but it is faster since it does not actually scan the tables. This is most useful on large tables.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001811634849__section5258164117111"><h4 class="sectiontitle">Precautions</h4><ul id="EN-US_TOPIC_0000001811634849__ul109914532288"><li id="EN-US_TOPIC_0000001811634849__li89911953142814">Exercise caution when running the <strong id="EN-US_TOPIC_0000001811634849__b15370828193319">TRUNCATE TABLE</strong> statement. Before running this statement, ensure that the table data can be deleted. After you run the <strong id="EN-US_TOPIC_0000001811634849__b1076203412337">TRUNCATE TABLE</strong> statement to delete table data, the data cannot be restored.</li></ul>
<ul id="EN-US_TOPIC_0000001811634849__ul878427856"><li id="EN-US_TOPIC_0000001811634849__li1720841116479">The <strong id="EN-US_TOPIC_0000001811634849__b429211207353">TRUNCATE</strong> operation on global temporary tables only truncates data of the current session. Data of other sessions is not affected.</li><li id="EN-US_TOPIC_0000001811634849__li6172205115217">In the storage-compute decoupling architecture, it is not possible to perform the <strong id="EN-US_TOPIC_0000001811634849__b95405251983">TRUNCATE</strong> operation on common tables and temporary tables simultaneously.</li></ul>
<div class="warning" id="EN-US_TOPIC_0000001811634849__note4981811124813"><span class="warningtitle"><img src="public_sys-resources/warning_3.0-en-us.png"> </span><div class="warningbody"><ul id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_ul7786313114814"><li id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_li491717148480">Avoid performing <strong id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_b14163113262214">ALTER TABLE</strong>, <strong id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_b1216419322228">ALTER TABLE PARTITION</strong>, <strong id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_b11164143210226">DROP PARTITION</strong>, and <strong id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_b1316443222213">TRUNCATE</strong> operations during peak hours to prevent long SQL statements from blocking these operations or SQL services.</li><li id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_li1791717149483">For more information about development and design specifications, see "GaussDB(DWS) Development and Design Proposal" in the <em id="EN-US_TOPIC_0000001811634849__en-us_topic_0000001188588994_i16354420141010">GaussDB(DWS) Developer Guide</em>.</li></ul>
</div></div>
</div>
<div class="section" id="EN-US_TOPIC_0000001811634849__section276852132810"><h4 class="sectiontitle">TRUNCATE TABLE</h4><ul id="EN-US_TOPIC_0000001811634849__ul16406153814283"><li id="EN-US_TOPIC_0000001811634849__li578417717511"><strong id="EN-US_TOPIC_0000001811634849__b1162765335815">TRUNCATE TABLE</strong> has the same function as a <strong id="EN-US_TOPIC_0000001811634849__b8627145318582">DELETE</strong> statement with no <strong id="EN-US_TOPIC_0000001811634849__b14627185315815">WHERE</strong> clause, emptying a table.</li><li id="EN-US_TOPIC_0000001811634849__li61083133514"><strong id="EN-US_TOPIC_0000001811634849__b7456083144625">TRUNCATE TABLE</strong> uses less system and transaction log resources as compared with <strong id="EN-US_TOPIC_0000001811634849__b67104755144625">DELETE</strong>.<ul id="EN-US_TOPIC_0000001811634849__ul102061210627"><li id="EN-US_TOPIC_0000001811634849__li1620616101527"><strong id="EN-US_TOPIC_0000001811634849__b64113529144625">DELETE</strong> deletes a row each time, and records the deletion of each row in the transaction log.</li><li id="EN-US_TOPIC_0000001811634849__li1720711108217"><strong id="EN-US_TOPIC_0000001811634849__b3244231504">TRUNCATE TABLE</strong> deletes all rows in a table by releasing the data page storing the table data, and records the releasing of the data page only in the transaction log.</li></ul>
</li><li id="EN-US_TOPIC_0000001811634849__li345019417284">The differences between <strong id="EN-US_TOPIC_0000001811634849__b2072610391807">TRUNCATE</strong>, <strong id="EN-US_TOPIC_0000001811634849__b1972619391202">DELETE</strong>, and <strong id="EN-US_TOPIC_0000001811634849__b972633912013">DROP</strong> are as follows:<ul id="EN-US_TOPIC_0000001811634849__ul1645074114287"><li id="EN-US_TOPIC_0000001811634849__li1220914101523"><strong id="EN-US_TOPIC_0000001811634849__b59296540015">TRUNCATE TABLE</strong> deletes content, releases space, but does not delete definitions.</li><li id="EN-US_TOPIC_0000001811634849__li121021014220"><strong id="EN-US_TOPIC_0000001811634849__b169751232017">DELETE TABLE</strong> deletes content, but does not delete definitions nor release space.</li><li id="EN-US_TOPIC_0000001811634849__li745184142819"><strong id="EN-US_TOPIC_0000001811634849__b199621241113317">DROP TABLE</strong> deletes content and definitions and releases space.</li></ul>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001811634849__se1a57ccfec154817927056d958d32929"><h4 class="sectiontitle">Syntax</h4><p id="EN-US_TOPIC_0000001811634849__p16616111711568"><strong id="EN-US_TOPIC_0000001811634849__en-us_topic_0058965900_b84235270616652">TRUNCATE</strong> empties a table or set of tables.</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811634849__sb029531e2bec4f23a18ac95c225708dd"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">TRUNCATE</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">ONLY</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="err">{</span><span class="p">[[</span><span class="n">database_name</span><span class="p">.]</span><span class="k">schema_name</span><span class="p">.]</span><span class="k">table_name</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">]</span><span class="err">}</span><span class="w"> </span><span class="p">[,</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">]</span>
<span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">CONTINUE</span><span class="w"> </span><span class="k">IDENTITY</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">CASCADE</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">RESTRICT</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001811634849__s76d09d017f8345e28254aa6f4539e4f1"><h4 class="sectiontitle">Parameter Description</h4><ul id="EN-US_TOPIC_0000001811634849__ub0a19e36b4fc4d188f964a07b16011c3"><li id="EN-US_TOPIC_0000001811634849__l36ce209efdb44eed9139ddf63c1bbb9d"><strong id="EN-US_TOPIC_0000001811634849__a67fdef64a20b4d5bab69b7e0977ce620">ONLY</strong><p id="EN-US_TOPIC_0000001811634849__a54541e4a7b65413fb0119cdd67091633">If <strong id="EN-US_TOPIC_0000001811634849__b84235270694252">ONLY</strong> is specified, only the specified table is cleared. Otherwise, the table and all its subtables (if any) are cleared.</p>
</li><li id="EN-US_TOPIC_0000001811634849__li6459344161615"><strong id="EN-US_TOPIC_0000001811634849__a403f72158c71455fab293a3f2a592521">database_name</strong><p id="EN-US_TOPIC_0000001811634849__p1459184431613">Database name of the target table</p>
</li><li id="EN-US_TOPIC_0000001811634849__li117865291617"><strong id="EN-US_TOPIC_0000001811634849__a77f54bb1d5f84a3b8c05302f3f958438">schema_name</strong><p id="EN-US_TOPIC_0000001811634849__p1079652111617">Schema name of the target table</p>
</li><li id="EN-US_TOPIC_0000001811634849__l69c398319e694324a3255932abb82529"><strong id="EN-US_TOPIC_0000001811634849__ad2716b2a5d2643d0b4cb1bf4bb5efad8">table_name</strong><p id="EN-US_TOPIC_0000001811634849__a0441a7adc34f4aa8b91d484e58c1f099">Specifies the name (optionally schema-qualified) of a target table.</p>
<p id="EN-US_TOPIC_0000001811634849__a36667360fc6c42e698a84ad68e4bf291">Value range: an existing table name</p>
</li><li id="EN-US_TOPIC_0000001811634849__l22125bd36a364a778f0e73057f914ac3"><strong id="EN-US_TOPIC_0000001811634849__afd83560c78e94943aefaaa26821bfe5f">CONTINUE IDENTITY</strong><p id="EN-US_TOPIC_0000001811634849__aba2be56ff98241168dbb279766692a79">Does not change the values of sequences. This is the default.</p>
</li><li id="EN-US_TOPIC_0000001811634849__ladf0dd789db941f1bf9774b4b4ad6f30"><strong id="EN-US_TOPIC_0000001811634849__a5f9f7c3da3064230b3d456968c0b24e6">CASCADE | RESTRICT</strong><ul id="EN-US_TOPIC_0000001811634849__u920967ea646b490fbe752eaea52f73d3"><li id="EN-US_TOPIC_0000001811634849__l5cb8183c4e294a509003ef2c88501a58"><strong id="EN-US_TOPIC_0000001811634849__b842352706161412">CASCADE</strong>: automatically truncates all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to <strong id="EN-US_TOPIC_0000001811634849__b100851575733626">CASCADE</strong>.</li><li id="EN-US_TOPIC_0000001811634849__l01eaac29733f4212b161fe35c0256581"><strong id="EN-US_TOPIC_0000001811634849__b842352706161428">RESTRICT</strong> (default): refuses to truncate if any of the tables have foreign-key references from tables that are not listed in the command.</li></ul>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001811634849__s7d61ee022bd94b699b6ea6f2b8da713b"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001811634849__p14593204213503">Clear a partitioned table:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811634849__screen1736922105312"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">TRUNCATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0118.html">DDL Syntax</a></div>
</div>
</div>