Is there a way to disable foreign key checks? -Medoo - medoo

I am trying to remove some rows in my tables that are constrained by foreign keys. Is there a way to disable foreign key checks to allow the deletion of these items? Possibly through Medoo::raw() ?

Foreign keys can be disabled by putting $database->query('SET FOREIGN_KEY_CHECKS = 0'); before your normal Medoo statement. Don't forget to run $database->query('SET FOREIGN_KEY_CHECKS = 1'); after you are finished with the need to disable.

Related

How can I change the constraints of FOREIGN KEY in code first mode?

my error :
Introducing FOREIGN KEY constraint 'FK_Table_ArticleViolations_Table_Users_vUser_ID' on table 'Table_ArticleViolations' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
I need to use an PK in a few other tables as FK
How can I change the constraints of FOREIGN KEY in code first mode ?
Thank you

Is it possible to have both foreign key constraint and a trigger that keep the referential integrity?

Is it possible to have both foreign key constraint and a trigger that keep the referential integrity?
I don't think that is possible and we have to remove the constraint and keep the referential integrity using trigger. To extend my description, suppose you have two tables one of them has a primary key and the other has a foreign key referring to the first table, primary key column. So now is it possible to add the following code
create or replace TRIGGER TABLE1_TABLE2
after update of TABLE1_PRIMARY_KEY on TABLE1
Of course it came to my mind to lose the constraint and continue with the trigger but is it the only way?
By the way this code is just for practice, I know it isn't a good idea to update the primary key of a table.
I found a useful article on one of Microsoft websites confirming my idea that the constraints should be removed. But of course the solution for Oracle may be different from any other database. The article is about Implementing Referential Integrity and Cascading Actions and the part that is related is "Implementing ON DELETE CASCADE and ON UPDATE CASCADE with triggers" and after that comes "Before continuing, you need to drop the existing foreign keys".
There is no problem doing that in Oracle. You could have both the constraint and the trigger at the same time.

Is there any way to generate a cascade delete statement?

Let's say I have a row in Table A I wish to delete but there a multiple foreign key constraints in other tables. Is there a way to automatically generate the delete command?
I think a tool that would recursively look at the foreign keys and in turn generate a delete statement to take care of all foreign keys should exist, but I can't find one.
I'm on MSSql server2008
When setting up your Foreign Key relationships there is an ON DELETE CASCADE you can add.
MSDN Cascading Referential Integrity Constraints
ON DELETE CASCADE
Specifies that if an attempt is made to delete a row with a key referenced by foreign
keys in existing rows in other tables, all rows containing those foreign keys are also
deleted. If cascading referential actions have also been defined on the target tables,
the specified cascading actions are also taken for the rows deleted from those tables.
SO even has a solution where you are not adding it to the table:
In SQL Server 2005, can I do a cascade delete without setting the property on my tables?

Is it possible to alter the cascading behaviour of existing mysql foreign keys?

I have two tables in mysql, both are type InnoDB and there is an existing foreign key from the first to the second.
However, the foreign key was created without any cascading behaviour. The table is large, and any changes to indexes or new keys locks the table for ~20 minutes.
Is it possible to add "ON DELETE CASCADE" behaviour to an existing foreign key without dropping the key and recreating it?
Unfortunately, no. On Delete and On Update are systemic attributes of the foreign key itself and can only be specified when creating the constraint.

Changing a record in a table (sql server) that has foreign keys?

Does anyone know if there is a quicker way of editing a record that has foreign keys in a table (in sql server).. i will explain.. i have approx 5 tables that have there own ID but are linked together using a foreign key...
Hence i needed to change the foreign key (the contract number in my case), but i had to copy each record to a new record and edit it that way...
As if i try to edit the contract number it gives me the standard error of being associated and violates a foreign key etc
Surly there must be a better way?
ANy ideas?
are you talking about changing the PK and then updating all the Fks? In that case enable cascade updates and this will be done automagically
same with deletes, you enable cascade deletes
ON DELETE CASCADE
Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted. If cascading referential actions have also been defined on the target tables, the specified cascading actions are also taken for the rows deleted from those tables.
ON UPDATE CASCADE
Specifies that if an attempt is made to update a key value in a row, where the key value is referenced by foreign keys in existing rows in other tables, all of the foreign key values are also updated to the new value specified for the key. If cascading referential actions
I'm not an SQL expert, but can't you set something like ON UPDATE CASCADE to automatically update the foreign key when the primary key is changed?
Or try disabling the integrity constraint, do your changes and attempt to re-enable the constraint. Basically, if you didn't do it right you will get an error then (can't enable a constraint that would be violated).