database - 想根据以下要求创建表
问题描述
表列详细信息如下:
id - number, Description - varchar 2, partition_0_to_5 - varchar2, partition_6_to_10 - varchar2, partition_11_to_15 - varchar2, status - varcha2
但是 partition_0_to_5 、 partition_6_to_10 和 partition_11_to_15 中的值来自一个主表,并且可以有多个值。假设 ID 号 1 属于两个分区,即 1. Build 和 6. C,那么这些值应该放在 partition_0_to_5 和 partition_6_to_10 列中。
- 分区阶段的主表包含 id 和任务列。
- 开始
- 建造
- 测试
- 注册测试
- 一个
- 乙
- C
- D
- 乙
- F
- G
- H
- 我
- Ĵ
- ķ
- 大号
解决方案
从我的角度来看,这是一个错误的数据模型。为了将来的缘故,您应该对其进行规范化,因为 - 创建新分区时您会做什么(16 - 20)?改变表?修改您已经编写的所有代码以包含新分区?祝你好运!
(此外,您的问题与建模无关,而是在插入期间“解决”,即在应用程序代码中 - 而不是数据模型中。在我看来,这是 - 正如我所说 - 错误的)。
这是我的建议;想想看。
这是“主”表;在您当前的模型中,它包含 3 个附加列(分区):
SQL> create table table_1
2 (id_tab number primary key,
3 description varchar2(20),
4 status varchar2(2)
5 );
Table created.
分区列表。如果创建了新分区(16 - 20),您只需在该表中插入一个新行 - 不应更改任何代码,也不应更改任何表。
SQL> create table part
2 (id_part number primary key,
3 description varchar2(20)
4 );
Table created.
SQL> insert into part (id_part, description)
2 select 1, ' 0 - 5' from dual union all
3 select 2, ' 6 - 10' from dual union all
4 select 3, '11 - 15' from dual;
3 rows created.
分区阶段:
SQL> create table part_stage
2 (id_part_stage number primary key,
3 id_part number references part (id_part),
4 task varchar2(20)
5 );
Table created.
SQL> insert into part_stage (id_part_stage, id_part, task)
2 select 0, 1, 'start' from dual union all
3 select 1, 1, 'build' from dual union all
4 select 2, 1, 'test' from dual union all
5 select 3, 1, 'reg test' from dual union all
6 select 4, 1, 'A' from dual union all
7 select 5, 1, 'B' from dual union all
8 select 6, 2, 'C' from dual union all
9 select 7, 2, 'D' from dual union all
10 select 8, 2, 'E' from dual union all
11 select 9, 2, 'F' from dual union all
12 select 10, 2, 'G' from dual union all
13 select 11, 3, 'H' from dual union all
14 select 12, 3, 'I' from dual union all
15 select 13, 3, 'J' from dual union all
16 select 14, 3, 'K' from dual union all
17 select 15, 3, 'L' from dual;
16 rows created.
该表用于将主表 ( table_1
) “映射”到分区阶段 ( part_stage
)。这样做很灵活,因此您可以根据需要为每个主记录添加任意数量的分区阶段。如果创建了新分区,也没有问题 - 它(再次)只是tmap
表中的另一行。
SQL> create table tmap
2 (id_map number primary key,
3 id_tab number references table_1 (id_tab),
4 id_part_stage number references part_stage (id_part_stage)
5 );
Table created.
顺序(对于主键):
SQL> create sequence seqmap;
Sequence created.
插入会是什么样子?
SQL> declare
2 l_id_tab number := seqmap.nextval;
3 begin
4 -- parent record
5 insert into table_1 (id_tab, description, status)
6 values (l_id_tab, 'Test insert', 'A1');
7
8 -- child record (build)
9 insert into tmap (id_map, id_tab, id_part_stage)
10 values (seqmap.nextval, l_id_tab, 1);
11
12 -- child record (C)
13 insert into tmap (id_map, id_tab, id_part_stage)
14 values (seqmap.nextval, l_id_tab, 6);
15 end;
16 /
PL/SQL procedure successfully completed.
插入了什么?
SQL> select * from table_1;
ID_TAB DESCRIPTION ST
---------- -------------------- --
1 Test insert A1
SQL> select * from tmap;
ID_MAP ID_TAB ID_PART_STAGE
---------- ---------- -------------
2 1 1
3 1 6
“报告”显示什么属于什么:
SQL> select a.id_tab, a.description, a.status, p.description, ps.task
2 from table_1 a join tmap t on t.id_tab = a.id_tab
3 join part_stage ps on ps.id_part_stage = t.id_part_stage
4 join part p on p.id_part = ps.id_part
5 where a.id_tab = 1;
ID_TAB DESCRIPTION ST DESCRIPTION TASK
---------- -------------------- -- -------------------- --------------------
1 Test insert A1 0 - 5 build
1 Test insert A1 6 - 10 C
SQL>
创建新分区时会发生什么?没什么,只是一些插入:
SQL> insert into part (id_part, description)
2 select 4, '16 - 20' from dual;
1 row created.
SQL> insert into part_stage (id_part_stage, id_part, task)
2 select 16, 4, 'M' from dual union all
3 select 17, 4, 'N' from dual union all
4 select 18, 4, 'O' from dual union all
5 select 19, 4, 'P' from dual union all
6 select 20, 4, 'Q' from dual;
5 rows created.
向现有table_1
记录添加新的分区阶段:
SQL> insert into tmap (id_map, id_tab, id_part_stage)
2 values (seqmap.nextval, 1, 20);
1 row created.
结果:
SQL> select a.id_tab, a.description, a.status, p.description, ps.task
2 from table_1 a join tmap t on t.id_tab = a.id_tab
3 join part_stage ps on ps.id_part_stage = t.id_part_stage
4 join part p on p.id_part = ps.id_part
5 where a.id_tab = 1;
ID_TAB DESCRIPTION ST DESCRIPTION TASK
---------- -------------------- -- -------------------- --------------------
1 Test insert A1 0 - 5 build
1 Test insert A1 6 - 10 C
1 Test insert A1 16 - 20 Q
SQL>
看?我没有更改任何代码,没有更改任何表格......什么都没有。一切正常。在您的数据模型中,您还有很多工作要做。
再一次考虑这样做。正确的方式,在我看来。
推荐阅读
- uber-api - Uber API 集成范围
- ssl - 如何将密码套件设置为 2048 位非对称密钥
- swift - Swift 中 pthread_mutex_lock 的替代方案?
- python - pygame中的子弹典当
- python - 将数据帧从 pandas 转换为 pyspark 到 Foundry 的数据类型
- xamarin.android - Xamarin Android:如何向左滑动 recyclerview 项目
- java - java - 如何在java中的固定位置使用一些索引将数组列表元素向右移动
- google-chrome - 如何处理 ERR_CONNECTION_CLOSED 错误
- regex - 从具有特定值和另一个值的单元格中过滤数据
- css - 使用变换缩放功能时出现白线