First introduction to PostgreSQL (1)

Chinese translation website: Document Index: The world's most powerful open source database...

Official website download address: Community DL Page

Windows 10 installation ignores (third-party and graphical visualization tools are selected for installation)

1. Start using

1. Start the postgreSQL service

Enter the service installation location/bin, execute the cmd command, and start the service through the command

pg_ctl.exe -D "D:\Program Files\PostgreSQL\12\data" start

#Description: Replace D:\Program Files\PostgreSQL\12\data with the actual /data storage directory during installation

If the following results appear during execution, the startup is successful!

Startup exception, solution:

1. Delete the existing /data directory

2. Reinitialize the /data storage file and execute

initdb.exe -D "data storage location" -E UTF-8 --locale=chs -U postgres -W

Example: C:\Program Files\PostgreSQL\12\data #This is the default location

2. Connect to the database locally and create test libraries and tables. This time, Navicat Premium 15 version is used to connect.

Create a test database - test_db

Create a test data table - test sets the table to auto-increment the primary key

(1) Create a test data table

CREATE TABLE "public"."test" ( "id" int4 NOT NULL, "name" varchar(255) COLLATE "pg_catalog"."default", "create_time" timestamp(6), CONSTRAINT "test_pkey" PRIMARY KEY ("id") ) ; ALTER TABLE "public"."test" OWNER TO "postgres"; COMMENT ON COLUMN "public"."test"."id" IS '主键'; COMMENT ON COLUMN "public"."test"."name" IS '姓名'; COMMENT ON COLUMN "public"."test"."create_time" IS '时间';

(2) Create a primary key auto-increment sequence (select table—>Others—>New sequence)

(3) Primary key column association sequence settings

nextval('sequence name'::regclass)

(4) Support data type description

name

Alias

describe

bigint

you8

Signed 8-byte integer

bigserial

serial8

Auto-increasing 8-byte integer

bit [ (n) ]

Fixed length bit string

bit varying [ (n) ]

wander [ (n) ]

variable length bit string

boolean

bool

Logical boolean (true/false)

box

Ordinary box on plane

bytea

binary data (

"byte array"

character [ (n) ]

char [ (n) ]

Fixed length string

character varying [ (n) ]

varchar [ (n) ]

variable length string

cidr

IPv4 or IPv6 network address

circle

circle on plane

date

Calendar date (year, month, day)

double precision

float8

Double precision floating point number (8 bytes)

inet

IPv4 or IPv6 host address

integer

int

int4

you8

-32768~ +32767

-‭21474836478 ~ ‭2147483647‬

-9223372036854775808 ~ +9223372036854775807

interval [ fields ] [ (p) ]

period

json

Text JSON data

jsonb

Binary JSON data, decomposed

line

Infinitely long line on the plane

lsg

line segment on plane

macadr

MAC (Media Access Control) address

Macaddr8

MAC (Media Access Control) address (EUI-64 format)

money

currency quantity

numeric [ (p, s) ]

decimal [ (p, s) ]

Exact numbers with selectable precision

path

geometric path on plane

pg_lsn

PostgreSQL

Log sequence number

point

Geometric points on the plane

polygon

Closed geometric path on plane

real

float4

Single precision floating point number (4 bytes)

smallint

int2

Signed 2-byte integer

smallserial

serial2

Auto-increasing 2-byte integer

serial

serial4

Auto-increasing 4-byte integer

text

variable length string

time [ (p) ] [ without time zone ]

Time of day (no time zone)

time [ (p) ] with time zone

timetz

Time of day, including time zone

timestamp [ (p) ] [ without time zone ]

Date and time (no time zone)

timestamp [ (p) ] with time zone

timestamptz

Date and time, including time zone

tsquery

Text search query

tsvector

文本搜索文档

txid_snapshot

用户级别事务ID快照

uuid

通用唯一标识码

xml

XML数据

(5)、事务说明

在PostgreSQL中,开启一个事务需要将SQL命令用BEGIN和COMMIT命令包围起来。事务提交

BEGIN; UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; -- etc etc COMMIT;

记住那个银行数据库,假设我们从Alice的账户扣款100美元,然后存款到Bob的账户,结果直到最后才发现我们应该存到Wally的账户。我们可以通过使用保存点来做这件事:

事务回退 SAVEPOINT和 ROLLBACK

BEGIN; UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; SAVEPOINT my_savepoint; UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Bob'; -- oops ... forget that and use Wally's account ROLLBACK TO my_savepoint; UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Wally'; COMMIT;

(6)、索引说明

Btree索引

Btree索引使用Btree数据结构来存储索引数据,可用于处理等值查询和范围查询,包括<、<=、=、>=、>等运算符,以及BETWEEN、IN、IS NULL、IS NOT NULL 等条件。

Btree索引可用于模式匹配查询,如“col LIKE′foo%′”或“col~′^foo′”,但是不能用于“col LIKE′%bar′”之类的后缀模糊匹配查询。Btree索引还可以用于查询结果集排序,如ORDER BY排序。

Hash索引

Hash索引根据每一行数据的索引字段计算哈希码,并维护哈希码、记录指针对应关系。对于哈希码相同的数据来说,可以采用链表来解决冲突。Hash索引的查询速度很快。

GiST索引

GiST(Generalized Search Tree)是一种平衡的树型结构访问方法,可作为一种基础模板来实现任意索引模式。B-tree索引和许多其他的索引模式都可以用GiST索引来实现。GiST 索引适用于多维数据类型和集合数据类型。GiST 多列索引支持在查询条件中包含索引字段的子集。PostgreSQL包含了全文检索、几何数据类型等多个用GiST实现的索引方法。

SP-GiST索引

SP-GiST索引与GiST索引类似,可作为一种基础模板来实现多种搜索方法。SP-GiST索引主要实现非平衡的基于硬盘的数据结构,如四叉树、k-d树和radix树。

GIN索引

GIN索引是一种通用倒排索引(GIN stands for generalized inverted indexes),可以处理包含多个键值,如 hstore, array,jsonb, 和 range 类型。用它来全文搜索或JSON键值的效率很高。GIN 允许用户开发自定义访问方法的数据类型索引,可以支持多种不同用户定义的索引策略。

BRIN索引

BRIN索引BRIN表示块范围索引。BRIN索引存储连续相邻的数据块统计信息,可以大大缩小索引占用空间。对于数据量比较大的表来说,BRIN索引比B-Tree索引插入数据的速度要快,两者的范围查询效率相当。BRIN索引通常用于线性顺序排列的列,如订单表的创建日期。

布尔过滤索引

Bloom Filter is a space-efficient random data structure that uses a bit array to represent a set concisely and can determine whether an element belongs to the set. This high efficiency of Bloom Filter comes at a certain cost: when determining whether an element belongs to a certain set, elements that do not belong to this set may be mistaken for belonging to this set (false positive). Therefore, Bloom Filter is not suitable for those "zero error" applications. In applications that can tolerate low error rates, Bloom Filter exchanges very few errors for great savings in storage space.

It is not recommended to use indexes in the following scenarios:

Do not use indexes for tables with small data volumes

Tables with frequent large-volume insert and update operations do not need to be indexed

There are a large number of null values ​​on the column and the index should not be built.

Column values ​​are frequently maintained and indexes should not be built.

Guess you like

Origin blog.csdn.net/qq_42697946/article/details/130343769