How to put Cascade on Delete once the Fk constraint has already been defined [duplicate] - sql

I have a table representing users. When a user is deleted I get:
DELETE statement conflicted with the REFERENCE constraint
Apparently, CASCADE DELETE is not as easy as I imagined in SQL Server, and the option needs to be added to the table.
The problem is: I cannot figure out how to add the CASCADE DELETE option.
I'm using: SQL Server 2008. Any ideas how to do this?

Read this Microsoft article first. Read Me. I use the GUI during design so here is a picture of how it is selected in SSMS.
The syntax added to the foreign key is " ON DELETE CASCADE "

Here's the way I would add the "cascading delete" feature to an existing foreign key in SQL Server Management Studio.
First, find your foreign key, and open it's "DROP and CREATE To" in a new Query window.
Then, just add "ON DELETE CASCADE" to the "ADD CONSTRAINT" command:
Then just hit hit the "Execute" button to run the query.
Job done !

Google ALTER TABLE DROP CONSTRAINT, then ALTER TABLE ADD CONSTRAINT:
ALTER TABLE
Here's a quick example:
CREATE TABLE A
(
ID INTEGER NOT NULL UNIQUE
);
CREATE TABLE B
(
ID INTEGER NOT NULL UNIQUE
CONSTRAINT fk__B__A
REFERENCES A (ID)
);
-- Oops! Forgot the CASCADE referential actions.
-- DROP the constraint then recreate it:
ALTER TABLE B DROP
CONSTRAINT fk__B__A;
ALTER TABLE B ADD
CONSTRAINT fk__B__A
FOREIGN KEY (ID)
REFERENCES A (ID)
ON DELETE CASCADE
ON UPDATE CASCADE;

Related

Weird mysql behavior when adding foreign key

I am trying to add a simple foreign key like so
ALTER TABLE `4over4local`.`wf_job_status`
ADD CONSTRAINT `fk_sales_job_status_sales_job1`
FOREIGN KEY (`job_id`)
REFERENCES `4over4local`.`sales_job` (`job_id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
But i am getting a very weird error with some database table named #sql-d74_9 that doesn't even exist.
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`4over4local`.`#sql-d74_9`,
CONSTRAINT `fk_sales_job_status_sales_job1` FOREIGN KEY (`job_id`) REFERENCES `sales_job` (`job_id`)
ON DELETE CASCADE ON UPDATE CASCADE)
I have previously made very similar queries and they all worked fine except this one. What could cause this and what i am doing wrong? I am using MariaDB 13
The error indicates that you have rows in the table that do not satisfy the foreign key constraint you are trying to add. You need to fix your data before you can create the constraint.
You can exhibit offending rows with the following query:
select wjs.*
from wf_job_status wjs
where not exists (select 1 from sales_job sj where sj.job_id = wjs.job_id)

Firebird: Alter "anonymous" foreign key

I need to alter an existing foreign key from "on delete restrict" to "on delete cascade". Unfortunaltey this bug sneaked through Q/A.
In my database I have several forign key relationships that were automatically named (INTEG_1, INTEG_2, ...). The name of the constraint I have to fix is another in a new installation than in an Update from Version 2 and even another than when this Version 2 previously has been updated from Version 1.
As the referencing table only has one foreign key, this statement gives me the name of the constraint:
SELECT RDB$CONSTRAINT_NAME
FROM RDB$RELATION_CONSTRAINTS
where RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
and RDB$RELATION_NAME = 'MY_TABLE_NAME'
then I can drop and afterwards recreate the foreign key (with a "real" name this time)
alter table MY_TABLE_NAME
drop constraint <result from above>;
alter table MY_TABLE_NAME
add constraint fk_my_table_name_purpose foreign key (other_id)
references other_table(id) on delete cascade;
However, I try to avoid working directly with system tables and I'd like to know whether there is a better / more elegant way to alter my foreign key.
There's no better way, the system tables are the only way to figure out the constraint name.

SQL How to change value With all References

Is there an easier way to change a single value in a row together with its Reference , and that Reference's reference, Instead of backtracking through 20 tables to find the beginning of it all?
Links to Answers
--On Cascade (Thanks Javier)
ALTER TABLE catalog DROP CONSTRAINT aa
ALTER TABLE catalog ADD CONSTRAINT
(FOREIGN KEY (stock_num, manu_code) REFERENCES stock
ON DELETE CASCADE CONSTRAINT ab)
Have you tried to define the reference as "ON UPDATE CASCADE"?

