简体   繁体   中英

What is going on with this SQL PK/FK error?

I am having an issue with my SQL code. I am creating two tables, one named Orders_For, and another named Result. Result should pull the "Date," and "Time" from Orders_For as foreign keys. These foreign keys from Orders_for are also Result's primary keys. Can someone please help point me in the right direction?

I am getting Error:

ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"

Here is the code that is giving this and the result:

Code (Easier to understand in images):

--ORDERS_FOR(OrdersForDate, OrdersForTime, PhysicianID, TestNumber, TestName, PatientID)

CREATE TABLE ORDERS_FOR(
    
OrdersForDate VARCHAR(50),
    
OrdersForTime VARCHAR(50),
    
O_PhysicianID CHAR(5),
    
O_TestNumber CHAR(5),
    
O_TestName VARCHAR(45),
   
O_PatientID CHAR(10),
    

CONSTRAINT Orders_ForPK_ok PRIMARY KEY(OrdersForDate,OrdersForTime,O_PhysicianID,O_TestNumber,O_TestName,O_PatientID),
    CONSTRAINT Orders_ForFK FOREIGN KEY(O_PhysicianID)
        REFERENCES PHYSICIAN(PhysicianID),
    CONSTRAINT Orders_ForFK_tol FOREIGN KEY(O_TestNumber,O_TestName)
        REFERENCES TEST(TestNumber,TestName),
    --CONSTRAINT Orders_ForFK FOREIGN KEY(O_TestName)
      -- REFERENCES TEST(TestName),
    CONSTRAINT Orders_ForFK_toll FOREIGN KEY(O_PatientID)
        REFERENCES PATIENT(PatientID)
    );
        
--RESULT(Date, Time, Result)
CREATE TABLE RESULT(
   ResultDate VARCHAR(50),
   ResultTime VARCHAR(50),
   ResultGiven VARCHAR(100),
   CONSTRAINT ResultPK_o PRIMARY KEY(ResultDate,ResultTime),
   CONSTRAINT ResultFK_tol FOREIGN KEY(ResultDate,ResultTime)
        REFERENCES ORDERS_FOR(OrdersForDate,OrdersForTime)
  --CONSTRAINT ResultFK_n2 FOREIGN KEY(ResultTime)
       --REFERENCES ORDERS_FOR(OrdersForTime)
    );

And here is the Script Output:

Table ORDERS_FOR created.

Error starting at line : 86 in command -
CREATE TABLE RESULT(
ResultDate VARCHAR(50),
ResultTime VARCHAR(50),
ResultGiven VARCHAR(100),
CONSTRAINT ResultPK_o PRIMARY KEY(ResultDate,ResultTime),
CONSTRAINT ResultFK_tol FOREIGN KEY(ResultDate,ResultTime)
REFERENCES ORDERS_FOR(OrdersForDate,OrdersForTime)
--CONSTRAINT ResultFK_n2 FOREIGN KEY(ResultTime)
--REFERENCES ORDERS_FOR(OrdersForTime)
)

Error report -
ORA-02270: no matching unique or primary key for this column-list 02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS catalog view

These are also attached in the images.

code

result

A foreign key references a primary (or unique) key. Ie for a row in the child table there is exactly one related row in the parent table.

You want a foreign key on (ResultDate, ResultTime) . But this is not a primary or unique key in the parent table ORDERS_FOR . There can be many rows for the same date and time, because the two columns are only part of the primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM