The simplest way I suggest is to add to the field name one CONSTRAINT single index. When trying to insert a new entry for the same name, Mysql will generate an error (which can be handled by caller insert).
To add the Constraint run the code:
ALTER TABLE registro_tb ADD CONSTRAINT nome UNIQUE
NOTE: There can be no duplicate records in the name field when performing this operation!
However, even with single active Constraint, it is possible to tell the MYSQL engine to ignore duplicate key error while trying to insert:
INSERT IGNORE INTO registro_tb (nome, tag)
VALUES ('Darwin', '99649')
This way it will be trivial to enter the record in the bank, without worrying if it already exists. This option is also more efficient and faster than trying to verify if the data exists before inserting and is indicated for scenarios with high data volume.
If you want to update the input if it already exists, you can use the following syntax:
INSERT IGNORE INTO registro_tb (nome, tag)
VALUES ('Darwin', '99649')
ON DUPLICATE KEY UPDATE tag=VALUES(tag)
In that case name needs to be a key Primary (as far as I know).
Easy, when creating table structure, indicate that the field in question is
unique
this way if they try to create a record with this repeated field, themySql
will return an integrity breach attempt error– MarceloBoni
a specific column or all? depending on the case,
replace into
may be an option– Daniel Omine
It would be the combination of the line, it would work as a "Dictionary" .
– MacYoshi
http://stackoverflow.com/questions/690632/how-do-i-update-a-row-in-a-table-or-insert-it-if-it-doesnt-exist
– Reginaldo Rigo