doc-exports/docs/dws/dev/dws_06_0017.html
Lu, Huayi a24ca60074 DWS DEVELOPER 811 version
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2023-01-19 13:37:49 +00:00

77 lines
9.2 KiB
HTML

<a name="EN-US_TOPIC_0000001098830934"></a><a name="EN-US_TOPIC_0000001098830934"></a>
<h1 class="topictitle1">Bit String Types</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001098830934__a9868628d91ca44e5b3644935d31f5e05">Bit strings are strings of 1's and 0's. They can be used to store bit masks.</p>
<p id="EN-US_TOPIC_0000001098830934__af997b2014ffd47209add46388ae97ea6"><span id="EN-US_TOPIC_0000001098830934__text1670543084">GaussDB(DWS)</span> supports two SQL bit types: <strong id="EN-US_TOPIC_0000001098830934__en-us_topic_0058965598_b842352706103333">bit(n)</strong> and <strong id="EN-US_TOPIC_0000001098830934__en-us_topic_0058965598_b842352706103341">bit varying(n)</strong>, where <strong id="EN-US_TOPIC_0000001098830934__en-us_topic_0058965598_b842352706103347">n</strong> is a positive integer.</p>
<p id="EN-US_TOPIC_0000001098830934__abbbd734c9e564b3cac9a092ed8c395fe">The <strong id="EN-US_TOPIC_0000001098830934__en-us_topic_0085032598_b842352706103439">bit</strong> type data must match the length <strong id="EN-US_TOPIC_0000001098830934__en-us_topic_0085032598_b842352706103446">n</strong> exactly. It is an error to attempt to store shorter or longer bit strings. The <strong id="EN-US_TOPIC_0000001098830934__b842352706104137">bit varying</strong> data is of variable length up to the maximum length <strong id="EN-US_TOPIC_0000001098830934__b842352706104214">n</strong>; longer strings will be rejected. Writing <strong id="EN-US_TOPIC_0000001098830934__b842352706104248">bit</strong> without a length is equivalent to <strong id="EN-US_TOPIC_0000001098830934__b842352706104310">bit(1)</strong>, while <strong id="EN-US_TOPIC_0000001098830934__b842352706104328">bit varying</strong> without a length specification means unlimited length.</p>
<div class="note" id="EN-US_TOPIC_0000001098830934__n48cf2cb5f53046888aed0b5cb02c0cfb"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001098830934__ab9da60b095364e7bbcc373593e76dd4a">If one explicitly casts a bit-string value to <strong id="EN-US_TOPIC_0000001098830934__b84235270610454">bit(n)</strong>, it will be truncated or zero-padded on the right to be exactly <strong id="EN-US_TOPIC_0000001098830934__b842352706104549">n</strong> bits, without raising an error.</p>
<p id="EN-US_TOPIC_0000001098830934__a4a4d092fba8f48e58425d0e791330379">Similarly, if one explicitly casts a bit-string value to <strong id="EN-US_TOPIC_0000001098830934__b842352706104630">bit varying(n)</strong>, it will be truncated on the right if it is more than <strong id="EN-US_TOPIC_0000001098830934__b842352706104643">n</strong> bits.</p>
</div></div>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001098830934__s43130560ca6646d1b4ccca311a14e85b"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span>
<span class="normal">24</span>
<span class="normal">25</span>
<span class="normal">26</span>
<span class="normal">27</span>
<span class="normal">28</span>
<span class="normal">29</span></pre></div></td><td class="code"><div><pre><span></span><span class="c1">-- Create a table:</span>
<span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">bit_type_t1</span><span class="w"> </span>
<span class="p">(</span><span class="w"></span>
<span class="w"> </span><span class="n">BT_COL1</span><span class="w"> </span><span class="nb">INTEGER</span><span class="p">,</span><span class="w"></span>
<span class="w"> </span><span class="n">BT_COL2</span><span class="w"> </span><span class="nb">BIT</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span><span class="w"></span>
<span class="w"> </span><span class="n">BT_COL3</span><span class="w"> </span><span class="nb">BIT</span><span class="w"> </span><span class="nb">VARYING</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span><span class="n">DISTRIBUTE</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">REPLICATION</span><span class="p">;</span><span class="w"></span>
<span class="c1">--Insert data:</span>
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">bit_type_t1</span><span class="w"> </span><span class="k">VALUES</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="s1">'101'</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="s1">'00'</span><span class="p">);</span><span class="w"></span>
<span class="c1">-- Specify the type length. An error is reported if an inserted string exceeds this length.</span>
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">bit_type_t1</span><span class="w"> </span><span class="k">VALUES</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="s1">'10'</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="s1">'101'</span><span class="p">);</span><span class="w"></span>
<span class="n">ERROR</span><span class="p">:</span><span class="w"> </span><span class="nb">bit</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="k">length</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="k">not</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="k">type</span><span class="w"> </span><span class="nb">bit</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="w"></span>
<span class="n">CONTEXT</span><span class="p">:</span><span class="w"> </span><span class="n">referenced</span><span class="w"> </span><span class="k">column</span><span class="p">:</span><span class="w"> </span><span class="n">bt_col2</span><span class="w"></span>
<span class="c1">-- Specify the type length. Data is converted if it exceeds this length.</span>
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">bit_type_t1</span><span class="w"> </span><span class="k">VALUES</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="s1">'10'</span><span class="p">::</span><span class="nb">bit</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span><span class="w"> </span><span class="n">B</span><span class="s1">'101'</span><span class="p">);</span><span class="w"></span>
<span class="c1">-- View data:</span>
<span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">bit_type_t1</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="n">bt_col1</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">bt_col2</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">bt_col3</span><span class="w"> </span>
<span class="c1">---------+---------+---------</span>
<span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">101</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">00</span><span class="w"></span>
<span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">100</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">101</span><span class="w"></span>
<span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="k">rows</span><span class="p">)</span><span class="w"></span>
<span class="c1">-- Delete the tables:</span>
<span class="k">DROP</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">bit_type_t1</span><span class="p">;</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0008.html">Data Types</a></div>
</div>
</div>