pg update语句(pgsql 我刚不小心执行了update 语句,请问有什么办法回退到原来的值)

本文目录
- pgsql 我刚不小心执行了update 语句,请问有什么办法回退到原来的值
- PostgreSQL中实现Update前的备份骚操作
- postgresql的update inner join
- pgsql触发器:当向一张表中插入或更新一条记录时,同时向另一张表也插入或更新一条记录
- postgreSQL 怎么把时间字段设置成空
- postgresql的update语句需要添加什么关键字才能让语句运行完成后不返回影响行数,注意,不是获取
- postgre sql一个update标签可以写2个update语句么
- postgresql update 时 不能用表别名吗
- postgreSQL创建一个触发器函数:更新过student1表的数据后,更新student1_stats表中数据
- 想请问 如果是想让数据库里的某一列数值加1 怎么写update 语句合适呢
pgsql 我刚不小心执行了update 语句,请问有什么办法回退到原来的值
你知道原来的值吗?update t_so_so_member set receiver_mobile=原值
不知道就看看以前的备份有没有?如果也没有就无法了。
PostgreSQL中实现Update前的备份骚操作
参考文章: 【PostgreSQL 如何实现upsert与新旧数据自动分离】
很多业务也行有这样的需求,新的数据会不断的插入,并且可能会有更新。 对于更新的数据,需要记录更新前的记录到历史表。 这个需求有点类似于审计需求,即需要对记录变更前后做审计。 本文的目的并不是审计,而且也可能不期望使用触发器。
还有什么方法呢?
PostgreSQL 这么高大上,当然有,而且还能在一句SQL里面完成,看法宝。
创建一张当前状态表,一张历史记录表。
插入一条不存在的记录,不会触发插入历史表的行为。
注意替代变量
插入一条不存在的记录,不会触发插入历史表的行为。
插入一条已存在的记录,并且有数据的变更,触发数据插入历史表的行为。
插入一条已存在的记录,并且已存在的记录值和老值一样,不会触发将数据插入历史表的行为。
执行计划
postgresql的update inner join
在mysql中遇到依赖表a的数据来大量更新表b的数据时可以使用update join的语法
在postgresql也可以做到 语法和mysql有一些差别
这是postgresql的update语法
update中可以包含一个form子句 当包含form子句时 where子句中需要指明update的表和form子句的关联关系
例如:
employee:id name attendance_duration ...
attendance: employee_id duration date ...
将全部员工上月的出勤时间累加到employee表的attendance_duration中
update employee set attendance_duration=attendance_duration+t.ad
from (select sum(duration) from attendance where date《... and date 》... group by employee_id) as t
where employee.id =attendance.employee_id
利用这个特性配合case when等语法实现复杂业务逻辑 可以避免大量数据逐一更新
能极大提高update性能
pgsql触发器:当向一张表中插入或更新一条记录时,同时向另一张表也插入或更新一条记录
digoal=# create table a (aid int primary key, aname text, time timestamp);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for table "a"
CREATE TABLE
digoal=# create table b (id int primary key, name text, time timestamp);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "b_pkey" for table "b"
CREATE TABLE
digoal=# create or replace function tg_a () returns trigger as $$
declare
begin
case TG_OP
when ’INSERT’ then
insert into b(id,name,time) values (NEW.aid, NEW.aname, NEW.time);
when ’UPDATE’ then
update b set id=NEW.aid, name=NEW.aname, time=NEW.time where id=OLD.aid;
when ’DELETE’ then
delete from b where id=OLD.aid;
when ’TRUNCATE’ then
truncate b;
else
return NULL;
end case;
return NULL;
end;
$$ language plpgsql;
digoal=# create trigger tg_a after INSERT OR DELETE OR UPDATE ON a for each row execute procedure tg_a();
CREATE TRIGGER
digoal=# create trigger tg_a_truncate after truncate ON a for each statement execute procedure tg_a();
CREATE TRIGGER
digoal=# insert into a select generate_series(1,10),’digoal’,clock_timestamp();
INSERT 0 10
digoal=# select * from a;
aid | aname | time
-----+--------+----------------------------
1 | digoal | 2013-02-03 18:59:37.592479
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(10 rows)
digoal=# select * from b;
id | name | time
----+--------+----------------------------
1 | digoal | 2013-02-03 18:59:37.592479
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(10 rows)
digoal=# delete from a where aid=1;
DELETE 1
digoal=# select * from b;
id | name | time
----+--------+----------------------------
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(9 rows)
digoal=# select * from a;
aid | aname | time
-----+--------+----------------------------
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(9 rows)
digoal=# update a set aname=’new’ where aid=2;
UPDATE 1
digoal=# select * from a where aid=2;
aid | aname | time
-----+-------+----------------------------
2 | new | 2013-02-03 18:59:37.592667
(1 row)
digoal=# select * from b where id=2;
id | name | time
----+------+----------------------------
2 | new | 2013-02-03 18:59:37.592667
(1 row)
digoal=# truncate a;
TRUNCATE TABLE
digoal=# select * from a;
aid | aname | time
-----+-------+------
(0 rows)
digoal=# select * from b;
id | name | time
----+------+------
(0 rows)
postgreSQL 怎么把时间字段设置成空
直接用update语句就可以。
如果是全表更新,语法:
update 表名 set 时间字段=null;
如果是更新部分数据,语法:
update 表名 set 时间字段=null where 条件;
注意事项:
时间字段必须允许为空,否则执行会报错。
postgresql的update语句需要添加什么关键字才能让语句运行完成后不返回影响行数,注意,不是获取
可以先执行update语句(update的条件是存在的判断条件),然后调用get diagnostics获得上一个SQL语句执行所影响的行数,如果影响行数为0,则说明不存在,那么再执行insert语句。
结构类似:
declare
v_cnt integer;
begin
update .... -- 执行更新语句
where ...; -- 这里的条件是存在的判断条件
get diagnostics v_cnt = row_count; -- 将影响行数的值赋给v_cnt
if v_cnt = 0 then
insert into ...; -- 执行插入语句
end if;
end;
postgre sql一个update标签可以写2个update语句么
可以。
在单条UPDATE中模拟死锁,需要借助扫描方法,以及明确让AB两个会话分别锁定一条记录后再锁定对方已经锁定的记录,利用批量update的语法以及values子句即可实现,这里利用了嵌套循环,确保被更新的表被多次扫描,并且每次扫描时两个会话更新的记录被错开,达到死锁效果,同时为了让速度慢下来,使用pgsleep函数,让每一条更新都放缓1秒。
postgresql update 时 不能用表别名吗
可以用 update a set a.name=b.name from table1 a inner join table2 b on a.id=b.id 以上,希望对你有所帮助! 可以update table1 t1 ,pcfcsm
postgreSQL创建一个触发器函数:更新过student1表的数据后,更新student1_stats表中数据
PostgreSQL中大概是这样创建触发器:
首先需要创建触发器调用的函数:
create or replace function tg_update()
returns trigger
as $$
begin
-- 更新SQL, 可以使用NEW和OLD分别取新记录和旧记录
update student1_stats src
set ....
where ...;
return null; -- 要返回null
end;
$$ language plpgsql;
然后,创建触发器:
create trigger trigger_name
after update on studen1
for each row execute procedure tg_update();
想请问 如果是想让数据库里的某一列数值加1 怎么写update 语句合适呢
1、创建测试表,create table test_update(id number);
2、插入测试数据,
insert into test_update values(1);
insert into test_update values(12);
insert into test_update values(23);
3、查询表中数据,select t.*, rowid from test_update t
4、执行update语句,update test_update set id = id+1;
5、再次查询数据,发现数据已变化;select t.*, rowid from test_update t

更多文章:
service pack 3(操作系统版本升级(SP) Service Pack 3当中的“Service Pack 3”是什么意思)
2026年9月22日 10:20
html代码怎么写大佬教程(html网页的题来个大佬,写代码,题目在图上)
2026年9月22日 10:10
结构体内又一个struct(c++ 在结构体中再嵌入一个结构体如何调用)
2026年9月22日 09:40
cocos creator中文(cocoscreator和cocoscreator3d的区别)
2026年9月22日 02:30







