PostgreSQL命令笔记

教程传送门 -> PostgreSQL 教程 | 菜鸟教程

(1) 连接数据库 

[root@xxx ~]# psql -U postgres -h localhost
Password for user postgres:
psql (13.4)
Type "help" for help.

postgres=#

说明:

-h host 指定服务器的主机名
-p port 指定服务器监听的端口,或者 socket 文件
-U username 连接数据库的用户名

(2)查看数据库 \list

postgres-# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres-#

(3) 创建数据库
方法一:SQL命令 CREATE DATABASE

postgres=# CREATE DATABASE firstdb;
CREATE DATABASE
postgres=# \list firstdb
                               List of databases
  Name   |  Owner   | Encoding |   Collate   |    Ctype    | Access privileges
---------+----------+----------+-------------+-------------+-------------------
 firstdb | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(1 row)

postgres=#

方法二:createdb命令,注:createdb 是一个 SQL 命令 CREATE DATABASE 的封装

[root@xxx bin]# pwd
/usr/pgsql-13/bin
[root@xxx bin]# createdb test -U postgres -h localhost
Password:
[root@xxx bin]#

(4) 选择数据库 \c <dbname>

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 firstdb   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)

postgres=# \c firstdb
Password:
You are now connected to database "firstdb" as user "postgres".
firstdb=#

也可以连接的时候指定数据库

[root@xxx bin]# psql -U postgres -h 127.0.0.1 firstdb
Password for user postgres:
psql (13.4)
Type "help" for help.

firstdb=#


(5) 删除数据库

方法一:SQL命令 DROP DATABASE

postgres=# DROP DATABASE test;
DROP DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 firstdb   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=#

方法二:dropdb 命令
 

[root@xxx bin]# pwd
/usr/pgsql-13/bin
[root@xxx bin]# createdb test1 -U postgres -h localhost
Password:
[root@xxx bin]#

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 firstdb   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test1     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)

postgres=#
-----------------------------------------------------

[root@xxx bin]# dropdb test1 -U postgres -h localhost
Password:
[root@xxx bin]#

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 firstdb   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=#

(6) 创建表格 CREATE TABLE

