0

This is my table with some sample data

C ID | D ID    | Details |
--------------------------
 a   | b_data1 | d1      |
 a   | b_data2 | d2      |
 b   | b_data1 | d1      |
 b   | b_data2 | d2      |
 c   | b_data1 | d1      |
 c   | b_data2 | d2      |

When I run this query##

INSERT IGNORE INTO table_name (C_ID, D_ID, Details) VALUES ('C', 'b_data3','d3') ('C', 'b_data2','d2')

It inserts both those rows when it's supposed to ignore the second value pair ('C', 'b_data2','d2')

No indexes /primary key are defined.

I want to make sure there is no duplicate row, means data in three columns combined should make the row unique. I can't make it unique .As I have illustrated here, a can keep same content as b but a should not have a duplicate.

3 Answers 3

2

INSERT IGNORE means "ignore rows that break unique constraints, instead of failing the query". You need to define UNIQUE columns for it to work as you expect it to — e.g. CREATE TABLE tablename (col1 type, col2 type, col3 type, UNIQUE (col1, col2, col3))

Sign up to request clarification or add additional context in comments.

3 Comments

I cant make it unique as this is a collection page, E.g should be able to store a > b_data1 > d1 and a > b_data2 > d2 and b b > b_data1 > d1 and b > b_data2 > d2 table about i illustrated. a can have same content of collection b but cant have same in a
If you define UNIQUE (col1, col2, col3), this means you cannot have the same values in ALL of these columns. I.e., you can have the same value in col1 as long as either col2 or col3 is different. Or you can have the same value in col2 and col3 as long as col1 is different. It is a compound index on all three columns, does not mean that each of them needs to be unique.
Thanks alot for ur helps .. I'm clear now on its usage .. i cant create unique/primary key in this table as there is no unique single colum. its unique when all 3 combined. i had to query and do !in_array($data, $query_array) to over come this
1

MySQL enforces unique constraints with UNIQUE indexes. If you wish to add the index without recreating the table, you would do:

CREATE UNIQUE INDEX index_name ON table (C_ID, D_ID, Details)

In addition to INSERT IGNORE and the already mentioned REPLACE, you can also use INSERT...ON DUPLICATE KEY UPDATE for more control over what happens when insert runs into a duplicate unique.

3 Comments

but i cant make it unique. i have illustrated there . a can keep same content as b but a should not have a duplicate
I'm afraid, MySql requires primary key or uniqe index to keep duplicates away.
Thanks alot for ur helps .. I'm clear now on its usage .. i cant create unique/primary key . i had to query and do !in_array($data, $query_array) to over come this
0

You should define composed uniqe index as @lanzz suggested. If you want new records to replace the old ones in the case of index clash, use replace statement.

1 Comment

but i cant make it unique. i have illustrated there . a can keep same content as b but a should not have a duplicate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.