I´m using linq to sql and I have a lot of tables with foreign keys leading to the UserId.
Is it possible to have some of these foreign tables cleaned upon deletion.
For example I want the users profile (other table) to be deleted automatically with the user but not the users forum posts.
Is this possible or do I have to handle this with code?
I think this link is very usefull.
LINQ to SQL does not support or
recognize cascade-delete operations.
If you want to delete a row in a table
that has constraints against it, you
must complete either of the following
tasks:
Set the ON DELETE CASCADE rule in the foreign-key constraint in the
database.
Use your own code to first delete the child objects that prevent the
parent object from being deleted.
I am not sure with code, but couldn't you set the Cascade on Delete option in SQL?
Related
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?
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.
I have designed an diagram in sql server 2008.
as you can see in the photo:
Now in Value table I set the cascading delete ON for Feature and ProductDetail but I see the error below:
'Feature' table saved successfully 'ProductDetail' table saved
successfully 'Value' table
- Unable to create relationship 'ValueFeature'. Introducing FOREIGN KEY constraint 'ValueFeature' on table 'Value' 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.
What is it wrong with my design?
I think I have faced the same problem some years ago, in SQL server 2005. I suppose that nothing is wrong with your design. If you delete a Feature you want all the Value records to be deleted. Nothing wrong with that. If you don't have other cascades, the automatic delete will not propagate further. However, it seems that SQL Server is not clever enough to understand that, and does not allow you to have such a relationship, just because you have foreign keys that form a circle. I think that if you remove a foreign key (just for testing) and break the circle (for example delete the FK between the ProductDetail and Product) there would be no error.
Check this stackoverflow question too..
Hope I helped!
I've created a simple database relation consisting of the entities entry and keywords.
Any entry can have n keywords, but each keyword may only exist once in the keywords table.
To realize this, I've created three tables: entries, keywords and entries2keywords. I'd like to maintain semantic integrity, so a DELETE or UPDATE procedure should propagate to the related tables.
Another requirement for the database setup is to be re-entrant, meaning that I can just re-run the creation script in which case all data should simply be deleted, as in DROP TABLE.
However, my current script fails with a #1217 error when re-running it:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
I've linked to the database script here because it's too large to paste.
So, than, dropping entries2keywords (table with constraints) will solve the issue :)
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.