SQL Server - Insert and update specification (Foreign key relationship) - sql

I just want to know if there is any way to write some function or stored procedure in sql server to change the delete rule programmatically?
Example: I have to change the delete rule from Cascade to none or vice versa but I don't want to do graphically.
Because I have to do quite often and want to revert it back to its initial stage.

You can't alter a foreign key constraint to change the cascade option, you will have to drop it and create it again with the option you want. Make sure to set it on a transaction as you don't want inconsistent data getting stuck in the middle of the change.
The alter commands of an example:
ALTER TABLE [SchemaName].[TableName] DROP CONSTRAINT [ConstraintName]
ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD CONSTRAINT [ConstraintName]
FOREIGN KEY([ColumnName]) REFERENCES [SchemaName].[YetAnotherTableName]([ColumnName]) ON DELETE CASCADE;

Let's assume you have a foreign key already called fk_table1_table2 with cascade none.
You would have to drop the constraint and then recreate a constraint with cascade:
ALTER TABLE [dbo].[TableName] DROP CONSTRAINT [FK_Table1_Table2]
GO
ALTER TABLE [dbo].[TableName] WITH NOCHECK ADD CONSTRAINT [FK_Table1_Table2] FOREIGN KEY([Table2])
REFERENCES [dbo].[Table2] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[TableName] CHECK CONSTRAINT [FK_Table1_Table2]
GO
This is generated from ssms. You can do this like here:
You can right click on any Key and generate the code for you ;)

Related

SQL Server cascade on delete

I'm using SQL Server 2016 and trying to implement cascade on delete. It does work, but not if I put the code in a single script. The code for dropping the constraints on --subproject person and --activity then adding the constraint again have to be in separate files.
So this is in one file
ALTER TABLE dbo.SubProjectPerson
DROP CONSTRAINT SubProjectPerson_SubProject
and this is in another file
ALTER TABLE dbo.SubProjectPerson
ADD CONSTRAINT SubProjectPerson_SubProject
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;
That is the only way it works
Here is the script that I have
BEGIN TRY
BEGIN TRANSACTION
--subproject person
ALTER TABLE dbo.SubProjectPersonRole
DROP CONSTRAINT SubProjectPersonRole_SubProjectPerson
ALTER TABLE dbo.SubProjectPersonRole
ADD CONSTRAINT SubProjectPersonRole_SubProjectPerson
FOREIGN KEY (SubProjectPersonID)
REFERENCES dbo.SubProjectPerson(SubProjectPersonID)
ON DELETE CASCADE;
ALTER TABLE dbo.SubProjectPerson
DROP CONSTRAINT SubProjectPerson_SubProject
ALTER TABLE dbo.SubProjectPerson
ADD CONSTRAINT SubProjectPerson_SubProject
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;
--activity
ALTER TABLE dbo.Activity
DROP CONSTRAINT Activity_SubProject
ALTER TABLE dbo.Activity
ADD CONSTRAINT Activity_SubProject
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;
--subproject
ALTER TABLE dbo.SubProjectDocument
DROP CONSTRAINT SubProjectDocument_SubProject
ALTER TABLE dbo.SubProjectDocument
ADD CONSTRAINT SubProjectDocument_SubProject
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;
ALTER TABLE dbo.SubProjectNote
DROP CONSTRAINT SubProjectNote_SubProject
ALTER TABLE dbo.SubProjectNote
ADD CONSTRAINT SubProjectNote_SubProject
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;
--communication thread
ALTER TABLE dbo.CommunicationThread
DROP CONSTRAINT CommunicationThread_SubProjectID
ALTER TABLE dbo.CommunicationThread
ADD CONSTRAINT CommunicationThread_SubProjectID
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH
This long script is running fine with no errors but no changes are being picked up for those two tables unless I run them in separate files. What's wrong here?
Try this. Use GO for every statement.
ALTER TABLE dbo.SubProjectDocument
DROP CONSTRAINT SubProjectDocument_SubProject
GO
ALTER TABLE dbo.SubProjectDocument
ADD CONSTRAINT SubProjectDocument_SubProject
FOREIGN KEY (SubProjectID)
REFERENCES dbo.SubProject(SubProjectID)
ON DELETE CASCADE;

Dropping and adding a constraint in sql

