开发者

MySQL: Appending records: find then append or append only

I'm writing a program, in C++, to access tables in MySQL via the MySql C++ Connector.

I retrieve a record from the User (via GUI or Xml file).

Here are my questions:

  1. Should I search the table first for the given record, then append if it doesn't exist,

  2. Or append the record, and let MySQL append the record if it is unique?

Here is my example table:

mysql> describe ing_titles;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| ID_Title | int(11)  | NO   | PRI | NULL    |       |
| Title    | char(32) | NO   |     | NULL    |       |
+----------+----------+------+-----+---------+-------+

In judgment, I am looking for a solution that will enable my program to respond quickly to the User.

During development, I have small tables (less than 5 records), but I am expecting them to grow when I formally release the application.

FYI: I am using Visual Stud开发者_JAVA技巧ion 2008, C++, wxWidgets, and MySQL C++ Connector on Windows XP and Vista.


Mark the field in question with a UNIQUE constraint and use INSERT ... ON DUPLICATE KEY UPDATE or INSERT IGNORE.

The former will update the records if they already exists, the latter will just do nothing.

Searching the table first is not efficient, since it requires two roundtrips to the server: the first one to search, the second one to insert (or to update).

The syntaxes above do the same in one sentence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