永发信息网

怎么查看 PostgreSQL里的数据库的表是否存在

答案:2  悬赏:0  手机版
解决时间 2021-04-04 18:55
  • 提问者网友:沉默菋噵
  • 2021-04-03 18:25
怎么查看 PostgreSQL里的数据库的表是否存在
最佳答案
  • 五星知识达人网友:躲不过心动
  • 2021-04-03 18:33
往表里更新数据前先查询一遍被更新的数据存不存在。
通常做法是使用select 查询过滤一遍,然后再决定是否更新,怎么更新。
在PG库里,除了以上方法外,还有一种更能提升性能的办法,
使用perform来代替select。
Example:
Create or replace function test.insert_exist_test(i_id int,i_info text)
returns void
as
$BODY$
--author: kenyon
--created:2012_03_05
--purpose:test insert into a table if exists
declare
begin perform 1 from test.exists_test where id = i_id;
if not found then
insert into test.exists_test(id,info) values (i_id,i_info);
return;
else
update test.exists_test set info=i_info where id=i_id;
return;
end if;
exception when others then
raise exception 'Insert exists_test(id,info) values(%,%) error.',i_id,i_info;
return;
end;
$BODY$
language plpgsql;
使用:

select test.insert_exist_test(1,'kevin');
select test.insert_exist_test(2,'BruceLee');
select test.insert_exist_test(3,'Jacky');
select test.insert_exist_test(1,'kenyon');
全部回答
  • 1楼网友:封刀令
  • 2021-04-03 18:38
点击确定后大家便可以查看postsql已有的数据库了; 注:pgAdmin3的数据库和查询一个表; 9.1 SELECT; 要从一个表中检索数据就是查询这个表。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