emma perkins
emma perkins

Reputation: 769

using INSERT IGNORE across two columns

Im looking to use INSERT IGNORE so that I avoid entering duplicate fields into my database but i want to check this across 2 columns not 1.

So only if columns DateandTime AND Horse don't exists together in the same row

Upvotes: 1

Views: 568

Answers (1)

DocRattie
DocRattie

Reputation: 1423

You shoud work with indices.

If you want to check that the values for DateandTime and Horse are both not set, give each a unique index.

If you want to check it the combinaiton of both doesn't exist jet, give them a combined index that is set to unique.

I recommend to use phpmyadmin. You can set indices in the Structure tab of a table.


There is no php needed to achive this.


Tt you Comment about a combined index:

ALTER TABLE Race_Records ADD  UNIQUE INDEX unique_index ( `Horse`, `DateandTime` ) ;

the unique_index is the name of the index. You can choose that yourself, it you warn.

All Arguments in the () are used as the cols, that are added to that index.

And you need to set the UNIQUE to make it an unique index. Otherwise it will only be a normal index that only helps with searching.

Upvotes: 3

Related Questions