I am developing a bookstore website using PHP/MySQL. I have set some tables in MySQL and I'd like to ask about key constraints.

I created some testing tables I to define how to set key constrains:There are two parent tables with one primary key each and no foreign keysThere is one child table with no primary key and two foreing keys, one from each parent table.I'd like to be able to load .csv files using MySQL importing feature in all three tables, and also delete/change information from each of them.

Here my questions:Should I define key constraints in both parents and child tables?What should I set in each case for ON DELETE and ON UPDATE?

Thanks a lot!!

r937
—
2012-06-20T16:53:20Z —
#2

Sir_Arcturua said:

Should I define key constraints in both parents and child tables?

no, only in child tables

Sir_Arcturua said:

What should I set in each case for ON DELETE and ON UPDATE?

for the time being, don't bother with this part, leave it out and let it use the defaults

if you are just learning, the main thing is to see how a foreign key works "out of the box"

for example, if a parent row has one or more child rows, and you try to delete the parent row, what normally happens is that it won't let you do that, because the child row would then be an orphan, its foreign key to the parent row would be invalid

this is called relational integrity

there are options to override the "out of the box" behaviour, but you don't need to learn them yet

Sir_Arcturua
—
2012-06-20T17:14:32Z —
#3

Thanks a lot for your answer!

When you say:"for the time being, don't bother with this part, leave it out and let it use the defaults"

Do you mean I should not define any key constrain? (I guess I should. If not, how could I set the relationship)Or do you mean I should set ON DELETE no action and ON UPDATE no action? It also gives me "castade", "set null" and "restrict" options.

r937
—
2012-06-20T17:35:57Z —
#4

Sir_Arcturua said:

Do you mean I should not define any key constrain?

no, you should

Sir_Arcturua said:

Or do you mean I should set ON DELETE no action and ON UPDATE no action? It also gives me "castade", "set null" and "restrict" options.

i'm saying omit those, and let them take whatever the default is

exactly where do you see these options?

Sir_Arcturua
—
2012-06-20T18:05:35Z —
#5

Hi,

I am working in CPanel, phpMyAdmin. Select database, select table, select "structure" tab and then press "Relation view" button to see Relations page.

Thanks!!

r937
—
2012-06-20T18:39:00Z —
#6

darn it holmes, just as i feared -- learning how databases work via the phpmyadmin ptui user-friendly interface

choose the RESTRICT option, because that's mysql's default

Sir_Arcturua
—
2012-06-20T19:17:12Z —
#7

Understood, thanks!

One last question. These three sample tables contain static information, it is, not updated by the website when in use. Just to read from them.If I wanted to change something in one of the parent tables, I will be restricted by the relationship with the child table. How could I do it?

r937
—
2012-06-20T19:27:47Z —
#8

Sir_Arcturua said:

If I wanted to change something in one of the parent tables, I will be restricted by the relationship with the child table.

this is true ~only~ if you are trying to update or delete the value of a primary key column -- all other changes to the parent table are okay

Sir_Arcturua
—
2012-06-20T23:53:01Z —
#9

Thanks a lot!!!

Mittineague
—
2014-09-22T23:59:58Z —
#10

This topic is now archived. It is frozen and cannot be changed in any way.