Welcome 微信登录

首页 / 数据库 / MySQL / PostgreSQL删除表中重复数据行

采用PostgreSQL 9.2 官方文档例子为例:CREATE TABLE weather (
city      varchar(80),
temp_lo int,          -- low temperature
temp_hi int,          -- high temperature
prcp      real,       -- precipitation
date      date
);INSERT INTO weather VALUES
("San Francisco", 46, 50, 0.25, "1994-11-27"),
("San Francisco", 43, 57, 0, "1994-11-29"),
("Hayward", 37, 54, NULL, "1994-11-29"),
("Hayward", 37, 54, NULL, "1994-11-29"); --- duplicated row这里有3中方法:第一种:替换法-- 剔除重复行的数据转存到新表weather_temp
SELECT DISTINCT city, temp_lo, temp_hi, prcp, date
INTO weather_temp
FROM weather;
-- 删除原表
DROP TABLE weather;
-- 将新表重命名为weather
ALTER TABLE weather_temp RENAME TO weather;或者-- 创建与weather一样的表weather_temp
CREATE TABLE weather_temp (LIKE weather INCLUDING CONSTRAINTS);
-- 用剔除重复行的数据填充到weather_temp中
INSERT INTO weather_temp SELECT DISTINCT * FROM weather;
-- 删除原表
DROP TABLE weather;
-- 将新重命名为weather.
ALTER TABLE weather_temp RENAME TO weather;通俗易懂,有很多毁灭性的操作如DROP,而且当数据量大时,耗时耗空间。不推荐。第二种: 添加字段法-- 添加一个新字段,类型为serial
ALTER TABLE weather ADD COLUMN id SERIAL;
-- 删除重复行
DELETE FROM weather WHERE id
NOT IN (
SELECT max(id)
FROM weather
GROUP BY city, temp_lo, temp_hi, prcp, date
);
-- 删除添加的字段
ALTER TABLE weather DROP COLUMN id;需要添加字段,「暂时不知道Postgres是如何处理添加字段的,是直接在原表追加呢,还是复制原表组成新表呢?」,如果是原表追加,可能就会因为新字段的加入而导致分页(一般block: 8k),如果是复制的话那就罪过了。不好。第三种:系统字段[查看 System Columns]DELETE FROM weather
WHERE ctid
NOT IN (
SELECT max(ctid)
FROM weather
GROUP BY city, temp_lo, temp_hi, prcp, date
);针对性强[Postgres独有],但是简单。PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里推荐阅读:在PostgreSQL中如何删除重复记录 http://www.linuxidc.com/Linux/2012-06/61630.htmUbuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htmPostgreSQL缓存详述Oracle Data Guard (RAC+DG) 归档删除策略及脚本相关资讯      PostgreSQL 
  • Ubuntu 16.04 下安装 PostgreSQL   (08月14日)
  • PostgreSQL 发布全系安全更新  (02月12日)
  • 使用pg_basebackup搭建PostgreSQL  (12/30/2015 09:00:29)
  • Linux下RPM包方式安装PostgreSQL  (03月04日)
  • PostgreSQL9.5新特性之行级安全性  (01月19日)
  • 利用pgpool实现PostgreSQL的高可用  (12/30/2015 08:54:36)
本文评论 查看全部评论 (0)
表情: 姓名: 字数