中文资料

中文资料

/**gp中的基本sql语法**/
--删除表
drop table testtb;
--创建表
CREATE TABLE testtb
(
id integer,
"name" character varying(2)
)
WITH (
OIDS=FALSE
)
DISTRIBUTED BY (id);
ALTER TABLE testtb OWNER TO gpadmin;
--WITH 用来说明表的存储属性,比如表的压缩
--DISTRIBUTED BY 定义表的分布键,这个键最好唯一,如果表中没有唯一键字段,可以定义DISTRIBUTED RANDOMLY作为分布建。 --表的创建
create table testTBbak (like testTB) -- like 复制表结构
create table testtbbak1 as select * from testtb distributed by (name) -- 数据保留,可以指定分布键。
select * into testtbbak2 from testTB -- into 保留数据,不能指定分布键
select * from testtbbak2 /**分区表**/
-- 按照时间分区 -- --创建视图
create view v_testtb as select * from testtb -- 删除数据
delete from testtb -- 速度慢,会写日志。
truncate testtb -- 速度快,但不会写日期。 /**序列的使用**/
--删除序列
drop sequence myseq
--创建序列(1是开始值,可以根据实际情况改变)
create sequence myseq start 1
--序列的使用
insert into testTB values(nextval('myseq'),'小明')
--重新设置序列值
select setval('myseq',1)
--查询
select id,name from testTB order by 1 -- explain 用于查看sql执行计划
explain select * from testtb /**字符串函数**/
-- || 字符串连接
select '胡' || '黄' || '腾'
-- length 字符长度
select length('89998')
-- substring 字符串截取,从那位开始,截几位
select substring('123456' from 2 for 3)
-- trim去字符前后空格
select trim(' 234 fds ')
-- lowere 字符转小写
select lower('RER')
-- upper 字符转大写
select upper('rer')
-- replace 替换字符
select replace('aabbddee','aa','mm')
-- position 查找指定字符位置
select position('de' in 'abdeaf')
-- split_part 根据指定字符查找给定字符的位置
select split_part('aaa|bbb|ccc|eee','|',2) /**时间的函数**/
-- age求俩日期之差
select age(timestamp '1987-12-03',timestamp '2015-06-17')
-- age如果一个日期则和当前日期求差
select age(timestamp '1987-12-03')
-- current_date 当前日期
select current_date
-- current_time 当前时间
select current_time
-- current_timestamp 当前时间戳
select current_timestamp
-- now 当前时间
select now()
-- extract 获取指定时间的具体某个参数
select extract(day from date '2015-06-18')
select extract(day from now())
-- 时间相加
select '2015-04-2 10:00:52'::timestamp + interval '10 days 2 hours 10 seconds'
-- 时间相减
select current_date - interval '10 days' /**gp中其他函数**/
-- greatest 取两值中的最大值
select greatest(3,9)
-- 列转行
insert into testtb(id,name)values (1,'ha');
insert into testtb(id,name)values (1,'he');
insert into testtb(id,name)values (1,'hi');
insert into testtb(id,name)values (2,'xb');
insert into testtb(id,name)values (2,'xh');
insert into testtb(id,name)values (2,'xm');
select id,string_agg(name,'|' order by name) from testtb group by id;
-- 行转列
select id,regexp_split_to_table(string_agg,E'\\|') str from texttb_m /**开窗函数**/
--建表
create table empsalary
(
depname varchar(20),
empno integer,
salary integer
)
distributed by (empno) insert into empsalary values ('develop',9,4500);
insert into empsalary values ('develop',1,3200);
insert into empsalary values ('develop',4,1000);
insert into empsalary values ('develop',2,9100);
insert into empsalary values ('develop',6,1000);
insert into empsalary values ('person1',5,3100);
insert into empsalary values ('person1',7,4100);
insert into empsalary values ('sales',3,2400);
insert into empsalary values ('sales',8,1200);
insert into empsalary values ('sales',10,5100); -- rank 及 row_number 函数的应用
select depname
,empno
,salary
,rank() over (partition by depname order by salary desc)
,row_number() over (partition by depname order by salary desc)
from empsalary
-- rank 识别重复记录
-- row_number 不识别重复记录 select *
,sum(salary) over () sum1
,sum(salary) over (order by salary) sum2
,sum(salary) over (partition by depname) sum3
,sum(salary) over (partition by depname order by salary) sum4
from empsalary -- grouping by 的使用(其实就是简化了union)
select depname ,sum(empno)
from empsalary
group by depname
union all
select depname ,sum(salary)
from empsalary
group by depname --等效于
select depname,sum(empno),sum(salary)
from empsalary
group by grouping sets(depname)

命令行

当我们费尽千辛万苦安装完数据库后,一定会迫不及待的想使用它。骚年,不要着急,且看我为您解析PostgreSQL的启动,登录,退出,关闭过程。

一 启动数据库服务器

1. 没有设置环境变量的情况下,postgresql用户下:

/usr/local/pgsql/bin/postgres -D data >./data/logfile 2>&1 &

其中/usr/local/pgsql/bin目录中存放着数据库的执行命令,/data是数据库的数据存放目录。小伙伴们可

以根据自己实际的安装目录调整上面的命令。

如下命令查看数据库进程有没有成功启动,当看到有几个postgres的进程时,说明启动成功。

ps aux | grep postgres

2. 设置环境变量的情况下

如果需要指定环境变量,请按如下方式:

