Cannot drop constraint - nonexistent constraint - sql

I am trying to drop constraints in my table like fk and pk but its raising an error.
Cannot drop constraint - nonexistent constraint
But when I check whether there is a constraint or not, it shows that there is a constraint, but still the drop isn't working.
select column_name,constraint_name from user_cons_columns where table_name='EMP';
COLUMN_NAME CONSTRAINT_NAME
------------------------------
EMPNO PK_EMP
DEPTNO FK_DEPTNO
alter table emp drop constraint deptno;
ERROR at line 1:
ORA-02443: Cannot drop constraint - nonexistent constraint

Try this:
ALTER TABLE EMP DROP CONSTRAINT FK_DEPTNO;

alter table emp drop constraint fk_deptno;

The syntax requires the table with the foreign key and the name of the constraint. After running into the same issue multiple times I try to call both to make sure I am not accidentally calling the wrong table.
alter [NameOfTable] Drop Constraint [NameOfConstraint]

It happens When you try to drop a constraint in child table but it actually exist in parent table. that's why you are showed nonexistent constraint. a query that bring you to the desired table is like:
select TABLE_NAME from dba_cons_columns where CONSTRAINT_NAME='constraint_name';
# result
TABLE_NAME
------------------------------
table_name
now you disable/drop that constraint from the resulted table.
ALTER TABLE table_name DISABLE CONSTRAINT constraint_name;
# Table altered.
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
# Table altered.

Related

How to ADD a CONSTRAINT NAME to an already EXISTING CONSTRAINT

Is there a way to give names to already existing constraints?
for example :
create table employee (emp_id number(10),emp_name varchar2(20),
dept_id number(10),foreign key(dept_id) references department(dept_id));
In the above query I haven't named the foreign key constraint so after the creation of the table can I give a name to it also can the foreign key constraint be dropped without dropping the column??
Yes you can rename a constraint like this:
alter table t rename constraint old_name to new_name
Edit: I've forgotten about the second question. Yes you can drop a constraint without dropping the column. If you do not know the name of the constraint you can find it in user_constraints table like this:
select constraint_name
from user_constraints
where table_name = 'your_table'
and constraint_type ='R'
For the second part, yes you can drop the foreign key constraint.

How to change the column length of a primary key in SQL Server?

I know how to change the length of a column, but my SQL statement fails because the column I'm trying to change is a PK, so I get the following error:
Msg 5074, Level 16, State 1, Line 1
The object 'PK_TableName' is dependent on column 'PersonID'.
PersonID = PK.
I've read What is the sql to change the field length of a table column in sql server which only applies to non-PK columns.
I tried this:
ALTER TABLE table_name
ALTER COLUMN column_name <new datatype>
See below sample example how to increase size of the primary column
Create a sample table
create table abc (id varchar(10) primary key)
Find primary constraint in key constraints tables
select object_name(object_id),* from sys.key_constraints where object_name(parent_object_id) = 'abc
Drop constraint
ALTER TABLE abc
DROP CONSTRAINT PK__abc__3213E83F74EAC69B
(Replace PK__abc__3213E83F74EAC69B with constraint name you receive.)
Add not null
ALTER TABLE abc alter column id varchar(20) NOT NULL;
Add primary key again
ALTER TABLE abc
ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (id)
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE table_name
ALTER COLUMN column_name datatype
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)
SQLServer 2008 did not allow me to change a primary key with data so I deactivated all the constraints, performed the command and activated all the constraints again. The commands are:
EXEC sp_MSforeachtable #command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"
-- commands here
EXEC sp_MSforeachtable #command1="ALTER TABLE ? CHECK CONSTRAINT ALL"

Move existing Foreign Key to different file group

I used the following code
ALTER TABLE Table_name
DROP CONSTRAINT constraint_name
ALTER TABLE Table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name) REFERENCES ref_table (ref_column)
ON [filegroup_name]
But I got this error
Incorrect syntax near 'filegroup_name'
Constraint is nothing but the rule made on the table.So create table in the FileGroup you wish.
Primary keys you may need to mention[filegroup name] along with the syntax since by default CLustered index will get created. [Just in case if you want your index pages in different filegroup instead of Primary filegroup]
Hence there is no need to specify ON [FileGroupName] while adding the foreign key constraint.
Hence go with:
ALTER TABLE Table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name) REFERENCES ref_table (ref_column)

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.