Whats wrong with this IF statement? - mysql

I'm learning about MySQL, I'm using PHPMyAdmin in wamp server. Now I'm trying to make a trigger for a table but I'm having troubles when I insert data in the parent table. I just want to evaluate a field named Es_Ban and depending on its value fill a table o another
here is my code:
IF new.es_ban = 1 THEN
INSERT INTO Banca(Ban_Due,Nom_Ban,Mon_Banca)
VALUES (new.Usu_Log, new.Ban_Age, 0 );
END IF;
After inserting a row in the parent named logins I get this error:
1452 - Cannot add or update a child row: a foreign key constraint
fails (`prueba`.`banca`, CONSTRAINT `banca_ibfk_1` FOREIGN KEY
(`Ban_Due`) REFERENCES `logins` (`Usu_Log`) ON DELETE CASCADE ON UPDATE CASCADE
The only way I got it to work was by delenting the relationship between Login and Banca...

Related

how can I delete my child row in sql if it's failing?

When I execute the next SQL code:
delete from product where idproduct = 2;
I get the next error:
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`store`.`sale_detail`, CONSTRAINT `fk_sale_detail_product1` FOREIGN KEY (`idproduct`) REFERENCES `product` (`idproduct`) ON DELETE NO ACTION ON UPDATE NO ACTION)
when I try to delete the child of my table dont want, but however I just want to delete product rows without delete brands..
Image of brand table: Brand table
Image of product table: Product table
You can't since that violates Referencial Integrity Constraint rule. You should chose cascading option saying ON DELETE CASCADE.
You need to delete the child row first and then delete the parent row.
You can temporarily disable FK check saying SET FOREIGN_KEY_CHECKS=0; and enable them once done with deletion.
Drop all FK constraint, perform delete and then recreate FK constraint again.
Best option: Go for a soft delete by having a Status column in your table and just update that column to Deleted instead.
You have a foreign key constraint on your store.sale_detail table in the idproduct column. So if you are trying to delete a product you need to be sure that product is not referenced on the sale_detail table.
Another solution is to modify your foreign key so when you delete a row on the product table the referenced row on the sale_detail gets NULL on the corresponding column. Actually you have:
ON DELETE NO ACTION
But you can have:
ON DELETE SET NULL
Also, you can force to delete all references on the sale_detail table:
ON DELETE CASCADE
Hope it helps.

adding records to table without specifing the foreign key

I have two tables in my database :
-etudiant(id(PK),nom,prenom,tel,adresse,filiere(FK))
-filiere(id(PK),libelle)
In the etudiant table I have a foreign key filiere which references the table filiere.
Sometimes I want to add records to etudiant table without specifing the foreign key filiere, but I get this error message when I do that :
Cannot add or update a child row: a foreign key constraint fails
(gecole.etudiant, CONSTRAINT etudiant_ibfk_1 FOREIGN KEY
(filiere) REFERENCES filiere (id) ON DELETE CASCADE ON UPDATE
CASCADE)
You're getting this error because you're trying to add/update a row to etudiant table that does not have a valid value for the etudiant_ibfk_1 field based on the values currently stored in filiere. If you post some more code I can help you diagnose the specific cause.
Update :
Try DISABLE KEYS or
SET FOREIGN_KEY_CHECKS=0;
make sure to
SET FOREIGN_KEY_CHECKS=1;
after.

Cannot add or update a child row(new in mysql workbench)

I am making a project on medical shop.
I have made three tables which are as follows:
customer
medicine
medSold
I want to connect these tables in such a way that the medsold table can fetch the data from both medicine table and customer table.
the EER diagram is as follows:
when I am executing the following query:
insert into medSold(medsold_id,med_id,c_id,generic_name,brand_name,mft_date,exp_date,billing_date,med_name) values ('','','','sumthing1','dntknw1','2012-11-10','2014-12-20','','crocine');
it is giving the following error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (mysql1.medSold, CONSTRAINT c_id FOREIGN KEY (c_id) REFERENCES customer (c_id) ON DELETE NO ACTION ON UPDATE NO ACTION)
why its happening?
You can disable foreign key checks if you forcefully want to insert records.
set foreign_key_checks=0;
insert into medSold(medsold_id,med_id,c_id,generic_name,brand_name,mft_date,exp_date,billing_date,med_name) values ('','','','sumthing1','dntknw1','2012-11-10','2014-12-20','','crocine');
set foreign_key_checks=1;
This is basic error in foriegn key relations.
You must insert records in your parent tables before inserting the child table..
There is no c_id in the child table that you try to enter in foreign key table..
Check that c_id is the same on both tables(type, size, not null and unsigned flags) and make sure that customer table contains the key you're reffering to.
Also, if you want to make foreign key empty, use (NULL) instead of ''
Use
set foreign_key_checks=0;
insert into medSold(medsold_id,med_id,c_id,generic_name,brand_name,mft_date,exp_date,billing_date,med_name) values ('','','','sumthing1','dntknw1','2012-11-10','2014-12-20','','crocine');
set foreign_key_checks=1;
This will disable the foreign key.

ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails

Im trying to make a simple data base but when i try to add data to either the album of song tables i get this error.
ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails (newschema.songs, CONSTRAINT fk_Songs_Albums1 FOREIGN KEY (Albums_AlbumId) REFERENCES Albums (AlbumId) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO newschema.Songs (SongTitle) VALUES ('Song1')
Im not sure if the structure is correct at all! but I am pretty desperate to get this working!
Any help would be really appreciated.
Thanks
You are using this query
INSERT INTO newschema.Songs (SongTitle) VALUES ('Song1')
So you insert a new song without a reference to any album. According to your schema that does not work. Every song must belong to an album.
So it should be at least something like this
INSERT INTO newschema.Songs (SongTitle, Albums_AlbumId)
VALUES ('Song1', 1)
If you want such references being optional you have to allow NULLs in such columns.

MySQL strange FK error

Here are 2 tables.
Full size image
I'm trying to create relationship between them by creating foreign keys courses:parent<->child.parent_cid <=> courses.id and courses:parent<->child.child_cid <=> courses.id
SQL looks like that
ALTER TABLE `courses: parent<->child` ADD CONSTRAINT `cpc.parent_cid_courses.id` FOREIGN KEY (`parent_cid`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `courses: parent<->child` ADD CONSTRAINT `cpc.child_cid_courses.id` FOREIGN KEY (`child_cid`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Getting this error message
What am I doing wrong? Any suggestions?
My first suggestion: rename the child table and the foreign key constraints using only alphanumeric characters and underscores.
The error message implies that there are invalid foreign key values in the child table. You can tell the MySQL server to ignore those values like this before running the ALTER TABLE statements:
set foreign_key_checks = 0;
Or you can fix the data by either adding the missing parent rows or deleting the invalid child rows before adding the constraints.
You are trying to add foreign keys. The error means that child table has data which doesn't exist in parent table.
In your case cpc.parent_cid_courses.id.parent_cid has wrong values, there are no corresponding values in parent field courses.id.