Can I drop a foreign key without using constraint? - sql

ALTER TABLE table1
ADD FOREIGN KEY attrib1 REFERENCES table2(attrib2)
GO
It works - but how can I drop it?
If I try
DROP FOREIGN KEY (attrib1) REFERENCES table2(attrib2)
it says
Incorrect syntax near the keyword 'FOREIGN'.

In SQL Server you need to drop foreign key constraint by using below query:
Query :
ALTER TABLE [dbo].[Table_Name]
DROP CONSTRAINT [Constraint_Name]
Example:
ALTER TABLE [dbo].[SpeakerDetail]
DROP CONSTRAINT [FK_SpeakerId_UserID]

Related

Alter a table to add a foreign key constraint with a default value

How do I alter a table to have a column a foreign key constraint with a default value?
In T-SQL if you're using SQL-Server, just be careful to put a real key for default.
Edit: As said in the comments, be careful that you really need a default value for a foreign key, there could be a design problem.
ALTER TABLE [tablename]
ADD CONSTRAINT [CK_columnname_default]
DEFAULT [your_default_value] FOR [columnname];
GO
ALTER TABLE [tablename]
ADD CONSTRAINT [FK_nameForeignKey] FOREIGN KEY (columnname)
REFERENCES [dbo].[oTable] (oTableID)
ON UPDATE CASCADE;
GO
Well I did the following and it worked for me:
ALTER TABLE Employee
ADD RegionID varchar(3)
CONSTRAINT [Default_Value_Region] default ('US') NOT NULL,
CONSTRAINT [FK_EMPLOYEE_Region] FOREIGN KEY([RegionID])
REFERENCES [dbo].[Region] ([Code])
GO

How to Alter Constraint

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,.....)

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;

How to add a column and make it a foreign key in single MySQL statement?

In mysql, can I add a column and foreign key in the same statement? And what is the proper syntax for adding the fk?
Here is my SQL:
ALTER TABLE database.table
ADD COLUMN columnname INT DEFAULT(1),
FOREIGN KEY (fk_name) REFERENCES reftable(refcolumn) ON DELETE CASCADE;
...and the accompanying error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY (fk_name) REFERENCES reftable(refcolumn) ON DELETE CASCADE' at line 4
Try this:
ALTER TABLE database.table
ADD COLUMN columnname INT DEFAULT(1),
ADD FOREIGN KEY fk_name(fk_column) REFERENCES reftable(refcolumn) ON DELETE CASCADE;
The following query adds a column by alter query and the constraint query makes it a FK in a single mysql query. You can do it like this,
SYNTAX:
ALTER TABLE `SCHEMANAME`.`TABLE1`
ADD COLUMN `FK_COLUMN` BIGINT(20) NOT NULL,
ADD CONSTRAINT `FK_TABLE2_COLUMN` FOREIGN KEY (`FK_COLUMN`)
REFERENCES `SCHEMANAME`.`TABLE2`(`PK_COLUMN`);
EXAMPLE:
ALTER TABLE `USERDB`.`ADDRESS_TABLE`
ADD COLUMN `USER_ID` BIGINT(20) NOT NULL AFTER `PHONE_NUMBER`,
ADD CONSTRAINT `FK_CUSTOMER_TABLE_CUSTOMER_ID` FOREIGN KEY (`USER_ID`)
REFERENCES `USERDB`.`CUSTOMER_TABLE`(`CUSTOMER_ID`);
This can be simplified a bit. You just need to add the "ADD" keyword before "FOREIGN KEY". Adding example below.
ALTER TABLE database.table
ADD COLUMN columnname INT DEFAULT(1),
ADD FOREIGN KEY (fk_name) REFERENCES reftable(refcolumn) ON DELETE CASCADE;
You can use it.
ALTER TABLE database.table
ADD COLUMN columnname INT DEFAULT(1);
ALTER TABLE database.table add FOREIGN KEY (fk_name) REFERENCES reftable(refcolumn) ON DELETE CASCADE;

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.