Create Foreign key in Sql

How To create Foreign key in sql table :

You have a code(for table)like this:

create table person( name     varchar2(5),
                               Id_no       number(3),
                              Address    number(10) );

and you have another table of place:

create table place(name   varchar2(5)primary key,
                             location  varchar2(5),
                             owner_name  varchar2(5) );

Now you want to make owner_name  as Foreign key which relate name of person to the owner name of place table. so there are two methods to make a column foreign key one in table order and other one is column order.
===================================================
Column  Order:
create table place(name              varchar2(5)    primary key,
                             location           varchar2(5),
                             owner_name   varchar2(5)   references   person(name) );
==================================================
Table Order:
create table place(name              varchar2(5)   primary key,
                             location           varchar2(5),
                             owner_name   varchar2(5),
                            constraint   n_pk    foreign key (owner_name)  references   person(name) );

Note:All the bold words are reserved word in Sql.

See Also :How to create Primary Key in data base table using Sql Command.


No comments

Powered by Blogger.