I want to drop and add a constraint
ALTER TABLE [dbo].[Entity] DROP CONSTRAINT [x_UpdateDate1]
ALTER TABLE [dbo].[Entity] ADD CONSTRAINT [x_UpdateDate1] DEFAULT ([dbo].[GETSYSTEMDATE]()) FOR [CreateDate]
I want to find the alternative for this. As dropping and adding a constraint is not advisable. Is there any better way to handle
No. You cannot alter a constraint, you need to drop it and then recreate it just as you did.
No, there's no other way than to drop and re-create constraints (check also sqlauthority.com blog entry)
You can disable constraints with:
ALTER TABLE tbl NOCHECK CONSTRAINT all
and then turn them back on with:
ALTER TABLE tbl WITH CHECK CHECK CONSTRAINT all
of course this SQL turns off ALL constraints on the table but you can specify an individual constraint like this:
ALTER TABLE tbl NOCHECK CONSTRAINT Constraint1

Is there a way of forcing sql INSERT or disabling foreign key check?

I just found out a SQL Server bug (described here). Now I am wondering, if there is any possibility of forcing an INSERT statement without performing foreign key checks? Like disabling this checks server-wide for few minutes, or somehow disabling (not deleting) FK's on specific tables?
ALTER TABLE [schema].[table] NOCHECK CONSTRAINT [constraint_name]
Then
ALTER TABLE [schema].[table] CHECK CONSTRAINT [constraint_name]
However, this results in your FK being not trusted, and when you re-enable the constraint it will take time to re-validate it.
Drop Constraint
ALTER TABLE Table_Name
DROP CONSTRAINT fk_ConstraintName
Drop the constraint once you have done the operation then create it again.
Disable Constraint
ALTER TABLE MyTable
NOCHECK CONSTRAINT fk_Constraint_Name
Enable Constraint
ALTER TABLE MyTable
CHECK CONSTRAINT fk_Constraint_Name
Check Disable Foreign Key Constraints with INSERT and UPDATE Statements:
To disable a foreign key constraint for INSERT and UPDATE statements
In Object Explorer, expand the table with the constraint and then
expand the Keys folder.
Right-click the constraint and select Modify.
In the grid under Table Designer, click Enforce Foreign Key
Constraint and select No from the drop-down menu.
Click Close.
or simple use this command:
ALTER TABLE tablename
NOCHECK CONSTRAINT fk_ConstraintName

How to drop more than one constraint at once (Oracle, SQL)

I'm changing constraints in my database and I need to drop some of them. I know that for a single constraint, the command is following:
ALTER TABLE tblApplication DROP CONSTRAINT constraint1_name;
However, when I try
ALTER TABLE tblApplication DROP (
CONSTRAINT constraint1_name,
CONSTRAINT constraint2_name,
CONSTRAINT constraint3_name
);
it doesn't work and I need to do:
ALTER TABLE tblApplication DROP CONSTRAINT constraint1_name;
ALTER TABLE tblApplication DROP CONSTRAINT constraint2_name;
ALTER TABLE tblApplication DROP CONSTRAINT constraint3_name;
Is there a way to remove more than one constraint in a single command? I'd like to avoid repeating ALTER TABLE tblApplication, just like with the ADD command:
ALTER TABLE tblApplication ADD (
CONSTRAINT contraint1_name FOREIGN KEY ... ENABLE,
CONSTRAINT contraint2_name FOREIGN KEY ... ENABLE,
CONSTRAINT contraint3_name FOREIGN KEY ... ENABLE
);
Yes you can. You just need to repeat 'drop constraint' per constraint. e.g.
alter table t1
drop constraint fk1
drop constraint fk2
/
Edit: I tested this against Oracle 11, and it worked fine. Don't know about older versions.
There is an alternative form to drop constraints related to a column in a table, also dropping the column with CASCADE:
ALTER TABLE table1 DROP (columnName) CASCADE CONSTRAINTS;
It is tested on Oracle 11g
example: we can drop constraints in MySQL by creating constraints to the variables like this way.
create table sample(id int, name varchar(30), marks int, constraint uid unique(id), constraint un unique(name));
alter table sample drop constraint uid, drop constraint un;
Yes, we can drop multiple at once:
ALTER TABLE TABLE NAME
DROP CONSTRAINTS CONSTRAINT VALUE
DROP CONSTRAINTS CONSTRAINT VALUE;

Disable Foreign key constraint on a table?

Can i temporarily disable a foreign key constraint. How do i do this?
To temporarily disable a constraint (foreign keys are constraints):
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint
To re-enable a constraint
ALTER TABLE MyTable CHECK CONSTRAINT MyConstraint
Incidentally, this is why you need "Alter table" permissions when you BCP or Bulk Insert data into a table. Using the default configuration, check constraints and foreign keys are not checked.