以下 SQL 查询的目标是返回居住在 San Fran 或 LA 的每位女性经理的姓名和薪水。我很困惑为什么这个查询会返回男性经理和女性经理......我最初认为定义约束“gender ='F'”以及自然加入会摆脱所有男性经理立即上市。
select Lastname, FirstName, MidInitial, salary, gender
from Employee natural join Works natural join Manages
where Lastname = ManagerLastname and
FirstName = MFirstName and
MidInitial = MMidInitial and
gender = 'F' and
city = 'San Fran' or city = 'LA';
--RESULT:
LASTNAME FIRSTNAME MIDINITIAL SALARY GENDER
Brandy Dan L 42000 M
Clemson Ann M 39000 F
Gill Mary L 48700 F
Simon Eric K 45000 M
有没有人认为上述查询有任何本质上的错误?仅供参考:这是该数据库表的架构:
create table Employee(
Lastname varchar(10),
FirstName varchar(10),
MidInitial char(1),
gender char(1),
street varchar(10),
city varchar(10),
primary key(Lastname, FirstName, MidInitial));
create table company(
company_name varchar(20),
city varchar(10),
primary key(company_name));
create table Works(
Lastname varchar(10),
FirstName varchar(10),
MidInitial char(1),
company_name varchar(20),
salary numeric(8,2),
primary key(Lastname, FirstName, MidInitial, company_name),
foreign key(Lastname, FirstName, MidInitial) references Employee,
foreign key(company_name) references company);
create table Manages(
Lastname varchar(10),
FirstName varchar(10),
MidInitial char(1),
ManagerLastname varchar(10),
MFirstName varchar(10),
MMidInitial char(1),
start_date date,
primary key(Lastname, FirstName, MidInitial, ManagerLastname, MFirstName, MMidInitial),
foreign key(Lastname, FirstName, MidInitial) references Employee);
另外,我正在为 Oracle 11g R2 使用 SQL。
谢谢你看看这个!