PostgreSQL 创建数据库可以用以下三种方式:
CREATE DATABASE
SQL 语句来创建;createdb
命令来创建;pdAdmin
工具;CREATE DATABASE dbname;
postgres=# CREATE DATABASE testdb;
createdb [option ...] [dbname [description]]
参数说明:
选项 | 描述 |
---|---|
-D tablespace | 指定数据库默认表空间。 |
-e | 将 createdb 生成的命令发送到服务端。 |
-E encoding | 指定数据库的编码。 |
-l locale | 指定数据库的语言环境。 |
-T template | 指定创建此数据库的模板。 |
–help | 显示 createdb 命令的帮助信息。 |
-h host | 指定服务器的主机名。 |
-p port | 指定服务器监听的端口,或者 socket 文件。 |
-U username | 连接数据库的用户名。 |
-w | 忽略输入密码。 |
-W | 连接时强制要求输入密码。 |
使用封装的命令 createdb
创建命令时,不能已经登录到 pg 数据库命令行,而需要在数据库安装的 bin 目录下,直接使用命令创建;
栗子:
[10307440@zte.intra@LIN-874286D0CB5 bin]$ createdb -h localhost -p 5432 -U postgres sdtestbd
口令:
[10307440@zte.intra@LIN-874286D0CB5 bin]$
\l
查看已经存在的数据库:postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
sdtestbd | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
\c + 数据库名
进入数据库:postgres=# \c sdtestbd
您现在已经连接到数据库 "sdtestbd",用户 "postgres".
PostgreSQL 删除数据库可以用以下三种方式:
DROP DATABASE
SQL 语句来删除;dropdb
命令来删除;pgAdmin
工具;DROP DATABASE [ IF EXISTS ] name
postgres=# DROP DATABASE sdtestbd;
DROP DATABASE
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)
dropdb [connection-option...] [option...] dbname
参数说明:
选项 | 描述 |
---|---|
-e | 显示 dropdb 生成的命令并发送到数据库服务器。 |
-i | 在做删除的工作之前发出一个验证提示。 |
-V | 打印 dropdb 版本并退出。 |
–if-exists | 如果数据库不存在则发出提示信息,而不是错误信息。 |
–help | 显示有关 dropdb 命令的帮助信息。 |
-h host | 指定运行服务器的主机名。 |
-p port | 指定服务器监听的端口,或者 socket 文件。 |
-U username | 连接数据库的用户名。 |
-w | 连接数据库的用户名。 |
-W | 连接时强制要求输入密码。 |
–maintenance-db=dbname | 删除数据库时指定连接的数据库,默认为 postgres,如果它不存在则使用 template1。 |
跟创建数据库时的命令一样,还是得进入 bin 目录下:
dropdb -h localhost -p 5432 -U postgres runoobdb
CREATE TABLE
语句来创建数据库表格。CREATE TABLE
语法格式如下:CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( 一个或多个列 )
);
CREATE TABLE
是一个关键词,用于告诉数据库系统将创建一个数据表。CREATE TABLE
在当前数据库创建一个新的空白表,该表将由发出此命令的用户所拥有。postgres=# CREATE DATABASE pgtestdb
postgres-# ;
CREATE DATABASE
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
pgtestdb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
postgres=# \c pgtestdb
您现在已经连接到数据库 "pgtestdb",用户 "postgres".
pgtestdb=# CREATE TABLE COMPANY(
pgtestdb(# ID INT PRIMARY KEY NOT NULL,
pgtestdb(# NAME TEXT NOT NULL,
pgtestdb(# ADDRESS CHAR(50),
pgtestdb(# SALARY REAL
pgtestdb(# );
CREATE TABLE
\d
查看表是否创建成功:pgtestdb=# \d
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------+--------+----------
public | company | 数据表 | postgres
(1 行记录)
\d tablename
查看表格信息:pgtestdb=# \d COMPANY
数据表 "public.company"
栏位 | 类型 | 校对规则 | 可空的 | 预设
---------+---------------+----------+----------+------
id | integer | | not null |
name | text | | not null |
address | character(50) | | |
salary | real | | |
索引:
"company_pkey" PRIMARY KEY, btree (id)
pgtestdb=# CREATE TABLE DEPARTMENT(
pgtestdb(# ID INT PRIMARY KEY NOT NULL,
pgtestdb(# EDPT CHAR(50) NOT NULL,
pgtestdb(# EMP_ID INT NOT NULL
pgtestdb(# );
CREATE TABLE
pgtestdb=# \d
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+------------+--------+----------
public | company | 数据表 | postgres
public | department | 数据表 | postgres
(2 行记录)
DROP TABLE
语句来删除表格,包含表格数据、规则、触发器等,所以删除表格要慎重,删除后所有信息就消失了。DROP TABLE table_name;
pgtestdb=# \d
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+------------+--------+----------
public | company | 数据表 | postgres
public | department | 数据表 | postgres
(2 行记录)
pgtestdb=# DROP TABLE department;
DROP TABLE
pgtestdb=# \d
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------+--------+----------
public | company | 数据表 | postgres
(1 行记录)
CREATE SCHEMA
语句来创建模式;CREATE SCHEMA myschema.mytable (
...
);
pgtestdb
下创建模式 testSchema:pgtestdb=# CREATE SCHEMA testSchema;
CREATE SCHEMA
pgtestdb=# CREATE TABLE testSchema.COMPANY(
pgtestdb(# ID INT NOT NULL,
pgtestdb(# NAME VARCHAR(9) NOT NULL,
pgtestdb(# AGE INT NOT NULL,
pgtestdb(# ADDRESS CHAR(20),
pgtestdb(# SALARY DECIMAL(18, 2),
pgtestdb(# PRIMARY KEY(ID)
pgtestdb(# );
CREATE TABLE
pgtestdb=# SELECT * FROM testschema.COMPANY
pgtestdb-# ;
id | name | age | address | salary
----+------+-----+---------+--------
(0 行记录)
pgtestdb=# DROP SCHEMA testschema;
错误: 无法删除 模式 testschema 因为有其它对象倚赖它
描述: 表 testschema.company 倚赖于 模式 testschema
提示: 使用 DROP .. CASCADE 把倚赖对象一并删除.
pgtestdb=# DROP SCHEMA testschema CASCADE;
注意: 递归删除 表 testschema.company
DROP SCHEMA
INSERT INTO
语句用于向表中插入新记录。INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
输出信息 | 描述 |
---|---|
INSERT oid 1 | 只插入一行并且目标表具有 OID的返回信息, 那么 oid 是分配给被插入行的 OID。 |
INSERT 0 # | 插入多行返回的信息, # 为插入的行数。 |
pgtestdb=# CREATE TABLE COMPANY(
pgtestdb(# ID INT NOT NULL,
pgtestdb(# NAME TEXT NOT NULL,
pgtestdb(# AGE INT NOT NULL,
pgtestdb(# ADDRESS CHAR(50) NOT NULL,
pgtestdb(# SALARY REAL,
pgtestdb(# JOIN_DATE DATE,
pgtestdb(# PRIMARY KEY(ID)
pgtestdb(# );
CREATE TABLE
pgtestdb=# \d company
数据表 "public.company"
栏位 | 类型 | 校对规则 | 可空的 | 预设
-----------+---------------+----------+----------+------
id | integer | | not null |
name | text | | not null |
age | integer | | not null |
address | character(50) | | not null |
salary | real | | |
join_date | date | | |
索引:
"company_pkey" PRIMARY KEY, btree (id)
pgtestdb=# INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE)
pgtestdb-# VALUES(1, 'Alice', 19, 'California', 19999.00, '2021-07-14');
INSERT 0 1
pgtestdb=# INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, JOIN_DATE)
pgtestdb-# VALUES (2, 'Mona', 18, 'Texas', '2021-07-14');
INSERT 0 1
pgtestdb=# INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE)
pgtestdb-# VALUES (3, 'Gorden', 45, 'WHOCARE', 100.00, DEFAULT);
INSERT 0 1
pgtestdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00, '2007-12-13' ), (5, 'David', 27, 'Texas', 85000.00, '2007-12-13');
INSERT 0 2
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
2 | Mona | 18 | Texas | | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
SELECT column1, column2,...columnN FROM table_name;
SELECT * FROM table_name;
pgtestdb=# SELECT ID, NAME, AGE FROM COMPANY;
id | name | age
----+--------+-----
1 | Alice | 19
2 | Mona | 18
3 | Gorden | 45
4 | Mark | 25
5 | David | 27
(5 行记录)
运算符 | 描述 | 实例 |
---|---|---|
+ |
加 | a + b 结果为 5 |
- |
减 | a - b 结果为 -1 |
* |
乘 | a * b 结果为 6 |
/ |
除 | b / a 结果为 1 |
% |
模(取余) | b % a 结果为 1 |
^ |
指数 | a ^ b 结果为 8 |
|/ |
平方根 | |/ 25.0 结果为 5 |
||/ |
立方根 | ||/ 27.0 结果为 3 |
! |
阶乘 | 5 ! 结果为 120 |
!! |
阶乘(前缀操作符) | !! 5 结果为 120 |
运算符 | 描述 | 实例 |
---|---|---|
= | 等于 | (a = b) 为 false |
!= | 不等于 | (a != b) 为 true |
<> | 不等于 | (a <> b) 为 true |
> | 大于 | (a > b) 为 false |
< | 小于 | (a < b) 为 true |
>= | 大于等于 | (a >= b) 为 false |
<= | 小于等于 | (a <= b) 为 true |
运算符 | 描述 |
---|---|
AND |
逻辑与运算符。如果两个操作数都非零,则条件为真。 |
NOT |
逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 |
OR |
逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 |
a | b | NOT a | a AND b | a OR b |
---|---|---|---|---|
TRUE | TRUE | FALSE | TRUE | TRUE |
TRUE | FALSE | FALSE | TRUE | |
TRUE | NULL | NULL | TRUE | |
FALSE | FALSE | TRUE | FALSE | FALSE |
FALSE | NULL | FALSE | NULL | |
NULL | NULL | NULL | NULL | NULL |
p | q | p&q | p | q |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 |
1 | 0 | 0 | 1 |
SELECT column1, column2, columnN
FROM table_name
WHERE [CONDITION | EXPRESSION];
SELECT column1, column2, columnN
FROM table_name
WHERE SINGLE VALUE MATCHTING EXPRESSION;
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
2 | Mona | 18 | Texas | | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE SALARY < 20000;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
(2 行记录)
SELECT numerical_expression as OPERATION_NAME
[FROM table_name WHERE CONDITION] ;
pgtestdb=# SELECT (99 + 1) AS RESULT;
result
--------
100
(1 行记录)
此外 PostgreSQL 还内置了一些数学函数,如:
以下实例查询 COMPANY 表的记录总数:
pgtestdb=# SELECT COUNT(*) AS "RECORDS" FROM COMPANY;
RECORDS
---------
5
(1 行记录)
pgtestdb=# SELECT CURRENT_TIMESTAMP;
current_timestamp
-------------------------------
2021-09-30 16:37:59.760127+08
(1 行记录)
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1]
pgtestdb=# SELECT * FROM COMPANY WHERE AGE >= 20 AND SALARY >= 65000;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(2 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE AGE >= 20 OR SALARY >= 65000;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(3 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE SALARY IS NOT NULL;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(4 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE NAME LIKE "Al%";
错误: 字段 "Al%" 不存在
第1行SELECT * FROM COMPANY WHERE NAME LIKE "Al%";
^
pgtestdb=# SELECT * FROM COMPANY WHERE NAME LIKE 'Al%';
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
(1 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE AGE IN (18, 25);
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
2 | Mona | 18 | Texas | | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
(2 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE AGE NOT IN (18, 25);
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
5 | David | 27 | Texas | 85000 | 2007-12-13
(3 行记录)
pgtestdb=# SELECT * FROM COMPANY WHERE AGE BETWEEN 18 AND 25;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
2 | Mona | 18 | Texas | | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
(3 行记录)
pgtestdb=# SELECT AGE FROM COMPANY
pgtestdb-# WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000);
age
-----
19
18
45
25
27
(5 行记录)
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
2 | Mona | 18 | Texas | | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(5 行记录)
pgtestdb=# UPDATE COMPANY SET SALARY = 19991 WHERE ID = 2;
UPDATE 1
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
2 | Mona | 18 | Texas | 19991 | 2021-07-14
(5 行记录)
DELETE FROM table_name WHERE [condition];
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+--------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
3 | Gorden | 45 | WHOCARE | 100 |
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
2 | Mona | 18 | Texas | 19991 | 2021-07-14
(5 行记录)
pgtestdb=# DELETE FROM COMPANY WHERE ID = 3;
DELETE 1
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
2 | Mona | 18 | Texas | 19991 | 2021-07-14
(4 行记录)
%
;_
;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_';
pgtestdb=# SELECT * FROM COMPANY WHERE AGE::text LIKE '2%';
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(2 行记录)
::
作用:将一种类型的常量转化成指定类型的常量;limit
子句用于限制 SELECT 语句中查询的数据的数量。SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows]
SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows] OFFSET [row num]
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
2 | Mona | 18 | Texas | 19991 | 2021-07-14
(4 行记录)
pgtestdb=# SELECT * FROM COMPANY LIMIT 2;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
(2 行记录)
pgtestdb=# SELECT * FROM COMPANY LIMIT 2 OFFSET 1;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(2 行记录)
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
pgtestdb=# SELECT * FROM COMPANY
pgtestdb-# ;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
1 | Alice | 19 | California | 19999 | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
2 | Mona | 18 | Texas | 19991 | 2021-07-14
(4 行记录)
pgtestdb=# SELECT * FROM COMPANY ORDER BY AGE ASC;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
2 | Mona | 18 | Texas | 19991 | 2021-07-14
1 | Alice | 19 | California | 19999 | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
(4 行记录)
pgtestdb=# SELECT * FROM COMPANY ORDER BY NAME DESC;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
2 | Mona | 18 | Texas | 19991 | 2021-07-14
4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13
5 | David | 27 | Texas | 85000 | 2007-12-13
1 | Alice | 19 | California | 19999 | 2021-07-14
(4 行记录)
SELECT column-list
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2....columnN
ORDER BY column1, column2....columnN
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 15000
5 | David | 27 | Texas | 15000
6 | Kim | 22 | South-Hall | 45000
(6 行记录)
pgtestdb=# SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;
name | sum
-------+-------
Teddy | 20000
David | 15000
Paul | 20000
Kim | 45000
Mark | 15000
Allen | 15000
(6 行记录)
COUNT
来统计一下不同工资的人数;pgtestdb=# SELECT SALARY, COUNT(*) FROM COMPANY GROUP BY SALARY;
salary | count
--------+-------
20000 | 2
45000 | 1
15000 | 3
(3 行记录)
pgtestdb=# SELECT SALARY, COUNT(*) FROM COMPANY GROUP BY SALARY ORDER BY SALARY DESC;
salary | count
--------+-------
45000 | 1
20000 | 2
15000 | 3
(3 行记录)
WITH
name_for_summary_data AS (
SELECT Statement)
SELECT columns
FROM name_for_summary_data
WHERE conditions <=> (
SELECT column
FROM name_for_summary_data)
[ORDER BY columns]
name_for_summary_data
:是 WITH 子句的名称;
name_for_summary_data
可以与现有的表名相同,并且具有优先级。
实例没看懂,感觉像是将某一部分功能提出来了一个函数,有兴趣的参考 菜鸟教程:WITH 子句
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
pgtestdb=# SELECT SALARY, COUNT(*) FROM COMPANY GROUP BY SALARY HAVING SALARY < 18000;
salary | count
--------+-------
15000 | 3
(1 行记录)
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 15000
5 | David | 27 | Texas | 15000
6 | Kim | 22 | South-Hall | 45000
(6 行记录)
pgtestdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
pgtestdb-# VALUES (8, 'Paul', 32, 'California', 20000.00 );
INSERT 0 1
pgtestdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
pgtestdb-# VALUES (9, 'Allen', 25, 'Texas', 15000.00 );
INSERT 0 1
pgtestdb=# SELECT * FROM COMPANY;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 15000
5 | David | 27 | Texas | 15000
6 | Kim | 22 | South-Hall | 45000
8 | Paul | 32 | California | 20000
9 | Allen | 25 | Texas | 15000
(8 行记录)
pgtestdb=# SELECT NAME FROM COMPANY;
name
-------
Paul
Allen
Teddy
Mark
David
Kim
Paul
Allen
(8 行记录)
pgtestdb=# SELECT DISTINCT NAME FROM COMPANY;
name
-------
Teddy
David
Paul
Kim
Mark
Allen
(6 行记录)