当前位置: 首页 > news >正文

PostgreSQL逻辑管理结构

1.数据库逻辑结构介绍

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 常见问题

3. 模式

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

3.1 什么是模式

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

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

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

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

3.2 模式的使用
postgres=# create schema maxdba;
CREATE SCHEMA
postgres=# \dnList of schemasName  |  Owner   
--------+----------maxdba | postgrespublic | postgres
(2 rows)postgres=# drop schema maxdba;
DROP SCHEMA
postgres=# 
postgres=# create schema authorization postgres;
CREATE SCHEMA
postgres=# 
postgres=# \dnList of schemasName   |  Owner   
----------+----------postgres | postgrespublic   | 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=# \dList of relationsSchema  |    Name     | Type  |  Owner   
----------+-------------+-------+----------postgres | t1          | table | postgrespostgres | t2          | table | postgrespostgres | v1          | view  | postgrespublic   | class       | table | postgrespublic   | ipdb1       | table | postgrespublic   | ipdb2       | table | postgrespublic   | jtest01     | table | postgrespublic   | jtest02     | table | postgrespublic   | jtest03     | table | postgrespublic   | score       | table | postgrespublic   | student     | table | postgrespublic   | student_bak | table | postgrespublic   | t           | table | postgrespublic   | test02      | table | postgrespublic   | test1       | table | postgrespublic   | testtab05   | table | postgrespublic   | testtab06   | table | postgrespublic   | testtab07   | table | postgrespublic   | testtab08   | table | postgrespublic   | testtab09   | table | postgres
(20 rows)postgres=# alter schema postgres rename to postgresold;
ALTER SCHEMA
postgres=# \dnList of schemasName     |  Owner   
-------------+----------maxdba      | postgresosdba       | postgrespostgresold | postgrespublic      | 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 babyTable "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 baby2Table "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 修改表

·增加字段。

·删除字段。

·增加约束。

·删除约束。

·修改默认值。

·删除默认值。

·修改字段数据类型。

·重命名字段。

·重命名表。

相关文章:

PostgreSQL逻辑管理结构

1.数据库逻辑结构介绍 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 L…...

高匿IP有什么作用

在互联网的蓬勃发展中&#xff0c;IP地址作为网络通信的基础&#xff0c;一直扮演着举足轻重的角色。而在诸多IP地址中&#xff0c;高匿IP地址则是一种特殊类型&#xff0c;其作用和价值在某些特定场合下尤为突出。那么&#xff0c;高匿IP地址究竟有哪些用处呢&#xff1f; 首先…...

Ubuntu Linux 23.10安装manimgl

1. 简介&#xff1a;manimgl是使用Python语言开发数学动画的一个库。用来创建数学动画。版本有很多&#xff0c;今天介绍manimgl&#xff0c;他要依赖OpenGL库。 2. 打开Shell命令行&#xff0c;连接上互联网。先安装opengl。 Shell>>> sudo apt install l…...

modesim verilog仿真验证基本流程(新建工程方式)

文章目录 环境搭建一、在modelsim里创建一个新的工程二、新建verilog设计文件及仿真激励文件三、仿真结果本文演示如何使用modelsim新建工程进行功能仿真。 环境搭建 本文中采用的modelsim版本如下: modelsim altera 10.3d一、在modelsim里创建一个新的工程 打开modelsim软…...

SpringBoot+AOP+自定义注解,优雅实现日志记录

文章目录 前言准备阶段1、数据库日志表2、自定义注解编写3、AOP切面类编写4、业务层4.1、Service 层&#xff1a;4.2 Service 实现层&#xff1a; 5、测试 前言 首先我们看下传统记录日志的方式是什么样的&#xff1a; DeleteMapping("/deleteUserById/{userId}") …...

多式联运路径优化问题:基于拓扑排序的遗传算法染色体编码

一、什么是拓扑排序 在图论中&#xff0c;拓扑排序&#xff08;Topological Sorting&#xff09;是一个有向无环图&#xff08;DAG, Directed Acyclic Graph&#xff09;的所有顶点的线性序列。且该序列必须满足下面两个条件&#xff1a; 每个顶点出现且只出现一次。若存在一…...

Go 方法集合与选择receiver类型

