doc-exports/docs/dws/dev/dws_04_0059.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

53 lines
12 KiB
HTML

<a name="EN-US_TOPIC_0000001145495017"></a><a name="EN-US_TOPIC_0000001145495017"></a>
<h1 class="topictitle1">Schema</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001145495017__a6213d09d1ceb4fcdbb4713aa5eac0bc0">Schemas function as models. Schema management allows multiple users to use the same database without mutual impacts, to organize database objects as manageable logical groups, and to add third-party applications to the same schema without causing conflicts.</p>
<p id="EN-US_TOPIC_0000001145495017__af9fc01f07d9c452e8ec701ab0779911a">Each database has one or more schemas. Each schema contains tables and other types of objects. When a database is created, a schema named <strong id="EN-US_TOPIC_0000001145495017__b050234019431">public</strong> is created by default, and all users have permissions for this schema. You can group database objects by schema. A schema is similar to an OS directory but cannot be nested.</p>
<p id="EN-US_TOPIC_0000001145495017__ad688620dd190447280515535bc37fe6e">The same database object name can be used in different schemas of the same database without causing conflicts. For example, both <strong id="EN-US_TOPIC_0000001145495017__b1512010402459">a_schema</strong> and <strong id="EN-US_TOPIC_0000001145495017__b112811842154510">b_schema</strong> can contain a table named <strong id="EN-US_TOPIC_0000001145495017__b979665724519">mytable</strong>. Users with required permissions can access objects across multiple schemas of the same database.</p>
<p id="EN-US_TOPIC_0000001145495017__aa8ec337a2a1741718cfbde0e195582ff">If a user is created, a schema named after the user will also be created in the current database.</p>
<p id="EN-US_TOPIC_0000001145495017__a4e09fc2cca2e4804823edb42e2e728ab">Database objects are generally created in the first schema in a database search path. For details about the first schema and how to change the schema order, see <a href="#EN-US_TOPIC_0000001145495017__s54bcf3a1631c42058bbbe25df4b8523b">Search Path</a>.</p>
<div class="section" id="EN-US_TOPIC_0000001145495017__sb4d9c2e675ab4e8d9d57f896c9e4823e"><h4 class="sectiontitle">Creating, Modifying, and Deleting Schemas</h4><ul id="EN-US_TOPIC_0000001145495017__u715e78514c8942b9b47a132292c687ea"><li id="EN-US_TOPIC_0000001145495017__lcdbbd3e6b4b34adb9f1c4ae8b32272e5">To create a schema, use <strong id="EN-US_TOPIC_0000001145495017__b02741451784">CREATE SCHEMA</strong>. Any user can create a schema.</li><li id="EN-US_TOPIC_0000001145495017__lbd8983740cec41f0ac03ec4fc2681dd5">To change the name or owner of a schema, use <strong id="EN-US_TOPIC_0000001145495017__b41468184811">ALTER SCHEMA</strong>. Only the schema owner can do so.</li><li id="EN-US_TOPIC_0000001145495017__l0935434e5be94f74b874d80ba28517a7">To delete a schema and its objects, use <strong id="EN-US_TOPIC_0000001145495017__b1528412271684">DROP SCHEMA</strong>. Only the schema owner can do so.</li><li id="EN-US_TOPIC_0000001145495017__l9e8c3ba62b5e4f98a00dca8bb19144fc">To create a table in a schema, use the <em id="EN-US_TOPIC_0000001145495017__i8878171810501">schema_name</em><strong id="EN-US_TOPIC_0000001145495017__b18592112145013">.</strong><em id="EN-US_TOPIC_0000001145495017__i19041523175019">table_name</em> format to specify the table. If <em id="EN-US_TOPIC_0000001145495017__i206971627115015">schema_name</em> is not specified, the table will be created in the first schema in <a href="#EN-US_TOPIC_0000001145495017__s54bcf3a1631c42058bbbe25df4b8523b">Search Path</a>.</li><li id="EN-US_TOPIC_0000001145495017__l7f2438b17b424139bf02c49f7e947597">To view the owner of a schema, perform the following join query on the system catalogs <strong id="EN-US_TOPIC_0000001145495017__b263316289512">PG_NAMESPACE</strong> and <strong id="EN-US_TOPIC_0000001145495017__b1569503118513">PG_USER</strong>. Replace <em id="EN-US_TOPIC_0000001145495017__i9401135125110">schema_name</em> in the statement with the name of the schema to be queried.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001145495017__s0986197b2f4e4d5ea8265f4930ed218e"><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">SELECT</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">nspname</span><span class="p">,</span><span class="n">u</span><span class="p">.</span><span class="n">usename</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">nspowner</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">pg_namespace</span><span class="w"> </span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="n">pg_user</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">nspname</span><span class="o">=</span><span class="s1">'schema_name'</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">nspowner</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">u</span><span class="p">.</span><span class="n">usesysid</span><span class="p">;</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001145495017__lecaae9ea66d64072b1ab715073698836">To view a list of all schemas, query the system catalog <strong id="EN-US_TOPIC_0000001145495017__b181191213165214">PG_NAMESPACE</strong>.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001145495017__s940ec357a54f4cb7b89d609e8408fe7c"><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">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">pg_namespace</span><span class="p">;</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001145495017__l22931b8d595944e0a42a778fd3c64b37">To view a list of tables in a schema, query the system catalog <strong id="EN-US_TOPIC_0000001145495017__b771110805316">PG_TABLES</strong>. For example, the following query will return a table list from <strong id="EN-US_TOPIC_0000001145495017__b7110851105312">PG_CATALOG</strong> in the schema.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001145495017__s9de64766691d4cbbab0d430b3242d618"><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">SELECT</span><span class="w"> </span><span class="k">distinct</span><span class="p">(</span><span class="n">tablename</span><span class="p">),</span><span class="n">schemaname</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">pg_tables</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">schemaname</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'pg_catalog'</span><span class="p">;</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001145495017__s54bcf3a1631c42058bbbe25df4b8523b"><a name="EN-US_TOPIC_0000001145495017__s54bcf3a1631c42058bbbe25df4b8523b"></a><a name="s54bcf3a1631c42058bbbe25df4b8523b"></a><h4 class="sectiontitle">Search Path</h4><p id="EN-US_TOPIC_0000001145495017__en-us_topic_0156599225_p331913481347">A search path is defined in the <a href="dws_04_0925.html#EN-US_TOPIC_0000001145894759__sc0cb61109de045049d5802f668be2392">search_path</a> parameter. The parameter value is a list of schema names separated by commas (,). If no target schema is specified during object creation, the object will be added to the first schema listed in the search path. If there are objects with the same name across different schemas and no schema is specified for an object query, the object will be returned from the first schema containing the object in the search path.</p>
<ul id="EN-US_TOPIC_0000001145495017__u642fe49bc8c248128bac1e22fcbd9b91"><li id="EN-US_TOPIC_0000001145495017__en-us_topic_0156599225_li86784213224">To view the current search path, use <strong id="EN-US_TOPIC_0000001145495017__b119943341284">SHOW</strong>.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001145495017__sf1f71b75227f4932bf93778630cf4336"><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></pre></div></td><td class="code"><div><pre><span></span><span class="k">SHOW</span><span class="w"> </span><span class="n">SEARCH_PATH</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="n">search_path</span><span class="w"></span>
<span class="c1">----------------</span>
<span class="w"> </span><span class="ss">&quot;$user&quot;</span><span class="p">,</span><span class="k">public</span><span class="w"></span>
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001145495017__en-us_topic_0156599225_p118434120229">The default value of <strong id="EN-US_TOPIC_0000001145495017__b7983546315">search_path</strong> is <strong id="EN-US_TOPIC_0000001145495017__b23471421625">"$user",public</strong>. <strong id="EN-US_TOPIC_0000001145495017__b13918851126">$user</strong> indicates the name of the schema with the same name as the current session user. If the schema does not exist, <strong id="EN-US_TOPIC_0000001145495017__b76691039123">$user</strong> will be ignored. By default, after a user connects to a database that has schemas with the same name, objects will be added to all the schemas. If there are no such schemas, objects will be added to only to the <strong id="EN-US_TOPIC_0000001145495017__b13819232547">public</strong> schema.</p>
</li><li id="EN-US_TOPIC_0000001145495017__l82eda2461e1147e29a22769cbfcf295d">To change the default schema of the current session, run the <strong id="EN-US_TOPIC_0000001145495017__b65015461948">SET</strong> statement.<div class="p" id="EN-US_TOPIC_0000001145495017__a59279eef391a4566b8877de9a9b478f0">Run the following command to set <strong id="EN-US_TOPIC_0000001145495017__en-us_topic_0058967624_b842352706111029">search_path</strong> to <strong id="EN-US_TOPIC_0000001145495017__en-us_topic_0058967624_b842352706111038">myschema</strong> and <strong id="EN-US_TOPIC_0000001145495017__en-us_topic_0058967624_b842352706111041">public</strong> (<strong id="EN-US_TOPIC_0000001145495017__en-us_topic_0058967624_b842352706111047">myschema</strong> will be searched first):<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001145495017__s8a42b7c88f2a4805860be27977bacc7e"><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">SET</span><span class="w"> </span><span class="n">SEARCH_PATH</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="n">myschema</span><span class="p">,</span><span class="w"> </span><span class="k">public</span><span class="p">;</span><span class="w"></span>
<span class="k">SET</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</div>
</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0053.html">Managing Users and Their Permissions</a></div>
</div>
</div>