国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 数据库 > 数据库应用 > PostgreSQL学习第十篇 数据库逻辑结构介绍--数据库

PostgreSQL学习第十篇 数据库逻辑结构介绍--数据库

来源:程序员人生   发布时间:2017-02-17 09:14:25 阅读次数:4893次
PG中,数据的组织结构可以分为以下3层:

	1. 数据库:1个PostgreSQL数据库服务下可以管理多个数据库;
	2. 表、索引:注:1般的PG中,表的术语叫relation,而其他数据库中为table
	3. 数据行:注:PG中行的术语1般为tuple,而在其他数据库中则为row

在PostgreSQL中,1个数据库服务(或叫实例)下可以有多个数据库,而1个数据库不能属于多个实例。但是在Oracle中,1个实例只能有1个数据库,但1个数据库可以在多个实例中(rac)

数据库基本操作:创建数据库、删除数据库、修改数据库等

创建数据库:
create database db_name;
可以指定所属用户、模板(默许为templete1)、字符编码,表空间等

修改数据库:
alter database db_name with option;

可以修改连接限制、重命名数据库、修改数据库所属用户等

删除数据库:
drop database db_name;

示例:
postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
(3 rows)

postgres=# create database test owner=postgres template=template1 encoding=utf8 tablespace=pg_default connection limit=⑴;
CREATE DATABASE
postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
 test      | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ |                       | 7225 kB | pg_default |
(4 rows)

postgres=# alter database test rename to testn;
ALTER DATABASE
postgres=# create tablespace test location '/home/postgres/test';
CREATE TABLESPACE
postgres=# alter database testn tablespace test;
ALTER DATABASE
postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
 testn     | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ |                       | 7225 kB | test       |
(4 rows)

postgres=#
postgres=# drop database test;
ERROR:  database "test" does not exist
postgres=# drop database if exists test;
NOTICE:  database "test" does not exist, skipping
DROP DATABASE
postgres=# drop database if exists testn;
postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
(3 rows)


 

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生