(九)PostgreSQL的schema管理

PostgreSQL的schema管理

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

1 创建schema

CREATE SCHEMA将一个新的模式输入到当前数据库中。schema名称必须与当前数据库中现有schema的名称不同。
schema本质上是一个命名空间:它包含命名对象(表、数据类型、函数和运算符),这些对象的名称可以与其他schema中存在的其他对象的名称重复。命名对象可以通过将schema名称作为前缀“限定”其名称来访问,也可以通过设置包括所需schema的搜索路径来访问。指定非限定对象名称的CREATE命令在当前schema中创建对象(位于搜索路径前面的对象,可以使用函数current_schema确定)。
或者,CREATE SCHEMA可以包括在新schema中创建对象的子命令。除了使用AUTHORIZATION子句,所有创建的对象都将归该用户所有之外,子命令的处理方式基本上与创建schema后发出的单独命令相同。

语法

CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ]
[ schema_element [ ... ] ]
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element
[ ... ] ]
CREATE SCHEMA IF NOT EXISTS schema_name
[ AUTHORIZATION role_specification ]
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
where role_specification can be:
user_name
| CURRENT_ROLE
| CURRENT_USER
| SESSION_USER

参数解析

  • schema_name
    The name of a schema to be created. If this is omitted, the user_name is used as the schema
    name. The name cannot begin with pg_, as such names are reserved for system schemas.
    要创建的schema的名称。如果省略此项,则使用user_name作为schema名称。名称不能以pg_开头,因为这样的名称是为系统架构保留的。
  • user_name
    The role name of the user who will own the new schema. If omitted, defaults to the user executing the command. To create a schema owned by another role, you must be able to SET ROLE to that role.
    将拥有新schema的角色名称。如果省略,则默认为执行命令的用户。若要创建另一个角色拥有的schema,您必须能够将角色设置为该角色。
  • schema_element
    An SQL 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 accepted as clauses within CREATE SCHEMA. Other kinds of objects may be created in separate commands after the schema is created.
    定义要在schema中创建的对象的SQL语句。目前,CREATE SCHEMA中只接受CREATE TABLE、CREATE VIEW、CREATE INDEX、CREATE SEQUENCE、CREATE TRIGGER和GRANT作为子句。在创建schema之后,可以在单独的命令中创建其他类型的对象。
  • IF NOT EXISTS
    Do nothing (except issuing a notice) if a schema with the same name already exists. schema_element subcommands cannot be included when this option is used.
    如果已经存在具有相同名称的架构,则不执行任何操作(除非发出通知)。使用此选项时,schema_element子命令不能包含在内。

case 1:在数据库white中创建schema yewu2

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
(2 rows)

white=# create schema yewu2;
CREATE SCHEMA
white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | postgres
(3 rows)

case 2:在数据库white中创建schema yewu3,并指定owner为test2

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | postgres
(3 rows)

white=# create schema yewu3 AUTHORIZATION test2;
CREATE SCHEMA
white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | postgres
 yewu3  | test2
(4 rows)

2 修改schema

ALTER SCHEMA 表示更改schema的定义。
您必须拥有该schema才能使用 ALTER SCHEMA的命令。若要重命名schema,您还必须具有数据库的CREATE权限。要更改所有者,您必须能够将SET ROLE设置为新的拥有者角色,并且该角色必须具有数据库的CREATE权限。(请注意,超级用户自动拥有所有这些权限。)

语法

ALTER SCHEMA name RENAME TO new_name
ALTER SCHEMA name OWNER TO { new_owner | CURRENT_ROLE |
CURRENT_USER | SESSION_USER }

参数解析

  • name
    The name of an existing schema.

  • new_name
    The new name of the schema. The new name cannot begin with pg_, as such names are reserved
    for system schemas.

  • new_owner
    The new owner of the schema.

case 1:修改schema yewu2的owner 为test2

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | postgres
 yewu3  | test2
(4 rows)

white=# alter schema yewu2 owner to test2;
ALTER SCHEMA
white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | test2
 yewu3  | test2
(4 rows)

case2:修改schema yewu2的名称为yewu2_new

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | test2
 yewu3  | test2
(4 rows)

white=# alter schema yewu2 rename to yewu2_new;
ALTER SCHEMA
white=# \dn
        List of schemas
   Name    |       Owner       
-----------+-------------------
 public    | pg_database_owner
 yewu1     | postgres
 yewu2_new | test2
 yewu3     | test2
(4 rows)

3 删除schema

DROP SCHEMA 表示从数据库中删除schema。
schema只能由其所有者或超级用户删除。请注意,所有者可以删除模式(从而删除schema所有包含的对象),即使他们不拥有schema中的某些对象。

语法

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

参数解析

  • IF EXISTS
    Do not throw an error if the schema does not exist. A notice is issued in this case.

  • name
    The name of a schema.

  • CASCADE
    Automatically drop objects (tables, functions, etc.) that are contained in the schema, and in turn
    all objects that depend on those objects (see Section 5.14).

  • RESTRICT
    Refuse to drop the schema if it contains any objects. This is the default.

case 1:删除schema yewu1。因为yewu1下有表,故删除失败

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | test2
 yewu3  | test2
(4 rows)

white=# drop schema yewu1;
ERROR:  cannot drop schema yewu1 because other objects depend on it
DETAIL:  table yewu1.t1 depends on schema yewu1
table yewu1.t2 depends on schema yewu1
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

case2:删除schema yewu2,带参数cascade

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu2  | test2
 yewu3  | test2
(4 rows)

white=# drop schema yewu2;
ERROR:  cannot drop schema yewu2 because other objects depend on it
DETAIL:  table yewu2.t2 depends on schema yewu2
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
white=# drop schema yewu2 CASCADE;
NOTICE:  drop cascades to table yewu2.t2
DROP SCHEMA
white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu3  | test2
(3 rows)

case3:删除schema yewu3,带参数restrict。因为yewu3下有表,故删除失败

white=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 public | pg_database_owner
 yewu1  | postgres
 yewu3  | test2
(3 rows)

white=# drop schema yewu3 restrict;
ERROR:  cannot drop schema yewu3 because other objects depend on it
DETAIL:  table yewu3.t2 depends on schema yewu3
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

更多内容请查看对应数据库版本的官方文档。

谨记:心存敬畏,行有所止。

  • 76
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值