Is it possible to have primary key and foreign key in one table if yes so what is the use of foreign key?

Showing Answers 1 - 6 of 6 Answers

neha

  • Nov 22nd, 2006
 

yes u can,foreign key is used to cut short the repeated use of primary key data.

manu

  • Feb 22nd, 2007
 

yes it possible 2 have foriegn key constraint on the same table where there is a primary key.this is can done by adding a constraint to the table using alter table tblname . the refernce column and table being the base table.
         thus one table can have both primary key as well as foreign key constraint.

  Was this answer useful?  Yes

Nikhil_4_Oracle

  • Mar 15th, 2007
 

Dear All,

Yes its possible.


Alter table reftab add constraint pk_reftab_col1 primary key;


Alter table reftab add constraint fk_reftab_col2 (col2) references reftab (clo1);


it will work....But there is no sence in doing so..u wll just make duplicate data in same table. Also it will increases the data redudancy...

Regards,

Nikhil
 

  Was this answer useful?  Yes

Shri

  • Nov 1st, 2007
 

Dear Vivek,
If I understand your question well, you mean to say that "is there any possibility to have both primary & foreign keys in the same table".
Well, the answer is No. The foreign key (by definition) in a table is the key which is the reference of primary key of different table i.e. the foreign key in a table is the primary key of another table. Therefore, in order to make use of foreign key concept, the tables should be different (one containing primary key while other containing foreign key).
(Please let me know for any issues).

  Was this answer useful?  Yes

Yes it is possible to have primary key and foreign key in one table.
For e.g the schema for table category is as follows
Category_id: int primary key
Category_title: varchar(30)
parent_id: int(3)

In the above table schema column "parent_id" is used to denote parent  category id which references "category_id" column in same table.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions