Marybeauty Posted April 4, 2019 Share Posted April 4, 2019 Using MySQL and PHP. I have two tables mobiles and brands. mobiles has { mobileID |brandID | price| BrandName| MobileName} brands has { brandID | BrandName } In admin panel admin is able to delete both the table indivdiually but I want that if brand is deleted all the related mobiles should also be deleted How is this possible? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 4, 2019 Share Posted April 4, 2019 If you're looking for a PHP answer then what you do is make sure that your code deletes from both tables. You should also set up a foreign key constraint to make sure that you cannot delete brands without first deleting the mobiles. If you're looking for a MySQL answer then you set up a foreign key with cascading deletes, so that deleting brands will automatically delete mobiles. If you're looking for advice, do not delete data. If you don't want it around anymore then somehow mark or flag records you don't want to see, but still keep it. Quote Link to comment Share on other sites More sharing options...
Marybeauty Posted April 4, 2019 Author Share Posted April 4, 2019 28 minutes ago, requinix said: If you're looking for a MySQL answer then you set up a foreign key with cascading deletes, so that deleting brands will automatically delete mobiles. how to do this sorry but I am new. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 4, 2019 Share Posted April 4, 2019 When in doubt, start by checking the documentation. Basic syntax to add it to an existing table is ALTER TABLE this_table ADD FOREIGN KEY (this_column, ...) REFERENCES other_table (other_column, ...) ON DELETE CASCADE Foreign keys go on the table that is using the values from another table, so "this" table would be your mobiles table and the "other" one is the brands table. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 5, 2019 Share Posted April 5, 2019 I hate to sound rude and crude, but if you are 'new' then why are you in charge of such a serious project? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 5, 2019 Share Posted April 5, 2019 2 minutes ago, ginerjm said: I hate to sound rude and crude, but if you are 'new' then why are you in charge of such a serious project? I didn't see anything to suggest it's a serious project. How about giving them the benefit of the doubt? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 5, 2019 Share Posted April 5, 2019 Requinix - as usual you are "more right" than I. OP - sorry for my lack of understanding. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.