CREATE UNIQUE INDEX statement terminated because a duplicate key was found [duplicate] - sql

This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 2 months ago.
I want to check the duplicate values in my SQL table. and remove those data.
So I first checked the duplicate values using
SELECT COUNT(*)
[Se_Id],
[Service_Previous_Step],
[Service_Next_Step]
FROM [Mondo-PROD].[dbo].[ServiceWorkflows]
GROUP BY
[Se_Id],
[Service_Previous_Step],
[Service_Next_Step]
HAVING COUNT(*) > 1;
Then I use this
ALTER TABLE [dbo].[ServiceWorkflows]
ADD CONSTRAINT UQ_ServiceWorkflows_ServiceId_PreviousStep_NextStep
UNIQUE ([Service_Id], [Service_Previous_Step], [Service_Next_Step]);
But I get errors as
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.ServiceWorkflows' and the index name 'UQ_ServiceWorkflows_ServiceId_PreviousStep_NextStep'. The duplicate key value is (96, 1, 2).

You can use DELETE to remove duplicates from your table like:
WITH CTE AS(
SELECT [Se_Id],
[Service_Previous_Step],
[Service_Next_Step],
RN = ROW_NUMBER()OVER(PARTITION BY Se_Id ORDER BY Se_Id)
FROM [dbo].[ServiceWorkflows]
)
DELETE FROM CTE WHERE RN > 1
If your table already has duplicate records in it then adding a constraint won't delete the duplicate records and hence the error which you are getting.

Related

SQL - Insert only explicit values and not repeated ones [duplicate]

