2015-03-31 43 views
2

我有CAMPUS_IDs(NY1,PA1,VA1)和CLASS_IDs(大約10個班級) 所以我必須創建一個表,其中有Campus_id和Class_id列,並在哪個校園中分配班級被教。問題在於所有校園都教授一些課程,其中一些課程僅在一個校園內教授。如何設計該表?我試圖創建下面的表,但它很凌亂:尋找在哪裏教授課程的校園

_____________________ 
| class_id |campus_id 
--------------------- 
| math101 | NY1 | 
--------------------- 
| pyt218 | PA1 | 
--------------------- 
| C767  | VA1 | 
--------------------- 
| pyt218 | PA1 | 
_____________________ 

並且具有class_id的campus_id是另一個表的外鍵。

+0

這對我來說看起來是正確的,是什麼問題? – Bob 2015-03-31 01:14:21

+0

問題是math101例如可能會在NY1和PA1校區中教授。 – 2015-03-31 03:57:46

+0

這將是class_campus表中的兩個條目。一個用於NY1的math101和一個用於PA1的math101。您將使表中的class_id和campus_id唯一。你目前的樣本有兩次與PA1連接的pyt218,我認爲這不是有效的。如果是這樣,請解釋。 – Bob 2015-03-31 12:29:44

回答

1

所以你想列出所有的班級教學的校園?對於像

class_id | campus_id 
math101 | NY1, PA1 
pyt218 | PA1, VA1, NY1 
... 

的輸出,你可以使用現有的wm_concat功能的Oracle 10g:

with d as (
    select 'NY1' as campus_id, 'math101' as class_id from dual union all 
    select 'PA1' as campus_id, 'math101' as class_id from dual union all 
    select 'VA1' as campus_id, 'pyt218' as class_id from dual union all 
    select 'PA1' as campus_id, 'pyt218' as class_id from dual union all 
    select 'PA1' as campus_id, 'class3' as class_id from dual union all 
    select 'VA1' as campus_id, 'class3' as class_id from dual union all 
    select 'VA1' as campus_id, 'math101' as class_id from dual union all 
    select 'NY1' as campus_id, 'class3' as class_id from dual 
) 
select class_id, wm_concat(campus_Id) 
From d 
group by class_id 

在Oracle 11.2,也可以用來產生相同結果的新功能「LISTAGG」 :

with d as (
    select 'NY1' as campus_id, 'math101' as class_id from dual union all 
    ... 
) 
select class_id, listagg(campus_id, ',') within group (order by campus_id) 
From d 
group by class_id