加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

大连某公司机试POSTGRESQL题目,个人回来后总结的答案

发布时间:2020-12-13 17:31:29 所属栏目:百科 来源:网络整理
导读:机试失败了,给的是window 2003server的机器,postgresql 9.3,现在都习惯用ubuntu了自家笔记本都是ubuntu,以后面试不知道怎么办,回来参考postgresql官方文档一小时不到就全解决了,说实在的我真没接触过postgresql的存储过程,要我看着别人的代码模仿写真
机试失败了,给的是window 2003server的机器,postgresql 9.3,现在都习惯用ubuntu了自家笔记本都是ubuntu,以后面试不知道怎么办,回来参考postgresql官方文档一小时不到就全解决了,说实在的我真没接触过postgresql的存储过程,要我看着别人的代码模仿写真痛苦,尤其是没一点pgsql的语法基础,废话不说上题目和答案(说的是招聘JAVA结果全面的postgresql汗颜。。。) 环境ubuntu,postgresql 9.3 1.恢复pg_dump的输出文件 psql create user mianshi superuser login createdb password 'mianshi' create tablespace mianshi_ts owner mianshi location '/windows/data/pgdata' create database mianshi owner=mianshi tablespace=mianshi_ts q psql -h localhost -U mianshi i mianshi.bak 2.postgresql 递归实现 create table tb_tree(id int,pid int,name varchar(8000)); insert into tb_tree values(1,null,'祖父'),(2,1,'父亲'),(3,2,'本人'),(4,'弟弟'); with RECURSIVE t(id,pid,name) as (select * from tb_tree where name='本人' union all select tb_tree.* from tb_tree,t where tb_tree.id=t.pid) select * from t; 3.计算一个时间的年龄 select age(timestamp '1985-05-15 5:5:5'); 4.计算两个时间距离的天数 select current_date - (timestamp '2014-2-27 1:1:1')::date; 5.计算两个时间间隔的秒数 select extract(epoch from current_timestamp - (timestamp '2014-2-27 1:1:1')::date); 6.创建存储过程使用for遍历某个表 create or replace function mianshi_func01() returns void as $$ declare --l_id tb_tree.id%type; --l_pid tb_tree.pid%type; --l_name tb_tree.name%type; l_record RECORD; begin for l_record in select * from tb_tree loop raise info 'ID%,父ID%,名称%',l_record.id,l_record.pid,l_record.name; end loop; end; $$ language plpgsql; 7.使用游标遍历某个表 create or replace function mianshi_func02() returns void as $$ declare l_id tb_tree.id%type; l_pid tb_tree.pid%type; l_name tb_tree.name%type; l_cursor cursor for select * from tb_tree; begin open l_cursor; fetch l_cursor into l_id,l_pid,l_name; while found loop raise info 'ID%,l_id,l_name; fetch l_cursor into l_id,l_name; end loop; close l_cursor; end; $$ language plpgsql; 8.备份数据库完成后删除数据库 pg_dump -f another.bak mianshi drop database mianshi;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读