i have a parent table named "Studio" and many other child tables having "studio_id" as foreign key .Now i want to delete all the related rows from child tables if the parent record is deleted.How can this be possible?
Enable cascading deletes in your schema. The documentation for this is on http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Related
I have question about changing value in child table automatically if value of table mother change value.
I have two tables are brand(mother) and cat(child).
I add foreign key is brand_id in table cat related to primary key id in table brand.
If I change brand_name from brand table, I also want brand_name in table cat changed automatically. Is it possible? and how can I do it.
Thank you so much.
Brand:
Cat:
You could create a custom trigger on the brand table which before update would perform the necessary updates on the cat table.
Here is a link to the documentation for MySQL triggers:
https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html
UPDATE:
As commented by #kerbholz, proper data modeling would suggest that the brand name column shouldn't exist in the cat column as it creates duplication of data. If this is purely for your client to see the data, perhaps building them a custom view using properly modeled tables is a better solution.
Link to appropriate documentation:
https://dev.mysql.com/doc/refman/8.0/en/create-view.html
I am having 4 tables
1. **user**
primary key:user_id
2. **task_details**
primary key:task_id
foreign key:user_id
3.**task_folder**
primary key:task_folder_id
foreign key:user_id
4.**task_details_folder_mapping**
primary key:task_details_folder_mapping_id
foreign key:task_id,task_folder_id
If I delete one folder from my project then that particular task in that folder must be deleted. But folder and tasks is deleting and updated in the front end. But task which is deleted is still shown in the table task_details. But the folder details are deleted from the tables task_folder and task_details_folder_mapping.
Please do help me to rectify the error. I am using codeigniter
Set ON DELETE CASCADE in your foreign key column task_Folder_id of task_details_folder_mapping table
ON DELETE CASCADE allows you to delete data from child tables automatically when you delete the data from the parent table.
In phpmyadmin..first of all should make data engine innodb..then go to parent table and add a cascade on deleting primary key ...
I wish this is helpful for you .thanks
I wish to know what SQL is needed to be passed to implement multiple categories for an article.
I have created 3 tables.
ARTICLES:
id, title, content
CATEGORIES:
id, name
RELATION:
article_id, cat_id
I am successfully able to create the first two tables, and store data in them when user submits the form. But, I have no clue how to update the RELATION table. I searched on stackoverflow, and I learned that I need to use a many to many relationship. I have idea about it. But, I do not know how to do it practically i.e. the SQL.
The Categories are obviously added while the post is published, So i need to update this table only after the first two tables have been updated.
If someone can guide me to a tutorial or in the right direction I shall be greatful.
Assuming that post and article are synonyms, then each time a new post is published and its category is determined you need to 'INSERT' a record into 'RELATION' table.
When you originally create tables you will need to identify Primary and Foreign keys (CONSTRAINTS) and (if so desired) specify whether CASCADE should be enabled.
Apparently you already know how to CREATE tables and INSERT rows.
You may want to Google for PRIMARY KEY, FOREIGN KEY, CASCADE ON DELETE in conjunction with MYSQL and PHP.
Also see if the following helps any: How do I use on delete cascade in mysql?.
This should be taken care of by your SQL DBMS. For example, if you set your relationship's foreign keys to cascade delete or update, when something changes in the parent, the children will also be deleted/updated. Can you give me an example of an update that you would expect to make to the first two tables and the resulting update to the RELATION table?
I am trying to understand relationships fully, and have gone through alot of tutorials. Still i linger on a few things (using MySQLWORKBENCH):
1. Every user can upload a product
2. Every product can have multiple categories, bids, ratings, etc
3. Every user can have multiple phonenumbers
(there are more, but this is the basic setup)
Is this correct?:
1 - I used a 1:n relationship, since every user can upload multiple products.
2 and 3. I used n:m relationship, since there can be multiple products with multiple categories, bids, ratings, etc.
DELETE/UPDATE:
I used ON UPDATE CASCADE and ON DELETE CASCADE everywhere where there is a foreign key...that being 'product', 'category_tags_has_products', 'bid_price_has_product', 'phone_has_user'.
I tried to delete a product like this (php): mysql_query("DELETE FROM product WHERE id='$id'");
I get a 1054 error, which is a foreign key error.
What are the best practises here? It is to my understanding that i shouldn't need to do deletions in any other than the parent-table?
Thanx!
You have a lot of identifying relationships, which mean that the foreign key form part of the primary key on the second table. This is not necessary in most instances, and is only really useful in instances such as link tables.
To this end I would change the user->product link to be non-identifying, which will make user_id a Foreign Key instead of being part of the Primary Key. In workbench the shortcut for a 1:n non-identifying relationship is key '2' (where as '4' is identifying). This in turn should remove the user_id fields from the tables which product links onto.
When you delete a product, it should cascade to the 3 link tables that it links to. At present it may be that it is trying to delete from users also depending on how the FK is set up. The following should cascade deletions (assuming a deletion is permanent and you just want to clear out all linked records)
DELETE FROM product -> deletes from any table with product_id in
DELETE FROM user -> deletes from any table with user_id in
The same applies for phone, rating, bid_price, category_tags.
I hope this if of use, if you need any further pointers feel free to shout :)
The relations look correct.
To find out what is wrong with your query check the $id variable.
Or check the whole query and then run it in the console/phpMyAdmin/etc:
$query = "DELETE FROM product WHERE id='$id'";
var_dump($query);
mysql_query($query);
P.S.: and don't forget to escape all data got from the users! Like this:
$id = mysql_real_escape_string($_GET['id']);
Lets take the example from Yelp: http://www.yelp.com/boston
You can see that it's a website with several different categories, each category containing a listing of places. Should I include all the different places/listing in a single table, or let each category have its own tables?
EDIT: this means having tables 'places_restaurants' and 'places_nightlife', instead of just having the single table 'places' and every entry of every different category will be stored in one huge table... Will this affect performance?
One table per category will require that you CREATE a table every time there's a new category. I'd prefer CATEGORY and PLACE tables, with a one-to-many or many-to-many relationship between them.
You should keep all of the categories in the same table and then have a CategoryID which actually maps each category to the specific / desired category. Your application should be built in a way that is inherently extensible which creating tables each time is definitely not.
It depends. You could normalize the database so that all categories are in their own table, and only referred to from other tables by a foreign key. But there are some arguments that performance outweighs normalization, and so it may be beneficial to keep category names both in their own table of record, and also to include a category name column in other, frequently-joined tables.
If you took the second approach, you would need to ensure data integrity by implementing UPDATE and DELETE triggers such that whenever a category changes in the table of record (presumably, not often), that other tables containing copies of category names also get updated.
It still depends on the application ,also, all the categories is a many to many fields with a main table and of course beliving u have some unique columns in each table