I have a table called addresses with a unique constraint on two fields: (address, and hostname). I realized I need to add 1 more field to the constraint. I have a piece of row I need to insert with equal address and hostname but not coin_id. When trying to insert I get this.
ERROR: duplicate key value violates unique constraint "address_uniq"
DETAIL: Key (address, hostname)=(GfFrqCJtwuSSJyv6D1STtrT146N8p9cLtd, NHWithdrawal) already exists.
SQL state: 23505
I have tried viewing my constraints with the following:
select * from information_schema.table_constraints
where constraint_type = 'UNIQUE'
I am not able to see the address_uniq constraint in that list.
How can I do the following:
Locate this constraint with psql
Alter/Update this constraint, and add 1 more column to it
Your constraint_type should also be "PRIMARY KEY" so the best way to locate
the constraint is using constraint_name.
select * from information_schema.table_constraints where constraint_name = 'address_uniq'
You can drop the existing constraint
ALTER TABLE your_table_name DROP CONSTRAINT address_uniq;
and add a new one:
ALTER TABLE your_table_name ADD CONSTRAINT address_uniq PRIMARY KEY(address, hostname, coin_id);
or
ALTER TABLE your_table_name ADD CONSTRAINT address_uniq UNIQUE(address, hostname, coin_id);
[How can I] Locate this constraint with psql
We can get the same error message if we violate a primary key constraint.
# alter table addresses add constraint addresses_pk primary key (id);
ALTER TABLE
# insert into addresses values (1, 2, 4, 4);
ERROR: duplicate key value violates unique constraint "addresses_pk"
DETAIL: Key (id)=(1) already exists.
#
Try searching the information schema where constraint_type = 'PRIMARY KEY'.
Note that we don't have to give a primary key constraint a name, as Postgres will generate a default of <table_name>_pkey. So for this to be the solution in your case it means whoever created the primary key gave it an explicit name of address_uniq, which would be confusing.
So a more likely possibility is that you have a unique index on those columns. Indexes don't show up in the information schema. You can check like this:
select * from pg_indexes where tablename = 'addresses';
[How can I] Alter/Update this constraint, and add 1 more column to it
If your problem is an index then do this:
# drop index address_uniq;
DROP INDEX
# create unique index address_uniq on addresses (address, hostname, coin_id);
CREATE INDEX
#
If it turns out it is a primary key constraint it's a similar process:
# alter table addresses drop constraint address_uniq;
ALTER TABLE
# alter table addresses add constraint address_uniq primary key (address, hostname,coin_id);
ALTER TABLE
#
Related
So I'm learning some SQL as a bit of a side project since my SQL sucks. I have the following table I created:
CREATE TABLE deliveries (
pid INTEGER,
FOREIGN KEY (pid) REFERENCES person_lives_at,
);
I want to alter it to be a table like this one:
CREATE TABLE deliveries (
pid INTEGER,
FOREIGN KEY (pid) REFERENCES employee,
);
How can I achieve that? I'm using Oracle SQL Developer
Since you created an FK constraint without a name, oracle assigned a system-generated name to the constraint like SYS_xxxxxx. To find the constraint name:
select constraint_name from all_Constraints
where table_name = 'DELIVERIES'
In my test case, it returned "SYS_C0016779". I then drop the constraint:
alter table deliveries drop constraint SYS_C0016779
Then add the new constraint:
ALTER TABLE deliveries
ADD CONSTRAINT PID_EMP_FK -- or whatever you want to call it.
FOREIGN KEY (pid)
REFERENCES employee(pid); -- or whatever the name of the column is
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 a Table with 4 Columns
Each Column will be A,B,C,D
Column A is the Primary key.
Column B has unique name constraint.
Now I want to remove the unique constraint for column B and give a unique constraint by combining the columns B, C and D. So the table will allow only one row with a particular value in columns B,C and D.
How can I give this type of a constraint?
I tried giving the composite unique key like :
ALTER TABLE TABLENAME ADD CONSTRAINT CONSTRAINT_NAME UNIQUE (COLUMN_B, COLUMN_C, COLUMN_D)
But it is checking whether any one of the constraint is present rather than checking for the combination of unique key constraint.
Create a unique key on those columns
ALTER TABLE YourTable
add CONSTRAINT YourTable_unique UNIQUE (B, C, D);
Oracle/PLSQL: Unique Constraints
First of all you should drop an existing Constraint by using below ALTER Query.
ALTER TABLE table_name
DROP CONSTRAINT myUniqueConstraint;
Now, you can create a UNIQUE Constraint by using the keyword UNIQUE with the combination of required Columns.
For Example:
ALTER TABLE table_name
ADD CONSTRAINT myUniqueConstraint UNIQUE(B, C, D);
Detailed explanation of UNIQUE Constraint here.
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
CREATE UNIQUE INDEX constraint_name ON table_name
(B,C,D)
I have a table in Oracle which has following schema:
City_ID Name State Country BuildTime Time
When I declared the table my primary key was both City_ID and the BuildTime, but now I want to change the primary key to three columns:
City_ID BuildTime Time
How can I change the primary key?
Assuming that your table name is city and your existing Primary Key is pk_city, you should be able to do the following:
ALTER TABLE city
DROP CONSTRAINT pk_city;
ALTER TABLE city
ADD CONSTRAINT pk_city PRIMARY KEY (city_id, buildtime, time);
Make sure that there are no records where time is NULL, otherwise you won't be able to re-create the constraint.
You will need to drop and re-create the primary key like this:
alter table my_table drop constraint my_pk;
alter table my_table add constraint my_pk primary key (city_id, buildtime, time);
However, if there are other tables with foreign keys that reference this primary key, then you will need to drop those first, do the above, and then re-create the foreign keys with the new column list.
An alternative syntax to drop the existing primary key (e.g. if you don't know the constraint name):
alter table my_table drop primary key;
Sometimes when we do these steps:
alter table my_table drop constraint my_pk;
alter table my_table add constraint my_pk primary key (city_id, buildtime, time);
The last statement fails with
ORA-00955 "name is already used by an existing object"
Oracle usually creates an unique index with the same name my_pk. In such a case you can drop the unique index or rename it based on whether the constraint is still relevant.
You can combine the dropping of primary key constraint and unique index into a single sql statement:
alter table my_table drop constraint my_pk drop index;
check this:
ORA-00955 "name is already used by an existing object"
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.