快捷搜索:  汽车  科技

postgresql常用函数语法(PostgreSQL基础用法)

postgresql常用函数语法(PostgreSQL基础用法)cd /etc/init.d/ chmod a x postgresql启动|重启|停止服务cd /usr/local/pgsql/postgresql-10.1/contrib/start-scripts/ cp linux /etc/init.d/postgresql修改文件执行权限PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin export PGHOME=/usr/local/pgsql export PGDATA=/usr/local/pgsql/data保存,退出:x执行以下命令,使环境变量生效。source /home/postgres/.bash_profile设置开机自启动将/usr/local/pgsql/postgresql-10.1/contrib/start-scripts/下的linux文件拷贝到/etc/ini

简述

本文主要介绍postgresql基础用法。

配置环境变量

切换用户

su - postgres

postgresql常用函数语法(PostgreSQL基础用法)(1)

编辑.bash_profile文件

postgresql常用函数语法(PostgreSQL基础用法)(2)

vi /home/postgres/.bash_profile

添加以下内容

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin export PGHOME=/usr/local/pgsql export PGDATA=/usr/local/pgsql/data

postgresql常用函数语法(PostgreSQL基础用法)(3)

保存,退出

:x

执行以下命令,使环境变量生效。

source /home/postgres/.bash_profile设置开机自启动

将/usr/local/pgsql/postgresql-10.1/contrib/start-scripts/下的linux文件拷贝到/etc/init.d/目录下,并命名为postgresql。

cd /usr/local/pgsql/postgresql-10.1/contrib/start-scripts/ cp linux /etc/init.d/postgresql

postgresql常用函数语法(PostgreSQL基础用法)(4)

修改文件执行权限

cd /etc/init.d/ chmod a x postgresql

postgresql常用函数语法(PostgreSQL基础用法)(5)

启动|重启|停止服务

#启动 service postgresql start #重启 service postgresql restart #停止 service postgresql stop

postgresql常用函数语法(PostgreSQL基础用法)(6)

设置开机自启动

chkconfig --add postgresql

postgresql常用函数语法(PostgreSQL基础用法)(7)

用户

创建用户

切换到postgres

su - postgres

postgresql常用函数语法(PostgreSQL基础用法)(8)

输入./psql

postgresql常用函数语法(PostgreSQL基础用法)(9)

查看数据库列表

postgresql常用函数语法(PostgreSQL基础用法)(10)

创建用户

create user username with password '****';

postgresql常用函数语法(PostgreSQL基础用法)(11)

删除用户

drop user username;数据库

#创建数据库指定所属者

create database db_test owner user_test;

postgresql常用函数语法(PostgreSQL基础用法)(12)

权限赋值

#将db_test所有权限赋值给username

grant all on database db_test to user_test;

postgresql常用函数语法(PostgreSQL基础用法)(13)

查看创建的数据库

postgresql常用函数语法(PostgreSQL基础用法)(14)

删除数据库

drop database db_test;

postgresql常用函数语法(PostgreSQL基础用法)(15)

选择数据库db_test

\c db_test;

postgresql常用函数语法(PostgreSQL基础用法)(16)

创建表格

CREATE TABLE DEPARTMENT(ID INT PRIMARY KEY NOT NULL DEPT CHAR(50) NOT NULL EMP_ID INT NOT NULL);

postgresql常用函数语法(PostgreSQL基础用法)(17)

删除表格

DROP TABLE DEPARTMENT;

postgresql常用函数语法(PostgreSQL基础用法)(18)

退出

\q

postgresql常用函数语法(PostgreSQL基础用法)(19)

SQL语句

INSERT:插入新记录

INSERT INTO TABLE_NAME (column1 column2 column3 ...columnN) VALUES (value1 value2 value3 ...valueN);

使用 INSERT INTO 语句时,字段列必须和数据值数量相同,且顺序也要对应。

如果我们向表中的所有字段插入值,则可以不需要指定字段,只需要指定插入的值即可:

INSERT INTO TABLE_NAME VALUES (value1 value2 value3 ...valueN);

postgresql常用函数语法(PostgreSQL基础用法)(20)

SELECT:查询记录

SELECT column1 column2 ...columnN FROM table_name;

读取表中的所有数据

SELECT * FROM table_name;

postgresql常用函数语法(PostgreSQL基础用法)(21)

UPDATE:更新记录

UPDATE table_name SET column1 = value1 column2 = value2.... columnN = valueN WHERE [condition];

我们可以同时更新一个或者多个字段。

我们可以在 WHERE 子句中指定任何条件。

postgresql常用函数语法(PostgreSQL基础用法)(22)

DELETE:删除记录

DELETE FROM table_name WHERE [condition];

如果没有指定WHERE子句,PostgreSQL表中的所有记录将被删除。

postgresql常用函数语法(PostgreSQL基础用法)(23)

LIKE:模糊查询

LIKE 子句中,通常与通配符结合使用,通配符表示任意字符,在 PostgreSQL 中,主要有以下两种通配符:

百分号 %

下划线 _

如果没有使用以上两种通配符,LIKE 子句和等号 = 得到的结果是一样的。

SELECT FROM table_name WHERE column LIKE 'XXXX%'; 或者 SELECT FROM table_name WHERE column LIKE '%XXXX%'; 或者 SELECT FROM table_name WHERE column LIKE 'XXXX_'; 或者 SELECT FROM table_name WHERE column LIKE '_XXXX'; 或者 SELECT FROM table_name WHERE column LIKE '_XXXX_';

postgresql常用函数语法(PostgreSQL基础用法)(24)

limit:限制 SELECT 语句中查询的数据的数量。

SELECT column1 column2 column FROM table_name LIMIT [no of rows]

postgresql常用函数语法(PostgreSQL基础用法)(25)

LIMIT 子句与 OFFSET 子句一起使用时的语法:

SELECT column1 column2 columnN FROM table_name LIMIT [no of rows] OFFSET [row num]

从第三位开始提取 3 个记录

postgresql常用函数语法(PostgreSQL基础用法)(26)

ORDER BY:对一列或者多列数据进行升序(ASC)或者降序(DESC)排列。

SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1 column2 .. columnN] [ASC | DESC];

postgresql常用函数语法(PostgreSQL基础用法)(27)

GROUP BY:和 SELECT 语句一起使用,用来对相同的数据进行分组。GROUP BY 在一个 SELECT 语句中,放在 WHRER 子句的后面,ORDER BY 子句的前面。

SELECT column-list FROM table_name WHERE [ conditions ] GROUP BY column1 column2....columnN ORDER BY column1 column2....columnN

HAVING:筛选分组后的各组数据,WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建的分组上设置条件。

SELECT column1 column2 FROM table1 table2 WHERE [ conditions ] GROUP BY column1 column2 HAVING [ conditions ] ORDER BY column1 column2

HAVING 子句必须放置于 GROUP BY 子句后面,ORDER BY 子句前面

DISTINCT:与 SELECT 语句一起使用,用于去除重复记录,只获取唯一的记录。

SELECT DISTINCT column1 column2 .....columnN FROM table_name WHERE [condition]

ok,以上就是PostgreSQL基础用法,看完记得转发、点赞和收藏。如果有错误,欢迎批评指正,记得点点关注呦,感谢支持!

(云渺书斋)

猜您喜欢: