Click to See Complete Forum and Search --> : query regarding the SQL in oracle


payalsaxena1504
05-03-2006, 09:12 PM
i'm new to SQL used in oracle 8i .i've a confusion regarding primary key & foreign key.is it necessary for a foreign key in a table should be of same name as the primary key of master table which it is referencing.i mean can i keep a foreign key field of a table a different name from the primary key field of master table.if it is possible then what will be the syntax of creation of table having foreign key field.as the suyntax is

create table table_name
field1 type(size),
field 2 type (size) referencing master table_name ;

here field 2 is foreign key referencing to master table having the primary key of same name as field 2 in the table going to be created.
if we give different names to primary key & foreign key then what will be the syntax?

thanks for help!

chazzy
05-03-2006, 09:37 PM
do you mean a table who has a foreign key on itself? i'm not sure what you mean. no, the key does not have to be named anything in particular, but you must note any changes that must corrected in constraints.

payalsaxena1504
05-04-2006, 07:52 AM
hi !thnx for replying! no i does'nt mean a self referencing table.actually i'm learning from Evan Bayross book of oracle.in this in most of the examples the primary key in a table & it's foreign key in another table both have the same name .for eg.
if we have two table product_master & sales order then if product_id field is a primary key in product_master table &product_id is the foreign key in the sales order table referencing to the product_master table.then the syntax of creation of sales oreder table willbe like the following:
create table sales order

order_no varchar2(5) primary key
product_id varchar2(6) references product_master;

now my question is can there be a field name like p_id in sales order table instead of product_id ,reerencing to product_master table.if is it possible then
in such case how we use it to reerence product_master table whilecreating the sales order table,i mean what will bw the syntax in that case?
thnx for help!

chazzy
05-04-2006, 08:08 AM
it's common to have, yes, but not required. the syntax stays the same...

payalsaxena1504
05-04-2006, 08:10 AM
thnx a lot!