一、数据库逻辑结构介绍

数据库:应用连接到一个数据库时,一般不能访问其它数据库,除非使用dblink等其他手段。

表、索引:postgresql中标的术语为relation,其它数据库中成为table。

数据行:每张表有多行数据,postgresql中称为tuple,其它数据库称row。

注意:在postgresql中,一个数据库(实例)下可以有多个数据库,而一个数据库不能属于多个实例。这跟oracle数据库不同。

二、数据库基本操作。

1.1  创建数据库:

create database name

  [ [with]  [owner [=] user_name ]  //  指定新建的数据库属于哪个用户,不指定使用当前用户。

    [template [=] template]      //  模板名(从哪个模板创建,不指定使用默认template1)

    [encoding [=] encoding ]  // 字符集编码    

    [lc_collate [=] lc_ctype]

    [tablespace [=] tablespace ]      // 指定的新数据库关联 表空间 的名字

    [connection limit [=] connlimit]   // 数据库可以接受多少并发连接,默认-1(无限制)

[]  中的参数都可省略为create database db_name;

1.2  修改数据库的方法:

alter database name [  [with]  option  [...]  ]

option 可以是:

  connection limit connlimit

  alter database name rename to new_name;

  alter database name owner to new_owner;

  alter database name set tablespace new_tablespace;

  alter database name set configuration_parameter from current;

  alter database name reset configuration_paramter;

  alter database name reset all

eg: 修改数据库的最大连接数

testdb=# alter database testdb connection limit 10;
ALTER DATABASE
Time: 6.557 ms

  eg: 修改数据库名称

testdb=# alter database test rename to testdb1;
ALTER DATABASE
Time: 9.537 ms

  eg:关闭在数据库testdb上的默认索引扫描

testdb=# alter database testdb set enable_indexscan to off;
ALTER DATABASE
Time: 12.012 ms

  

1.3  删除数据库

drop database [if exists] name;

注意:如果有人连接这个数据库,则不能删除;

不能再事物块中删除数据库;可以修改。

三、模式schema

1.1 定义:模式可以理解为一个命名空间或者目录。不同模式下可以有相同名称的表,函数等对象且互相不冲突。每个模式的对象可以相互调用。

一个数据库可以包含多个模式,一个模式中可以包含表、函数以及数据库对象。

postgresql中,不能同时访问不同数据库中的对象,而模式没有此限制。schema的这点概念类似于mysql中的database。

使用schema的原因:
  允许多个用户在使用同一个数据库时互不干扰。

  把数据库对象放在不同的模式下,然后组成逻辑组,让他们更便于管理。

  第三方应用可以放在不同的模式中,这样就不会和其它对象的名字冲突了。

2.1 模式的使用

create schema schemaname [ authorization username ] [ schema_elemane [...]]

2.1.1 创建、查看、删除一个schema

testdb=# create schema osdba;
CREATE SCHEMA
Time: 4.575 ms
testdb=# \dn
List of schemas
+--------+----------+
| Name | Owner |
+--------+----------+
| osdba | postgres |
| public | postgres |
+--------+----------+
(2 rows) testdb=# drop schema osdba;
DROP SCHEMA
Time: 7.558 ms
testdb=#

2.1.2 为repl用户创建模式repl;

postgres=# create schema authorization repl;
CREATE SCHEMA
postgres=#

2.1.3   创建模式的同时,在这个模式下创建一些表的视图:

postgres=# create schema osdba
create table t1 (id int, title text)
create table t2 (id int, content text)
create view v1 as
select a.id,a.title,b.content from t1 a,t2 b where a.id=b.id;
CREATE SCHEMA

  可以修改名称和owner,语法同数据库

testdb=#
alter schema osdba rename to osdbaold;
ALTER SCHEMA

  修改拥有者

testdb=# alter schema osdbaold owner to repl;
ALTER SCHEMA

  

2.2 模式的搜索路径

testdb=# show search_path;
search_path
-----------------
"$user", public
(1 row)

  

postgresql逻辑结构(一)的更多相关文章

  1. [转]PostgreSQL 逻辑结构 和 权限体系 介绍

    摘要: 本文旨在帮助用户理解PostgreSQL的逻辑结构和权限体系,帮助用户快速的理解和管理数据库的权限. 逻辑结构 最上层是实例,实例中允许创建多个数据库,每个数据库中可以创建多个schema,每 ...

  2. postgresql逻辑结构--表(二)

    一.创建表 语法: create table table_name( col01_name data_type, col02_name data_type, col03_name data_type, ...

  3. postgresql逻辑结构--用户及权限管理(七)

    一.用户和角色 二.创建用户和角色 三.权限管理 四.

  4. postgresql逻辑结构--索引(六)

    一.索引简介 二.索引分类 三.创建索引 四.修改索引 五.删除索引

  5. postgresql逻辑结构--视图(五)

    定义 一.创建视图 1.语法 create [or replace ]  [ temp |  temporary ]  view name [(column_name [,...])]  as que ...

  6. postgresql逻辑结构--表空间(四)

    一.创建表空间 1. 语法:create tablespace tablespace_name [owner user_name] location 'directory' postgres=# cr ...

  7. postgresql逻辑结构--触发器(三)

    触发器(tigger)是一种由事物自动触发执行的特殊存储过程,这些事件可以是对一个表进行INSERT.UPDATE.DELETE等操作. 一.创建触发器 create   [ constraint ] ...

  8. 学习推荐-Postgresql学习手册

    Postgresql之旅: http://www.cnblogs.com/stephen-liu74/archive/2012/06/08/2315679.html postgresql逻辑结构+权限 ...

  9. 浅谈PostgreSQL用户权限

    问题 经常在PG群里看到有人在问“为什么我对表赋予了权限:但是还是不能访问表” 解析 若你看懂德哥这篇文章PostgreSQL逻辑结构和权限体系介绍:上面对你就不是困扰你的问题 解决这个问题很简单:在 ...

随机推荐

  1. window+R

    好记性不如烂笔头, window+R:打开运行 等同于:所有程序-->附件-->运行

  2. noip第6课作业

    1.    数据统计 [问题描述] 输入N个整数,求出它们的最小值.最大值和平均值(保留3位小数).输入保证这些数都是不超过1000的整数.(1<=N<=1000) [样例输入] 8 2 ...

  3. codeforces877c

    C. Slava and tanks time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. [kuangbin]树链剖分 D - 染色

    https://vjudge.net/contest/251031#problem/Dhttps://blog.csdn.net/kirito_acmer/article/details/512019 ...

  5. Linux查看History记录加时间戳小技巧

    Linux查看History记录加时间戳小技巧 熟悉bash的都一定知道使用history可以输出你曾经输入过的历史命令,例如[root@servyou_web ~]# history | more ...

  6. C#解密退款req_info结果通知

    微信支付退款结果通知API地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_16&index=10 static v ...

  7. [leetcode.com]算法题目 - Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. 面向对象——单例模式,五种方式

    单例模式:多次实例化的结果指向同一个实例 实现方式 一.使用类方法(调用创新对象,函数返回原定对象) import settings class Mysql: __instance = None de ...

  9. Windows安装python3.x后,pip list警告!DEPRECATION: The default format will switch to columns in the future.

    前言(凑字数专用) 这个警告虽然不影响你的正常使用,但是每次都好几行红色警告,总是给人一种怪怪的感觉(当然不是FBI的警告了……),所以咱们还是把他解决掉~ 网上好多解决办法都是Ubuntu的解决办法 ...

  10. Mac 下查看端口是否被占用

    1. lsof -i :8080 2. netstat -anp tcp | grep 8080 3. nc -w 10 -n -z 127.0.0.1 8070-8090