PostgreSQL 数据库线上环境脏数据处理: 删除重复数据表某一列设置默认值给已有表数据某一列填充数据。
Posted Hi,all
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL 数据库线上环境脏数据处理: 删除重复数据表某一列设置默认值给已有表数据某一列填充数据。相关的知识,希望对你有一定的参考价值。
一、环境
PostgreSQL 9
二、准备数据
/*
Navicat PGSQL Data Transfer
Source Server : localhost_postgresql
Source Server Version : 90617
Source Host : localhost:5432
Source Database : odc_test
Source Schema : public
Target Server Type : PGSQL
Target Server Version : 90617
File Encoding : 65001
Date: 2021-07-22 18:41:11
*/
-- ----------------------------
-- Table structure for test_name
-- ----------------------------
DROP TABLE IF EXISTS "public"."test_name";
CREATE TABLE "public"."test_name" (
"id" int8 NOT NULL,
"name" varchar(32) COLLATE "default" NOT NULL,
"age" int4,
"sex" char(1) COLLATE "default",
"card" varchar(25) COLLATE "default",
"tel" varchar(11) COLLATE "default"
)
WITH (OIDS=FALSE)
;
COMMENT ON COLUMN "public"."test_name"."name" IS '姓名';
COMMENT ON COLUMN "public"."test_name"."age" IS '年龄';
COMMENT ON COLUMN "public"."test_name"."sex" IS '性别0男 1女';
COMMENT ON COLUMN "public"."test_name"."card" IS '身份证号';
COMMENT ON COLUMN "public"."test_name"."tel" IS '手机号';
-- ----------------------------
-- Records of test_name
-- ----------------------------
INSERT INTO "public"."test_name" VALUES ('1260784726849396737', 'tom', '12', '0', '123456', '13597257976');
INSERT INTO "public"."test_name" VALUES ('1263069543609675778', 'tom', '12', '0', '123456', '13597257976');
INSERT INTO "public"."test_name" VALUES ('1263073522485833730', 'lucy', '20', '1', '123457', '13597257977');
INSERT INTO "public"."test_name" VALUES ('1265564161713741826', 'lucy', '20', '1', '123457', '13597257977');
INSERT INTO "public"."test_name" VALUES ('1265910154359529473', 'lucy', '20', '1', '123457', '13597257977');
INSERT INTO "public"."test_name" VALUES ('1265919135994896386', 'role', '21', '1', '123458', '13597257978');
INSERT INTO "public"."test_name" VALUES ('1267419682026344450', 'role', '21', '1', '123458', '13597257978');
-- ----------------------------
-- Alter Sequences Owned By
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table test_name
-- ----------------------------
ALTER TABLE "public"."test_name" ADD PRIMARY KEY ("id");
四、重复数据
五、处理重复数据
1、删除重复的数据,只保留一条数据
DELETE FROM test_name t1 WHERE t1.id not in (
SELECT DISTINCT min(t.id) FROM test_name t GROUP BY t.card,t.tel
)
2、执行结果
六、已存在表字段,某一列添加默认值
ALTER TABLE test_name ALTER COLUMN tel SET DEFAULT '-1';
七、已存在表数据,某一列填充数据
1、原数据
2、执行sql
UPDATE test_name SET tel = '-1' WHERE id in(
SELECT id FROM test_name WHERE tel IS NULL
)
3、执行结果
以上是关于PostgreSQL 数据库线上环境脏数据处理: 删除重复数据表某一列设置默认值给已有表数据某一列填充数据。的主要内容,如果未能解决你的问题,请参考以下文章