开发者

Problems with correlated update in oracle SQL

My question would be a little too long so:

TL;DR

Oracle correlated update doesn't work as expected, leaving victim column without changes.

Tables structure

STUDENTS:

STUDENT_ID  NUMBER(5,0)         
LAST_NAME   VARCHAR2(15 BYTE)   
FIRST_NAME  VARCHAR2(15 BYTE)   
MIDDLE_NAME     VARCHAR2(15 BYTE)   
FINANCIAL_AID   NUMBER(7,2) 
CLASS_NO    VARCHAR2(15 BYTE)       
SPECIALITY      VARCHAR2(100 BYTE)

CLASSES:

CLASS_NO    VARCHAR2(15 BYTE)   
SPECIALITY_NO   NUMBER(5,0) 

SPECIALITIES:

SPECIALITY_ID   NUMBER(5,0)
SPECIALITY_NAME VARCHAR2(40 BYTE)

Task

Ba开发者_JAVA技巧sicaly, I need to augment STUDENTS table's column SPECIALITY with SPECIALITY_NAME values (right now it's populated with nulls) which I can get via connecting with CLASSES and SPECIALITIES tables (yep, I know that's weird and will broke database normalization, but that's the task).

So here is what I'm trying to do:

UPDATE STUDENTS S SET SPECIALITY = (SELECT SPECIALITY_NAME FROM 
  (SELECT * FROM STUDENTS NATURAL JOIN
    CLASSES NATURAL JOIN SPECIALITIES) ALLS
  WHERE S.STUDENT_ID = ALLS.STUDENT_ID)

Problem

Oracle says that N rows updated., hence query result seems to be ok, but SPECIALITY column in STUDENTS table still contains only nulls.

What am I doing wrong?


Try this instead

update students s set specialty = 
   (select sp.specialty_name from classes c
    join specialities sp
      on sp.speciality_number = c.speciality_number
    where c.class_no = s.class_no)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