doc-exports/docs/dws/dev/dws_06_0225.html
Lu, Huayi ef0ada5a59 DWS DEV 20240716 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2024-11-02 09:07:47 +00:00

41 lines
9.2 KiB
HTML

<a name="EN-US_TOPIC_0000001460881028"></a><a name="EN-US_TOPIC_0000001460881028"></a>
<h1 class="topictitle1">TRUNCATE</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001460881028__sbd36550b75f74804baacfa83e897796e"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001460881028__p9350410219">Quickly removes all rows from a database table.</p>
<p id="EN-US_TOPIC_0000001460881028__af57d7228990f4842beafa608dd6b125d"><strong id="EN-US_TOPIC_0000001460881028__b11200142917191">TRUNCATE</strong> has the same effect as an unqualified <strong id="EN-US_TOPIC_0000001460881028__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_0000001460881028__section5258164117111"><h4 class="sectiontitle">Precautions</h4><p id="EN-US_TOPIC_0000001460881028__p1364125272410">Exercise caution when running the <strong id="EN-US_TOPIC_0000001460881028__b058784911135">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_0000001460881028__b205757524136">TRUNCATE TABLE</strong> statement to delete table data, the data cannot be restored.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001460881028__section8536131932413"><h4 class="sectiontitle">TRUNCATE TABLE</h4><ul id="EN-US_TOPIC_0000001460881028__ul878427856"><li id="EN-US_TOPIC_0000001460881028__li578417717511"><strong id="EN-US_TOPIC_0000001460881028__b1162765335815">TRUNCATE TABLE</strong> has the same function as a <strong id="EN-US_TOPIC_0000001460881028__b8627145318582">DELETE</strong> statement with no <strong id="EN-US_TOPIC_0000001460881028__b14627185315815">WHERE</strong> clause, emptying a table.</li><li id="EN-US_TOPIC_0000001460881028__li61083133514"><strong id="EN-US_TOPIC_0000001460881028__b7456083144625">TRUNCATE TABLE</strong> uses less system and transaction log resources as compared with <strong id="EN-US_TOPIC_0000001460881028__b67104755144625">DELETE</strong>.<ul id="EN-US_TOPIC_0000001460881028__ul102061210627"><li id="EN-US_TOPIC_0000001460881028__li1620616101527"><strong id="EN-US_TOPIC_0000001460881028__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_0000001460881028__li1720711108217"><strong id="EN-US_TOPIC_0000001460881028__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_0000001460881028__li1465718241155">The differences between <strong id="EN-US_TOPIC_0000001460881028__b2072610391807">TRUNCATE</strong>, <strong id="EN-US_TOPIC_0000001460881028__b1972619391202">DELETE</strong>, and <strong id="EN-US_TOPIC_0000001460881028__b972633912013">DROP</strong> are as follows:<ul id="EN-US_TOPIC_0000001460881028__ul11208110828"><li id="EN-US_TOPIC_0000001460881028__li1220914101523"><strong id="EN-US_TOPIC_0000001460881028__b59296540015">TRUNCATE TABLE</strong> deletes content, releases space, but does not delete definitions.</li><li id="EN-US_TOPIC_0000001460881028__li121021014220"><strong id="EN-US_TOPIC_0000001460881028__b169751232017">DELETE TABLE</strong> deletes content, but does not delete definitions nor release space.</li><li id="EN-US_TOPIC_0000001460881028__li421116102219"><strong id="EN-US_TOPIC_0000001460881028__b660071211117">DROP TABLE</strong> deletes content and definitions, and releases space.</li></ul>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001460881028__se1a57ccfec154817927056d958d32929"><h4 class="sectiontitle">Syntax</h4><p id="EN-US_TOPIC_0000001460881028__p16616111711568"><strong id="EN-US_TOPIC_0000001460881028__en-us_topic_0058965900_b84235270616652">TRUNCATE</strong> empties a table or set of tables.</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460881028__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>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001460881028__s76d09d017f8345e28254aa6f4539e4f1"><h4 class="sectiontitle">Parameter Description</h4><ul id="EN-US_TOPIC_0000001460881028__ub0a19e36b4fc4d188f964a07b16011c3"><li id="EN-US_TOPIC_0000001460881028__l36ce209efdb44eed9139ddf63c1bbb9d"><strong id="EN-US_TOPIC_0000001460881028__a67fdef64a20b4d5bab69b7e0977ce620">ONLY</strong><p id="EN-US_TOPIC_0000001460881028__a54541e4a7b65413fb0119cdd67091633">If <strong id="EN-US_TOPIC_0000001460881028__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_0000001460881028__li6459344161615"><strong id="EN-US_TOPIC_0000001460881028__a403f72158c71455fab293a3f2a592521">database_name</strong><p id="EN-US_TOPIC_0000001460881028__p1459184431613">Database name of the target table</p>
</li><li id="EN-US_TOPIC_0000001460881028__li117865291617"><strong id="EN-US_TOPIC_0000001460881028__a77f54bb1d5f84a3b8c05302f3f958438">schema_name</strong><p id="EN-US_TOPIC_0000001460881028__p1079652111617">Schema name of the target table</p>
</li><li id="EN-US_TOPIC_0000001460881028__l69c398319e694324a3255932abb82529"><strong id="EN-US_TOPIC_0000001460881028__ad2716b2a5d2643d0b4cb1bf4bb5efad8">table_name</strong><p id="EN-US_TOPIC_0000001460881028__a0441a7adc34f4aa8b91d484e58c1f099">Specifies the name (optionally schema-qualified) of a target table.</p>
<p id="EN-US_TOPIC_0000001460881028__a36667360fc6c42e698a84ad68e4bf291">Value range: an existing table name</p>
</li><li id="EN-US_TOPIC_0000001460881028__l22125bd36a364a778f0e73057f914ac3"><strong id="EN-US_TOPIC_0000001460881028__afd83560c78e94943aefaaa26821bfe5f">CONTINUE IDENTITY</strong><p id="EN-US_TOPIC_0000001460881028__aba2be56ff98241168dbb279766692a79">Does not change the values of sequences. This is the default.</p>
</li><li id="EN-US_TOPIC_0000001460881028__ladf0dd789db941f1bf9774b4b4ad6f30"><strong id="EN-US_TOPIC_0000001460881028__a5f9f7c3da3064230b3d456968c0b24e6">CASCADE | RESTRICT</strong><ul id="EN-US_TOPIC_0000001460881028__u920967ea646b490fbe752eaea52f73d3"><li id="EN-US_TOPIC_0000001460881028__l5cb8183c4e294a509003ef2c88501a58"><strong id="EN-US_TOPIC_0000001460881028__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_0000001460881028__b100851575733626">CASCADE</strong>.</li><li id="EN-US_TOPIC_0000001460881028__l01eaac29733f4212b161fe35c0256581"><strong id="EN-US_TOPIC_0000001460881028__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_0000001460881028__s7d61ee022bd94b699b6ea6f2b8da713b"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001460881028__p14593204213503">Clear a partitioned table:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460881028__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>