Subject Re: [ib-support] How to add a foreign key after creating a table
Author Sujan Sreenath
Hello Stevio,

Here is what you asked for.

Sujan


At 03:40 AM 11-09-02 +0000, you wrote:
>Hi everybody,
>
>Suppose there are two tables.
>
>CREATE TABLE TABLE1 (PKEY1 INTEGER NOT NULL PRIMARY KEY)
>
>CREATE TABLE TABLE2 (FKEY1 INTEGER NOT NULL)
>
>after running this SQL i want to add a foreign key contraint to FKEY1 on
>TABLE2, i tried the following
>
>ALTER TABLE TABLE2
>ADD CONSTRAINT cons_name FOREIGN KEY FKEY1 REFRENCES TABLE1.PKEY1
>
>but this does not work, any suggestions?

You haven't said what the error was, but a good place to start would be to
get the syntax and spelling correct.

ALTER TABLE TABLE2
ADD CONSTRAINT cons_name FOREIGN KEY (FKEY1) REFERENCES TABLE1(PKEY1)

Note that "REFERENCES" has four Es. Also, if the FK references the PK in
the other table then including the name of the PK column is optional. It
is mandatory where you are referencing a UNIQUE key that is not the PK.

If you're still getting an error then, come back here and tell us what the
message is.

heLen