CREATE SCHEMA

Synopsis

Use the CREATE SCHEMA statement to create a new schema in the current database.A schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names can duplicate those of other objects existing in other schemas.Named objects in a schema can be accessed by using the schema name as prefix or by setting the schema name in the search path.

Syntax

  1. create_schema_name ::= CREATE SCHEMA [ IF NOT EXISTS ] schema_name
  2. [ AUTHORIZATION role_specification ]
  3. [ schema_element [ ... ] ]
  4. create_schema_role ::= CREATE SCHEMA [ IF NOT EXISTS ] AUTHORIZATION
  5. role_specification [ schema_element [ ... ] ]
  6. schema_element ::= '<YSQL DDL statement>'
  7. role_specification ::= role_name | CURRENT_USER | SESSION_USER

create_schema_name

CREATE SCHEMA - 图1

create_schema_role

CREATE SCHEMA - 图2

schema_element

CREATE SCHEMA - 图3

role_specification

CREATE SCHEMA - 图4

Where

  • schema_name is the name of the schema being created. If no schema_name is specified, the role_name is used.

  • role_name is the role who will own the new schema. If omitted, it defaults to the user executing the command. To create a schema owned by another role, you must be a direct or indirect member of that role, or be a superuser.

  • schema_element is a YSQL statement defining an object to be created within the schema.Currently, only CREATE TABLE, CREATE VIEW, CREATE INDEX, CREATE SEQUENCE, CREATE TRIGGER and GRANT are supported as clauses within CREATE SCHEMA.Other kinds of objects may be created in separate commands after the schema is created.

Examples

  • Create a new schema.
  1. yugabyte=# CREATE SCHEMA IF NOT EXIST branch;
  • Create a schema for a user.
  1. yugabyte=# CREATE ROLE John;
  2. yugabyte=# CREATE SCHEMA AUTHORIZATION john;
  • Create a schema that will be owned by another role.
  1. yugabyte=# CREATE SCHEMA branch AUTHORIZATION john;
  • Create a schema and an object within that schema.
  1. yugabyte=# CREATE SCHEMA branch
  2. CREATE TABLE dept(
  3. dept_id INT NOT NULL,
  4. dept_name TEXT NOT NULL,
  5. PRIMARY KEY (dept_id)
  6. );

See also