How to Alter Constraint - sql

SQL How to Alter Constraint
Below is 1 of my constraint
CONSTRAINT ACTIVEPROG_FKEY1 FOREIGN KEY(ActiveProgCode) REFERENCES PROGRAM(ActiveProgCode),
I want to add in
ON DELETE CASCADE
to the constraint above.
How do i alter that existing constraint ACTIVEPROG_FKEY1 and add
ON DELETE CASCADE
to constraint ACTIVEPROG_FKEY1
Consider ACTIVEPROG_FKEY1 is at Table ACTIVEPROG

You can not alter constraints ever but you can drop them and then recreate.
Have look on this
ALTER TABLE your_table DROP CONSTRAINT ACTIVEPROG_FKEY1;
and then recreate it with ON DELETE CASCADE like this
ALTER TABLE your_table
add CONSTRAINT ACTIVEPROG_FKEY1 FOREIGN KEY(ActiveProgCode) REFERENCES PROGRAM(ActiveProgCode)
ON DELETE CASCADE;
hope this help

No. We cannot alter the constraint, only thing we can do is drop and recreate it
ALTER TABLE [TABLENAME] DROP CONSTRAINT [CONSTRAINTNAME]
Foreign Key Constraint
Alter Table Table1 Add Constraint [CONSTRAINTNAME] Foreign Key (Column) References Table2 (Column) On Update Cascade On Delete Cascade
Primary Key constraint
Alter Table Table add constraint [Primary Key] Primary key(Column1,Column2,.....)

Related

How to add multiple foreign keys with nocheck for each one?

i need to create multiple fk for diffrent fields with nocheck for each fk,
all the examples i see is only for 1 fk at a time,
i need something like this :
ALTER TABLE contact
WITH NOCHECK ADD CONSTRAINT [FK_check5]
FOREIGN KEY (accountid) REFERENCES dbo.account ([accountid]),
WITH NOCHECK ADD CONSTRAINT [FK_check6]
FOREIGN KEY (roleid) REFERENCES dbo.role ([id])
the disabled must be on the create level and not after it
seperate your alter table statement into two
ALTER TABLE contact
WITH NOCHECK ADD CONSTRAINT [FK_check5]
FOREIGN KEY (accountid) REFERENCES dbo.account ([accountid]);
ALTER TABLE CONTACTS
WITH NOCHECK ADD CONSTRAINT [FK_check6]
FOREIGN KEY (roleid) REFERENCES dbo.role ([id]);
The 'NoCheck' will apply to all the constraints you add in the statement, you don't need to repeat it:
Example below (tested in SQL 2012):
CREATE TABLE account (accountid int, PRIMARY KEY (accountid))
GO
CREATE TABLE [role] (id int, PRIMARY KEY (id))
GO
CREATE TABLE contact (accountid int, roleid int)
GO
insert into contact values (1,1)
GO
ALTER TABLE contact
WITH NOCHECK
ADD
CONSTRAINT [FK_check5] FOREIGN KEY (accountid) REFERENCES [account] ([accountid])
,CONSTRAINT [FK_check6] FOREIGN KEY (roleid) REFERENCES [role] ([id])
If your intention is to only have the foreign key for ER diagramming, you can turn off enforcing of the constraint. See the below article for more information.
https://learn.microsoft.com/en-us/sql/relational-databases/tables/disable-foreign-key-constraints-with-insert-and-update-statements
as per the example provided in the article:
USE AdventureWorks2012;
GO
ALTER TABLE Purchasing.PurchaseOrderHeader
NOCHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
GO
That being said, I would recommend that you NEVER do this in a production system on an ongoing basis. Foreign key constraints exist for a reason, to maintain data integrity. If you are inserting data in a way that violates foreign key constraints you may want to review your data design.

SQL ON UPDATE CASCADE - Composite key

I have 2 tables
Table1(PKFK1, PKFK2)
Table2(PKFK1, PKFK2, PKFK3)
I want to do an update cascade on table2 so that every time pkfk2 is updated in table1 is does the same in table2. I have tried the following:
alter table Table2
ADD CONSTRAINT fk_cascadeUP FOREIGN KEY (PKFK1) REFERENCES Table1(PKFK1) ON UPDATE CASCADE,
ADD CONSTRAINT fk_cascadeUP2 FOREIGN KEY (PKFK2) REFERENCES Table1 (PKFK2) ON UPDATE CASCADE;
This gives an error. How can I do the update cascade?
Each ALTER TABLE needs to be its own statement. Try this:
alter table Table2
ADD CONSTRAINT fk_cascadeUP FOREIGN KEY (PKFK1) REFERENCES Table1(PKFK1) ON UPDATE CASCADE
alter table Table2
ADD CONSTRAINT fk_cascadeUP2 FOREIGN KEY (PKFK2) REFERENCES Table1 (PKFK2) ON UPDATE CASCADE
First, you will have to delete the foreign key, and then add the foreign key with your desired requirements. Drop the foreign key from the table and then add foreign key with "ON UPDATE CASCADE".
It would be somewhat like:
ALTER TABLE Table2
DROP FOREIGN KEY [foreign key name]
ADD CONSTRAINT fk_cascadeup2 FOREIGN KEY (PKFK2) REFERENCES Table1 (PKFK2) ON UPDATE
CASCADE;
The other method can be "set foreign_key_checks = 0" and after altering the foreign key, you can set it back to "1".

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;

