gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

117 líneas
9.4KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE SCHEMA</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-createrule.html" title="CREATE RULE" /><link rel="next" href="sql-createsequence.html" title="CREATE SEQUENCE" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE SCHEMA</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createrule.html" title="CREATE RULE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createsequence.html" title="CREATE SEQUENCE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATESCHEMA"><div class="titlepage"></div><a id="id-1.9.3.80.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE SCHEMA</span></h2><p>CREATE SCHEMA — define a new schema</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE SCHEMA <em class="replaceable"><code>schema_name</code></em> [ AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> ] [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]
  4. CREATE SCHEMA AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]
  5. CREATE SCHEMA IF NOT EXISTS <em class="replaceable"><code>schema_name</code></em> [ AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> ]
  6. CREATE SCHEMA IF NOT EXISTS AUTHORIZATION <em class="replaceable"><code>role_specification</code></em>
  7. <span class="phrase">where <em class="replaceable"><code>role_specification</code></em> can be:</span>
  8. <em class="replaceable"><code>user_name</code></em>
  9. | CURRENT_USER
  10. | SESSION_USER
  11. </pre></div><div class="refsect1" id="id-1.9.3.80.5"><h2>Description</h2><p>
  12. <code class="command">CREATE SCHEMA</code> enters a new schema
  13. into the current database.
  14. The schema name must be distinct from the name of any existing schema
  15. in the current database.
  16. </p><p>
  17. A schema is essentially a namespace:
  18. it contains named objects (tables, data types, functions, and operators)
  19. whose names can duplicate those of other objects existing in other
  20. schemas. Named objects are accessed either by <span class="quote">“<span class="quote">qualifying</span>”</span>
  21. their names with the schema name as a prefix, or by setting a search
  22. path that includes the desired schema(s). A <code class="literal">CREATE</code> command
  23. specifying an unqualified object name creates the object
  24. in the current schema (the one at the front of the search path,
  25. which can be determined with the function <code class="function">current_schema</code>).
  26. </p><p>
  27. Optionally, <code class="command">CREATE SCHEMA</code> can include subcommands
  28. to create objects within the new schema. The subcommands are treated
  29. essentially the same as separate commands issued after creating the
  30. schema, except that if the <code class="literal">AUTHORIZATION</code> clause is used,
  31. all the created objects will be owned by that user.
  32. </p></div><div class="refsect1" id="id-1.9.3.80.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>schema_name</code></em></span></dt><dd><p>
  33. The name of a schema to be created. If this is omitted, the
  34. <em class="replaceable"><code>user_name</code></em>
  35. is used as the schema name. The name cannot
  36. begin with <code class="literal">pg_</code>, as such names
  37. are reserved for system schemas.
  38. </p></dd><dt><span class="term"><em class="replaceable"><code>user_name</code></em></span></dt><dd><p>
  39. The role name of the user who will own the new schema. If omitted,
  40. defaults to the user executing the command. To create a schema
  41. owned by another role, you must be a direct or indirect member of
  42. that role, or be a superuser.
  43. </p></dd><dt><span class="term"><em class="replaceable"><code>schema_element</code></em></span></dt><dd><p>
  44. An SQL statement defining an object to be created within the
  45. schema. Currently, only <code class="command">CREATE
  46. TABLE</code>, <code class="command">CREATE VIEW</code>, <code class="command">CREATE
  47. INDEX</code>, <code class="command">CREATE SEQUENCE</code>, <code class="command">CREATE
  48. TRIGGER</code> and <code class="command">GRANT</code> are accepted as clauses
  49. within <code class="command">CREATE SCHEMA</code>. Other kinds of objects may
  50. be created in separate commands after the schema is created.
  51. </p></dd><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  52. Do nothing (except issuing a notice) if a schema with the same name
  53. already exists. <em class="replaceable"><code>schema_element</code></em>
  54. subcommands cannot be included when this option is used.
  55. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.80.7"><h2>Notes</h2><p>
  56. To create a schema, the invoking user must have the
  57. <code class="literal">CREATE</code> privilege for the current database.
  58. (Of course, superusers bypass this check.)
  59. </p></div><div class="refsect1" id="id-1.9.3.80.8"><h2>Examples</h2><p>
  60. Create a schema:
  61. </p><pre class="programlisting">
  62. CREATE SCHEMA myschema;
  63. </pre><p>
  64. </p><p>
  65. Create a schema for user <code class="literal">joe</code>; the schema will also be
  66. named <code class="literal">joe</code>:
  67. </p><pre class="programlisting">
  68. CREATE SCHEMA AUTHORIZATION joe;
  69. </pre><p>
  70. </p><p>
  71. Create a schema named <code class="literal">test</code> that will be owned by user
  72. <code class="literal">joe</code>, unless there already is a schema named <code class="literal">test</code>.
  73. (It does not matter whether <code class="literal">joe</code> owns the pre-existing schema.)
  74. </p><pre class="programlisting">
  75. CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
  76. </pre><p>
  77. </p><p>
  78. Create a schema and create a table and view within it:
  79. </p><pre class="programlisting">
  80. CREATE SCHEMA hollywood
  81. CREATE TABLE films (title text, release date, awards text[])
  82. CREATE VIEW winners AS
  83. SELECT title, release FROM films WHERE awards IS NOT NULL;
  84. </pre><p>
  85. Notice that the individual subcommands do not end with semicolons.
  86. </p><p>
  87. The following is an equivalent way of accomplishing the same result:
  88. </p><pre class="programlisting">
  89. CREATE SCHEMA hollywood;
  90. CREATE TABLE hollywood.films (title text, release date, awards text[]);
  91. CREATE VIEW hollywood.winners AS
  92. SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
  93. </pre></div><div class="refsect1" id="id-1.9.3.80.9"><h2>Compatibility</h2><p>
  94. The SQL standard allows a <code class="literal">DEFAULT CHARACTER SET</code> clause
  95. in <code class="command">CREATE SCHEMA</code>, as well as more subcommand
  96. types than are presently accepted by
  97. <span class="productname">PostgreSQL</span>.
  98. </p><p>
  99. The SQL standard specifies that the subcommands in <code class="command">CREATE
  100. SCHEMA</code> can appear in any order. The present
  101. <span class="productname">PostgreSQL</span> implementation does not
  102. handle all cases of forward references in subcommands; it might
  103. sometimes be necessary to reorder the subcommands in order to avoid
  104. forward references.
  105. </p><p>
  106. According to the SQL standard, the owner of a schema always owns
  107. all objects within it. <span class="productname">PostgreSQL</span>
  108. allows schemas to contain objects owned by users other than the
  109. schema owner. This can happen only if the schema owner grants the
  110. <code class="literal">CREATE</code> privilege on their schema to someone else, or a
  111. superuser chooses to create objects in it.
  112. </p><p>
  113. The <code class="literal">IF NOT EXISTS</code> option is a
  114. <span class="productname">PostgreSQL</span> extension.
  115. </p></div><div class="refsect1" id="id-1.9.3.80.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterschema.html" title="ALTER SCHEMA"><span class="refentrytitle">ALTER SCHEMA</span></a>, <a class="xref" href="sql-dropschema.html" title="DROP SCHEMA"><span class="refentrytitle">DROP SCHEMA</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createrule.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createsequence.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE RULE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE SEQUENCE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1