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.
Related
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.
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)
I have simple table creating script in Postgres 9.1. I need it to create the table with
2-attributes PK only if it does not exist.
CREATE TABLE IF NOT EXISTS "mail_app_recipients"
(
"id_draft" Integer NOT NULL,
"id_person" Integer NOT NULL
) WITH (OIDS=FALSE); -- this is OK
ALTER TABLE "mail_app_recipients" ADD PRIMARY KEY IF NOT EXISTS ("id_draft","id_person");
-- this is problem since "IF NOT EXISTS" is not allowed.
Any solution how to solve this problem? Thanks in advance.
You could do something like the following, however it is better to include it in the create table as a_horse_with_no_name suggests.
if NOT exists (select constraint_name from information_schema.table_constraints where table_name = 'table_name' and constraint_type = 'PRIMARY KEY') then
ALTER TABLE table_name
ADD PRIMARY KEY (id);
end if;
Why not include the PK definition inside the CREATE TABLE:
CREATE TABLE IF NOT EXISTS mail_app_recipients
(
id_draft Integer NOT NULL,
id_person Integer NOT NULL,
constraint pk_mail_app_recipients primary key (id_draft, id_person)
)
You can try to DROP it before creating it (DROP has the IF EXISTS clause):
ALTER TABLE mail_app_recipients DROP CONSTRAINT IF EXISTS mail_app_recipients_pkey;
ALTER TABLE mail_app_recipients ADD CONSTRAINT mail_app_recipients_pkey PRIMARY KEY ("id_draft","id_person");
Note that this require that you give a name to the primary key constraint - in this example mail_app_recipients_pkey.
I can identify the error message that its due to unique value constraint, my table is 'branches',and where did SYS_C004023 come. I have checked the branches table and there is no value duplication. What could be the issue.
where did SYS_C004023 come
This is a system-generated constraint name, which Oracle creates when a constraint is created without being explicitly named e.g.
create table mytable (col1 integer primary key);
The primary key constraint on mytable will be system-generated since I didn't explicitly name it like this:
create table mytable (col1 integer constraint mytable_pk primary key);
You can find out what table this constraint is on like this:
select table_name
from all_constraints
where owner = 'HR'
and constraint_name = 'SYS_C004023';
And you can find out which columns it makes unique like this:
select column_name
from all_cons_columns
where owner = 'HR'
and constraint_name = 'SYS_C004023';
there is no value duplication
No, there won't be, thanks to the constraint. What there has been is a failed attempt to insert or update a row so that the uniqueness constraint is violatedd.
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.