Dropping and adding a constraint in sql - 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

Related

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

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 ;)

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 can I alter a primary key constraint using SQL syntax?

I have a table that is missing a column in its primary key constraint. Instead of editing it through SQL Server, I want to put this in a script to add it as part of our update scripts.
What syntax can I use to do this? Must I drop and recreate the key constraint?
Yes. The only way would be to drop the constraint with an Alter table then recreate it.
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)
PRIMARY KEY CONSTRAINT cannot be altered, you may only drop it and create again. For big datasets it can cause a long run time and thus - table inavailability.
Performance wise there is no point to keep non clustered indexes during this as they will get re-updated on drop and create.
If it is a big data set you should consider renaming the table (if possible , any security settings on it?), re-creating an empty table with the correct keys migrate all data there.
You have to make sure you have enough space for this.
In my case, I want to add a column to a Primary key (column4).
I used this script to add column4
ALTER TABLE TableA
DROP CONSTRAINT [PK_TableA]
ALTER TABLE TableA
ADD CONSTRAINT [PK_TableA] PRIMARY KEY (
[column1] ASC,
[column2] ASC,
[column3] ASC,
[column4] ASC
)
PRIMARY KEY CONSTRAINT can only be drop and then create again.
For example in MySQL:
ALTER TABLE table_name DROP PRIMARY KEY;
ALTER TABLE table_name ADD PRIMARY KEY (Column1,Column2);
you can rename constraint objects using sp_rename (as described in this answer)
for example:
EXEC sp_rename N'schema.MyIOldConstraint', N'MyNewConstraint'

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;

Dropping then adding a constraint fails in oracle

I'm trying to move a primary key constraint to a different column in oracle. I tried this:
ALTER TABLE MY_TABLE
DROP CONSTRAINT c_name;
ALTER TABLE MY_TABLE
ADD CONSTRAINT c_name PRIMARY KEY
(
"COLUMN_NAME"
) ENABLE;
This fails on the add constraint with an error saying the the constraint already exists even though i just dropped it. Any ideas why this is happening
If the original constraint was a primary key constraint, Oracle creates an index to enforce the constraint. This index has the same name as the constraint (C_NAME in your example). You need to drop the index separately from the constraint. So you will need to do a :
ALTER TABLE <table1> DROP CONSTRAINT C_NAME;
DROP INDEX C_NAME;
ALTER TABLE <table1> ADD CONSTRAINT C_NAME PRIMARY KEY
( COLUMN_2 ) ENABLE;
The safest way is to first add a unique index. Try this:
create unique index new_pk on <tabname> (column_2);
Then drop the old PK:
alter table <tabname> drop primary key drop index;
(Optional) Rename the index:
alter index new_pk rename to c_name;
And then add the PK again indicating the index to be used:
alter table <tabname> add constraint c_name
primary key (column_2) using index c_name;
I don't know if this is a similar problem but, in DB2, you have to actually commit following the alter table statement. Otherwise it's still hanging around.