Re: Can someone help me with some syntax please?
Posted in 1996
Hi,
First make sure that you have a Primary Key on the parent table i.e.
master table with the column 'master_key' .
The syntax for creating PK is :
alter table <table name> add constraint primary key (<column name>)
constraint <primary key name>
In your case it will be:
alter table master add constraint primary key (master_key)
constraint pk_master
Then you can create the FK as follows:
alter table <child table name> add constraint foreign key (<column name>)
references <parent table name> (column name) constraint <fk name >
or you can use this also :
alter table <child table name> add constraint foreign key (<column name>)
references <parent table name> constraint <fk name >
In your case :
alter table detail add constraint foreign key (master_key)
references master (master_key) constraint fk_detail_master
OR
alter table detail add constraint foreign key (master_key)
references master constraint fk_detail_master
Hope this helps you.
You can get these syntax in SQL manuals. (Reference, syntax, tutorial etc.)
Thanks & Regards,
Srinivas Reddy
********************************************************8
On Mon, 16 Sep 1996, Steve Proctor wrote:
> Suppose I have two tables
>
> vvvvvvvvvvvvvvvvvvvvvvvvvvv
>
> table master
>
> master_key char(1)
>
> table detail
>
> detail_key char(1)
> master_key char(1)
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> The Question :
>
> What is the syntax if I want to alter the table detail so that the
> column master_key is a foreign key to the column master_key in the
> master table?
>