How to add 'ON DELETE CASCADE' in ALTER TABLE statement
You can not add ON DELETE CASCADE
to an already existing constraint. You will have to drop
and re-create
the constraint. The documentation shows that the MODIFY CONSTRAINT
clause can only modify the state of a constraint (i-e: ENABLED/DISABLED
...).
How to add on delete cascade constraints?
I'm pretty sure you can't simply add on delete cascade
to an existing foreign key constraint. You have to drop the constraint first, then add the correct version. In standard SQL, I believe the easiest way to do this is to
- start a transaction,
- drop the foreign key,
- add a foreign key with
on delete cascade
, and finally - commit the transaction
Repeat for each foreign key you want to change.
But PostgreSQL has a non-standard extension that lets you use multiple constraint clauses in a single SQL statement. For example
alter table public.scores
drop constraint scores_gid_fkey,
add constraint scores_gid_fkey
foreign key (gid)
references games(gid)
on delete cascade;
If you don't know the name of the foreign key constraint you want to drop, you can either look it up in pgAdminIII (just click the table name and look at the DDL, or expand the hierarchy until you see "Constraints"), or you can query the information schema.
select *
from information_schema.key_column_usage
where position_in_unique_constraint is not null
ALTER TABLE to add ON DELETE CASCADE statement
I found the answer:
//in Fruit object
@OneToMany(mappedBy = "fruit", orphanRemoval=true)
private List<Grape> grapes;
//in Grape object
@OneToOne
private Fruit fruit;
How to add on delete cascade option in mysql?
You have a typo in the Order
table. You have PRIMAY
where it should be PRIMARY
.
After correcting this, I tried creating the tables and all statements worked fine, including the last one.
Trying to 'Delete Cascade' from an already created table with 'alter'
Your best option is to drop the account column from task
. When you need to task
with account information just get it through project
. This also makes maintenance must easier. With account in both task
and project
how do you ensure both accounts are the same.
Since Postgres does not have alter constraint...
you need to drop and recreate the FK in project
and task
. Following are the alter statements you need.
alter table task drop column account;
alter table task drop constraint task_project_fkey; -- verify constraint name
alter table task add constraint task_project_fkey
foreign key (project)
references project(id)
on delete cascade;
alter table project drop constraint project_account_fkey; -- verify constraint name
alter table project add constraint project_account_fkey
foreign key (account)
references account(id)
on delete cascade;
As you took the lazy way of defining FK, you will have to verify the constraint names.
NOTE: It is poor practice to name a column the same a the table it is in. So perhaps project.name (or project.description) instead of project.project. Especially since any FK referring to project table do not refer to the
project column in that table. Of course the same applies task
and any other table in your system.
Add ON DELETE CASCADE to existing column in Laravel
Drop foreign key first. Thanks to Razor for this tip
$table->dropForeign('answers_user_id_foreign');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
Related Topics
Differencebetween Views and Materialized Views in Oracle
Can Select * Usage Ever Be Justified
How to Read the Contents of an .SQL File into an R Script to Run a Query
Any Way to Achieve Fulltext-Like Search on Innodb
Errors in SQL Server While Importing CSV File Despite Varchar(Max) Being Used for Each Column
Joining Multiple Tables in SQL
SQL Server Identity Column Values Start at 0 Instead of 1
Transposing an SQL Result So That One Column Goes Onto Multiple Columns
Script to Kill All Connections to a Database (More Than Restricted_User Rollback)
Sqlplus Statement from Command Line
How to Flush Output from Pl/SQL in Oracle
Rodbc Temporary Table Issue When Connecting to Ms SQL Server
Why Using a Udf in a SQL Query Leads to Cartesian Product
How to Show Row Numbers in Postgresql Query