Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: How to create primary key/foreign key?

Re: How to create primary key/foreign key?

From: <Mujeeb_ur_Rehman_at_hotmail.com>
Date: Fri, 14 Aug 1998 08:08:38 GMT
Message-ID: <6r0ra6$j21$1@nnrp1.dejanews.com>


Hi :)

Using the following table structure for an example

table1 (reg_no number, emp_name varchar2(30), .......)

table2 (course_id, course_date, reg_no, .....

Now to create a primary key on table1 use the following syntax

alter table table1 add constraint PK_Table1 Primary Key (
  Reg_No
) Using Index
pctfree 10 initrans 5 tablespace tablespace_name storage (initial xx K/M next xx K/M pctincrease xx) /

Above PK_table1 is the name of the constraint which is optional. But it is always better to give a meaningful name to a constraint instead of having Oracle given name in the format sys_CXXXXXX, where XXXXXXX is number sequence.

Now u cannot create a foriegn key on the same field in the same table. But u can create a foreign key from a second table to the first table. Let say u want to create a foreign key refference for table2 the syntax would be

alter table table2 add constraint FK_table1 Foreign Key (
 reg_no
) References
table1 (reg_no)
/

Hope this help and answer your question.


In article <paulsian-1308981207560001_at_1cust144.tnt15.det3.da.uu.net>,   paulsian_at_sprintmail.com (Paul Sian) wrote:
> I have a table where I need to create a primary key and a foreign key for
> the same column. How do I do this?
> Thanks
>

-----== Posted via Deja News, The Leader in Internet Discussion ==----- http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum Received on Fri Aug 14 1998 - 03:08:38 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US