postgres@lgr-pc:~$ vi .bash_profile

添加如下内容:

PGDATA=/usr/local/pgsql/data;

PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin

export PGDATA PATH

即指定pgdata和bin的目录,这里可以根据自己的实际目录指定。

编辑完环境变量文件后,运行如下命令使环境变量生效:

postgres@lgr-pc:~$ . .bash_profile

设置完环境变量,运行如下命令启动服务器:

postgres@lgr-pc:~$ pg_ctl start

二 登录服务器

当安装完数据库后,我们会有一个系统用户,一个数据库,一个数据库用户,他们默认的名称为:postgres

1. 如果没有设置bin目录的环境变量,那么

postgres@lgr-pc:~$ /usr/local/pgsql/bin/psql

这样默认登录到postgres库中,当然也可以在psql后面加上库的名称,这样就可以登录到指定库中。如登录到test库:

postgres@lgr-pc:~$ /usr/local/pgsql/bin/psql test

2. 如果您也像我一样设置了bin目录的环境变量,那么

postgres@lgr-pc:~$ psql

这样默认的也是登录到postgres库中,同样的我们可以指定数据库名称,登录到指定库。

postgres@lgr-pc:~$ psql test

三 退出登录

退出登录就很简单了,我们可以运行\q,或者ctrl+d

postgres=# \q

四 关闭数据库服务器

关闭:

postgres@lgr-pc:~$ pg_ctl stop

重启:

postgres@lgr-pc:~$ pg_ctl restart

postgresql 常用速查的更多相关文章

  1. jQuery 常用速查

    jQuery 速查 基础 $("css 选择器") 选择元素,创建jquery对象 $("html字符串") 创建jquery对象 $(callback) $( ...

  2. C++常用速查

    int main() { int arr[2][5] = { {1,8,12,20,25}, {5,9,13,24,26} }; } void f(double p[][10]) { } #inclu ...

  3. python轻量级orm框架 peewee常用功能速查

    peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM.它有很少的(但富有表现力的)概念,使它易于学习和直观的使用. 常见orm数据库框架 Django ORM peewee ...

  4. 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册

    <zw版·Halcon-delphi系列原创教程> zw版-Halcon常用函数Top100中文速查手册 Halcon函数库非常庞大,v11版有1900多个算子(函数). 这个Top版,对 ...

  5. 转收藏:Git常用命令速查表

    一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...

  6. Linux常用命令速查备忘

    Linux常用命令速查备忘   PS:备忘而已,详细的命令参数说明自己man 一. 启动,关机,登入,登出相关命令 [login] 登录 [logout] 登出 [exit] 登出 [shutdown ...

  7. python 下的数据结构与算法---2:大O符号与常用算法和数据结构的复杂度速查表

    目录: 一:大O记法 二:各函数高阶比较 三:常用算法和数据结构的复杂度速查表 四:常见的logn是怎么来的 一:大O记法 算法复杂度记法有很多种,其中最常用的就是Big O notation(大O记 ...

  8. web 开发:CSS3 常用属性——速查手册!

    web 开发:CSS3 常用属性——速查手册! CSS3 简介:http://www.runoob.com/css3/css3-intro.html 1.目录 http://caniuse.com/ ...

  9. Git 常用命令速查表(图文+表格)

    一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...

随机推荐

  1. VUE的语法笔记

    v-model = 'content' {{contents}} //vue 双向视图的绑定 v-text 只能返回一个文本内容 v-html 不仅可以返回文本内容还可以返回html标签 v-for ...

  2. Qt__输入对话框(QInputDialog)

    #include <QInputDialog> ...... bool isOK; QString text = QInputDialog::getText(NULL, "Inp ...

  3. linux sed的一些技巧

    sed -i '$a # This is a test' regular_express.txt 由於 $ 代表的是最后一行,而 a 的动作是新增,因此该文件最后新增『# This is a test ...

  4. BZOJ1115[POI2009]石子游戏——阶梯Nim游戏

    题目描述 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏.问先手是否必 ...

  5. 自学Linux Shell12.7-控制循环break、continue命令

    点击返回 自学Linux命令行与Shell脚本之路 12.7-控制循环break.continue命令 break命令.break命令用于跳出循环,使用break可以跳出任何类型的循环:for.whi ...

  6. BZOJ3730 震波 | 动态点分治

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  7. FreeRTOS不允许在中断服务程序和临界段中执行不确定的性的操作

    举例 等待事件标志组的任务,要是在中断服务程序中设置事件标志组,但不知道当前有多少个任务在等待此事件标志,这个操作即为不确定性操作,为了不在中断服务程序中执行此不确定性操作,只在中断服务程序中给一确定 ...

  8. 安装 Minio服务

    #MINIO SERVER Minio是在Apache License v2.0下发布的对象存储服务器.它与Amazon S3云存储服务兼容. 它最适合存储非结构化数据,如照片,视频,日志文件,备份和 ...

  9. java date总结

    Java 8 中 Date与LocalDateTime.LocalDate.LocalTime互转   Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant ...

  10. 织梦DedeCMS信息发布员发布文章阅读权限不用审核自动开放亲测试通过!

    文章发布员在织梦dedecms后台添加文章时却要超级管理员审核,这无疑是增加了没必要的工作. 登录该账号发布文章你会发现该文章显示的是待审核稿件,且并没有生成静态文件,在前台是看不到这篇文章的,而多数 ...