forked from docs/doc-exports
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>
62 lines
15 KiB
HTML
62 lines
15 KiB
HTML
<a name="EN-US_TOPIC_0000001188270502"></a><a name="EN-US_TOPIC_0000001188270502"></a>
|
|
|
|
<h1 class="topictitle1">CREATE SCHEMA</h1>
|
|
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001188270502__s628a72e4ad6147cf9cc089febf125678"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001188270502__en-us_topic_0059777945_p549110246542"><strong id="EN-US_TOPIC_0000001188270502__b842352706173217">CREATE SCHEMA</strong> creates a schema.</p>
|
|
<p id="EN-US_TOPIC_0000001188270502__a0ffd19c6ba5e4023bff33e70ba93367b">Named objects are accessed either by "qualifying" their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s). When creating named objects, you can also use the schema name as a prefix.</p>
|
|
<p id="EN-US_TOPIC_0000001188270502__acfdefd1c708c4c1593590de6f8a742b7">Optionally, <strong id="EN-US_TOPIC_0000001188270502__b32932289135258">CREATE SCHEMA</strong> can include sub-commands to create objects within the new schema. The sub-commands are treated essentially the same as separate commands issued after creating the schema, If the <strong id="EN-US_TOPIC_0000001188270502__b842352706153719">AUTHORIZATION</strong> clause is used, all the created objects are owned by this user.</p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270502__sd8a756b505184705a8897156f5eb52c1"><h4 class="sectiontitle">Precautions</h4><ul id="EN-US_TOPIC_0000001188270502__ul795312913276"><li id="EN-US_TOPIC_0000001188270502__l9ea8070116f4409aac32e54cb1fd8c69">A database can have one or more schemas, and a schema can contain tables and other data objects, such as data types, functions, and operators. One object name can be used in different schemas. For example, both Schema1 and Schema2 can contain a table named <strong id="EN-US_TOPIC_0000001188270502__b15620530154614">mytable</strong>.</li><li id="EN-US_TOPIC_0000001188270502__l47ad400f2c0042599087040976e758a0">Different from databases, schemas are not isolated. You can access the objects in a schema of the connected database based on your schema permissions. To manage schema permissions, you need to have a good understanding of the database permissions.</li><li id="EN-US_TOPIC_0000001188270502__l176a8e8e06ee4d51b07e81e6cd6c8d53">A schema named with the <strong id="EN-US_TOPIC_0000001188270502__b132114512464">PG_</strong> prefix cannot be created because this type of schema is reserved for the database system.</li><li id="EN-US_TOPIC_0000001188270502__la4e77ab6847e40f3a6a797d10ad2cee3">If a user is created, a schema named after the user will also be created in the current database.</li><li id="EN-US_TOPIC_0000001188270502__li1695317294272">As long as the current user has the CREATE permission, the user can create a schema.</li><li id="EN-US_TOPIC_0000001188270502__li10953229182712">The owner of an object created by a system administrator in a schema with the same name as a common user is the common user, not the system administrator.</li><li id="EN-US_TOPIC_0000001188270502__l77b4f0021ec24e83955182de1850911f">To reference a table that is not modified with a schema name, the system uses <strong id="EN-US_TOPIC_0000001188270502__b842352706132428">search_path</strong> to find the schema that the table belongs to. <strong id="EN-US_TOPIC_0000001188270502__b145086511478">pg_temp</strong> and <strong id="EN-US_TOPIC_0000001188270502__b1509855471">pg_catalog</strong> are always the first two schemas to be searched no matter whether or how they are specified in <strong id="EN-US_TOPIC_0000001188270502__b1150917511477">search_path</strong>. <strong id="EN-US_TOPIC_0000001188270502__b842352706132359">search_path</strong> is a schema name list, and the first table detected in it is the target table. If no target table is found, an error will be reported. (If a table exists but the schema it belongs to is not listed in <strong id="EN-US_TOPIC_0000001188270502__b842352706103922">search_path</strong>, the search fails as well.) The first schema in <strong id="EN-US_TOPIC_0000001188270502__b842352706103940">search_path</strong> is called <strong id="EN-US_TOPIC_0000001188270502__b842352706103952">current schema</strong>. This schema is the first one to be searched. If no schema name is declared, newly created database objects are saved in this schema by default.</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270502__s6d289579f18149c1a17ec06712dde98a"><h4 class="sectiontitle">Syntax</h4><ul id="EN-US_TOPIC_0000001188270502__ueb686891603d403480637aa0cf189f2c"><li id="EN-US_TOPIC_0000001188270502__la0976fa1070d4c8b9088a4ed573350f2">Create a schema based on a specified name:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188270502__sa863f654e9d44d4f9bbc808b7ee28222"><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">CREATE</span><span class="w"> </span><span class="k">SCHEMA</span><span class="w"> </span><span class="k">schema_name</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">AUTHORIZATION</span><span class="w"> </span><span class="n">user_name</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">WITH</span><span class="w"> </span><span class="n">PERM</span><span class="w"> </span><span class="k">SPACE</span><span class="w"> </span><span class="s1">'space_limit'</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="n">schema_element</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>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</li><li id="EN-US_TOPIC_0000001188270502__l191a2077497d462c88cfbcee6848037c">Create a schema based on a user name:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188270502__s75304cfa12934b3b83df7cae6d5c8728"><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">CREATE</span><span class="w"> </span><span class="k">SCHEMA</span><span class="w"> </span><span class="k">AUTHORIZATION</span><span class="w"> </span><span class="n">user_name</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">WITH</span><span class="w"> </span><span class="n">PERM</span><span class="w"> </span><span class="k">SPACE</span><span class="w"> </span><span class="s1">'space_limit'</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="n">schema_element</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>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270502__s137c7d80e527499da3008e35aa5115aa"><h4 class="sectiontitle">Parameter Description</h4><ul id="EN-US_TOPIC_0000001188270502__ub308d7ce1ea94a509a9fa185772ce356"><li id="EN-US_TOPIC_0000001188270502__lf6df67d8504e48428af0f53cf4ced03f"><strong id="EN-US_TOPIC_0000001188270502__af0784a43101c4e78b5083c98d58a40c0">schema_name</strong><p id="EN-US_TOPIC_0000001188270502__a2115acd4630e42e78f9032aef52c2541">Indicates the name of the schema to be created.</p>
|
|
<div class="notice" id="EN-US_TOPIC_0000001188270502__n7c79f5a45287445cbd1241ff5145bad7"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><ul id="EN-US_TOPIC_0000001188270502__ul13364185615206"><li id="EN-US_TOPIC_0000001188270502__li7364155610208">The name must be unique, </li><li id="EN-US_TOPIC_0000001188270502__li536455622010">and cannot start with <strong id="EN-US_TOPIC_0000001188270502__b59010337174627">pg_</strong>.</li></ul>
|
|
</div></div>
|
|
<p id="EN-US_TOPIC_0000001188270502__a0bbc161a37a64d1ab6b735d0363eaa54">Value range: a string. It must comply with the naming convention rule.</p>
|
|
</li><li id="EN-US_TOPIC_0000001188270502__l4676a2dbfd484819858b12ad8e546b30"><strong id="EN-US_TOPIC_0000001188270502__a4d0fa29a8cb445c88aaf404f24740da7">AUTHORIZATION user_name</strong><p id="EN-US_TOPIC_0000001188270502__a724c2de5a7b34ea384d2b497c9ad0bd7">Indicates the name of the user who will own this schema. If <strong id="EN-US_TOPIC_0000001188270502__en-us_topic_0058966152_b16195942174627">schema_name</strong> is not specified, <strong id="EN-US_TOPIC_0000001188270502__en-us_topic_0058966152_b11545754174627">user_name</strong> will be used as the schema name. In this case, <strong id="EN-US_TOPIC_0000001188270502__en-us_topic_0058966152_b36802925174627">user_name</strong> can only be a role name.</p>
|
|
<p id="EN-US_TOPIC_0000001188270502__a62ab8950e22041369b3b4329bab70fb1">Value range: An existing user name/role.</p>
|
|
</li><li id="EN-US_TOPIC_0000001188270502__li1566194263915"><strong id="EN-US_TOPIC_0000001188270502__b86610421397">WITH PERM SPACE 'space_limit'</strong><p id="EN-US_TOPIC_0000001188270502__p36612424397">Indicates the storage upper limit of the permanent table in the specified schema. If <strong id="EN-US_TOPIC_0000001188270502__b57121655103217">space_limit</strong> is not specified, the space is not limited.</p>
|
|
<p id="EN-US_TOPIC_0000001188270502__p766114214399">Value range: A string consists of an integer and unit. The unit can be K/M/G/T/P currently. The unit of parsed value is K and cannot exceed the range that can be expressed in 64 bits, which is 1 KB to 9007199254740991 KB.</p>
|
|
</li><li id="EN-US_TOPIC_0000001188270502__lc1d779f3255e4b9cb81d8d0f7b549d7a"><strong id="EN-US_TOPIC_0000001188270502__a0976e211b7af4e9fad8c24d4ebb895a8">schema_element</strong><p id="EN-US_TOPIC_0000001188270502__acab2a4ed92e049a3b4f5f4494ba7d742">Indicates an SQL statement defining an object to be created within the schema. Currently, only <strong id="EN-US_TOPIC_0000001188270502__b20811126174627">CREATE TABLE</strong>, <strong id="EN-US_TOPIC_0000001188270502__b53082411174627">CREATE VIEW</strong>, <strong id="EN-US_TOPIC_0000001188270502__b7979658174627">CREATE INDEX</strong>, <strong id="EN-US_TOPIC_0000001188270502__b4708058174627">CREATE PARTITION</strong>, and <strong id="EN-US_TOPIC_0000001188270502__b84235270620473">GRANT</strong> are accepted as clauses within <strong id="EN-US_TOPIC_0000001188270502__b9622538174627">CREATE SCHEMA</strong>.</p>
|
|
<p id="EN-US_TOPIC_0000001188270502__a31a7e8559d684565ad276e5c3ef84714">Objects created by sub-commands are owned by the user specified by <strong id="EN-US_TOPIC_0000001188270502__b51142028174627">AUTHORIZATION</strong>.</p>
|
|
</li></ul>
|
|
<div class="note" id="EN-US_TOPIC_0000001188270502__n9a9767ebd2724611b35932b1d8cf3ef5"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001188270502__af946322ebcb441e8a11af77cc9fd97ee">If objects in the schema on the current search path are with the same name, specify the schemas different objects are in. You can run the <strong id="EN-US_TOPIC_0000001188270502__b2388300813553">SHOW SEARCH_PATH</strong> command to check the schemas on the current search path.</p>
|
|
</div></div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270502__s5bf3a4d02e2d4bea92404b3442f9ffb8"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001188270502__p16145115031914">-- Create the <strong id="EN-US_TOPIC_0000001188270502__b152637805633619">role1</strong> role.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188270502__screen126111355181914"><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">CREATE</span><span class="w"> </span><span class="k">ROLE</span><span class="w"> </span><span class="n">role1</span><span class="w"> </span><span class="n">IDENTIFIED</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="s1">'{Password}'</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188270502__p201459508198">Create a schema named <strong id="EN-US_TOPIC_0000001188270502__b102965564533619">role1</strong> for the <strong id="EN-US_TOPIC_0000001188270502__b150851098733619">role1</strong> role. The owner of the <strong id="EN-US_TOPIC_0000001188270502__b59598001233619">films</strong> and <strong id="EN-US_TOPIC_0000001188270502__b62549231233619">winners</strong> tables created by the clause is <strong id="EN-US_TOPIC_0000001188270502__b164750029633619">role1</strong>.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188270502__screen1845618282013"><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></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">SCHEMA</span><span class="w"> </span><span class="k">AUTHORIZATION</span><span class="w"> </span><span class="n">role1</span>
|
|
<span class="w"> </span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">films</span><span class="w"> </span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="nb">text</span><span class="p">,</span><span class="w"> </span><span class="n">release</span><span class="w"> </span><span class="nb">date</span><span class="p">,</span><span class="w"> </span><span class="n">awards</span><span class="w"> </span><span class="nb">text</span><span class="p">[])</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="k">CREATE</span><span class="w"> </span><span class="k">VIEW</span><span class="w"> </span><span class="n">winners</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="n">release</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">films</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">awards</span><span class="w"> </span><span class="k">IS</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270502__sf5b9082b29dc4619be18df64147e273a"><h4 class="sectiontitle">Helpful Links</h4><p id="EN-US_TOPIC_0000001188270502__ab9d043f673ca47ea82b9e1b144d08686"><a href="dws_06_0136.html">ALTER SCHEMA</a>, <a href="dws_06_0204.html">DROP SCHEMA</a></p>
|
|
</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>
|
|
|