PostgreSQL
Relational Database
Official website: https://www.postgresql.org/
Download link: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
Database
Create database
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
postgres | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
template0 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)
postgres=# create database mydb;
CREATE DATABASE
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
mydb | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
postgres | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
template0 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
Use database
postgres=# \c mydb;
您现在已经连接到数据库 "mydb",用户 "postgres".
Delete database
postgres=# drop database mydb
DROP DATABASE
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
postgres | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
template0 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)
Table
Create table
create table posts (
id serial primary key,
title varchar(255) not null,
content text check(length(content) > 8),
is_draft boolean default true,
is_del boolean default false,
created_date timestamp default 'now'
);
Table constraint
not null: can not be empty
unique: the value in all data must be unique
check: field setting conditions
default: default value
primary key (not null, unique) : 主键
View table
mydb=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------+--------+----------
public | mytable | 数据表 | postgres
public | posts | 数据表 | postgres
mydb=# \d posts;
数据表 "public.posts"
栏位 | 类型 | 校对规则 | 可空的 | 预设
--------------+-----------------------------+----------+----------+-----------------------------------------------------------
id | integer | | not null | nextval('posts_id_seq'::regclass)
title | character varying(255) | | not null |
content | text | | |
is_draft | boolean | | | true
is_del | boolean | | | false
created_date | timestamp without time zone | | | '2020-09-19 11:30:36.484723'::timestamp without time zone
索引:
"posts_pkey" PRIMARY KEY, btree (id)
检查约束限制
"posts_content_check" CHECK (length(content) > 8)
Delete table
mydb=# drop table mytable;
DROP TABLE
mydb=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+-------+--------+----------
public | posts | 数据表 | postgres
Table structure changes
-
alter table [tablename] …
-
create index …
-
drop index …
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
--------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
player | character varying(255) | | not null |
score | real | | |
team | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
mydb=# alter table users add fullname varchar(255);
ALTER TABLE
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
----------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
player | character varying(255) | | not null |
score | real | | |
team | character varying(255) | | |
fullname | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
mydb=# alter table users drop fullname;
ALTER TABLE
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
--------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
player | character varying(255) | | not null |
score | real | | |
team | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
mydb=# alter table users rename player to nba_player;
ALTER TABLE
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
------------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
nba_player | character varying(255) | | not null |
score | real | | |
team | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
mydb=# alter table users alter nba_player type varchar(100);
ALTER TABLE
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
------------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
nba_player | character varying(100) | | not null |
score | real | | |
team | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
mydb=# create index nba_player_idx on users(nba_player);
CREATE INDEX
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
------------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
nba_player | character varying(100) | | not null |
score | real | | |
team | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
"nba_player_idx" btree (nba_player)
mydb=# drop index nba_player_idx;
DROP INDEX
mydb=# \d users;
数据表 "public.users"
栏位 | 类型 | 校对规则 | 可空的 | 预设
------------+------------------------+----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
nba_player | character varying(100) | | not null |
score | real | | |
team | character varying(255) | | |
索引:
"users_pkey" PRIMARY KEY, btree (id)
index
By default, PostgreSQL allows reading tables (SELECT statements) while creating indexes, but writing to tables (INSERT, UPDATE, DELETE) will be blocked until the index is created
Index type
B-tree
By default, the CREATE INDEX command will create a B-tree index, which is suitable for most situations.
B-tree is suitable for processing equal and range queries on data that can be stored sequentially. Especially when an indexed field involves one of the following operators for comparison, PostgreSQL's query planner will consider the use of B-tree index. IS NULL or IS NOT NULL conditions in the index column can be combined with B-tree index use.
<
<=
=
>=
>
between
in
col like 'foo%'
col ~ '^foo
Hash
Hash index can only handle simple equality comparisons. When an indexed column involves the use of the = operator for comparison, the query planner will consider using the Hash index. The following command is used to create a Hash index:
CREATE INDEX name ON table USING hash (column);
GiST、SP-GiST、GIN
EVERYTHING
Field Type
Type introduction: https://www.postgresql.org/docs/12/datatype.html
Numerical
int
real (floating point)
serial (serial type-version)
Character type
char (immutable, add space TODO)
varchar (variable)
text
Boolean
boolean
Date type
data (date)
time (hours, minutes and seconds)
timestamp (year, month, day, hour, minute, second)
Featured Type
array
inet (network address type)
json
xml
INSERT statement
- insert into [tablename] (field, …) values (value, …);
When inserting SQL fails due to the check function, the auto-incremented ID will also be occupied.
mydb=# \d users;
Table "public.users"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
name | character varying(20) | | |
age | integer | | |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"u_user_age_idx" UNIQUE, btree (name, age)
"u_age_nn_idx" btree (age) WHERE age IS NOT NULL
Number of child tables: 1 (Use \d+ to list them.)
mydb=# SELECT * FROM USERS;
id | name | age
----+-------+-----
2 | Cindy | 16
4 | Dove |
5 | Awa | 14
6 | Seven | 18
1 | Bob | 46
(5 rows)
# 插入相同的 “名称” 和 “年龄” 失败 - 受唯一索引限制
mydb=# insert into users (name, age) values ('Bob', 46);
ERROR: duplicate key value violates unique constraint "u_user_age_idx"
DETAIL: Key (name, age)=(Bob, 46) already exists.
# 插入数据,如果已经存在则忽略
mydb=# insert into users (name, age) values ('Bob', 46) on conflict do nothing;
INSERT 0 0
# 插入数据,如果已经存在则更新 “年龄” 为 47
mydb=# insert into users (name, age) values ('Bob', 46) on conflict (name, age) do update set age = 47;
INSERT 0 1
mydb=# select * from users;
id | name | age
----+-------+-----
2 | Cindy | 16
4 | Dove |
5 | Awa | 14
6 | Seven | 18
1 | Bob | 47
(5 rows)
# 插入数据,如果已经存在则在原来的基础上 加2
mydb=# insert into users (name, age) values ('Bob', 47) on conflict (name, age) do update set age = users.age + 2;
INSERT 0 1
mydb=# select * from users;
id | name | age
----+-------+-----
2 | Cindy | 16
4 | Dove |
5 | Awa | 14
6 | Seven | 18
1 | Bob | 49
(5 rows)
# 插入数据,如果已经存在则在原来的基础上添加上新增的值
mydb=# insert into users (name, age) values ('Bob', 49) on conflict (name, age) do update set age = users.age + excluded.age;
INSERT 0 1
mydb=# select * from users; id | name | age
----+-------+-----
2 | Cindy | 16
4 | Dove |
5 | Awa | 14
6 | Seven | 18
1 | Bob | 98
(5 rows)
SELECT statement
mydb=# select id, player, score, team from users;
id | player | score | team
----+---------+-------+---------
1 | kuli | 28.3 | yongshi
2 | hadeng | 30.2 | huojian
3 | adu | 25.6 | yongshi
4 | azhan | 27.8 | qishi
5 | shengui | 31.3 | leiting
6 | baibian | 19.8 | rehuo
(6 行记录)
mydb=# select id, player, score, team from users where player like 'a%';
id | player | score | team
----+--------+-------+---------
3 | adu | 25.6 | yongshi
4 | azhan | 27.8 | qishi
(2 行记录)
# _ 为占位符
mydb=# select id, player, score, team from users where player like 'ad_';
id | player | score | team
----+--------+-------+---------
3 | adu | 25.6 | yongshi
(1 行记录)
mydb=# select id, player, score, team from users order by score desc;
id | player | score | team
----+---------+-------+---------
5 | shengui | 31.3 | leiting
2 | hadeng | 30.2 | huojian
1 | kuli | 28.3 | yongshi
4 | azhan | 27.8 | qishi
3 | adu | 25.6 | yongshi
6 | baibian | 19.8 | rehuo
(6 行记录)
# limit {取的条数} offset {偏移量}
mydb=# select id, player, score, team from users order by score desc limit 1 offset 0;
id | player | score | team
----+---------+-------+---------
5 | shengui | 31.3 | leiting
(1 行记录)
mydb=# select id, player, score, team from users order by score desc limit 1 offset 1;
id | player | score | team
----+--------+-------+---------
2 | hadeng | 30.2 | huojian
(1 行记录)
mydb=# select id, player, score, team from users order by score desc limit 2 offset 1;
id | player | score | team
----+--------+-------+---------
2 | hadeng | 30.2 | huojian
1 | kuli | 28.3 | yongshi
(2 行记录)
mydb=# select id, player, score, team from users order by score desc limit 2 offset 2;
id | player | score | team
----+--------+-------+---------
1 | kuli | 28.3 | yongshi
4 | azhan | 27.8 | qishi
(2 行记录)
mydb=# select team, max(score) from users group by team;
team | max
---------+------
yongshi | 28.3
qishi | 27.8
rehuo | 19.8
leiting | 31.3
huojian | 30.2
(5 行记录)
mydb=# select team, max(score) from users group by team having max(score) > 30;
team | max
---------+------
leiting | 31.3
huojian | 30.2
(2 行记录)
mydb=# select team, max(score) from users group by team having max(score) > 30 order by max(score);
team | max
---------+------
huojian | 30.2
leiting | 31.3
(2 行记录)
mydb=# select id, player, score, team, concat(player, '/', team) as "info" from users;
id | player | score | team | info
----+---------+-------+---------+-----------------
1 | kuli | 28.3 | yongshi | kuli/yongshi
2 | hadeng | 30.2 | huojian | hadeng/huojian
3 | adu | 25.6 | yongshi | adu/yongshi
4 | azhan | 27.8 | qishi | azhan/qishi
5 | shengui | 31.3 | leiting | shengui/leiting
6 | baibian | 19.8 | rehuo | baibian/rehuo
(6 行记录)
# 对数据进行随机排序 (random() 获取随机数)
mydb=# select id, player, score, team from users order by random();
id | player | score | team
----+---------+-------+---------
2 | hadeng | 30.2 | huojian
3 | adu | 25.6 | yongshi
6 | baibian | 19.8 | rehuo
5 | shengui | 31.3 | leiting
4 | azhan | 27.8 | qishi
1 | kuli | 28.3 | yongshi
(6 行记录)
# select for update
# [线程A] 开启事务
mydb=# begin;
BEGIN
# [线程A] 查询 ID = 1 的数据,并加锁
mydb=# select * from users where id = 1 for update;
id | name | age
----+------+-----
1 | Bob | 21
(1 row)
# [线程B] 更新阻塞
mydb=# update users set age = 22 where id = 1;
# [线程A] 提交事务
mydb=# commit;
# [线程B] 更新成功
UPDATE 1
Table association
mydb=# select * from twitters;
id | user_id | content
----+---------+----------------------------------
1 | 1 | 今天又是大胜,克莱打的真好!
2 | 2 | 今晚我得了60分,哈哈!
3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | 4 | 明年我也可能转会西部
5 | 5 | 我都双20+了,怎么球队就是不胜呢?
6 | 1 | 明年听说有条大鱼要来,谁呀?
(6 行记录)
mydb=# select * from users;
id | nba_player | score | team
----+------------+-------+---------
1 | kuli | 28.3 | yongshi
2 | hadeng | 30.2 | huojian
3 | adu | 25.6 | yongshi
5 | shengui | 31.3 | leiting
6 | baibian | 19.8 | rehuo
4 | azhan | 29.1 | qishi
(6 行记录)
mydb=# select * from users u, twitters t where u.id = t.user_id;
id | nba_player | score | team | id | user_id | content
----+------------+-------+---------+----+---------+----------------------------------
1 | kuli | 28.3 | yongshi | 1 | 1 | 今天又是大胜,克莱打的真好!
2 | hadeng | 30.2 | huojian | 2 | 2 | 今晚我得了60分,哈哈!
3 | adu | 25.6 | yongshi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | azhan | 29.1 | qishi | 4 | 4 | 明年我也可能转会西部
5 | shengui | 31.3 | leiting | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
1 | kuli | 28.3 | yongshi | 6 | 1 | 明年听说有条大鱼要来,谁呀?
(6 行记录)
mydb=# select * from users u join twitters t on u.id = t.user_id;
id | nba_player | score | team | id | user_id | content
----+------------+-------+---------+----+---------+----------------------------------
1 | kuli | 28.3 | yongshi | 1 | 1 | 今天又是大胜,克莱打的真好!
2 | hadeng | 30.2 | huojian | 2 | 2 | 今晚我得了60分,哈哈!
3 | adu | 25.6 | yongshi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | azhan | 29.1 | qishi | 4 | 4 | 明年我也可能转会西部
5 | shengui | 31.3 | leiting | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
1 | kuli | 28.3 | yongshi | 6 | 1 | 明年听说有条大鱼要来,谁呀?
(6 行记录)
# left join 等同与 left outer join
mydb=# select * from users u left join twitters t on u.id = t.user_id;
id | nba_player | score | team | id | user_id | content
----+------------+-------+---------+----+---------+----------------------------------
1 | kuli | 28.3 | yongshi | 1 | 1 | 今天又是大胜,克莱打的真好!
2 | hadeng | 30.2 | huojian | 2 | 2 | 今晚我得了60分,哈哈!
3 | adu | 25.6 | yongshi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | azhan | 29.1 | qishi | 4 | 4 | 明年我也可能转会西部
5 | shengui | 31.3 | leiting | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
1 | kuli | 28.3 | yongshi | 6 | 1 | 明年听说有条大鱼要来,谁呀?
6 | baibian | 19.8 | rehuo | | |
(7 行记录)
mydb=# select * from users u left outer join twitters t on u.id = t.user_id;
id | nba_player | score | team | id | user_id | content
----+------------+-------+---------+----+---------+----------------------------------
1 | kuli | 28.3 | yongshi | 1 | 1 | 今天又是大胜,克莱打的真好!
2 | hadeng | 30.2 | huojian | 2 | 2 | 今晚我得了60分,哈哈!
3 | adu | 25.6 | yongshi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | azhan | 29.1 | qishi | 4 | 4 | 明年我也可能转会西部
5 | shengui | 31.3 | leiting | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
1 | kuli | 28.3 | yongshi | 6 | 1 | 明年听说有条大鱼要来,谁呀?
6 | baibian | 19.8 | rehuo | | |
(7 行记录)
# cross join 为全部交叉关联 (此时数据量很大,慎用)
mydb=# select * from users u cross join twitters t;
id | nba_player | score | team | id | user_id | content
----+------------+-------+---------+----+---------+----------------------------------
1 | kuli | 28.3 | yongshi | 1 | 1 | 今天又是大胜,克莱打的真好!
2 | hadeng | 30.2 | huojian | 1 | 1 | 今天又是大胜,克莱打的真好!
3 | adu | 25.6 | yongshi | 1 | 1 | 今天又是大胜,克莱打的真好!
5 | shengui | 31.3 | leiting | 1 | 1 | 今天又是大胜,克莱打的真好!
6 | baibian | 19.8 | rehuo | 1 | 1 | 今天又是大胜,克莱打的真好!
4 | azhan | 29.1 | qishi | 1 | 1 | 今天又是大胜,克莱打的真好!
1 | kuli | 28.3 | yongshi | 2 | 2 | 今晚我得了60分,哈哈!
2 | hadeng | 30.2 | huojian | 2 | 2 | 今晚我得了60分,哈哈!
3 | adu | 25.6 | yongshi | 2 | 2 | 今晚我得了60分,哈哈!
5 | shengui | 31.3 | leiting | 2 | 2 | 今晚我得了60分,哈哈!
6 | baibian | 19.8 | rehuo | 2 | 2 | 今晚我得了60分,哈哈!
4 | azhan | 29.1 | qishi | 2 | 2 | 今晚我得了60分,哈哈!
1 | kuli | 28.3 | yongshi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
2 | hadeng | 30.2 | huojian | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
3 | adu | 25.6 | yongshi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
5 | shengui | 31.3 | leiting | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
6 | baibian | 19.8 | rehuo | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | azhan | 29.1 | qishi | 3 | 3 | 获胜咱不怕,缺谁谁尴尬.
1 | kuli | 28.3 | yongshi | 4 | 4 | 明年我也可能转会西部
2 | hadeng | 30.2 | huojian | 4 | 4 | 明年我也可能转会西部
3 | adu | 25.6 | yongshi | 4 | 4 | 明年我也可能转会西部
5 | shengui | 31.3 | leiting | 4 | 4 | 明年我也可能转会西部
6 | baibian | 19.8 | rehuo | 4 | 4 | 明年我也可能转会西部
4 | azhan | 29.1 | qishi | 4 | 4 | 明年我也可能转会西部
1 | kuli | 28.3 | yongshi | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
2 | hadeng | 30.2 | huojian | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
3 | adu | 25.6 | yongshi | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
5 | shengui | 31.3 | leiting | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
6 | baibian | 19.8 | rehuo | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
4 | azhan | 29.1 | qishi | 5 | 5 | 我都双20+了,怎么球队就是不胜呢?
1 | kuli | 28.3 | yongshi | 6 | 1 | 明年听说有条大鱼要来,谁呀?
2 | hadeng | 30.2 | huojian | 6 | 1 | 明年听说有条大鱼要来,谁呀?
3 | adu | 25.6 | yongshi | 6 | 1 | 明年听说有条大鱼要来,谁呀?
5 | shengui | 31.3 | leiting | 6 | 1 | 明年听说有条大鱼要来,谁呀?
Update and delete
-
update [tablename] set [field=newvalue, …] where …
-
delete from [tablename] where …
view
View concept
View is an object derived from one or more tables. A view is different from a table. The view is a virtual table, that is, the data corresponding to the view is not actually stored. Only the definition of the view is stored in the database. When operating on the data of the view, the system operates according to the definition of the view and is associated with the view. The basic table.
Simple understanding
A view is a SELECT statement, which simplifies the SELECT statement commonly used in the business system into an object similar to a table, which is easy to read and develop.
-
create view …
-
drop view …
# 创建视图
mydb=# create view curry_twitters as select u.nba_player, t.content from users as u, twitters as t where u.id = t.user_id and u.id = 1;
CREATE VIEW
# 查询所有视图
mydb=# \dv
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+----------------+------+----------
public | curry_twitters | 视图 | postgres
(1 行记录)
# 查看指定视图的定义
mydb=# \d curry_twitters;
视图 "public.curry_twitters"
栏位 | 类型 | 校对规则 | 可空的 | 预设
------------+------------------------+----------+--------+------
nba_player | character varying(100) | | |
content | character varying(255) | | |
mydb=# select * from curry_twitters;
nba_player | content
------------+----------------------------
kuli | 今天又是大胜,克莱打的真好!
kuli | 明年听说有条大鱼要来,谁呀?
(2 行记录)
mydb=# select * from twitters;
id | user_id | content
----+---------+----------------------------------
1 | 1 | 今天又是大胜,克莱打的真好!
2 | 2 | 今晚我得了60分,哈哈!
3 | 3 | 获胜咱不怕,缺谁谁尴尬.
4 | 4 | 明年我也可能转会西部
5 | 5 | 我都双20+了,怎么球队就是不胜呢?
6 | 1 | 明年听说有条大鱼要来,谁呀?
(6 行记录)
mydb=# update twitters set content = '变更后的值' where id = 1;
UPDATE 1
mydb=# select * from curry_twitters;
nba_player | content
------------+----------------------------
kuli | 明年听说有条大鱼要来,谁呀?
kuli | 变更后的值
(2 行记录)
# 删除视图
mydb=# drop view curry_twitters;
DROP VIEW
mydb=# \dv
没有找到任何关系.
Affairs
Database Transaction (Database Transaction) refers to a series of operations performed as a single logical unit of work, either completely executed or not executed at all. Transaction processing can ensure that unless all operations within the transactional unit are successfully completed, data-oriented resources will not be permanently updated. By combining a set of related operations into a unit that either all succeeds or all fails, you can simplify error recovery and make your application more reliable. For a logical unit of work to become a transaction, it must satisfy the so-called ACID (atomicity, consistency, isolation, and durability) properties. Transaction is the logical unit of work in database operation, and the transaction management subsystem in the DBMS is responsible for transaction processing.
-
begin
-
commit
-
rollback
mydb=# select * from users;
id | nba_player | score | team
----+------------+-------+---------
1 | kuli | 28.3 | yongshi
2 | hadeng | 30.2 | huojian
3 | adu | 25.6 | yongshi
5 | shengui | 31.3 | leiting
6 | baibian | 19.8 | rehuo
4 | azhan | 29.1 | qishi
(6 行记录)
# 开启事务
mydb=# begin;
BEGIN
mydb=# update users set score = 50.0 where id = 1;
UPDATE 1
mydb=# update users set score = 60 where id = 2;
UPDATE 1
# 事务提交前查询 (可以查询到未提交读 - 与事务隔离级别有关)
mydb=# select * from users;
id | nba_player | score | team
----+------------+-------+---------
3 | adu | 25.6 | yongshi
5 | shengui | 31.3 | leiting
6 | baibian | 19.8 | rehuo
4 | azhan | 29.1 | qishi
1 | kuli | 50 | yongshi
2 | hadeng | 60 | huojian
(6 行记录)
# 事务回滚
mydb=# rollback;
ROLLBACK
# 再次查询,数据为原始数据
mydb=# select * from users;
id | nba_player | score | team
----+------------+-------+---------
1 | kuli | 28.3 | yongshi
2 | hadeng | 30.2 | huojian
3 | adu | 25.6 | yongshi
5 | shengui | 31.3 | leiting
6 | baibian | 19.8 | rehuo
4 | azhan | 29.1 | qishi
(6 行记录)
# 开始事务
mydb=# begin;
BEGIN
mydb=# update users set score = 50.0 where id = 1;
UPDATE 1
mydb=# update users set score = 60 where id = 2;
UPDATE 1
# 提价事务
mydb=# commit;
COMMIT
mydb=# select * from users;
id | nba_player | score | team
----+------------+-------+---------
3 | adu | 25.6 | yongshi
5 | shengui | 31.3 | leiting
6 | baibian | 19.8 | rehuo
4 | azhan | 29.1 | qishi
1 | kuli | 50 | yongshi
2 | hadeng | 60 | huojian
(6 行记录)