加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql创建schema

发布时间:2020-12-13 16:49:19 所属栏目:百科 来源:网络整理
导读:schema概念有点像命名空间或者把它想像成一个文件系统中的目录,差别就是这个schema下不能再有schema嵌套. 各个对象比如表、函数等存放在各个schema下,同一个schema下不能有重复的对象名字,但在不同schema下可以重复. 使用schema的作用 方便管理多个用户共享

schema概念有点像命名空间或者把它想像成一个文件系统中的目录,差别就是这个schema下不能再有schema嵌套.
各个对象比如表、函数等存放在各个schema下,同一个schema下不能有重复的对象名字,但在不同schema下可以重复.

使用schema的作用

  • 方便管理多个用户共享一个数据库,但是又可以互相独立.
  • 方便管理众多对象,更有逻辑性.
  • 方便兼容某些第三方应用程序,创建对象时是有schema的.

比如要设计一个复杂系统.由众多模块构成,有时候模块间又需要有独立性.各模块存放单独的数据库显然是不合适的.
这时候使用schema来分类各模块间的对象,再对用户进行适当的权限控制.这样逻辑也非常清晰.

创建schema


db01=# create schema schema01;
CREATE SCHEMA db01=# dn List of schemas Name | Owner ----------+---------- public | postgres schema01 | postgres (2 rows) 

在schema中创建对象

db01=# table schema01.t1(id int);
TABLE db01=# insert into schema01.t1 values(1);
INSERT 0 1 db01=# select * from t1;
ERROR:  relation "t1" does not exist
LINE 1: from t1;
                      ^
db01=# from schema01.t1;
 id
----
  1
(1 row)
db01=# from db01.schema01.t1;
 id
----
  1
(1 row)

默认是在public这个schema下.因此要带上schema名称.数据库名字如果要带上,只能是当前连接的数据库!!

删除schema

drop schema schema01;
ERROR:  cannot schema schema01 because other objects depend on it DETAIL: table schema01.t1 depends on schema schema01 HINT: Use DROP ... CASCADE to drop the dependent objects too. db01=# schema schema01 cascade;
NOTICE:  drop cascades table schema01.t1 DROP SCHEMA 

schema下有对象如果需要一起删除,需要带上cascade关键字.有点像使用rmdir删除目录一样,文件夹下有东西不然删除.

创建schema指定owner

默认是谁创建的schema,owner就是谁,当然也可以指定

schema s01 authorization hippo;
SCHEMA db01=# schema --------+---------- hippo | hippo public | postgres s01 | hippo (3 rows) 

指定了owner,不指定schema,则schema名字与owner一致。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读