How to handle duplicate records in a single column database?

The user can enter keywords in the text field and separate the keys with a comma.
So the input may be bananas, apple, orange, pineapple.

In my database, I have a table named keyword, and it contains only one column keyword, which is also the primary key.

I add keywords to the database $myArray = expload(',', $keywords).
Then I iterate over the array and do `INSERT INTO myTable '.

Now, if the keyword already exists, I will get an error.

I can overcome the error message using the instructions INSERT IGNORE INTO. If the record is a duplicate, the IGNORE keyword tells MySQL to cancel it without any errors.

My question is: is this a good way to do this? Or should I first check if a keyword exists?
I kind of think two questions against one. And will this affect server load?

+3
source share
4 answers

Paste ignore - it's great, it kills two birds with one stone.

insert ignore is theoretically non-standard sql, but it is still very useful. if you ever need to use some other storage mechanism, you can improve these minor things if such an event ever happens ... there is no need to go through hoops to pre-block your code in this case.

+4
source

INSERT IGNORE , , .

, , . , PHP - array_unique() . (. http://nl2.php.net/manual/en/function.array-unique.php)

:

  • INSERT IGNORE - .
  • , , MySQL - .
  • - , PHP, .

, - INSERT, :

$sql = INSERT INTO tab (keyword) VALUES ('word1'), ('word2'), ...

, , SQL. , , -

+1

INSERT IGNORE . , , REPLACE INTO, ( , , , , ).

- , SELECT , .

0

... :

, "", , , , , " ", , , , " , , " ", " ". , , dbms, . , ... , ...

0
source

Source: https://habr.com/ru/post/1728210/


All Articles