What's the different between RESTRICT and NO ACTION? - mysql

I'm trying to make a FK on a column, and now I'm thinking when exactly should I use ON DELETE RESTRICT? (or ON UPDATE RESTRICT). Isn't it the same as NO ACTION?
Well ON DELETE RESTRICT means you can't delete a given parent row if a child row exists that references the value for that parent row. If the parent row has no referencing child rows, then you can delete that parent row. Well its definition is the default behavior for a foreign key anyway.
Am I missing something?

They're equivalent. It even says so right there in the documentation:
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT.
There's a difference between them in databases that have deferred checks, but MySQL doesn't.

The only difference is coming out when you define a constraint as deferrable with an initially deferred or initially immediate mode.
NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary key value is not allowed to proceed if there is a related foreign key value in the referenced table. InnoDB rejects the delete or update operation for the parent table.
RESTRICT: Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION) is the same as omitting the ON DELETE or ON UPDATE clause. (Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT.)
See:
SQL SET CONTRAINS

Related

Is it possible in MySQL to apply the cascading deletion of a late added foreign key to remove lose records in a child table?

I have a big MySQL database that used no foreign keys. I have to sanitize the DB now (as far as possible). So I will add FOREIGN KEY's with ON DELETE CASCADE on some places.
This works for newly created rows. But since the database is old there exists a lot of "lost" records already ("lost" meaning child table has rows which reference non-existing/deleted parent table rows and thus can never be found because the join/where constraint happens on the reference field between child and parent). I could delete them with a JOIN between parent and child tables, or with a WHERE NOT EXISTS, but if it's somehow possible I'd like to have the DBMS do it by itself, since it now has the foreign keys anyways and doing it manually would be much more error prone and slow.
Is it possible to let a late-added cascade delete enabled foreign key delete already existing lost records in MySQL?

mySQL Restrict Cascade No Action settings

What is the best constraint to use on a forum table where users leave comments?
Assuming some users will be deleted at a later stage. if i delete a user who has commented, what happens to the users entry in the table?
Hope someone can explain.
There are two parts to this question:
how best to implement this? You can "soft-delete" user rows. This has the advantages of:
not losing user information
allowing users to be un-deleted
maintaining referential integrity without losing data linked to users
Soft-deleting can be implemented by adding another column to the users table, with a dateDeleted column -- if it's Null, then the user isn't deleted. I believe SO uses such a mechanism for deleting posts.
what does restrict cascade no action do? The MySQL docs say
RESTRICT: Rejects the delete or update operation for the parent table.
Specifying RESTRICT (or NO ACTION) is the same as omitting the ON
DELETE or ON UPDATE clause.
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to
RESTRICT. InnoDB rejects the delete or update operation for the parent
table if there is a related foreign key value in the referenced table.
Some database systems have deferred checks, and NO ACTION is a
deferred check. In MySQL, foreign key constraints are checked
immediately, so NO ACTION is the same as RESTRICT.
In other words, if you use this, you won't be able delete rows if doing so would break referential integrity.

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

MySQL RESTRICT and NO ACTION

What's the difference in a MySQL FK between RESTRICT and NO ACTION? From the doc they seem exactly the same. Is this the case? If so, why have both?
From MySQL Documentation: https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html
Some database systems have deferred
checks, and NO ACTION is a deferred
check. In MySQL, foreign key
constraints are checked immediately,
so NO ACTION is the same as RESTRICT.
It is to comply with standard SQL syntax. Like the manual says: (emphasis mine)
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT. The MySQL Server rejects the delete or update operation for the parent table if there is a related foreign key value in the referenced table. Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT.
They are identical in MySQL.
In the SQL 2003 standard there are 5 different referential actions:
CASCADE
RESTRICT
NO ACTION
SET NULL
SET DEFAULT
The difference between NO ACTION and RESTRICT is that according to the standard, NO ACTION is deferred while RESTRICT acts immediately.

How to delete a row from Parent table without delete a row from Child table

I want to delete a row from Parent table without deleting or modifying row in the child table. For this what type referential integrity constraints of mysql i have to use.
Could you explain the reason why you would need such a scenario?
I would consider flagging the parent row as deleted as opposed to "deleting" them permanently.
Updated: sorry, I was wrong...
Don't set referential integrity at all.
Cascade will delete children when deleting parent. Restrict will throw mySQL error. Set Null will set foreign key value in child table/tables to NULL. No Action according to this will be equivalent to Restrict.