Go 方法集合与选择receiver类型 文章目录 Go 方法集合与选择receiver类型一、receiver 参数类型对 Go 方法的影响二、选择 receiver 参数类型原则2.1 选择 receiver 参数类型的第一个原则2.2 选择 receiver 参数类型的第二个原则 三、方法集合&#xff08;Method Set&#xff0…...

Unity AudioClip和PCM音频数据的转化

1 PCM音频数据转化AudioClip 假设PCM音频当前是16Khz采样率&#xff0c;16bit数据 byte[] pcmBytesnew byte[10240];float[] floatClipData new float[audioBytes.Length/2];for (int i 0; i < audioBytes.Length; i2){ floatData[i / 2] (short)((audioBytes[i 1] <…...

linux配置vlan后网络不通

如果在Linux上配置了VLAN&#xff0c;但网络不通&#xff0c;这可能是由于多种原因导致的。以下是一些可能的原因和解决方法&#xff1a; 检查物理连接&#xff1a;首先&#xff0c;确保VLAN支持的物理网络连接正常。确保网络电缆连接正确&#xff0c;交换机端口配置正确&#…...

GORM:在Go中轻松管理数据库

GORM综合介绍 - Go对象关系映射库 在现代软件开发中&#xff0c;高效的数据库管理对于构建强大的应用程序至关重要。GORM是Go开发人员寻求与数据库进行交互的简化方式的宝贵工具。GORM是Go对象关系映射的缩写&#xff0c;它为Go的面向对象世界与数据库的关系世界之间提供了桥梁…...

Ubuntu18.04 下PCL的卸载与安装

目录 一、卸载有问题的PCL1.7 二、编译&&安装PCL1.8.1 2.1、安装PCL依赖 2.2、编译VTK 2.3、编译PCL源码 三、 总结 写这篇博客时&#xff0c;本文方法已经在笔记本Ubuntu和VM虚拟机成功安装PCL1.8.1&#xff0c;并且通过测试。 下文方法同样适用于ubuntu18.04。…...

SMTP邮件发送图片-如何在github中存储图片并访问

之前写了一篇文章 Go&#xff1a;实现SMTP邮件发送订阅功能&#xff08;包含163邮箱、163企业邮箱、谷歌gmail邮箱&#xff09;&#xff0c;实现了通过邮箱服务来发送邮件&#xff0c;但都是文字内容&#xff0c;要是想实现邮件发送图片&#xff0c;就需要将图片放到公网可访问…...

2023年软件系统架构师论文【回忆版】

2023年11月5日&#xff0c;全国计算机等级下半年考试&#xff0c;北京市软件架构师考试其中有个考点在首都经济贸易大学丰台校区&#xff09;&#xff0c;地址&#xff1a;北京市丰台区花乡张家路口121号&#xff08;北门入校&#xff09; 注意&#xff1a;机考的考试时间有所变…...

【使用python实现文件视频格式的转换】

1.视频格式转换有哪些常用方法&#xff1f; 视频格式转换的常用方法有以下几种&#xff1a; 使用专业的视频转换软件&#xff1a;这些软件可以支持多种视频格式之间的转换&#xff0c;如Adobe Premiere Pro、Final Cut Pro等。使用在线视频转换工具&#xff1a;有许多在线视频…...

新媒体运营的营销方案

一、目标客户群体 新媒体运营是通过社交媒体、短视频、直播等方式将信息快速传播出去&#xff0c;因此&#xff0c;适合的目标客户群体应该是年轻人群体&#xff0c;包括大学生、职场青年、年轻家庭等。 二、营销策略 1、社交媒体营销策略 借助社交媒体平台&#xff0c;建立企…...

Flutter 05 组件状态、生命周期、数据传递(共享)、Key

一、Android界面渲染流程UI树与FlutterUI树的设计思路对比 二、Widget组件生命周期详解 1、Widget组件生命周期 和其他的视图框架比如android的Activity一样&#xff0c;flutter中的视图Widget也存在生命周期&#xff0c;生命周期的回调函数体现在了State上面。组件State的生命…...

2.Vue3项目(二):vue项目创建,项目必需的基础依赖配置,项目集成各种第三方依赖

目录 一、环境配置 1.下载node.js 2.pnpm的配置 二、创建项目 1.先创建好项目文件夹...

【Mybatis源码】注册器 - TypeAliasRegistry