This question already has answers here:
Sql insert multiple rows if not exists
(7 answers)
Closed 10 months ago.
I have a SQL script with about 250 entries that are being inserted. The script on its own runs fine however, I run into problems in case of duplicate entries. I could try doing something like this:
IF NOT EXISTS (SELECT * FROM [TS].[Configs] WHERE [Id] = 15)
INSERT . . .
However, I cannot write this repeated statement over and over for the 250 entries. I have tried setting Identity_Insert to ON but I still get this error:
Exception Message: Violation of PRIMARY KEY constraint 'Tbl_LangFiles'. Cannot insert duplicate key in object '[TS].[Configs]'
How can I go about only inserting unique entries in this table?
Have you tried the UPSERT statement?
UPSERT INTO employees (id, name, email)
VALUES (2, ‘Dennis’, ‘dennisp#weyland.corp’);
This will attempt to insert a new record on the database, but if it already exists, it will take the parameters and update.
L.

Oracle - INSERT statement with generated primary key [duplicate]

This question already has answers here:
Can you SELECT everything, but 1 or 2 fields, without writer's cramp?
(12 answers)
Closed 8 years ago.
I have a table with many columns (20 or 25) and i want to build an insert statement on it, using select and generating another primary key
The simple soulution will be:
INSERT INTO myTable
SELECT mySeq.nextVal PRIMARY_KEY, COLUMN2, COLUMN3, COLUMN4...
FROM myTable
WHERE PRIMARY_KEY = 1
Since my table have many columns, there is a way to say "i give you primary key, and ALL the other columns are the same" without explain them?
Like:
INSERT INTO myTable
SELECT mySeq.nextVal m.* /* Sure this not work because i get again PRIMARY_KEY column*/
FROM myTable m
WHERE PRIMARY_KEY = 1
There is no way to specify something like SELECT * EXCEPT aColumn, you will have to write them manually as you already did, sorry.
Actually, you could do a subquery that select column from table info then query that subquery but that make non-sense to me.
If your table contains so many column that it is a pain to write them all, then you might want to re-design your database and split your table in multiple tables.

Duplicate key error with PostgreSQL INSERT with subquery

There are some similar questions on StackOverflow, but they don't seem to exactly match my case. I am trying to bulk insert into a PostgreSQL table with composite unique constraints. I created a temporary table (temptable) without any constraints, and loaded the data (with possible some duplicate values) in it. So far, so good.
Now, I am trying to transfer the data to the actual table (realtable) with unique index. For this, I used an INSERT statement with a subquery:
INSERT INTO realtable
SELECT * FROM temptable WHERE NOT EXISTS (
SELECT 1 FROM realtable WHERE temptable.added_date = realtable.added_date
AND temptable.product_name = realtable.product_name
);
However, I am getting duplicate key errors:
ERROR: duplicate key value violates unique constraint "realtable_added_date_product_name_key"
SQL state: 23505
Detail: Key (added_date, product_name)=(20000103, TEST) already exists.
My question is, shouldn't the WHERE NOT EXISTS clause prevent this from happening? How can I fix it?
The NOT EXISTS clause only prevents rows from temptable conflicting with existing rows from realtable; it will not prevent multiple rows from temptable from conflicting with each other. This is because the SELECT is calculated once based on the initial state of realtable, not re-calculated after each row is inserted.
One solution would be to use a GROUP BY or DISTINCT ON in the SELECT query, to omit duplicates, e.g.
INSERT INTO realtable
SELECT DISTINCT ON (added_date, product_name) *
FROM temptable WHERE NOT EXISTS (
SELECT 1 FROM realtable WHERE temptable.added_date = realtable.added_date
AND temptable.product_name = realtable.product_name
)
ORDER BY ???; -- this ORDER BY will determine which of a set of duplicates is kept by the DISTINCT ON

SQLite UPSERT with 2 constraints [duplicate]

This question already has answers here:
SQLite INSERT - ON DUPLICATE KEY UPDATE (UPSERT)
(5 answers)
SQL: How to update or insert if doesn't exist?
(2 answers)
Closed 9 years ago.
I have an sql table with 3 columns, none of which is UNIQUE ( but the pair name + role is ):
Name | Role | Votes
What I need to do is, write an sqllite query that stick to the following rules :
If a row with the given name and role already exist, votes is
incremented by 1
If not, a new row is created with Votes = 1
I've looked into INSERT OR REPLACE and this great post but it doesn't seem to help me that much, and I'm not even sure INSERT OR REPLACE is a viable option, since something like
INSERT OR REPLACE INTO words (name,role,votes)
VALUES ('foo','bar', coalesce((
select votes from words where name = 'foo' and role='bar'),0)+1)
always insterts and never replace
You simply need to create unique index over your 2 columns for this to work:
CREATE UNIQUE INDEX words_name_role_idx ON words (name,role)
Note that you do not create unique index for any single column, but for combination of 2 as a whole.
After that, your REPLACE INTO statement should start working correctly:
REPLACE INTO words (name,role,votes) VALUES ('foo','bar',
coalesce((
SELECT votes FROM words
WHERE name = 'foo' AND role='bar'),0
)+1
)
(note that I have changed counter to votes above).
This query will update your record with +1.
update todo set text='raj',complete='raj',pk=((SELECT pk FROM todo where text='raj' and complete='raj')+1) where (SELECT pk FROM todo where text='raj' and complete='raj')
EDIT YOUE QUERY
update words set name='foo',role='bar', votes =((SELECT votes FROM words where name='foo' and role='bar')+1) where (SELECT votes FROM words where name='foo' and role='bar')
And make insert query if this condition will not true.

insert ...select ... count ... on duplicate key update error

I created a table to hold counts of work orders by asset tag. I have 2 fields, asset_tag (which is unique) and the wo_count. I am trying to write a query that will insert/update the counts in the table. Through research on ON DUPLICATE KEY UPDATE, I have come up with this, but am getting unknown column errors.
INSERT INTO mod_workorder_counts (asset_tag, wo_count)
(SELECT t.asset_tag, count(*) AS cnt
FROM mod_workorder_data t
WHERE t.asset_tag IS NOT NULL
GROUP BY t.asset_tag)
ON DUPLICATE KEY UPDATE mod_workorder_counts.wo_count = t.cnt
When I run this I get #1054 - Unknown column 't.cnt' in 'field list'. I am not sure how to use the count values in the update.
Once you reference it as "cnt", you no longer need the "t." portion. You should reference it as just "cnt".