1.以下两种操作均不会删除表中数据
#删除带上回滚一起执行
delete from classinfo where classid=1;
ROLLBACK;
#退出当前事务
delete from classinfo where classid=1;
ABORT TRANSACTION;
2.定义枚举类型,current_mood为枚举类型,值只能输入'sad', 'ok', 'happy'其中一个,输入其它报错
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
name text,
current_mood mood
);
3.非常好用的PARTITION BY
想要分组后再进行条件筛选,每组选出符合条件的数据,group by 使用时select后面的字段必须出现在 GROUP BY 子句中或者在聚合函数中使用,所以使用起来非常不方便
SELECT
iiii,
rain_1hour,
data_time,
ROW_NUMBER () OVER (PARTITION BY iiii ORDER BY xxx desc)as rownumber
FROM
w_data_site_rain
WHERE
data_time < '20180522091500'
and rownumber<=1
获取到了分组中最大的一组值
4.循环删除不需要数据插入到新的表中
WITH moved_rows AS (
DELETE FROM w_data_site_rain
WHERE
data_time < '20180522091500'
RETURNING *
)
INSERT INTO w_data_site_rain (SELECT * FROM moved_rows)