I really cannot delete any row in my tables, it still write something about foreign key, i tried truncate table and it doesnt work, how can i solve this problem ?
It ussually happened when you’re not using cascade delete on your foreign key. It seems another key relationship prevents the deletion. For example, lets say you have 3 table
1. ‘country’ table have ‘id’, ‘name’
2. ‘city’ table have ‘id’, ‘name’ and ‘country_id’ which foreign key reference country(‘id’)
3. ‘maps’ table have ‘id’, ‘lat’,‘lng’ and ‘country_id’ which foreign key reference country(‘id’)
Lets say you have one row of data on each that table, like
1. country = (1, ‘Andorra’)
2. city = (1, ‘Andorra’ , 1)
3. maps = (1, ‘37,84934983’ , ‘7,493794739’, 1)
as you can see, both of city and maps ‘country_id’ field is pointing to same ‘id’ of country table. In this case, you cannot just delete city and country record because another table (map) have a record which pointing to ‘id’ field in country table.
To avoid above problems, you can use cascade delete to your foreign key, so that when you (for example) delete some ‘id’ record in country table, it automaticly deleting corresponding value in it’s child table.
Is good to use foreign keys in MySQL ?
Depending on your needs, but in my opinion, foreign key is helpfull concept in MySQL. It keep you from making mistake such as :
1. You cannot insert some value to child table, which doesn’t already exist in parent table.
2. You cannot deleting relational record, as i ilustrated above.