PostgreSQL初始化数据

1.建表插入数据

#创建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 );

2.查询表
postgres=# 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)

postgres=#



3.找出 AGE(年龄) 字段大于等于 25,并且 SALARY(薪资) 字段大于等于 65000 的数据
postgres=# \pset border 2;
Border style is 2.
postgres=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;
+----+-------+-----+----------------------------------------------------+--------+
| id | name  | age |                      address                       | salary |
+----+-------+-----+----------------------------------------------------+--------+
|  4 | Mark  |  25 | Rich-Mond                                          |  65000 |
|  5 | David |  27 | Texas                                              |  85000 |
+----+-------+-----+----------------------------------------------------+--------+
(2 rows)

postgres=#



找出 AGE(年龄) 字段大于等于 25,或者 SALARY(薪资) 字段大于等于 65000 的数据
postgres=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;
+----+-------+-----+----------------------------------------------------+--------+
| id | name  | age |                      address                       | salary |
+----+-------+-----+----------------------------------------------------+--------+
|  1 | Paul  |  32 | California                                         |  20000 |
|  2 | Allen |  25 | Texas                                              |  15000 |
|  4 | Mark  |  25 | Rich-Mond                                          |  65000 |
|  5 | David |  27 | Texas                                              |  85000 |
+----+-------+-----+----------------------------------------------------+--------+
(4 rows)

postgres=#




posted @ 2023-03-10 10:17  花之旭  阅读(73)  评论(0编辑  收藏  举报