微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

PG中,数据的组织结构可以分为以下三层:

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

在Postgresql中,一个数据库服务(或者叫实例)下可以有多个数据库,而一个数据库不能属于多个实例。然而在Oracle中,一个实例只能有一个数据库,但一个数据库可以在多个实例中(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-8 | en_US.UTF-8 |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =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=-1;
CREATE DATABASE
postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 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-8 | en_US.UTF-8 |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
 testn     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 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-8 | en_US.UTF-8 |                       | 7359 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7225 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
(3 rows)


 

原文地址:https://www.jb51.cc/postgresql/194147.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