tantan6611
asked on
How to set up a foreign key using phpMyAdmin
Hi ALL;
How to set up a foreign key to a table in MySQL using phpMyAdmin ( 2.9.2 )?
Please help
How to set up a foreign key to a table in MySQL using phpMyAdmin ( 2.9.2 )?
Please help
ASKER
Very nice But what should I asgin the colmun that I intend to put as a foreign key for another table .....a Primary key, index or unique key ?!
ASKER
gamebits...
Please explain in more details step 4 what should I do exactly...
4) That's where you set up the constraint.
Please explain in more details step 4 what should I do exactly...
4) That's where you set up the constraint.
Create an index on the field you want to use for foreign key constraint.
Then when you click on Relation view you will have 3 text boxes with the field(s) you want to use.
Lets suppose you have a table name Customer with a field name invoice_id, you also have a table named Invoice with the primary field named invoice_id.
If you open Relation view on Customer you will see a list of all the fields in that table at the top yu will see
Links to
invoice_id [drop down with the list of all the table fields you can link to] ON DELETE [drop down with the options Cascade
SET NULL
NO ACTION
RESTRICT]
ON UPDATE [drop down with the same options]
So you could setup that the invoice will be deleted if you delete the customer link to it [cascade]
I don't know how complex is your application but this is the basic on how to set the foreign key with phpmyadmin
Then when you click on Relation view you will have 3 text boxes with the field(s) you want to use.
Lets suppose you have a table name Customer with a field name invoice_id, you also have a table named Invoice with the primary field named invoice_id.
If you open Relation view on Customer you will see a list of all the fields in that table at the top yu will see
Links to
invoice_id [drop down with the list of all the table fields you can link to] ON DELETE [drop down with the options Cascade
SET NULL
NO ACTION
RESTRICT]
ON UPDATE [drop down with the same options]
So you could setup that the invoice will be deleted if you delete the customer link to it [cascade]
I don't know how complex is your application but this is the basic on how to set the foreign key with phpmyadmin
ASKER
Ok great this is what I'm doing ...
BUT after creating an index for invoice_id field in Customer table and then choosing
invoice_id [Invoice->invoice_id] ON DELETE [Cascade]ON UPDATE [Cascade]
And then press Save
I got this error messege in red at the top of the page
No index defined! ( invoice_id)
So what I did wrong?!!
BUT after creating an index for invoice_id field in Customer table and then choosing
invoice_id [Invoice->invoice_id] ON DELETE [Cascade]ON UPDATE [Cascade]
And then press Save
I got this error messege in red at the top of the page
No index defined! ( invoice_id)
So what I did wrong?!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Your right, the problem is that the tables are populated
So this means that I can not create a foreign key in populated tables?
So this means that I can not create a foreign key in populated tables?
You can create a foreign key in populated tables but the fields would have to be of the same type, change the type if needed.
Thanks for the points and the grade.
Thanks for the points and the grade.
ASKER
The types are the same (varchar) in both fields!
What error message do you get?
ASKER
it givs me the same error messege as before "No index defined! ( invoice_id)"!
Did you indexed invoice_id in both tables?
ASKER
yes...
invoice_id is a (primary key) in Invoice table and (index) in Customer table
invoice_id is a (primary key) in Invoice table and (index) in Customer table
2) Click on the Structure tab.
3) Under the table shown there is a link "Relation View"
4) That's where you set up the constraint.