Foreign Keys and Primary Keys at the same time
I am trying to create table (orderdetails2); the table has two FKs and a PKs on the two FK columns. Here is my code:
Version 1
create table OrderDetails2 (
PFOrder_ID Number(3) FOREIGN KEY REFERENCES Orderr(Order_ID),
PFProduct_ID Number(3) FOREIGN KEY REFERENCES Product(Product_ID),
CONSTRAINT PF PRIMARY KEY (PFOrder_ID,PFProduct_ID),
CONSTRAINT FK_1 FOREIGN KEY (PFProudct_ID)
REFERENCES Product(Product_ID),
CONSTRAINT FK_2 FOREIGN KEY (PFOrder_ID)
REFERENCES Orderr(Order_ID),
Version 2
create table OrderDetails2 (
PFOrder_ID Number(3)
PFProduct_ID Number(3)
CONSTRAINT PF PRIMARY KEY (PFOrder_ID,PFProduct_ID),
CON开发者_JAVA技巧STRAINT FK_1 FOREIGN KEY (PFProudct_ID) REFERENCES Product(Product_ID),
CONSTRAINT FK_2 FOREIGN KEY (PFOrder_ID) REFERENCES Orderr(Order_ID)
);
I am using Oracle express. A problem pops when I run the code. Here it is (for version 1):
ORA-00907: missing right parenthesis
What is the problem?
Starting from the variant of the code with:
create table OrderDetails2 (
PFOrder_ID Number(3)
PFProduct_ID Number(3)
CONSTRAINT PF PRIMARY KEY (PFOrder_ID,PFProduct_ID),
CONSTRAINT FK_1 FOREIGN KEY (PFProudct_ID) REFERENCES Product(Product_ID),
CONSTRAINT FK_2 FOREIGN KEY (PFOrder_ID) REFERENCES Orderr(Order_ID)
);
You are missing a comma after PFOrder_ID NUMBER(3)
and after PFPoduct_ID NUMBER(3)
.
CREATE TABLE OrderDetails2
(
PFOrder_ID NUMBER(3),
PFProduct_ID NUMBER(3),
CONSTRAINT PF PRIMARY KEY (PFOrder_ID, PFProduct_ID),
CONSTRAINT FK_1 FOREIGN KEY (PFProduct_ID) REFERENCES Product(Product_ID),
CONSTRAINT FK_2 FOREIGN KEY (PFOrder_ID) REFERENCES Orderr(Order_ID)
);
The PRIMARY KEY clause has to be separate from the PFProduct_ID column because it is a compound key. (Is Orderr
spelled correctly?) And PFProudct_ID
in the original is incorrect. If you're a programmer, you have to be able to spell consistently (even if not correctly in the broader sense of writing).
Change the last comma to a close parenthesis: )
.
CREATE TABLE OrderDetails2
(
...,
CONSTRAINT FK_2 FOREIGN KEY (PFOrder_ID) REFERENCES Orderr(Order_ID)
)
精彩评论