0

尝试在查询中将两个表等值连接在一起时,我收到错误代码 1052 Column FireStationHome in field list is ambiguous

这是我在下面使用的代码:

SELECT FiremanID, FiremanHomeStation,StationPhone
FROM FIRE_REPORT AS F, FIRE_STATION AS FS
WHERE F.FiremanHomeStation = FS.FiremanHomeStation
AND StationPhone ='703';

通过阅读论坛,我了解到我需要通过链接键链接第一个关系。FiremanHomeStation 是我需要运行以下方向的唯一链接键。

显示号码以“703”开头的所有消防员的 FiremanID、FiremanHomeStation 和电话号码。

使用 Boyce Codd 方法,我对以下内容进行了规范化

FIRE_REPORT(FireID,Alarms,Address,FiremanID,FiremanName,FiremanPhone,FiremanHomeStation,StationAddress,StationPhone,TruckID,LicensePlate)

规则:

每个火场可以分配数名消防员
每个火场可以分配数辆消防车
每个消防员可以分配到多个
火场 每个消防车并分配到多个火场
每个消防员有一个主站
每个站都是唯一的
每个站都有一个电话号码

主键

FireID, FiremanID, FiremanHomeStation,TruckID

FireID -> Alarms, Address
FiremanID -> FiremanName, FiremanPhone, FiremanHomeStation
FiremanHomeStation -> StationAddress, StationPhone
TruckID -> LicensePlate

FIRE_REPORT( FireID, FiremanID, FiremanHomeStation, TruckID )
FIRE( FireID , Alarms, Address)
FIREMAN_ID( FiremanID , FiremanName, FiremanPhone)
FIRE_STATION( FiremanHomeStation , StationAddress, StationPhone)
TRUCK( TruckID , LicensePlate)

参照完整性约束
FIRE_REPORT 中的 FireID 必须存在于 FIRE
中的 FireID 中 FIRE_REPORT 中的 FiremanID 必须存在于 FIREMAN_ID 中的 FiremanID 中
FIRE_REPORT 中的 FiremanHomeStation 必须存在于 FIRE_STATION 中的 FiremanHomeStation 中
FIRE_REPORT 中的 TruckID 必须存在于 TRUCK 中的 TruckID 中

create schema project_george;<br>
use project_george;<br>

CREATE TABLE FIRE   (<br>
    FireID int(3) NOT NULL,<br>
    Alarms int(1) NOT NULL,<br>
    Address varChar(25) NOT NULL,<br>
        CONSTRAINT FIRE_PK PRIMARY KEY(FireID),<br>
        CONSTRAINT FIRE_FR_FK FOREIGN KEY(FireID)<br>
        REFERENCES FIRE_REPORT(FireID)<br>
        ON UPDATE CASCADE<br>
);<br>
CREATE TABLE FIREMAN_ID (<br>
    FiremanID int(3) NOT NULL,<br>
    FiremanName varchar(25) NOT NULL,<br>
    FiremanPhone char(10) NOT NULL UNIQUE,<br>
        CONSTRAINT FI_PK PRIMARY KEY(FiremanID),<br>
        CONSTRAINT FI_FR_FK FOREIGN KEY(FiremanID)<br>
        REFERENCES FIRE_REPORT(FiremanID)<br>
        ON UPDATE CASCADE<br>
);<br>
CREATE TABLE FIRE_STATION   (<br>
    FiremanHomeStation varChar(25) NOT NULL UNIQUE,<br>
    StationAddress varChar(25) NOT NULL,<br>
    StationPhone char(10) NOT NULL,<br>
        CONSTRAINT FS_PK PRIMARY KEY(FiremanHomeStation),<br>
        CONSTRAINT FS_FR_FK FOREIGN KEY(FiremanHomeStation)<br>
        REFERENCES FIRE_REPORT(FiremanHomeStation)<br>
        ON UPDATE CASCADE<br>
);<br>
CREATE TABLE TRUCK  (<br>
    TruckID int(3) NOT NULL,<br>
    LicensePlate char(5) NOT NULL UNIQUE,<br>
            CONSTRAINT TRUCK_PK PRIMARY KEY(TruckID),<br>
            CONSTRAINT TRUCK_RC_FK FOREIGN KEY(TruckID)<br>
            REFERENCES FIRE_REPORT(TruckID)<br>
            ON UPDATE CASCADE<br>
);<br>

CREATE TABLE FIRE_REPORT    (<br>
    FireID  int(3)  NOT NULL,<br>
    FiremanID int(3) NOT NULL,<br>
    FiremanHomeStation varChar(25) NOT NULL,<br>
    TruckID int(3) NOT NULL,<br>
        CONSTRAINT FR_PK PRIMARY KEY(FireID,FiremanID,FiremanHomeStation,TruckID),<br>
        CONSTRAINT FR_F_FK FOREIGN KEY (FireID)<br>
        REFERENCES FIRE(FireID) <br>
            ON UPDATE CASCADE,<br>
        CONSTRAINT FR_FID_FK FOREIGN KEY(FiremanID)<br>
        REFERENCES FIREMAN_ID(FiremanID)<br>
            ON UPDATE CASCADE,<br>
        CONSTRAINT FR_FS_FK FOREIGN KEY(FiremanHomeStation)<br>
        REFERENCES FIRE_STATION(FiremanHomeStation)<br>
            ON UPDATE CASCADE,<br>
        CONSTRAINT FR_TRUCK_FK FOREIGN KEY(TruckID)<br>
        REFERENCES TRUCK(TruckID)<br>
            ON UPDATE CASCADE<br>
);<br><br>

INSERT INTO FIRE VALUES(100, 1, '4025 Medford dr, Annandale VA 22003');<br>
INSERT INTO FIRE VALUES(101, 2, '7031 Little River Turnpike Suite 5A
Annandale, VA 22003-5958');<br>
INSERT INTO FIRE VALUES(102, 3, '7001 Little River Turnpike, Annandale, VA 22003 ');<br>
INSERT INTO FIRE VALUES(103, 4, '8333 Little River Turnpike Annandale, VA 22003');<br>
INSERT INTO FIRE VALUES(104, 5, '1600 Pennsylvania Ave NW  Washington, DC 20500');<br><br>

INSERT INTO FIREMAN_ID VALUES(100, 'Guliza Ormuzokova', '703-221-9918');<br>
INSERT INTO FIREMAN_ID VALUES(101, 'Lex Steele', '703-911-4450');<br>
INSERT INTO FIREMAN_ID(FiremanID,FiremanName,FiremanPhone) VALUES(102, 'George Putnam', '703-222-9918');<br>
INSERT INTO FIREMAN_ID VALUES(103, 'Steven Drasner', '571-939-1234');<br>
INSERT INTO FIREMAN_ID VALUES(104, 'Judy Miller', '571-999-1234');<br>
INSERT INTO FIREMAN_ID VALUES(105, 'Lily Thai', '703-911-4451');<br><br>

INSERT INTO FIRE_STATION VALUES('Station 29', '571 Broad st
Fairfax VA 22030', '703-200-9918');<br>
INSERT INTO FIRE_STATION VALUES('Station 229', '8914 Little River Turnpike, Fairfax, VA 22031 ', '703-900-4450');<br>
INSERT INTO FIRE_STATION VALUES('Station 429', '3988 University Dr, Fairfax, VA 22030 ', '571-900-1234');<br><br>

INSERT INTO TRUCK VALUES(1, 'burn29');<br>
INSERT INTO TRUCK VALUES(2, 'burn19');<br>
INSERT INTO TRUCK VALUES(3, 'burn09');<br><br>

/************I can't figure this on out!!!*** *****/<br>
SELECT FiremanID, FiremanHomeStation,StationPhone<br>
FROM FIRE_REPORT AS F, FIRE_STATION AS FS<br>
WHERE F.FiremanHomeStation = FS.FiremanHomeStation<br>
AND StationPhone ='703';<br>
4

1 回答 1

0

FiremanHomeStation 存在于两个表中,但您没有指定要从哪个表中获得结果,这是不明确的。

SELECT FiremanID, FS.FiremanHomeStation,StationPhone
FROM FIRE_REPORT AS F, FIRE_STATION AS FS
WHERE F.FiremanHomeStation = FS.FiremanHomeStation
AND StationPhone ='703';

应该管用

于 2013-03-25T09:22:11.700 回答