GT22
GT22

Reputation: 503

updating a row in a mysql database?

Is it better to use INSERT, REPLACE or ON DUPLICATE KEY UPDATE, to update a row in my database?

I'm currently using:

mysql_query("insert into pages (pages_id, pagename, pagedesc, pagekey, pagecont) values('$pages_id', $pagename', '$pagedesc', '$pagekey', '$pagecont')");

to create new rows in the DB.

pages_id is an auto_increment field in the mysql database. I'm not sure which is the best way to update an existing row in the database?

Upvotes: 0

Views: 103

Answers (4)

superM
superM

Reputation: 8695

INSERT is always a heavier operation than UPDATE. INSERT has to allocate memory for the newly inserted item, while UPDATE performs search on the table and doesn't have look for new memory. This difference becomes critical when you have to insert many records in a short period of time. Better to INSERT when DB is not very busy and then UPDATE when necessary.

Upvotes: 0

Amarnasan
Amarnasan

Reputation: 15579

If you are updating an existing row (and you're 100% sure it exists), definitely go for UPDATE.

BUT

If you are "updating" a row which may not exist, go for "INSERT...ON DUPLICATE KEY UPDATE".

Upvotes: 3

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137527

Ironically, you use UPDATE to update a row in SQL.

UPDATE pages SET ... WHERE pages_id = $pages_id;

Upvotes: 3

Bono
Bono

Reputation: 4869

Use UPDATE and specify the pages_id?

Here's a tutorial on UPDATE: UPDATE tutorial

Little example:

mysql_query("UPDATE pages SET pagename = $pagename WHERE pages_id = $page_id");

On a completely different note though, stop using mysql* functions and start working with PDO, it's a lot safer!

Upvotes: 0

Related Questions