• postgresql逻辑结构(一)


    一、数据库逻辑结构介绍

    数据库:应用连接到一个数据库时,一般不能访问其它数据库,除非使用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)
    

      

  • 相关阅读:
    2016-5-6
    PE文件格式
    CheatEngine-内存修改
    ollydbg教程-动态调试
    IDA教程-静态反编译
    JavaFX常用汇总
    settings.xml样例文件
    Cassandra安装配置
    Linux安装配置相关
    Dom4j-读写xml
  • 原文地址:https://www.cnblogs.com/sunshine-long/p/9051228.html
Copyright © 2020-2023  润新知