0

I pull off from my project for a while because I stucked. I opened up my localhost to study phpmyadmin which for me the UI is confusing. So I created 2 table, set primary key and foreign key in them, let say table A has PK and table B has FK. I insert data into table A, but the table B still remain zero row of data. Why?

I strange thing in phpmyadmin, u can only set a column to have FK only if it's unique or u set the index to it. I don't get it.

Nikel Weis
  • 724
  • 10
  • 27
user3057928
  • 605
  • 1
  • 9
  • 12

1 Answers1

0

This is totally intended. If a table has a foreigen key this only means, that if you enter a row of data to table b, these means that this has to be present in table a as well. So it is mainly to keep your data in a consistent state. Have a look here for basics about foreigen keys and more here. Alternativly to phpmyadmin checkout MySQL-Workbench.

Hope this helps you for the start.

Community
  • 1
  • 1
Nikel Weis
  • 724
  • 10
  • 27
  • It means that I didn't did anything wrong? I notice if table A PK has let say a 1 int, in the table B's FK have only that value. so PK and FK wasn't about relationship? it's just the matter of data consistency? – user3057928 Dec 04 '13 at 11:10
  • It is indeed about relationship. It says a value that is present in your foreign key column in table b MUST be present in table a. But it does not say, that an entry in table a HAS to be present in table b. So it says something about a realtionship but furthermore about consistency. Checkout the links. – Nikel Weis Dec 04 '13 at 11:13
  • ok I understand a litte bit now. FK can be said is a reference? once PK been update / del, it will affect the FK? right? – user3057928 Dec 04 '13 at 11:17
  • Yes, you can see the FK as a reference. When the PK is deleted/updated it depends on how you define the "ON DELETE" and "ON UPDATE"-Action. for the FK. You could also define "ON DELETE" => NO ACTION. The result will be, that you cannot delete the PK. Please do not forget to cast your vote or mark an answer as correct. – Nikel Weis Dec 04 '13 at 11:29
  • TQ so much for the explanation – user3057928 Dec 04 '13 at 14:40