On delete restrict mysql is not working - mysql

I want to prevent deleting from parent table when he has children in other tables that.
I make like this
ALTER TABLE constant_det_tb
ADD CONSTRAINT fk_idparent
FOREIGN KEY (idparent)
REFERENCES constant_tb(id) ON DELETE RESTRICT
When I delete from parent constant_tb table, it delete the rows even the table has reference to another tables and it has records reference to it.

Make sure you have InnoDB as storage engine for all affected tables.
Check this (if not already) : http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
ON DELETE RESTRICT
reference option is all you need to achieve this.

Checking of foreign keys could be disabled for current session.
It can be enabled with
SET FOREIGN_KEY_CHECKS = 1;

Related

Script to Delete Data from Mysql Tables with Foreign Key Constraints

I have list of tables (Mysql) where some of them have reference.
I want to to generate delete script for all the tables based on time column.
NOTE: we can not user delete Cascade I can not disable the keys as it is production system.
You can temporarily disable check reference
SET foreign_key_checks = 0;

Delete a MySQL record involved in a foreign key without deleting children

i am trying to delete a row of information from a parent table without deleting anything from the child table. the foreign key constraint is set to delete no action. because i want to delete the information while leaving the corresponding information in the child table intact. i ant to be able to do this without changing it to delete cascade.......can anybody help me........p.s my database is literally a table consisting of parents and the child table is also literally children....HELP. i have tried doing this but i keep getting the error message. Foreign key constraint prevents update or delete.
You can define the child table Id to be NULLable and define the foreign key in ON DELETE SET NULL mode.
It means that when the parent record is deleted, children records or updated to replace the deleted Id by NULL.
For more information, view this page : https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

How can I add a "ON DELETE CASCADE"

I already have my forein keys and so on. I only need to add the ON DELETE CASCADE Option. How can I do that? Something with Alter Table?
I'm using mysql with phpmyadmin
I don't know which version of phpMyAdmin you're using, but on the 4.5.2 version, here's how it works if you want to do it without manually writing the SQL query :
Select your table
Go to Structure tab
Go to Relation view "sub-tab"
Define your constraints
Save your changes
Note that this is not possible with the MyISAM storage engine (seems to be the default one on my installation) and you have to select the InnoDB one :
Select your table
Go to Operations tab
Change Storage Engine to InnoDB
Save
For the older phpMyAdmin versions, I know that it was slightly different, as the Relation view was accessible by a link below the table structure.
The simplest way would be to delete the current constraint and add new one with ON DELETE
To delete: ALTER TABLE mytable DROP FOREIGN KEY 'fk_anothertable'
Then create it again:
ALTER TABLE mytable
ADD CONSTRAINT 'fk_anothertable'
FOREIGN KEY ('aColumn' )
REFERENCES 'anotherTable' ('aColumn' )
ON DELETE CASCADE
Unfortunately, you can only create and drop constraints using alter table, you cannot change an existing one. There is no separate alter constraint command either. Therefore all you can do is drop the foreign key and recreate it adding the on delete cascade clause.

Can I not enforce referential integrity in MySQL

I have a SQL table called "user" and a table "login" that has a foreign key constraint to a user. I want to be able to delete a row in the user table, even if there are login rows that reference it. Right now the database stops me from doing this.
Does anyone know how I can alter the table (through SQL or preferably through PHPmyAdmin to allow me to do this?
The tables were created automatically through Django.
Edit: To clarify: I don't want to cascade the delete. That is, I want the rows in the Login table to remain even though the user they reference is gone.
If you want this kind of behavior you have to create the foreign key with an ON DELETE CASCADE clause. With an ON DELETE CASCADE foreign key all rows referencing the user will be deleted with the user.
See: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
Edit: If you want to keep the user_id in your login tables you just have to drop the foreign key. Anyway, If you are asking this is because you should probably do a logical delete instead of a physical delete: Physical vs. logical / soft delete of database record?
Proper way to do this is to mark offending users as "inactive" so they can't login and you still maintain referential integrity of your database.
Deleting data from master table that has referential integrity links to some data in slave table is bad praxis.

Adding constraints in phpMyAdmin

I feel like I'm being stupid, but I can't find anywhere on the phpMyAdmin interface to add constraints to foreign keys e.g. CASCADE ON DELETE
I've looked for similar questions on here and on the phpMyAdmin wiki but I can't find anything about it.
I realise I could do this via the query interface, but I'd like to know how to do it through the graphical interface.
First, you should have your storage engine as InnoDB. Then select a table and go to 'Structure' tab.
Under the table you will see 'Relation view', click it. From there you could add constraints.
CASCADE
Whenever rows in the master (referenced) table are deleted (resp. updated), the respective rows of the child (referencing) table with a matching foreign key column will get deleted (resp. updated) as well. This is called a cascade delete (resp. update[2]).
RESTRICT
A value cannot be updated or deleted when a row exists in a foreign key table that references the value in the referenced table. Similarly, a row cannot be deleted as long as there is a reference to it from a foreign key table.
NO ACTION
NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the referential integrity check fails: the UPDATE or DELETE statement will result in an error.
SET NULL
The foreign key values in the referencing row are set to NULL when the referenced row is updated or deleted. This is only possible if the respective columns in the referencing table are nullable. Due to the semantics of NULL, a referencing row with NULLs in the foreign key columns does not require a referenced row.
Firstly, you should choose storage engine as InnoDB.
Follow this way: click database_name -> More -> Designer