1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| //HAVING 子句必须放置于 GROUP BY 子句后面,ORDER BY 子句前面 SELECT FROM WHERE GROUP BY HAVING ORDER BY
SELECT CURRENT_TIMESTAMP, CURRENT_ROLE, CURRENT_USER, CURRENT_CATALOG, CURRENT_SCHEMA; SELECT * FROM COMPANY WHERE AGE IS NOT NULL; SELECT * FROM COMPANY WHERE NAME LIKE 'Pa%'; SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 ); SELECT * FROM COMPANY WHERE AGE BETWEEN 20 AND 30; SELECT * FROM company WHERE EXISTS(SELECT ID from company where salary >= 65000); SELECT * FROM company WHERE ID in (SELECT ID from company where salary >= 65000); SELECT * FROM COMPANY WHERE AGE::text LIKE '2%';//类型转换 SELECT * FROM COMPANY WHERE salary::varchar LIKE '1___0'; SELECT * from company limit 3 offset 3; SELECT * FROM company ORDER BY age ASC, salary DESC; SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;//数据存在重复的时候 With CTE AS (Select ID , NAME , AGE , ADDRESS , SALARY FROM COMPANY ) Select * From CTE;//WITH 子句提供了一种编写辅助语句的方法,以便在更大的查询中使用 WITH moved AS (DELETE FROM company WHERE salary <= 20000 RETURNING *) SELECT * from moved WITH RECURSIVE t(n) AS (//可用于递归 还有疑问 VALUES (0) UNION ALL SELECT SALARY FROM COMPANY WHERE SALARY > 20000 ) SELECT sum(n) FROM t; SELECT name FROM COMPANY GROUP BY name HAVING count(name) > 2; //DISTINCT 关键字与 SELECT 语句一起使用,用于去除重复记录,只获取唯一的记录。 SELECT name,age,address,salary,dept FROM company as c LEFT JOIN department as d ON c.id = d.emp_id //左外连接 //UNION 操作符合并两个或多个 SELECT 语句的结果 SELECT name,age,address,salary,dept FROM company as c LEFT JOIN department as d ON c.id = d.emp_id WHERE age < 25 UNION SELECT name,age,address,salary,dept FROM company as c LEFT JOIN department as d ON c.id = d.emp_id WHERE age > 30 select name,age,address,salary,dept from company LEFT JOIN department ON company.id = department.emp_id WHERE dept is not null PostgreSQL 触发器可以在下面几种情况下触发: 在执行操作之前(在检查约束并尝试插入、更新或删除之前)。 在执行操作之后(在检查约束并插入、更新或删除完成之后)。 更新操作(在对一个视图进行插入、更新、删除时)。 触发器的 FOR EACH ROW 属性是可选的,如果选中,当操作修改时每行调用一次;相反,选中 FOR EACH STATEMENT,不管修改了多少行,每个语句标记的触发器执行一次。 索引不应该使用在较小的表上。 索引不应该使用在有频繁的大批量的更新或插入操作的表上。 索引不应该使用在含有大量的 NULL 值的列上。 索引不应该使用在频繁操作的列上。 ALTER TABLE company ADD CONSTRAINT AgeCheck CHECK(age > 18) INSERT INTO company("id", "name", age, address, salary) VALUES (10, 'fanchen', 18, 'Anhui', 20000) > 错误: 关系 "company" 的新列违反了检查约束 "agecheck" DETAIL: 失败, 行包含(10, fanchen, 18, Anhui, 20000) INSERT INTO company("id", "name", age, address, salary) VALUES (10, 'fanchen', 19, 'Anhui', 20000) ALTER TABLE table_name DROP CONSTRAINT 约束名; //删除约束 ALTER TABLE company DROP CONSTRAINT agecheck ALTER TABLE company DROP CONSTRAINT agecheck > OK TRUNCATE TABLE 用于删除表的数据,但不删除表结构。 CREATE VIEW company_dept AS SELECT "name", age, address, salary, dept FROM company LEFT JOIN department ON company."id" = department.emp_id //视图会随着来源表数据的更新而更新 DROP VIEW view_name; ACID 原子性(Atomicity):事务作为一个整体被执行,包含在其中的对数据库的操作要么全部被执行,要么都不执行。 一致性(Consistency):事务应确保数据库的状态从一个一致状态转变为另一个一致状态。一致状态的含义是数据库中的数据应满足完整性约束。 隔离性(Isolation):多个事务并发执行时,一个事务的执行不应影响其他事务的执行。 持久性(Durability):已被提交的事务对数据库的修改应该永久保存在数据库中。 BEGIN; 或者 BEGIN TRANSACTION;
COMMIT; 或者 END TRANSACTION;
ROLLBACK;
数据库中有两种基本的锁:排它锁(Exclusive Locks)和共享锁(Share Locks)。 如果数据对象加上排它锁,则其他的事务不能对它读取和修改。 如果加上共享锁,则该数据库对象可以被其他事务读取,但不能修改。 begin LOCK TABLE company IN ACCESS SHARE MODE//别人可以查到,但是没有提交事务之前的数据未更新 LOCK TABLE company1 IN ACCESS EXCLUSIVE MODE;//事务完成别人才能查到数据
SELECT * FROM company WHERE "id" IN (SELECT "id" FROM company WHERE salary < 20000)//子查询 INSERT INTO COMPANY_BKP SELECT * FROM COMPANY WHERE ID IN (SELECT ID FROM COMPANY);//把整个 COMPANY 表复制到 COMPANY_BKP下 //所有 AGE 大于 27 的客户的 SALARY 更新为原来的 0.50 倍 UPDATE COMPANY SET SALARY = SALARY * 0.50 WHERE AGE IN (SELECT AGE FROM COMPANY_BKP WHERE AGE >= 27 ); //删除 COMPANY 表中所有 AGE 大于或等于 27 的客户记录 DELETE FROM COMPANY WHERE AGE IN (SELECT AGE FROM COMPANY_BKP WHERE AGE > 27 ); PostgreSQL 使用序列来标识字段的自增长,数据类型有 smallserial、serial 和 bigserial 。这些属性类似于 MySQL 数据库支持的 AUTO_INCREMENT 属性。
|