zoukankan      html  css  js  c++  java
  • postgresql模式创建、修改、删除

    一个数据库包含一个或多个模式,而模式又包含表、序列、函数等,不同的模式可以包含相同名称的表、序列、函数等。模式本质上是命名空间,就像人的姓氏一样。一个用户只要有权限,连接到数据库后,可一次访问该数据库的任何模式下的对象。新建一个数据库会默认创建一个public模式,后续操作数据库对象如果没指定模式,则默认为public。例如之前创建的school数据库

    school=# dn+
                              List of schemas
      Name  |  Owner   |  Access privileges   |      Description      
    --------+----------+----------------------+------------------------
     public | postgres | postgres=UC/postgres+| standard public schema
            |          | =UC/postgres         |
    (1 row) 

     

     一、创建模式

    语法:

    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

     

    参数:

    schema_name

    模式名称,缺省使用user_name,且不能以pg_开头。

    user_name

    模式属于的用户,缺省为执行命令的用户。

    schema_element

    一条SQL语句,即创建模式后,在该模式下创建一个数据库对象。当前支持的子句有CREATE

    TABLE, CREATE VIEW, CREATE INDEX, CREATE SEQUENCE, CREATE TRIGGER and GRANT。

    IF NOT EXISTS

    如果模式已存在,使用该选项不会抛出错误。使用此选项不能使用schema_element子句。

    示例

    create schema schema_test authorization test1 create table tbl_test(a int) create view view_test as select * from tbl_test;

    访问模式下数据库对象在模式和数据库对象之间加一个句点即可

    school=# select * from schema_test.tbl_test ;
    
     a
    
    ---
    
    (0 rows)
    
     
    
    school=# select * from schema_test.view_test ;
    
     a
    
    ---
    
    (0 rows)

     

    二、模式修改 

    语法:

    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

    参数:

    name

    模式名称

    new_name

    模式新的名称,同样新名称也不能以pg_开头

    new_owner

    模式新用户名称

    示例

    school=# alter schema schema_test owner to postgres ;
    
    ALTER SCHEMA
    
    school=# alter schema schema_test rename to test;
    
    ALTER SCHEMA
    
    school=# dn+
    
                              List of schemas
    
      Name  |  Owner   |  Access privileges   |      Description      
    
    --------+----------+----------------------+------------------------
    
     public | postgres | postgres=UC/postgres+| standard public schema
    
            |          | =UC/postgres         |
    
     test   | postgres |                      |
    
    (2 rows)

    三、模式删除 

    语法: 

    school=# h drop schema

    Command:     DROP SCHEMA

    Description: remove a schema

    Syntax:

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

    参数:

    IF EXISTS

    如果模式不存在,不会抛出错误。

    name

    模式名称。

    CASCADE

    自动删除该模式下数据库对象。

    RESTRICT

    如果该模式下还存在数据库对象,则不允许删除该模式,RESTRICT为缺省值。

    示例:

    school=# drop schema test;
    
    ERROR:  cannot drop schema test because other objects depend on it
    
    DETAIL:  table test.tbl_test depends on schema test
    
    view test.view_test depends on schema test
    
    HINT:  Use DROP ... CASCADE to drop the dependent objects too.
    school=# drop schema test cascade;
    
    NOTICE:  drop cascades to 2 other objects
    
    DETAIL:  drop cascades to table test.tbl_test
    
    drop cascades to view test.view_test
    
    DROP SCHEMA

     

  • 相关阅读:
    关系型数据库和非关系型数据库的区别
    总结篇3-python数据结构和算法
    总结篇2-python进阶
    总结篇1-python基础
    测试sql星级判定函数
    1、Anyproxy简介
    Python内置logging模块-- 日志
    python+ selenium 绕过浏览器检测
    python-selenium,解决 遇到阿里无痕登录验证
    seldom
  • 原文地址:https://www.cnblogs.com/alianbog/p/4543107.html
Copyright © 2011-2022 走看看