Dumb_Shock
Dumb_Shock

Reputation: 1008

SQL Syntax help -- FOREIGN KEY

I have 2 tables, T1 and T2.

T1 has fields id, Key_id, title. (id is the primary key, Key_id and title are obtained from T2.)

And

T2 has fields id, Key_id, title and F4. (id is obtained from T1 and Key_id is the primary key in T2)

Can you help in writing the syntax for the above using Foreign key and Primary key

Please comment if is not clear.

Upvotes: 0

Views: 201

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234795

You obviously can't create a foreign key constraint from T1 to T2 before T2 is defined, and vice versa. The trick is to create one table without the FK constraint, create the second table, and then use ALTER TABLE to add the FK constraint to the first table.

Upvotes: 2

wonk0
wonk0

Reputation: 13962

Foreign Keys have their own chapter in the nice MySQL Manual: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

Upvotes: 0

Related Questions