PostgreSQL逻辑管理结构

这篇具有很好参考价值的文章主要介绍了PostgreSQL逻辑管理结构。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.数据库逻辑结构介绍

PostgreSQL逻辑管理结构,Postgres,数据库

2.数据库基本操作

2.1 创建数据库
CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace ]
[ CONNECTION LIMIT [=] connlimit ] ]

参数说明如下。

·OWNER [=] user_name:用于指定新建的数据库属于哪个用 户,如果不指定,新建的数据库就属于当前执行命令的用户。

·TEMPLATE [=] template:模板名(从哪个模板创建新数据 库),如果不指定,将使用默认模板数据库(template1)。

·[ENCODING [=] encoding]:创建新数据库使用的字符编码。 

·TABLESPACE [=] tablespace:用于指定和新数据库关联的表空 间名称。

·CONNECTION LIMIT [=] connlimit]:用于指定数据库可以接受 多少并发的连接。默认值为“-1”,表示没有限制。

postgres=# 
postgres=# CREATE DATABASE osdbadb;
CREATE DATABASE
postgres=#

postgres=# CREATE DATABASE testdb01 ENCODING 'UTF-8' TEMPLATE template0;
CREATE DATABASE
postgres=#
2.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 {TO |=}
{value|DEFAULT}。
·ALTER DATABASE name SET configuration_parameter FROM
CURRENT。
·ALTER DATABASE name RESET configuration_parameter。
·ALTER DATABASE name RESET ALL。

示例1,将数据库“testdb01”的最大连接数修改为“10”,命令 如下:

示例2,将数据库“testdb01”的名称改为“mydb01”,命令 如下:

示例3,改变数据库“testdb01”的配置参数,使用户一旦连接到 这个用户,某个配置参数就设置为指定的值。比如,关闭在数据库 “testdb01”上的默认索引扫描,命令如下:

postgres=# 
postgres=# 
postgres=# alter database testdb01 CONNECTION LIMIT 10;
ALTER DATABASE
postgres=# 
postgres=# 
postgres=# 
postgres=# 
postgres=# alter database testdb01 rename to mydb01;
ALTER DATABASE
postgres=# 
postgres=# 
postgres=# 
postgres=# 
postgres=# 
postgres=# ALTER DATABASE mydb01 SET enable_indexscan TO off;        
ALTER DATABASE
postgres=# 
2.3 删除数据库
postgres=# 
postgres=# drop database testdb01;
ERROR:  database "testdb01" does not exist
postgres=# 
postgres=# drop database if exists mydb01;
DROP DATABASE
postgres=# 

注意,如果还有用户连接在这个数据库上,将无法删除该数据 库。

2.4 常见问题

PostgreSQL逻辑管理结构,Postgres,数据库

PostgreSQL逻辑管理结构,Postgres,数据库

3. 模式

模式是数据库领域的一个基本概念,有些数据库把模式和用户合 二为一了,而PostgreSQL是有清晰的模式定义。

3.1 什么是模式

模式(Schema)是数据库中的一个概念,可以将其理解为一个命 名空间或目录,不同的模式下可以有相同名称的表、函数等对象而不 会产生冲突。提出模式的概念是为了便于管理,只要有权限,各个模 式的对象可以互相调用。

·允许多个用户使用同一个数据库且用户之间又不会互相干扰。

·把数据库对象放在不同的模式下组织成逻辑组,使数据库对象 更便于管理。

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

3.2 模式的使用
postgres=# create schema maxdba;
CREATE SCHEMA
postgres=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 maxdba | postgres
 public | postgres
(2 rows)

postgres=# drop schema maxdba;
DROP SCHEMA
postgres=# 
postgres=# create schema authorization postgres;
CREATE SCHEMA
postgres=# 
postgres=# \dn
   List of schemas
   Name   |  Owner   
----------+----------
 postgres | postgres
 public   | postgres
(2 rows)

postgres=#

在模式中可以修改名称和属主,

语法格式如下:

ALTER SCHEMA name RENAME TO newname

ALTER SCHEMA name OWNER TO newowner

