1

I have to import some data in my database . And there are some record already exist in table, not all. So what I want to do that:

  1. Run a php script for inserting data in table from input array

  2. Make data_id unique ( not primary key) in mysql table

  3. While inserting via php script if same data_id exist don't stop the execution but skip that record and process next.

something like this I want to do.

But my script stopping execution when data_id is repeated.

can anyone explain how can i handle this.

I don't want to apply check in database that id data_id is already exist then skip that record else insert because that will make page very slow.

  • Well, this is a contradiction that will be hard to resolve. You would have to either make data_id not unique, or fix the data - I don't think there's any other way – Pekka Jan 12 '11 at 11:09
  • If not unique then i have to apply check in whole table if that already exist. That will make page slow –  Jan 12 '11 at 11:11
  • I see. `INSERT IGNORE` might help as @predrag suggests - it will be easy to transform the SQL statements accordingly – Pekka Jan 12 '11 at 11:22

2 Answers2

2

You could use INSERT...ON DUPLICATE KEY UPDATE or INSERT IGNORE

Check this:

http://dev.mysql.com/doc/refman/5.5/en/insert.html

On duplicate key ignore?

"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"

Community
  • 1
  • 1
Diablo
  • 3,378
  • 1
  • 22
  • 28
1

Use insert ignore into table (fields) values (values)

This works

Cesar
  • 3,519
  • 2
  • 29
  • 43