PostgreSQL mode creation, modification, deletion

Source: Internet
Author: User

A database contains one or more schemas, and the schema contains tables, sequences, functions, and so on, and different schemas can contain tables, sequences, functions, and so on with the same name. A pattern is essentially a namespace, just like a person's last name. Once a user has permission to connect to the database, they can access objects in any mode of the database at once. Creating a new database creates a public mode by default, and the subsequent operations database object defaults to public if no schema is specified. For example, the school database created earlier

school=# \dn+
List of schemas
Name |  Owner |      Access Privileges | Description
--------+----------+----------------------+------------------------
Public | Postgres | postgres=uc/postgres+| Standard Public schema
| | =uc/postgres |
(1 row)

First, create a pattern

Grammar:

school=# \h Create Schema

Command:create SCHEMA

Description:define a new schema

Syntax:

CREATE SCHEMA schema_name [AUTHORIZATION user_name] [schema_element [...]

CREATE SCHEMA AUTHORIZATION user_name [schema_element [...]

CREATE SCHEMA IF not EXISTS schema_name [AUTHORIZATION user_name]

CREATE SCHEMA IF not EXISTS AUTHORIZATION user_name

Parameters:

Schema_name

The schema name, which uses user_name by default and cannot start with pg_.

User_name

The user that the pattern belongs to, the default is the user who executes the command.

Schema_element

An SQL statement that creates a schema, creating a database object in that mode. The currently supported clauses have create

TABLE, create VIEW, create INDEX, create SEQUENCE, create TRIGGER and GRANT.

IF not EXISTS

If the pattern already exists, using this option does not throw an error. You cannot use the schema_element clause with this option.

Example

Create Schema Authorization Create Table int Create View  as Select *  from Tbl_test;

In access mode, the database object adds a period between the schema and the database object

School=Select* from schema_test.tbl_test; a- -- (0  rows) school=Select* from Schema_ Test.view_test; A---(0 rows)

Second, mode modification

Grammar:

school=# \h Alter Schema

Command:alter SCHEMA

Description:change the definition of a schema

Syntax:

ALTER SCHEMA name RENAME to New_name

ALTER SCHEMA name OWNER to New_owner

Parameters:

Name

Schema name

New_name

The new name of the schema and the same new name cannot start with Pg_

New_owner

Mode New user Name

Example

School=#Alter SchemaSchema_test owner toPostgres;ALTER SCHEMASchool=#Alter SchemaSchema_test Rename totest;ALTER SCHEMASchool=# \DN+List ofSchemas Name|Owner|AccessPrivileges   |Description--------+----------+----------------------+------------------------  Public |Postgres|Postgres=Uc/Postgres+|Standard Public Schema        |          | =Uc/Postgres|Test|Postgres|                      |(2Rows

Third, mode delete

Grammar:

school=# \h Drop Schema

Command:drop SCHEMA

Description:remove a schema

Syntax:

DROP SCHEMA [IF EXISTS] name [, ...] [CASCADE | RESTRICT]

Parameters:

IF EXISTS

If the pattern does not exist, no error is thrown.

Name

The schema name.

CASCADE

Automatically deletes database objects in this mode.

RESTRICT

If a database object is still present in this mode, deleting the pattern is not allowed and restrict is the default.

Example:

School=#Drop Schematest; Error:cannotDrop SchemaTest because other objects depend onItdetail:TableTest.tbl_test depends on SchemaTestViewTest.view_test depends on SchemaTesthint: Use DROP...CASCADE  to DropThe dependent objects too.

School=dropschemacascade; NOTICE:  dropto2 other objectsdetail:  dropto  table  test.tbl_testdroptoview  test.view_test  DROPSCHEMA

PostgreSQL mode creation, modification, deletion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.