在需要引用另一个表中某列的子集,且这些子集必须是唯一的情况下,对一个表进行规范化处理。

我如何将这个关系规范化(即使其符合 1NF、2NF和3NF。)

CREATE TABLE IF NOT EXISTS series (
  series_id SERIAL PRIMARY KEY,
  dimension_ids INT[] UNIQUE,
  dataset_id INT REFERENCES dataset(dataset_id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS dimension (
  dimension_id SERIAL PRIMARY KEY,
  dim VARCHAR(50),
  val VARCHAR(50),
  dataset_id INT REFERENCES dataset(dataset_id) ON DELETE CASCADE,
  UNIQUE (dim, val, dataset_id)
);

的子集,其中 dimension_id中的记录进行唯一标识。series 表。

编辑

为了提供更多的信息,我想存储的数据来自XML结构,看起来像下面的东西

<?xml version="1.0" encoding="utf-8"?>
<message:StructureSpecificData >
   <message:Header>
      <message:ID>IREF757740</message:ID>
      <message:Test>false</message:Test>
      <message:Prepared>2020-04-09T14:55:23</message:Prepared>
   </message:Header>
   <message:DataSet ss:dataScope="DataStructure" ss:structureRef="CPI" xsi:type="ns1:DataSetType">
      <Series FREQ="M" GEOG_AREA="WC" UNIT="IDX">
         <Obs OBS_STATUS="A" OBS_VALUE="75.5" TIME_PERIOD="31-Jan-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="75.8" TIME_PERIOD="29-Feb-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="77" TIME_PERIOD="31-Mar-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="77.5" TIME_PERIOD="30-Apr-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="78" TIME_PERIOD="31-May-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="78.8" TIME_PERIOD="30-Jun-2008"/>
      </Series>
      <Series FREQ="M" GEOG_AREA="NC" UNIT="IDX">
         <Obs OBS_STATUS="A" OBS_VALUE="75.5" TIME_PERIOD="31-Jan-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="75.8" TIME_PERIOD="29-Feb-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="77" TIME_PERIOD="31-Mar-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="77.5" TIME_PERIOD="30-Apr-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="78" TIME_PERIOD="31-May-2008"/>
         <Obs OBS_STATUS="A" OBS_VALUE="78.8" TIME_PERIOD="30-Jun-2008"/>
      </Series>
   </message:DataSet>
</message:StructureSpecificData>

有一个数据集,包含 series (0…n),其中包含观测值(0…n)。这些序列由它们的 XML 属性唯一标识–我在数据模型中称之为维度。在我的例子中,我有两个 series按其所覆盖的地理区域不同。任何 series 可以有任意数量的维度。series 预计将从它的维度中查询,并且维度也将使用 series_id. 显而易见的解决办法是采用桥接表。

CREATE TABLE series_dimension
  series_id INT REFERENCES series(series_id) ON DELETE CASCADE,
  dimension_id INT REFERENCES dimension(dimension_id)
);

但是,这种解决办法允许出现以下情况。

|--------------------------|
| series_dimension         |
|-----------|--------------|
| series_id | dimension_id |
|-----------|--------------|
| 1         | 1            |
| 1         | 2            |
| 1         | 3            |
| 1         | 4            |
| 2         | 1            |
| 2         | 2            |
| 2         | 3            |
| 2         | 4            |
|-----------|--------------|

那就是,两个不同的 series 具有相同的维度,所以如果我查询一个 series 对于给定的尺寸集,我不能决定在尺寸的情况下 [1 2 3 4] 我是否在寻找 series_id=1或 series_id=2,这是不可接受的。因此,在这种情况下,我是否必须在具有参考完整性和 “我的 “之间做出决定。独特性 我刚才解释的属性?

解决方案:

考虑到你的期望值约为 20 尺寸,本例仅限于 60. 它确实需要一个受控的过程来定义每个 尺寸集 (系列)。


推理

-- DIM is a valid numeric identifier for a dimension.
--
valid_dim {DIM}
       PK {DIM}

CHECK ((DIM = 1) OR ((DIM > 1) AND (mod(DIM,2) = 0)))


-- data sample
  (DIM)
---------
  (2^0)
, (2^1)
, (2^2)
, ...
, (2^58)
, (2^59)
-- Dimension DIM, named DIM_NAME exists.
--
dimension {DIM, DIM_NAME}
       PK {DIM}
       AK {DIM_NAME}

FK {DIM} REFERENCES valid_dim {DIM}


-- data sample
(DIM, DIM_NAME)
---------------
  (2^0, 'FREQ')
, (2^1, 'GEOG_AREA')
, (2^2, 'UNIT')
, ...
, (2^58, 'AGE_GROUP')
, (2^59, 'HAIR_COLOR')

加载 seriesser_dim 可以从一个函数、应用程序或其他什么地方完成。然而,这应该是一个受控的过程。SER 对于一个给定的维度集来说是唯一的。| 是位式 OR 运营商。

-- Series SER, named SER_NAME exists.
--
series {SER, SER_NAME}
    PK {SER}
    AK {SER_NAME}


-- data sample
(SER, SER_NAME)
--------------------------------
  ((2^0 | 2^1 | 2^2)  , 'F-G-U')
, ((2^1 | 2^58)       , 'G-A'  )
, ((2^0 | 2^58 | 2^59), 'F-A-H')
-- Series SER has dimension DIM.
--
ser_dim {SER, DIM}
     PK {SER, DIM}

FK1 {SER} REFERENCES series    {SER}
FK2 {DIM} REFERENCES dimension {DIM}

CHECK ((DIM & SER) = DIM)

-- data sample
(SER, DIM)
--------------------------------
  ((2^0 | 2^1 | 2^2) , 2^0)
, ((2^0 | 2^1 | 2^2) , 2^1)
, ((2^0 | 2^1 | 2^2) , 2^2)

, ((2^1 | 2^58) , 2^1 )
, ((2^1 | 2^58) , 2^58)

, ((2^0 | 2^58 | 2^59), 2^0)
, ((2^0 | 2^58 | 2^59), 2^58)
, ((2^0 | 2^58 | 2^59), 2^59)

注释。

All attributes (columns) NOT NULL

PK = Primary Key
AK = Alternate Key (Unique)
FK = Foreign Key

PostgreSQL

-- DIM is a valid numeric identifier
-- for a dimension.
--
CREATE TABLE valid_dim (
      DIM bigint NOT NULL

    , CONSTRAINT pk_valid_dim PRIMARY KEY (DIM)

    , CONSTRAINT chk_valid_dim
        CHECK ( (DIM = 1)
                OR ( (DIM > 1)
                     AND (mod(DIM, 2) = 0) )
              )
);

-- define some of valid DIMs
INSERT INTO valid_dim (DIM)
VALUES
  ((2^ 0)::bigint)
, ((2^ 1)::bigint)
, ((2^ 2)::bigint)
-- fill this gap
, ((2^58)::bigint)
, ((2^59)::bigint) ;
-- Dimension DIM, named DIM_NAME exists.
--
CREATE TABLE dimension (
      DIM      bigint NOT NULL
    , DIM_NAME text   NOT NULL

    , CONSTRAINT pk_dim PRIMARY KEY (DIM)
    , CONSTRAINT ak_dim UNIQUE (DIM_NAME)

    , CONSTRAINT
        fk_dim   FOREIGN KEY (DIM)
        REFERENCES valid_dim (DIM)
);

-- define few dimensions
INSERT INTO dimension (DIM, DIM_NAME)
VALUES
  ((2^ 0)::bigint, 'FREQ')
, ((2^ 1)::bigint, 'GEOG_AREA')
, ((2^ 2)::bigint, 'UNIT')
, ((2^58)::bigint, 'AGE_GROUP')
, ((2^59)::bigint, 'HAIR_COLOR') ;
-- Series SER, named SER_NAME exists.
--
CREATE TABLE series (
     SER      bigint NOT NULL
   , SER_NAME text   NOT NULL

   , CONSTRAINT pk_series PRIMARY KEY (SER)
   , CONSTRAINT ak_series UNIQUE (SER_NAME)
);

-- define three series
INSERT INTO series (SER, SER_NAME)

SELECT bit_or(DIM) as SER, 'F-G-U' as SER_NAME
FROM dimension
WHERE DIM_NAME IN ('FREQ', 'GEOG_AREA', 'UNIT')

UNION

SELECT bit_or(DIM) as SER, 'G-A' as SER_NAME
FROM dimension
WHERE DIM_NAME IN ('GEOG_AREA', 'AGE_GROUP')

UNION

SELECT bit_or(DIM) as SER, 'F-A-H' as SER_NAME
FROM dimension
WHERE DIM_NAME IN ('FREQ', 'AGE_GROUP', 'HAIR_COLOR') ;
-- Series SER has dimension DIM.
--
CREATE TABLE ser_dim (
     SER bigint NOT NULL
   , DIM bigint NOT NULL

   , CONSTRAINT pk_ser_dim PRIMARY KEY (SER, DIM)

   , CONSTRAINT
       fk1_ser_dim FOREIGN KEY (SER)
             REFERENCES series (SER)

   , CONSTRAINT
       fk2_ser_dim FOREIGN KEY (DIM)
          REFERENCES dimension (DIM)

   , CONSTRAINT
        chk_ser_dim CHECK ((DIM & SER) = DIM)
);

-- populate ser_dim
INSERT INTO ser_dim (SER, DIM)
SELECT SER, DIM
FROM series
JOIN dimension ON true
WHERE (DIM & SER) = DIM ;

另一个选择是使用(实体化)视图来处理 ser_dim. 这取决于模型的其他部分:如果一个。FK 需要 {SER, DIM} 保留表格,否则视图会更好。

-- An option, instead of the table.
--
CREATE VIEW ser_dim
AS
SELECT SER, DIM
FROM series
JOIN dimension ON true
WHERE (DIM & SER) = DIM ;

测试

-- Show already defined series
-- and their dimensions.
SELECT SER_NAME, DIM_NAME
FROM ser_dim  
JOIN series    USING (SER)
JOIN dimension USING (DIM)
ORDER BY SER_NAME, DIM_NAME ;
-- Get SER for a set of dimensions;
-- use this when defining a series.
SELECT bit_or(DIM) AS SER
FROM dimension
WHERE DIM_NAME IN ('FREQ', 'GEOG_AREA', 'UNIT') ;
-- Find already defined series,
-- given a set of dimensions.
SELECT x.SER
FROM (
  SELECT bit_or(DIM) AS SER
  FROM dimension
  WHERE DIM_NAME IN ('FREQ', 'GEOG_AREA', 'UNIT')
  ) AS x
WHERE EXISTS
  (SELECT 1 FROM series AS s WHERE s.SER = x.SER) ;

概要

遗憾的是标准的SQL实现不支持断言、全数据库约束。SQL标准实际上定义了它们,但还没有运气。因此,并不是每一个业务约束都能在SQL中优雅地完成,通常需要一些创意和妥协。

给TA打赏
共{{data.count}}人
人已打赏
解决方案

Python/Marshmallow。TypeError: __init__() 缺少 1 个必需的位置参数:'cls_or_instance'。

2022-5-11 9:00:16

解决方案

在Lua中从表达式中提取变量

2022-5-11 9:00:18

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索