Table A's PK is being referenced by Table B's FK. Cannot drop Table A's PK

MS SQL Server
I'm making a star schema. I've set the PK's and FK for my tables and am now trying to write a procedure that will drop the constraints, truncate the tables, add the constraints again, and then repopulate the tables. When I try to drop the constraints I receive the error:
"The constraint PK_TIMEDIM is being referenced by table SalesFactTable, foreign key constraint FK_SALESFACTTABLE. Could not drop constraint."
EDIT: One problem solved. Another found. I receive the same error except now there are mysterious, auto-generated FK's such as FK__SalesFact__CUST___19DFD96B.
Please show me what I'm doing wrong.
ALTER PROCEDURE [dbo].[A11]
AS
BEGIN
--Drop constraints
ALTER TABLE SalesFactTable
DROP CONSTRAINT FK_SALESFACTTABLE
ALTER TABLE SalesFactTable
DROP CONSTRAINT PK_SALESFACTTABLE
ALTER TABLE TimeDim
DROP CONSTRAINT PK_TIMEDIM
ALTER TABLE CustomerDim
DROP CONSTRAINT PK_CUSTOMERDIM
ALTER TABLE PartDim
DROP CONSTRAINT PK_PARTDIM
--Truncate tables
TRUNCATE TABLE TimeDim
TRUNCATE TABLE CustomerDim
TRUNCATE TABLE PartDim
TRUNCATE TABLE SalesFactTable
--Add constraints
ALTER TABLE TimeDim
ADD CONSTRAINT PK_TIMEDIM PRIMARY KEY (TIME_ID)
ALTER TABLE CustomerDim
ADD CONSTRAINT PK_CUSTOMERDIM PRIMARY KEY (CUST_ID)
ALTER TABLE PartDim
ADD CONSTRAINT PK_PARTDIM PRIMARY KEY (PART_ID)
ALTER TABLE SalesFactTable
ADD CONSTRAINT FK_SALESFACTTABLE FOREIGN KEY (TIME_ID) REFERENCES TimeDim (TIME_ID),
FOREIGN KEY (CUST_ID) REFERENCES CustomerDim (CUST_ID),
FOREIGN KEY (PART_ID) REFERENCES PartDim (PART_ID)
ALTER TABLE SalesFactTable
ADD CONSTRAINT PK_SALESFACTTABLE PRIMARY KEY (TIME_ID, CUST_ID, PART_ID)
**Foreign keys are referenced by the Primary key, so you are not allowed to remove the primary key before removing the Foreign Key constraint.
so, you need to remove the foreign key first to remove the Primary key constraint from a table.
**
ALTER PROCEDURE [dbo].[A11]
AS
BEGIN
--Drop FK constraints
ALTER TABLE SalesFactTable
DROP CONSTRAINT FK_SALESFACTTABLE
--Drop PK constraints
ALTER TABLE SalesFactTable
DROP CONSTRAINT PK_SALESFACTTABLE
ALTER TABLE TimeDim
DROP CONSTRAINT PK_TIMEDIM
ALTER TABLE CustomerDim
DROP CONSTRAINT PK_CUSTOMERDIM
ALTER TABLE PartDim
DROP CONSTRAINT PK_PARTDIM
--Truncate tables
TRUNCATE TABLE TimeDim
TRUNCATE TABLE CustomerDim
TRUNCATE TABLE PartDim
TRUNCATE TABLE SalesFactTable
--Add constraints
ALTER TABLE TimeDim
ADD CONSTRAINT PK_TIMEDIM PRIMARY KEY (TIME_ID)
ALTER TABLE CustomerDim
ADD CONSTRAINT PK_CUSTOMERDIM PRIMARY KEY (CUST_ID)
ALTER TABLE PartDim
ADD CONSTRAINT PK_PARTDIM PRIMARY KEY (PART_ID)
ALTER TABLE SalesFactTable
ADD CONSTRAINT FK_SALESFACTTABLE FOREIGN KEY (TIME_ID) REFERENCES TimeDim (TIME_ID),
FOREIGN KEY (CUST_ID) REFERENCES CustomerDim (CUST_ID),
FOREIGN KEY (PART_ID) REFERENCES PartDim (PART_ID)
ALTER TABLE SalesFactTable
ADD CONSTRAINT PK_SALESFACTTABLE PRIMARY KEY (TIME_ID, CUST_ID, PART_ID)

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;