oracle SQL中关联更新的问题
我的问题有点太长了:
TL;DR
Oracle 相关更新无法按预期工作,使受害者列没有变化。
表结构
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
基本上,我需要使用 SPECIALITY_NAME 值来扩充 STUDENTS 表的 SPECIALTY 列(现在它填充有 null
),我可以通过连接 CLASSES 和 SPECIALTIES 表来获取该值(是的,我知道这很奇怪,并且会破坏数据库规范化,但这就是任务)。
所以这就是我想要做的:
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)
问题
Oracle 说 N 行已更新。,因此查询结果似乎没问题,但 STUDENTS 表中的 SPECIALTY 列仍然只包含空值。
我做错了什么?
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
Basicaly, I need to augment STUDENTS table's column SPECIALITY with SPECIALITY_NAME values (right now it's populated with null
s) 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
Try this instead