postgres=# 
postgres=# create schema postgres
postgres-# CREATE TABLE t1 (id int, title text)
postgres-# CREATE TABLE t2 (id int, content text)
postgres-# CREATE VIEW v1 AS SELECT a.id,a.title, b.content FROM t1 a,t2 b where a.id=b.id;
CREATE SCHEMA
postgres=# \d
             List of relations
  Schema  |    Name     | Type  |  Owner   
----------+-------------+-------+----------
 postgres | t1          | table | postgres
 postgres | t2          | table | postgres
 postgres | v1          | view  | postgres
 public   | class       | table | postgres
 public   | ipdb1       | table | postgres
 public   | ipdb2       | table | postgres
 public   | jtest01     | table | postgres
 public   | jtest02     | table | postgres
 public   | jtest03     | table | postgres
 public   | score       | table | postgres
 public   | student     | table | postgres
 public   | student_bak | table | postgres
 public   | t           | table | postgres
 public   | test02      | table | postgres
 public   | test1       | table | postgres
 public   | testtab05   | table | postgres
 public   | testtab06   | table | postgres
 public   | testtab07   | table | postgres
 public   | testtab08   | table | postgres
 public   | testtab09   | table | postgres
(20 rows)

postgres=# alter schema postgres rename to postgresold;
ALTER SCHEMA
postgres=# \dn
    List of schemas
    Name     |  Owner   
-------------+----------
 maxdba      | postgres
 osdba       | postgres
 postgresold | postgres
 public      | postgres
(4 rows)

postgres=#

搜索路径中的第一个模式叫当前模式。除了是搜索的第一个模式 之外,它还是在CREATE TABLE没有声明模式名时新建表所属的模式。 要显示当前搜索路径,使用下面的命令:

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

postgres=# 
3.3 模式的搜索路径
postgres=# 
postgres=# 
postgres=# show search_path;
   search_path   
-----------------
 "$user", public
(1 row)

postgres=# 

4.表

