tsql script to add delete cascade to existing tables - sql

is there a script that can be used to enable cascaded deletion for existing tables.
Thanks.

ALTER TABLE [wm].[TABLE_NAME] WITH NOCHECK ADD CONSTRAINT [FK_TABLE_NAME_PARENT_TABLE_NAME] FOREIGN KEY([FOREIGN_KEY])
REFERENCES [wm].[PARENT_TABLE_NAME] ([PRIVATE_KEY])
ON DELETE CASCADE
GO
TABLE_NAME: name of the table where the children are stored.
PARENT_TABLE_NAME: name of the table where the parents are stored.
This placeholders can be equal
FK_TABLE_NAME_PARENT_TABLE_NAME: just name for the constraint
FOREIGN_KEY: field in the child table for the connection with the parents, for example - ParentID
PRIMARY_KEY: field in the parents table, for example - ID
ALTER TABLE [wm].[Thumbs] WITH NOCHECK ADD CONSTRAINT [FK_Thumbs_Documents] FOREIGN KEY([DocID])
REFERENCES [wm].[Documents] ([ID])
ON DELETE CASCADE
GO

Related

SQLite multiple constraints on 1 FK

I have a table in SQLite with specified FK as follows:
CONSTRAINT "model" FOREIGN KEY("category_id") REFERENCES "category"("id") deferrable initially deferred and I need to add ON DELETE CASCADE. I know that in SQLite you can't add constraint with ALTER so how can I combine these 2 constraints when creating a new table?
In the CREATE statement of the new table use:
CONSTRAINT model FOREIGN KEY(category_id) REFERENCES category(id)
ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED
See the demo.

Drop unique index when having Primary Key index

I have the following table
CREATE TABLE steps (
hash_id text UNIQUE PRIMARY KEY,
depth integer
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX steps_hash_id_key ON steps(hash_id text_ops);
CREATE UNIQUE INDEX steps_pkey ON steps(hash_id text_ops);
But I'd like to remove the redundant UNIQUE index because PRIMARY KEY is already unique.
I've also many other tables linked to the hash_id of my steps table with foreign keys.
When I try to remove my UNIQUE index like this:
ALTER TABLE steps DROP CONSTRAINT steps_hash_id_key;
I get
ERROR: cannot drop constraint steps_hash_id_key on table steps because other objects depend on it
DETAIL: constraint steps_routes_step_hash_id_fkey on table steps_routes depends on index steps_hash_id_key
constraint steps_likes_dislikes_step_hash_id_fkey on table steps_likes_dislikes depends on index steps_hash_id_key
HINT: Use DROP ... CASCADE to drop the dependent objects too.
The matter is that I don't want to cascade delete anything, I'd just like to remove this duplicated unique index. Is it possible ?
Thanks to the discussion in comments (#Gordon Linoff) here's a way to fix this issue:
Drop foreign keys
Drop Primary Keys and Unique Keys
Recreate Primary Key only
Recreate Foreign Keys
ALTER TABLE steps_routes DROP CONSTRAINT steps_routes_step_hash_id_fkey;
ALTER TABLE steps_likes_dislikes DROP CONSTRAINT steps_likes_dislikes_step_hash_id_fkey;
ALTER TABLE steps DROP CONSTRAINT steps_pkey;
ALTER TABLE steps DROP CONSTRAINT steps_hash_id_key;
ALTER TABLE steps ADD PRIMARY KEY (hash_id);
ALTER TABLE steps_routes ADD FOREIGN KEY (step_hash_id) REFERENCES steps(hash_id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE steps_likes_dislikes ADD FOREIGN KEY (step_hash_id) REFERENCES steps(hash_id) ON DELETE CASCADE ON UPDATE CASCADE;

DB2 - Can I recreate a foreign key constraint in a table with data?

I'm having the following problem with DB2:
I need to change a column name (column "A"), but the thing is that column A has a PK constraint and a FK constraint. So I need to drop this constraints first, change the column name and then create the constraints again. But I remember a Professor told me once that you can't create a foreign key in a column which already has values. Is that true?
This is my script:
ALTER TABLE TARGET_TABLE
DROP PRIMARY KEY PK_A CASCADE;
ALTER TABLE TARGET_TABLE
RENAME COLUMN A TO B;
alter table TARGET_TABLE
add CONSTRAINT PK_B PRIMARY KEY( B);
alter table TARGET_TABLE add CONSTRAINT FK_B FOREIGN KEY( B) REFERENCES OTHER_TABLE(C);
Thanks in advance.
You can create a foreign key on a column which already has values. The only restriction is that the values must be be valid for the FK you are defining.
If not you will get an error such as
SQL0667N The FOREIGN KEY "I..." cannot be created because the table contains
rows with foreign key values that cannot be found in the parent key of the
parent table. SQLSTATE=23520

Database Diagram not showing table relation when new foreign key reference is added in SSMS 20114

I have three tables as follows -
But when I add the foreign key reference, the relation is ( a straight line in the Database Diagram) not shown.
Following is the reference I wrote.
ALTER TABLE [dbo].EmployeeDesignation
ADD CONSTRAINT FK_EmployeeDesignation_Employee FOREIGN KEY (EmployeeId)
REFERENCES Employee (EmployeeId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE [dbo].[EmployeeDesignation]
ADD CONSTRAINT FK_EmployeeDesignation_Designation FOREIGN KEY (DesignationId)
REFERENCES Designation (DesignationId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
In addition, When I add another two tables (Department and EmployeeDepartment) I can see the relation in the diagram
The code for the reference is as follows --
ALTER TABLE [dbo].EmployeeDepartment
ADD CONSTRAINT FK_EmployeeDepartment_Department FOREIGN KEY (DepartmentId)
REFERENCES Department (DepartmentId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE [dbo].EmployeeDepartment
ADD CONSTRAINT FK_EmployeeDepartment_Employee FOREIGN KEY (EmployeeId)
REFERENCES Employee (EmployeeId)
ON DELETE CASCADE
ON UPDATE CASCADE
;
What might be the reason That the later one showing relation line while the previous one is not showing?
Am I missing something?
Thanks !
Try closing SSMS and then reopening it again. Seems like the cache used by SSMS for some of functionality is not refreshed even upon closing and re-opening the database connection. check https://stackoverflow.com/a/4316415/364084

Removing a primary key column in SQL Server 2008

I have a table named master_employee with a column empid as the primary key, and it has 12 rows in it. This empid has been mapped as a foreign key to another table named dep_child. I want to delete the 12 records in the master_employee table but I was unable to do it.
You were unable to do the deletes from master_employees, as there is a referential integrity with dep_child. You would need to either disable the constraint or delete the records from dep_child, before being able to delete records from master_employees
Add this constraints to your master_employee table
ALTER TABLE "master_employee"
ADD CONSTRAINT "fk_emp11"
FOREIGN KEY ("emp_id")
REFERENCES "dep_child" ("emp_id")
ON DELETE CASCADE;