检测无效值

在 PostgreSQL 数据库中清理数据

Darryl Reeves, Ph.D.

Assistant Professor, Long Island University - Brooklyn

无效数据值

  camis   |              name               | inspection_date | score |            inspection_type            | ... 
 ---------+---------------------------------+-----------------+-------+---------------------------------------+-----
 ...      | ...                             | ...             | ...   | ...                                   | ...
 41659848 | LA BRISA DEL CIBAO              | 01/30/2018      | 20    | Cycle Inspection / Initial Inspection | ...
 40961447 | MESON SEVILLA RESTAURANT        | 03/19/2019      | 50    | Cycle Inspection / Initial Inspection | ...
 50063071 | WA BAR                          | 05/23/2018      | 15    | Cycle Inspection / Initial Inspection | ...
 50034992 | EMPANADAS MONUMENTAL            | 06/21/2019      | 17    | Cycle Inspection / Re-inspection      | ...
 50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/2020      | 10    | Cycle Inspection / Initial Inspection | ...
 ...      | ...                             | ...             | ...   | ...                                   | ...
  camis   |             name             | inspection_date | score |                 inspection_type                 | ... 
 ---------+------------------------------+-----------------+-------+-------------------------------------------------+-----
 ...      | ...                          | ...             | ...   | ...                                             | ...
 41104041 | THE SPARROW TAVERN           | 09/17/2019      | 13    | Cycle Inspection / Initial Inspection           | ...
 50016937 | BURGER KING                  | 09/14/2018      | 12    | Cycle Inspection / Re-inspection                | ...
 50066469 | DARBAR'S CHICKEN & RIBS      | 08/07/2017      | 11    | Pre-permit (Operational) / Reopening Inspection | ...
 41195691 | F & J PINE RESTAURANT        | 05/02/2019      | 26    | Cycle Inspection / Initial Inspection           | ...
 50015706 | EL RINCONCITO DE LOS SABORES | 12/18/2019      | A     | Cycle Inspection / Initial Inspection           | ...
 ...      | ...                          | ...             | ...   | ...                                             | ...
在 PostgreSQL 数据库中清理数据

用模式匹配处理无效数据

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score NOT SIMILAR TO '\\d+';
在 PostgreSQL 数据库中清理数据

用模式匹配处理无效数据

  • 仅限制非数字字符
  • 不限制值的长度
SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score NOT SIMILAR TO '\\d{1}' AND
  score NOT SIMILAR TO '\\d{2}' AND
  score NOT SIMILAR TO '\\d{3}';
在 PostgreSQL 数据库中清理数据

使用类型约束

  • 列应为整数值
  • 列不应允许非整数
ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;
  • SMALLINT:取值 -32,768 到 32,767
  • USING 子句指定旧值的转换方式
在 PostgreSQL 数据库中清理数据

回顾:正则表达式基础

Metacharacter Usage Example RE Example Match
\\d 匹配数字 (0-9) \\d\\d\\d '345'
? 匹配前一字符出现 0 或 1 次 x\\d? 'x5'
+ 匹配前一字符出现 1 次或更多 \\d+ '10'
* 匹配任意字符出现 0 次或更多 \\d* '3081'
[] 匹配方括号内任一字符 [a-z] 'f'
在 PostgreSQL 数据库中清理数据

类型约束启用范围约束

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score < 0;
在 PostgreSQL 数据库中清理数据

类型约束启用范围约束

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score <= -1;
在 PostgreSQL 数据库中清理数据

类型约束启用范围约束

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score < 0 OR
  score > 100;
在 PostgreSQL 数据库中清理数据

类型约束启用范围约束

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score < 0 OR
  score >= 101;
在 PostgreSQL 数据库中清理数据

BETWEEN 运算符

SELECT
  camis, name, inspection_date, score
FROM
  restaurant_inspection
WHERE
  score NOT BETWEEN 0 AND 100;
  camis   |          name           | inspection_date | score 
 ---------+-------------------------+-----------------+-------
 ...      | ...                     | ...             |   ...
 41702543 | TROPICAL GRILL          | 05/14/2018      |   109
 50074058 | PAD THAI                | 08/01/2018      |   101
 50085349 | DON CHILE MEXICAN GRILL | 12/04/2018      |   124
 50092932 | ENERGY JUICE BAR        | 06/24/2019      |   102
 41702543 | TROPICAL GRILL          | 05/14/2018      |   109
 50034653 | KAI FAN ASIAN CUISINE   | 12/06/2019      |    -1
 ...      | ...                     | ...             |   ...
在 PostgreSQL 数据库中清理数据

Passons à la pratique !

在 PostgreSQL 数据库中清理数据

Preparing Video For Download...