缺少右括号 - ORA-00907

时间:2015-09-14 12:23:49

标签: oracle

select distinct student.ID
from (student join takes using(ID))
join (instructor join teaches using(ID))
using(course_id, sec_id, semester, year)
where instructor.name = 'Einstein'
正确地指出鲍勃,那里有一个'_'。但现在它说USING不能有限定词。

学生:

create table student
(ID         varchar(5), 
 name           varchar(20) not null, 
 dept_name      varchar(20), 
 tot_cred       numeric(3,0) check (tot_cred >= 0),
 primary key (ID),
 foreign key (dept_name) references department
 on delete set null
 )

注意到:

create table takes
(ID         varchar(5), 
 course_id      varchar(8),
 sec_id         varchar(8), 
 semester       varchar(6),
 year           numeric(4,0),
 grade              varchar(2),
 primary key (ID, course_id, sec_id, semester, year),
 foreign key (course_id,sec_id, semester, year) references section
    on delete cascade,
 foreign key (ID) references student
    on delete cascade
)

讲师:

create table instructor
(ID         varchar(5), 
 name           varchar(20) not null, 
 dept_name      varchar(20), 
 salary         numeric(8,2) check (salary > 29000),
 primary key (ID),
 foreign key (dept_name) references department
    on delete set null
)

教导:

create table teaches
(ID         varchar(5), 
 course_id      varchar(8),
 sec_id         varchar(8), 
 semester       varchar(6),
 year           numeric(4,0),
 primary key (ID, course_id, sec_id, semester, year),
 foreign key (course_id,sec_id, semester, year) references section
    on delete cascade,
 foreign key (ID) references instructor
    on delete cascade
);

我按要求粘贴了结构。我希望有所帮助! 我也试过编辑引号但仍然没用。

2 个答案:

答案 0 :(得分:1)

问题不在于括号太少而是太多了。

我猜这里有点因为我不确切知道你的表是什么样的,但看来你的SELECT语句应该是:

select distinct student.ID
  from student
  join takes
    using(ID)
  join instructor
    using(ID)
  join teaches
    using(course id, sec id, semester, year)
  where instructor.name = 'Einstein'

此外,您需要注意您正在使用的编辑器。在你的问题中,围绕爱因斯坦的单引号'不是真正的单引号 - 它们似乎是Unicode撇号,因为它将由诸如Microsoft Word之类的文字处理器添加。字符串常量需要用单引号字符包围(') - 使用其他任何内容都会导致错误。

祝你好运。

答案 1 :(得分:1)

正如Bob Jarvis指出的那样,第三个using子句有空格,应该有下划线;然后改变那些(当你编辑问题时):

ORA-25154: column part of USING clause cannot have qualifier

因为选择列表有student.ID。当您使用using子句时,其中的列不能直接在连接表中引用,因此您需要执行以下操作:

select distinct ID
from (student join takes using(ID))
join (instructor join teaches using(ID))
using(course_id, sec_id, semester, year)
where instructor.name = 'Einstein';

......但现在会得到

ORA-00918: column ambiguously defined

...因为学生和教师都有ID列,这可能就是选择列表首先加上前缀的原因。您可以使用内联视图从选择列表范围中丢失教师ID:

select distinct ID
from (student join takes using(ID))
join (select course_id, sec_id, semester, year, name
  from instructor join teaches using(ID)) instructor
using(course_id, sec_id, semester, year)
where instructor.name = 'Einstein';

但实际上,您最好避免使用using,而是使用on语法和简化连接,例如:

select distinct stu.id
from instructor ins
join teaches tea on tea.id = ins.id
join takes tak on tak.course_id = tea.course_id
and tak.sec_id = tea.sec_id
and tak.semester = tea.semester
and tak.year = tea.year
join student stu on stu.id = tak.id
where ins.name = 'Einstein';

Also see an earlier related answer for more