postgres=# \c firstdb
You are now connected to database "firstdb" as user "postgres".
firstdb=#
firstdb=# CREATE TABLE COMPANY(
firstdb(#    ID INT PRIMARY KEY     NOT NULL,
firstdb(#    NAME           TEXT    NOT NULL,
firstdb(#    AGE            INT     NOT NULL,
firstdb(#    ADDRESS        CHAR(50),
firstdb(#    SALARY         REAL
firstdb(# );
CREATE TABLE
firstdb=#
firstdb=# CREATE TABLE DEPARTMENT(
firstdb(#    ID INT PRIMARY KEY      NOT NULL,
firstdb(#    DEPT           CHAR(50) NOT NULL,
firstdb(#    EMP_ID         INT      NOT NULL
firstdb(# );
CREATE TABLE
firstdb=#

(7) 查看表 

查看本数据库的全部表 \d(说明:除了可以看到table,还可以看到sequence)

firstdb=# \d 
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | company    | table | postgres
 public | department | table | postgres
(2 rows)

firstdb=#

查看本数据库的某张表 \d <table name>

firstdb=# \d company
                  Table "public.company"
 Column  |     Type      | Collation | Nullable | Default
---------+---------------+-----------+----------+---------
 id      | integer       |           | not null |
 name    | text          |           | not null |
 age     | integer       |           | not null |
 address | character(50) |           |          |
 salary  | real          |           |          |
Indexes:
    "company_pkey" PRIMARY KEY, btree (id)

firstdb=#

查看本数据库的某个序列 \d <sequence name>

firstdb=# \d oplog_id_seq
                        Sequence "public.company_id_seq"
  Type  | Start | Minimum |       Maximum       | Increment | Cycles? | Cache
--------+-------+---------+---------------------+-----------+---------+-------
 bigint |     1 |       1 | 9223372036854775807 |         1 | no      |     1
Owned by: public.company.id

firstdb=# 

(8) 删除表 DROP TABLE 

firstdb=# drop table department, company;
DROP TABLE
firstdb=# \d
Did not find any relations.
firstdb=#

(9) 插入 INSERT INTO
插入1行所有字段赋值

firstdb=# CREATE TABLE COMPANY(
firstdb(#    ID INT PRIMARY KEY     NOT NULL,
firstdb(#    NAME           TEXT    NOT NULL,
firstdb(#    AGE            INT     NOT NULL,
firstdb(#    ADDRESS        CHAR(50),
firstdb(#    SALARY         REAL,
firstdb(#    JOIN_DATE      DATE
firstdb(# );
CREATE TABLE
firstdb=#
firstdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.00,'2001-07-13');
INSERT 0 1

插入1行部分字段赋值

firstdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,JOIN_DATE) VALUES (2, 'Allen', 25, 'Texas', '2007-12-13');
INSERT 0 1
firstdb=#

说明:返回 INSERT 0 1,第2个数字代表插入的行数

插入的字段使用默认值 DEFAULT

firstdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (3, 'Teddy', 23, 'Norway', 20000.00, DEFAULT );
INSERT 0 1
firstdb=#
firstdb=# SELECT * FROM COMPANY;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  3 | Teddy |  23 | Norway                                             |  20000 |
(3 rows)

firstdb=#

插入多行

firstdb=# 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
firstdb=# SELECT * FROM COMPANY;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  3 | Teddy |  23 | Norway                                             |  20000 |
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
(5 rows)

firstdb=#

(10) 查询 SELECT

firstdb=# SELECT ID,NAME FROM company;
 id | name
----+-------
  1 | Paul
  2 | Allen
  3 | Teddy
  4 | Mark
  5 | David
(5 rows)

firstdb=#

查询当前时间

firstdb=# SELECT CURRENT_TIMESTAMP;
       current_timestamp
-------------------------------
 2021-10-12 15:27:59.351699+08
(1 row)

firstdb=#

(11) WHERE子句

firstdb=# SELECT * FROM COMPANY WHERE SALARY > 50000;
 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 rows)

firstdb=#

firstdb=# SELECT * FROM COMPANY WHERE AGE >= 25 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 rows)

firstdb=#

@字段不为空 IS NOT NULL

firstdb=# SELECT * FROM COMPANY WHERE JOIN_DATE IS NOT NULL;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
(4 rows)

firstdb=#

@LIKE

firstdb=# SELECT * FROM COMPANY WHERE NAME LIKE 'Pa%';
 id | name | age |                      address                       | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
  1 | Paul |  32 | California                                         |  20000 | 2001-07-13
(1 row)

firstdb=#

@IN
以下 SELECT 语句列出了 AGE(年龄) 字段为 25 或 27 的数据:

firstdb=# SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 );
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
(3 rows)

firstdb=#

@NOT IN
以下 SELECT 语句列出了 AGE(年龄) 字段不为 25 或 27 的数据:

firstdb=# SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 );
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  3 | Teddy |  23 | Norway                                             |  20000 |
(2 rows)

firstdb=#

@BETWEEN
以下 SELECT 语句列出了 AGE(年龄) 字段在 25 和 27 之间数据:

firstdb=# SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
(3 rows)

firstdb=#

(12) UPDATE语句

首先创建表COMPANY

DROP TABLE COMPANY;
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'David', 27, 'Texas', 85000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );

INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );

更新该表 id = 3 的记录

firstdb=# 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                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
(7 rows)

firstdb=# UPDATE COMPANY SET SALARY = 15000 WHERE ID = 3;
UPDATE 1
firstdb=# SELECT * FROM COMPANY WHERE ID = 3;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  3 | Teddy |  23 | Norway                                             |  15000
(1 row)

firstdb=#

更新全部记录的某字段

firstdb=# UPDATE COMPANY SET SALARY = 15000 WHERE ID = 3;
UPDATE 1
firstdb=# SELECT * FROM COMPANY WHERE ID = 3;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  3 | Teddy |  23 | Norway                                             |  15000
(1 row)

firstdb=# UPDATE COMPANY SET ADDRESS = 'Texas', SALARY=20000;
UPDATE 7
firstdb=# SELECT * FROM company;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | Texas                                              |  20000
  2 | Allen |  25 | Texas                                              |  20000
  4 | Mark  |  25 | Texas                                              |  20000
  5 | David |  27 | Texas                                              |  20000
  6 | Kim   |  22 | Texas                                              |  20000
  7 | James |  24 | Texas                                              |  20000
  3 | Teddy |  23 | Texas                                              |  20000
(7 rows)

firstdb=#

(13) DELETE语句
删除ID为2~4的记录

firstdb=# DELETE FROM COMPANY WHERE ID BETWEEN 2 AND 4;
DELETE 3
firstdb=# SELECT * FROM company;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | Texas                                              |  20000
  5 | David |  27 | Texas                                              |  20000
  6 | Kim   |  22 | Texas                                              |  20000
  7 | James |  24 | Texas                                              |  20000
(4 rows)

firstdb=#

删除整张表的数据

firstdb=# DELETE FROM COMPANY;
DELETE 4
firstdb=# SELECT * FROM company;
 id | name | age | address | salary
----+------+-----+---------+--------
(0 rows)

firstdb=#

(14) LIMIT 子句
取前4条记录

firstdb=# SELECT * FROM COMPANY LIMIT 4;
 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                                          |  65000
(4 rows)

firstdb=#

跳过前两条记录,从第三条记录开始取3条记录

firstdb=# SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  3 | Teddy |  23 | Norway                                             |  20000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  5 | David |  27 | Texas                                              |  85000
(3 rows)

firstdb=#

(15) ORDER BY 语句

firstdb=# SELECT * FROM COMPANY ORDER BY AGE, NAME;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  6 | Kim   |  22 | South-Hall                                         |  45000
  3 | Teddy |  23 | Norway                                             |  20000
  7 | James |  24 | Houston                                            |  10000
  2 | Allen |  25 | Texas                                              |  15000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  1 | Paul  |  32 | California                                         |  20000
(7 rows)

firstdb=#
firstdb=#
firstdb=# SELECT * FROM COMPANY ORDER BY AGE, NAME DESC;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  6 | Kim   |  22 | South-Hall                                         |  45000
  3 | Teddy |  23 | Norway                                             |  20000
  7 | James |  24 | Houston                                            |  10000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  2 | Allen |  25 | Texas                                              |  15000
  5 | David |  27 | Texas                                              |  85000
  1 | Paul  |  32 | California                                         |  20000
(7 rows)

firstdb=# SELECT * FROM COMPANY ORDER BY AGE DESC, NAME DESC;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | California                                         |  20000
  5 | David |  27 | Texas                                              |  85000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  2 | Allen |  25 | Texas                                              |  15000
  7 | James |  24 | Houston                                            |  10000
  3 | Teddy |  23 | Norway                                             |  20000
  6 | Kim   |  22 | South-Hall                                         |  45000
(7 rows)

firstdb=# SELECT * FROM COMPANY ORDER BY AGE DESC, NAME;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | California                                         |  20000
  5 | David |  27 | Texas                                              |  85000
  2 | Allen |  25 | Texas                                              |  15000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  7 | James |  24 | Houston                                            |  10000
  3 | Teddy |  23 | Norway                                             |  20000
  6 | Kim   |  22 | South-Hall                                         |  45000
(7 rows)

firstdb=#

(16) GROUP BY 语句

firstdb=# SELECT * FROM COMPANY ORDER BY NAME;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  2 | Allen |  25 | Texas                                              |  15000
  5 | David |  27 | Texas                                              |  85000
  7 | James |  24 | Houston                                            |  10000
 10 | James |  45 | Texas                                              |   5000
  9 | James |  44 | Norway                                             |   5000
  6 | Kim   |  22 | South-Hall                                         |  45000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  1 | Paul  |  32 | California                                         |  20000
  8 | Paul  |  24 | Houston                                            |  20000
  3 | Teddy |  23 | Norway                                             |  20000
(10 rows)

firstdb=# SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME ORDER BY NAME;
 name  |  sum
-------+-------
 Allen | 15000
 David | 85000
 James | 20000
 Kim   | 45000
 Mark  | 65000
 Paul  | 40000
 Teddy | 20000
(7 rows)

firstdb=#

(17) WITH 子句
http://www.jasongj.com/sql/cte/

(18) HAVING 子句

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

firstdb=# 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                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
(7 rows)

firstdb=#
firstdb=# SELECT NAME FROM COMPANY GROUP BY name HAVING count(name) < 2;
 name
-------
 Teddy
 David
 Paul
 Kim
 Mark
 Allen
 James
(7 rows)

firstdb=# INSERT INTO COMPANY VALUES (8, 'Paul', 24, 'Houston', 20000.00);
INSERT 0 1
firstdb=# INSERT INTO COMPANY VALUES (9, 'James', 44, 'Norway', 5000.00);
INSERT 0 1
firstdb=# INSERT INTO COMPANY VALUES (10, 'James', 45, 'Texas', 5000.00);
INSERT 0 1
firstdb=# 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                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
  8 | Paul  |  24 | Houston                                            |  20000
  9 | James |  44 | Norway                                             |   5000
 10 | James |  45 | Texas                                              |   5000
(10 rows)

firstdb=#

firstdb=# SELECT address FROM COMPANY GROUP BY address HAVING count(address) > 1;
                      address
----------------------------------------------------
 Houston
 Norway
 Texas
(3 rows)

firstdb=# SELECT address FROM COMPANY GROUP BY address HAVING count(address) > 2;
                      address
----------------------------------------------------
 Texas
(1 row)

firstdb=#

(19)DISTINCT 关键字

firstdb=# INSERT INTO COMPANY VALUES (11, 'James', 45, 'Texas', 15000.00);
INSERT 0 1
firstdb=#
firstdb=# SELECT address,name FROM COMPANY;
                      address                       | name
----------------------------------------------------+-------
 California                                         | Paul
 Texas                                              | Allen
 Norway                                             | Teddy
 Rich-Mond                                          | Mark
 Texas                                              | David
 South-Hall                                         | Kim
 Houston                                            | James
 Houston                                            | Paul
 Norway                                             | James
 Texas                                              | James
 Texas                                              | James
(11 rows)

firstdb=# SELECT DISTINCT address,name FROM COMPANY;
                      address                       | name
----------------------------------------------------+-------
 Texas                                              | James
 Norway                                             | James
 Rich-Mond                                          | Mark
 Texas                                              | Allen
 Houston                                            | James
 California                                         | Paul
 Houston                                            | Paul
 Texas                                              | David
 South-Hall                                         | Kim
 Norway                                             | Teddy
(10 rows)

firstdb=# SELECT DISTINCT address FROM COMPANY;
                      address
----------------------------------------------------
 Rich-Mond
 Houston
 South-Hall
 California
 Norway
 Texas
(6 rows)

firstdb=# SELECT DISTINCT NAME FROM COMPANY;
 name
-------
 Teddy
 David
 Paul
 Kim
 Mark
 Allen
 James
(7 rows)

firstdb=#
firstdb=# 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                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
  8 | Paul  |  24 | Houston                                            |  20000
  9 | James |  44 | Norway                                             |   5000
 10 | James |  45 | Texas                                              |   5000
 11 | James |  45 | Texas                                              |  15000
(11 rows)

firstdb=#

好的,我可以和你分享一些关于 PostgreSQL 的学习笔记。 1. PostgreSQL 是什么? PostgreSQL 是一种开源的关系型数据库管理系统,它支持 SQL 语言和 ACID 事务处理,并且具有丰富的特性和扩展性,可用于大型企业应用、Web 应用和嵌入式应用。 2. PostgreSQL 的特点 PostgreSQL 具有以下特点: - 开源免费 - 支持 ACID 事务处理 - 支持 SQL 语言 - 支持复杂查询和数据类型 - 支持存储过程和触发器 - 可以扩展和定制 - 可以跨平台运行 3. PostgreSQL 的安装 在 Windows 平台下,可以从官网下载 Windows 安装程序进行安装。在 Linux 平台下,可以使用包管理器安装,例如 Ubuntu 可以使用以下命令进行安装: ``` sudo apt-get install postgresql ``` 4. PostgreSQL 的基本操作 创建数据库: ``` CREATE DATABASE databasename; ``` 创建表: ``` CREATE TABLE tablename ( column1 datatype, column2 datatype, column3 datatype, .... ); ``` 插入数据: ``` INSERT INTO tablename (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 查询数据: ``` SELECT column1, column2, ... FROM tablename WHERE condition; ``` 更新数据: ``` UPDATE tablename SET column1 = value1, column2 = value2, ... WHERE condition; ``` 删除数据: ``` DELETE FROM tablename WHERE condition; ``` 5. PostgreSQL 的扩展 PostgreSQL 可以通过扩展来增强其功能,例如: - PostGIS:空间数据处理扩展 - PL/Python:Python 语言扩展 - pgAdmin:可视化管理工具 - pgpool-II:连接池和负载均衡工具 以上就是一些关于 PostgreSQL 的基本学习笔记,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值