Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary key composed of two foreign keys? Oracle

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)
);
like image 272
Lost_in_SQL_wilderness Avatar asked May 05 '13 05:05

Lost_in_SQL_wilderness


People also ask

What is the difference between primary key and foreign key?

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.

Does a table with two foreign keys form a unique key?

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.

How many primary keys can be created in a table?

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.

How do you define a FOREIGN KEY constraint in Oracle?

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.


1 Answers

just add this line after the constraints,

CONSTRAINT tb_PK PRIMARY KEY (Ono, Pno)
like image 103
John Woo Avatar answered Oct 13 '22 22:10

John Woo