4.1 创建表
postgres=# create table test01(id int primary key, note
postgres(# varchar(20));
CREATE TABLE
postgres=# create table test02(id1 int, id2 int, note
postgres(# varchar(20), CONSTRAINT pk_test02 primary key(id1,id2));
ERROR:  relation "test02" already exists
postgres=# drop table test02;
DROP TABLE
postgres=# create table test02(id1 int, id2 int, note
postgres(# varchar(20), CONSTRAINT pk_test02 primary key(id1,id2));
CREATE TABLE
postgres=# drop test03;
ERROR:  syntax error at or near "test03"
LINE 1: drop test03;
             ^
postgres=# drop table test03;
ERROR:  table "test03" does not exist
postgres=# create table test03(id1 int, id2 int, id3 int,
postgres(# note varchar(20), CONSTRAINT pk_test03 primary
postgres(# key(id1,id2), CONSTRAINT uk_test03_id3 UNIQUE(id3));
CREATE TABLE
postgres=# 
postgres=# CREATE TABLE child(name varchar(20), age int,
postgres(# note text, CONSTRAINT ck_child_age CHECK(age <18));
CREATE TABLE
postgres=# CREATE TABLE baby (LIKE child);
CREATE TABLE
postgres=# \d child;
                      Table "public.child"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 name   | character varying(20) |           |          | 
 age    | integer               |           |          | 
 note   | text                  |           |          | 
Check constraints:
    "ck_child_age" CHECK (age < 18)

postgres=# \d baby
                       Table "public.baby"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 name   | character varying(20) |           |          | 
 age    | integer               |           |          | 
 note   | text                  |           |          | 

postgres=# CREATE TABLE baby2 (LIKE child INCLUDING
postgres(# ALL);
CREATE TABLE
postgres=# \d baby2
                      Table "public.baby2"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 name   | character varying(20) |           |          | 
 age    | integer               |           |          | 
 note   | text                  |           |          | 
Check constraints:
    "ck_child_age" CHECK (age < 18)

postgres=# CREATE TABLE baby2 AS SELECT * FROM child WITH
postgres-# NO DATA;
ERROR:  relation "baby2" already exists
postgres=# CREATE TABLE baby3 AS SELECT * FROM child WITH 
NO DATA;
CREATE TABLE AS
postgres=# 
4.2 约束

·检查约束。

postgres=# CREATE TABLE persons (
postgres(# name varchar(40),
postgres(# age int CONSTRAINT check_age CHECK (age >= 0 and age
postgres(# <=150),
postgres(# sex boolean
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE books (
postgres(# book_no integer,
postgres(# name text,
postgres(# price numeric CHECK (price > 0),
postgres(# discounted_price numeric CHECK (discounted_price > 0),
postgres(# CHECK (price > discounted_price)
postgres(# );
CREATE TABLE
postgres=# 

·非空约束。

非空约束只是简单地声明一个字段必须不能是NULL。

postgres=# 
postgres=# CREATE TABLE books1 (
postgres(# book_no integer not null,
postgres(# name text,
postgres(# price numeric
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE books2 (
postgres(# book_no integer NOT NULL,
postgres(# name text,
postgres(# price numeric NOT NULL CHECK (price >0)
postgres(# );
CREATE TABLE
postgres=# 

·唯一约束。

唯一约束可以保证在一个字段或者一组字段中的数据相较于表中 其他行的数据是唯一的。

postgres=# 
postgres=# CREATE TABLE books3 (
postgres(# book_no integer UNIQUE,
postgres(# name text,
postgres(# price numeric
postgres(# );
CREATE TABLE
postgres=# 
postgres=# 
postgres=# 
postgres=# 
postgres=# 
postgres=# CREATE TABLE books4 (
postgres(# book_no integer,
postgres(# name text,
postgres(# price numeric,
postgres(# UNIQUE(book_no)
postgres(# );
CREATE TABLE
postgres=# 

·主键。

主键与唯一约束的区别是,主键不能为空。通常我们是建表时就 指定了主键:

postgres=# 
postgres=# CREATE TABLE books5 (
postgres(# book_no integer primary key,
postgres(# name text,
postgres(# price numeric,
postgres(# UNIQUE(book_no)
postgres(# );
CREATE TABLE
postgres=# ALTER TABLE books add constraint pk_books_book_no primary
postgres-# key (book_no);
ALTER TABLE
postgres=# 

·外键约束。

外键约束是对表之间关系的一种约束,用于约束本表中一个或多 个字段的数值必须出现在另一个表的一个或多个字段中。这种约束也 可以称为两个相关表之间的参照完整性约束。

postgres=# 
postgres=# CREATE TABLE class(
postgres(# class_no int primary key,
postgres(# class_name varchar(40)
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE student(
postgres(# student_no int primary key,
postgres(# student_name varchar(40),
postgres(# age int,
postgres(# class_no int REFERENCES class(class_no)
postgres(# );
CREATE TABLE
postgres=# select * from class;
 class_no | class_name 
----------+------------
(0 rows)

postgres=# insert into student values(1,'张三',13,10);
ERROR:  insert or update on table "student" violates foreign key constraint "student_class_no_fkey"
DETAIL:  Key (class_no)=(10) is not present in table "class".
postgres=# 

4.3 修改表

·增加字段。

·删除字段。

·增加约束。

·删除约束。

·修改默认值。

·删除默认值。

·修改字段数据类型。

·重命名字段。

·重命名表。文章来源地址https://www.toymoban.com/news/detail-741338.html

到了这里,关于PostgreSQL逻辑管理结构的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【postgresql 基础入门】数据库服务的管理

    ​ 专栏内容 : postgresql内核源码分析 手写数据库toadb 并发编程 ​ 开源贡献 : toadb开源库 个人主页 :我的主页 管理社区 :开源数据库 座右铭:天行健,君子以自强不息;地势坤,君子以厚德载物. 初始化集群 数据库服务管理 postgresql 数据库是一款通用的关系型数据,在开

    2024年02月10日
    浏览(37)
  • postgres篇---python连接postgres数据库2

    2024年02月11日
    浏览(36)
  • postgres篇---docker安装postgres,python连接postgres数据库

    Docker是一种开源容器化平台,它允许开发人员在轻松可移植的环境中构建、部署和运行应用程序。PostgreSQL是一种强大的关系型数据库,它是一种开源软件,提供了高可靠性、可扩展性和安全性。 在Docker中使用PostgreSQL,您可以通过Docker Hub上的PostgreSQL官方镜像来创建一个容器

    2024年02月10日
    浏览(38)
  • 数据库管理系统PostgreSQL部署安装完整教程

            PostgreSQL是一个开源的关系型数据库管理系统,它支持大量的数据类型和复杂的查询语言,可以用于各种应用程序。它是一个高性能的数据库,可以处理大量的数据,并且具有良好的可扩展性和可靠性。 目录 一.Linux系统安装PostgresSQL(Centos7) 1.更新yun源 2.安装Po

    2024年02月09日
    浏览(58)
  • PGSQL(PostgreSQL)数据库基础篇:PostgreSQL 的 主要优点 、 劣势 、体系结构 、核心功能 、安装教程。

    1.维护者是PostgreSQL Global Development Group,首次发布于1989年6月。 2.操作系统支持WINDOWS、Linux、UNIX、MAC OS X、BSD。 3.从基本功能上来看,支持ACID、关联完整性、数据库事务、Unicode多国语言。 4.表和视图方面,PostgreSQL支持临时表,而物化视图,可以使用PL/pgSQL、PL/Perl、PL/Python或其

    2024年04月26日
    浏览(46)
  • DBeaver数据库管理工具安装连接PostgreSQL和DM

    1. 安装 下载地址 https://dbeaver.io/download/ 2. 连接PostgreSQL 配置显示所有数据库 第二个勾选会显示模板数据库 点击 测试连接 ,然后下载驱动 连接成功 3. 连接DM8 3.1 下载驱动 地址 https://eco.dameng.com/document/dm/zh-cn/app-dev/java_Mybatis_frame.html 下载完成解压,将需要的 DmJdbcDriver18.jar 放到

    2024年02月16日
    浏览(39)
  • postgresql 使用之 存储架构 触摸真实数据的存储结构以及组织形式,存入数据库的数据原来在这里

    ​ 专栏内容 : postgresql内核源码分析 手写数据库toadb 并发编程 个人主页 :我的主页 座右铭:天行健,君子以自强不息;地势坤,君子以厚德载物. postgresql 数据库服务运行时,数据在磁盘上是如何存储的呢?这就涉及到了存储架构。 在文件系统中,我们可以看到以目录和文

    2024年02月14日
    浏览(31)
  • PostgreSQL逻辑管理结构

    1.数据库逻辑结构介绍 2.数据库基本操作 2.1 创建数据库 参数说明如下。 ·OWNER [=] user_name:用于指定新建的数据库属于哪个用 户,如果不指定,新建的数据库就属于当前执行命令的用户。 ·TEMPLATE [=] template:模板名(从哪个模板创建新数据 库),如果不指定,将使用默认模

    2024年02月06日
    浏览(33)
  • PostgreSQL数据库动态共享内存管理器——Dynamic shared memory areas

    dsm.c提供的功能允许创建后端进程间共享的共享内存段。DSA利用多个DSM段提供共享内存heap;DSA可以利用已经存在的共享内存(DSM段)也可以创建额外的DSM段。和系统heap使用指针不同的是,DSA提供伪指针,可以转换为backend-local指针,但是该伪指针可以在后端进程之间共享,可

    2024年02月15日
    浏览(34)
  • 新增PostgreSQL数据库管理功能,1Panel开源面板v1.9.3发布

    2024年1月15日,现代化、开源的Linux服务器运维管理面板1Panel正式发布v1.9.3版本。 在这一版本中,1Panel新增了PostgreSQL数据库管理功能,并且支持设置PHP运行环境扩展模版。此外,我们进行了30多项功能更新和问题修复。1Panel应用商店新增了3款应用,并且更新了22款应用。感谢社

    2024年01月18日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包