I have a question regarding a table creation. I want to combine the attributes of "Ono" and "Pno" into a primary key for a new table. These are both foreign keys, each from different tables. Do I just use a CONSTRAINT Ono_Pno_PK PRIMARY KEY (Ono,Pno)?
what I have used so far:
CREATE TABLE ODetails
(
Ono Number Not Null,
Pno Number Not Null,
Qty Number(3) Not Null,
Creation_Date Date Not Null,
Created_By VARCHAR(10) Not Null,
Last_Update_Date Date Not Null,
Last_Updated_By VARCHAR2(10) Not Null,
CONSTRAINT Ono_FK FOREIGN KEY (Ono) REFERENCES Orders (Ono),
CONSTRAINT Pno_FK FOREIGN KEY (Pno) REFERENCES Parts (Pno)
);
Primary Key-Foreign Key Relationships. A foreign key is a column or a set of columns in one table that references the primary key columns in another table. The primary key is defined as a column (or set of columns) where each value is unique and identifies a single row of the table.
Essentially both attributes which are foreign keys from other tables, together would form a unique key. Show activity on this post. No it doesn't. The above table has no primary key.
Any key, primary or unique, can be composed of 1, 2, 3 or more columns. Please check whether the table is having already a Primary Key or not. If already a Primary Key is there then, you cannot create one more Primary key.
The CONSTRAINT clause is optional. If you omit it, Oracle will assign a system-generated name to the foreign key constraint. Second, specify the FOREIGN KEY clause to defines one or more column as a foreign key and parent table with columns to which the foreign key columns reference.
just add this line after the constraints,
CONSTRAINT tb_PK PRIMARY KEY (Ono, Pno)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With