CREATE SCHEMA 中文man頁面
NAME
CREATE SCHEMA - 定義一個新的模式
SYNOPSIS
CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] CREATE SCHEMA AUTHORIZATION username [ schema_element [ ... ] ]
DESCRIPTION 描述
CREATE SCHEMA 將在當前數據庫里輸入一個新的模式。 該模式名將在當前數據庫里現存的所有模式名中唯一。
模式實際上是一個名字空間: 它包含命名對象(表,數據類型,函數和操作符)這些名字可以和其它模式里存在的其它對象重名。 命名對象要么是通過用模式名作為前綴"修飾"這些它們的名字進行訪問, 要么是通過設置一個搜索路徑包含所需要的模式。 無修飾的對象都是在當前模式中創建的(在搜索路徑最前面的;可以用函數 current_schema 來判斷)。
另外,CREATE SCHEMA 可以包括在新模式中創建對象的子命令。 這些子命令和那些在創建完模式后發出的命令沒有任何區別,只不過是如果使用了 AUTHORIZATION 子句, 那么所有創建的對象都將被該用戶擁有。
PARAMETERS 參數
- schemaname
要創建的模式名字。如果省略,則使用用戶名作為模式名。- username
將擁有該模式的用戶名。如果省略,缺省為執行該命令的用戶名。 只有超級用戶才能創建不屬于自己的模式。- schema_element
一個 SQL 語句,定義一個要在模式里創建的對象。 目前,只有 CREATE TABLE,CREATE VIEW, 和 GRANT 是在 CREATE SCHEMA 里面可以接受的子句。 其它類型的對象可以在創建完模式之后的獨立的命令里創建。
NOTES 注意
要創建模式,調用該命令的用戶必需在當前數據庫上有 CREATE 權限。(當然,超級用戶繞開這個檢查。)
EXAMPLES 例子
創建一個模式:
CREATE SCHEMA myschema;
為用戶 joe 創建模式 --- 模式也會叫 joe:
CREATE SCHEMA AUTHORIZATION joe;
創建一個模式并且在里面創建一個表:
CREATE SCHEMA hollywood CREATE TABLE films (title text, release date, awards text[]) CREATE VIEW winners AS SELECT title, release FROM films WHERE awards IS NOT NULL;
請注意上面的獨立的子命令不是由分號結尾的。
下面的命令是實現同樣結果的等效語句:
CREATE SCHEMA hollywood; CREATE TABLE hollywood.films (title text, release date, awards text[]); CREATE VIEW hollywood.winners AS SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
COMPATIBILITY 兼容性
SQL 標準允許在 CREATE SCHEMA 里面有一個 DEFAULT CHARACTER SET 子句,以及比目前 PostgreSQL 可以接受的更多的子命令。
SQL 標準聲明在 CREATE SCHEMA 里的子命令可以以任意順序出現。 目前 PostgreSQL 里的實現還不能處理所有子命令里需要提前引用的情況;有時候可能需要重排一下子命令的順序以避免前向引用。
在 SQL 標準里,模式的所有者總是擁有其中的所有對象。 PostgreSQL 允許模式包含非模式所有者所有的對象。 只有在模式所有者 CREATE 了自己的模式的權限給了其它人才可能出現。
SEE ALSO 參見
ALTER SCHEMA [alter_schema(7)], DROP SCHEMA [drop_schema(l)]
#p#
NAME
CREATE SCHEMA - define a new schema
SYNOPSIS
CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] CREATE SCHEMA AUTHORIZATION username [ schema_element [ ... ] ]
DESCRIPTION
CREATE SCHEMA will enter a new schema into the current database. The schema name must be distinct from the name of any existing schema in the current database.
A schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names may duplicate those of other objects existing in other schemas. 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). Unqualified objects are created in the current schema (the one at the front of the search path, which can be determined with the function current_schema).
Optionally, CREATE SCHEMA can include subcommands to create objects within the new schema. The subcommands are treated essentially the same as separate commands issued after creating the schema, except that if the AUTHORIZATION clause is used, all the created objects will be owned by that user.
PARAMETERS
- schemaname
- The name of a schema to be created. If this is omitted, the user name is used as the schema name.
- username
- The name of the user who will own the schema. If omitted, defaults to the user executing the command. Only superusers may create schemas owned by users other than themselves.
- schema_element
- An SQL statement defining an object to be created within the schema. Currently, only CREATE TABLE, CREATE VIEW, and GRANT are accepted as clauses within CREATE SCHEMA. Other kinds of objects may be created in separate commands after the schema is created.
NOTES
To create a schema, the invoking user must have CREATE privilege for the current database. (Of course, superusers bypass this check.)
EXAMPLES
Create a schema:
CREATE SCHEMA myschema;
Create a schema for user joe; the schema will also be named joe:
CREATE SCHEMA AUTHORIZATION joe;
Create a schema and create a table and view within it:
CREATE SCHEMA hollywood CREATE TABLE films (title text, release date, awards text[]) CREATE VIEW winners AS SELECT title, release FROM films WHERE awards IS NOT NULL;
Notice that the individual subcommands do not end with semicolons.
The following is an equivalent way of accomplishing the same result:
CREATE SCHEMA hollywood; CREATE TABLE hollywood.films (title text, release date, awards text[]); CREATE VIEW hollywood.winners AS SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
COMPATIBILITY
The SQL standard allows a DEFAULT CHARACTER SET clause in CREATE SCHEMA, as well as more subcommand types than are presently accepted by PostgreSQL.
The SQL standard specifies that the subcommands in CREATE SCHEMA may appear in any order. The present PostgreSQL implementation does not handle all cases of forward references in subcommands; it may sometimes be necessary to reorder the subcommands to avoid forward references.
According to the SQL standard, the owner of a schema always owns all objects within it. PostgreSQL allows schemas to contain objects owned by users other than the schema owner. This can happen only if the schema owner grants the CREATE privilege on his schema to someone else.
SEE ALSO
ALTER SCHEMA [alter_schema(7)], DROP SCHEMA [drop_schema(l)]