Mybatis中使用TypeAliasRegistry注册器用于管理类型与别名,Mybatis中许多功能的实现都需要从TypeAliasRegistry注册器中找到别名对应的类型,本篇我们介绍一下TypeAliasRegistry注册器的原理与使用 一、构造方法 TypeAliasRegistry注册器类提供了一个无参数的构造方法用于创…...

【wp】2023鹏城杯初赛 Web web1(反序列化漏洞)

考点&#xff1a; 常规的PHP反序列化漏洞双写绕过waf 签到题 源码&#xff1a; <?php show_source(__FILE__); error_reporting(0); class Hacker{private $exp;private $cmd;public function __toString(){call_user_func(system, "cat /flag");} }class A {p…...

三顾茅庐,七面阿里,成功上岸25k16薪,我行你也行~

写在片头&#xff1a;声明&#xff0c;勿杠 首先简单说一下&#xff0c;这三次面试阿里并不是一次性去面的&#xff0c;实际上第一次面试时候还在大四&#xff0c;找的实习岗&#xff0c;不太清楚是什么部门&#xff0c;别问我为什么还记得面试题&#xff0c;有记录和复盘的习…...

如何在看板中体现优先级变化

在看板中有效体现优先级变化的关键措施包括&#xff1a;采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中&#xff0c;设置任务排序规则尤其重要&#xff0c;因为它让看板视觉上直观地体…...

《通信之道——从微积分到 5G》读书总结

第1章 绪 论 1.1 这是一本什么样的书 通信技术&#xff0c;说到底就是数学。 那些最基础、最本质的部分。 1.2 什么是通信 通信 发送方 接收方 承载信息的信号 解调出其中承载的信息 信息在发送方那里被加工成信号&#xff08;调制&#xff09; 把信息从信号中抽取出来&am…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”

目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...

OPENCV形态学基础之二腐蚀

一.腐蚀的原理 (图1) 数学表达式&#xff1a;dst(x,y) erode(src(x,y)) min(x,y)src(xx,yy) 腐蚀也是图像形态学的基本功能之一&#xff0c;腐蚀跟膨胀属于反向操作&#xff0c;膨胀是把图像图像变大&#xff0c;而腐蚀就是把图像变小。腐蚀后的图像变小变暗淡。 腐蚀…...

Python+ZeroMQ实战:智能车辆状态监控与模拟模式自动切换

目录 关键点 技术实现1 技术实现2 摘要&#xff1a; 本文将介绍如何利用Python和ZeroMQ消息队列构建一个智能车辆状态监控系统。系统能够根据时间策略自动切换驾驶模式&#xff08;自动驾驶、人工驾驶、远程驾驶、主动安全&#xff09;&#xff0c;并通过实时消息推送更新车…...

华为OD机试-最短木板长度-二分法(A卷,100分)

此题是一个最大化最小值的典型例题&#xff0c; 因为搜索范围是有界的&#xff0c;上界最大木板长度补充的全部木料长度&#xff0c;下界最小木板长度&#xff1b; 即left0,right10^6; 我们可以设置一个候选值x(mid)&#xff0c;将木板的长度全部都补充到x&#xff0c;如果成功…...

Unity VR/MR开发-VR开发与传统3D开发的差异

视频讲解链接&#xff1a;【XR马斯维】VR/MR开发与传统3D开发的差异【UnityVR/MR开发教程--入门】_哔哩哔哩_bilibili...

鸿蒙HarmonyOS 5军旗小游戏实现指南

1. 项目概述 本军旗小游戏基于鸿蒙HarmonyOS 5开发&#xff0c;采用DevEco Studio实现&#xff0c;包含完整的游戏逻辑和UI界面。 2. 项目结构 /src/main/java/com/example/militarychess/├── MainAbilitySlice.java // 主界面├── GameView.java // 游戏核…...

Java多线程实现之Runnable接口深度解析

Java多线程实现之Runnable接口深度解析 一、Runnable接口概述1.1 接口定义1.2 与Thread类的关系1.3 使用Runnable接口的优势 二、Runnable接口的基本实现方式2.1 传统方式实现Runnable接口2.2 使用匿名内部类实现Runnable接口2.3 使用Lambda表达式实现Runnable接口 三、Runnabl…...