我有两张桌子:
CREATE TABLE CATEGORY (
cat_id NUMBER PRIMARY KEY,
cat_ust_id NUMBER REFERENCES Category( cat_id )
);
CREATE TABLE PRODUCT (
cat_1 NUMBER REFERENCES Category( cat_id ),
cat_2 NUMBER REFERENCES Category( cat_id ),
cat_3 NUMBER REFERENCES Category( cat_id ),
cat_4 NUMBER REFERENCES Category( cat_id ),
cat_id NUMBER PRIMARY KEY
REFERENCES Category( cat_id )
);
INSERT INTO Category
SELECT 1, NULL FROM DUAL UNION ALL
SELECT 2, NULL FROM DUAL UNION ALL
SELECT 11, 1 FROM DUAL UNION ALL
SELECT 112, 11 FROM DUAL UNION ALL
SELECT 202, 24 FROM DUAL UNION ALL
SELECT 24, 2 FROM DUAL UNION ALL
SELECT 2035, 203 FROM DUAL UNION ALL
SELECT 203, 20 FROM DUAL UNION ALL
SELECT 20, 2 FROM DUAL;
INSERT INTO Product
SELECT 1, NULL, NULL, NULL, 11 FROM DUAL UNION ALL
SELECT 2, NULL, NULL, NULL, 202 FROM DUAL UNION ALL
SELECT 1, NULL, NULL, NULL, 112 FROM DUAL UNION ALL
SELECT 2, NULL, NULL, NULL, 2035 FROM DUAL;
在 PRODUCT 表中,我必须根据 CATEGORY 表层次结构更新某些列才能获得此结果:
cat_1 | cat_2 | cat_3 | cat_4 | cat_id
---------------------------------------
1 | 11 | NULL | NULL | 11
2 | 24 | 202 | NULL | 202
1 | 11 | 112 | NULL | 112
2 | 24 | 203 | 2035 | 2035
我应该为此创建一个过程或函数吗?