SQL query error - can't see why?

I'm currently reading through this Yii application eBook: http://www.packtpub.com/agile-web-application-development-yii11-and-php5/book - and I'm having a problem inputting the tutorials DDL / SQL statements into PHPMyAdmin without it throwing up errors.
Would someone be kind enough to shed some light on why the following syntax is invalid? It might be something simple but I can't see it:
SQL statement:
ALTER TABLE tbl_issue
ADD CONSTRAINT FK_issue_project FOREIGN KEY (`project_id`)
REFERENCES tbl_project(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_issue
ADD CONSTRAINT `FK_issue_owner` FOREIGN KEY (`owner_id`)
REFERENCES tbl_user(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_issue
ADD CONSTRAINT `FK_issue_requester` FOREIGN KEY (`requester_id`)
REFERENCES tbl_user(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_project_user_assignment
ADD CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`)
REFERENCES tbl_project(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE tbl_project_user_assignment
ADD CONSTRAINT `FK_user_project` FOREIGN KEY (`user_id`)
REFERENCES tbl_user(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
INSERT INTO tbl_user (`email`, `username`, `password`)
VALUES
(`test1#notanaddress.com`,`Test_User_One`, MD5(`test1`)),
(`test2#notanaddress.com`,`Test_User_Two`, MD5(`test2`));
Error Message
Error
SQL query:
ALTER TABLE tbl_issue
ADD CONSTRAINT FK_issue_project FOREIGN KEY ( `project_id` )
REFERENCES tbl_project( `id` ) ON DELETE CASCADE ON UPDATE RESTRICT ;
MySQL said:
#1005 - Can't create table 'trackstar_test.#sql-c78_127' (errno: 121) (<a
href="server_engines.php?engine=InnoDB&page=Status&
token=252c0553975923580ca430b6e98c4243">Details...</a>)
Note:
All the tables in the database are set to innodb as their storage engine.
I've tried using different foreign key names for each FK, still get the same error.
Update:
After finding no solution to the problem, I deleted by DB, uninstalled Xampp and then redid everything again. Seems to work now. Sorry to not be able to tell future readers exactly what the cause was, but it was most probably to do with my Database config or the information I added to it.
Actual Problem is :
AS you are Following the book, there are a few insert/ update statements are executed on
tbl_proect,
tbl_issue
than you are trying to add Foreign Key Constraint. that checks the table data before applying. So, Here is the actual mistake, may be your tables contain a few records that violate the foreign key constraints. hence phpmyadmin doesnot allow you to alter table and generates error message.
Solution :
TRUNCATE TABLE `tbl_project`
TRUNCATE TABLE `tbl_issue`
Do Only one thing, clear all the tables . Empty tables. . And here's your problm resolved now phpmyadmin allows you to run commands.

How do I edit a table in order to enable CASCADE DELETE?

I have a table representing users. When a user is deleted I get:
DELETE statement conflicted with the REFERENCE constraint
Apparently, CASCADE DELETE is not as easy as I imagined in SQL Server, and the option needs to be added to the table.
The problem is: I cannot figure out how to add the CASCADE DELETE option.
I'm using: SQL Server 2008. Any ideas how to do this?
Read this Microsoft article first. Read Me. I use the GUI during design so here is a picture of how it is selected in SSMS.
The syntax added to the foreign key is " ON DELETE CASCADE "
Here's the way I would add the "cascading delete" feature to an existing foreign key in SQL Server Management Studio.
First, find your foreign key, and open it's "DROP and CREATE To" in a new Query window.
Then, just add "ON DELETE CASCADE" to the "ADD CONSTRAINT" command:
Then just hit hit the "Execute" button to run the query.
Job done !
Google ALTER TABLE DROP CONSTRAINT, then ALTER TABLE ADD CONSTRAINT:
ALTER TABLE
Here's a quick example:
CREATE TABLE A
(
ID INTEGER NOT NULL UNIQUE
);
CREATE TABLE B
(
ID INTEGER NOT NULL UNIQUE
CONSTRAINT fk__B__A
REFERENCES A (ID)
);
-- Oops! Forgot the CASCADE referential actions.
-- DROP the constraint then recreate it:
ALTER TABLE B DROP
CONSTRAINT fk__B__A;
ALTER TABLE B ADD
CONSTRAINT fk__B__A
FOREIGN KEY (ID)
REFERENCES A (ID)
ON DELETE CASCADE
ON UPDATE CASCADE;