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
Related
First off I am using both LibreBase and phpmyadmin
In short I have 2 tables a Student and a StudentFeedback table
Student contains:
StudentUserID(primary key) type=int(11) Null=no Default=none Extra=Auto_Increment
FirstName type=varchar(50) Null=Yes Default=Null
Surname type=varchar(50) Null=Yes Default=Null
StudentFeedback contains:
FeedbackID(primary key) type=int(11) Null=no Default=none Extra=Auto_Increment
Student(I think is foreign key) type=int(11) Null=Yes Default=Null
Feedback type=varchar(150) Null=no Default=none
Now what I want to happen is a student can be created using a form on librebase which is then added to the table e.g.
1 Joe Bloggs, 2 Bill Gates, 3 Steve Jobs.
However I have created another form to then add data to the feedback table so that Joe Bloggs or indeed Bill or Steve can use this form enter there user id and then add there feedback. This data will then be displayed by a variety of queries and reports which I am confident I can do. The problem arises when I try to add data via the librebase form.
The form to add a new student consists of:
Firstname (textbook)
Surname (textbox)
The data will however not enter/be input in to the table as librebase throws up an error box saying
Error inserting the new record
SQL Status: 23000
Error code: 1452
Cannot add or update a child row: a foreign key constraint fails >(`mydb`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`StudentUserID`) >REFERENCES `StudentFeedback` (`Student`) ON DELETE CASCADE ON UPDATE >CASCADE)
The same error appears if I try to add data into the form I have created to add feedback to the StudentFeedback table
Apologies if this makes next to no sense I'll happily answer any questions you may have.
While Creating foreign key you have to give referencing properties:
ON UPDATE CASCADE
ON DELETE CASCADE
please go through the referencing properties.
I've solved the problem now, I had a constraint blocking the input or certain data and so used an sql query to view all my database constraints and then used another query to remove the relevant constraint. After that it's as simple as adding the foreign key ensuring that I used on update cascade on delete cascade
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'm building a backpanel to my TV series website I have 2 tables. One called shows and the other called seasons.
Structure:
Shows - show_id PK, show_title
Seasons - season_id PK, show_id (Foreign key), season
I'm listing all the tv show names in a table in the backend with a delete button however when i press delete it will not delete the show because it has seasons attached too it. If i try delete a show that has no seasons attached it removes it from the database with no problem.
My code looks like this on remove.php:
$id=$_GET['id'];
// Delete show
$sql="DELETE FROM shows WHERE shows_id='$id'";
$result=mysql_query($sql);
I'm guessing i would also need to remove the seasons assigned too the same show but i'm unsure on how to go about doing this.
If you are using InnoDB for your MySQL table, life could be really simple for you here if you checked / changed the constraints on your Foreign Key.
Changing these depends on how you set it up, whether you're using command line interface or some GUI to manage your db.
But when you created the foreign key field in your seasons table, you set up constraints. If you update that constraint to be as follows:
On Update: Do Nothing
\\ On Delete: Cascade
Now any time you delete a row from shows, whatever associated season rows exist will be deleted automatically because of the cascade rule. Very helpful, and a must for setting up normalized databases.
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
$id=$_GET['id'];
// Delete all seasons for this show first.
$sql="DELETE FROM seasons WHERE show_id='$id'";
// Now, Delete show
$sql="DELETE FROM shows WHERE id='$id'";
First of all, i know there are many posts about this subject but the ones i checked didnt help me so i wanted to post my problem.
I am building a website that people can register as user then fill some different forms for me. I have 2 table one is "user" and the other one is "form" table. In user table i have user_id which is auto_incremented and this id connected to "form" table as foreign key. I want to see which user filled forms via user_id. When i try to register filled form to database i get "Cannot add or update a child row: a foreign key constraint fails" message. How can i fix this?
If you have set the user_id on on your form table as a foreign key pointing at the user_id in the user table, you must supply a valid user_id when you insert the form row.
You could set the user_id field on the form table to be allowed to be NULL this will mean it does not have to be set.
you are trying to add some data to your foreign key column that there is no same data in other table .
This mostly happens when there is a data in table and you want to create/ edit/ delete a foreign_key OR
the field that you want to make it foreign_key is not the same format with Table field
solution:
You have to make a
1)backup or copy of you data
2)remove the foreign_keys
3)and Truncate data from table
4)Finally re attach the foreign_keys
5)update your data
I hope it helps
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