Insert or Update?

December 29th, 2005, 12:30 am by lig

Ever have this situation:

PHP:
  1. $res = mysql_query("select id from table where id = $id");
  2. $record_exists = mysql_num_rows($res);
  3. ($record_exists) ? (update_record($id);) : (insert_record($id);) ;

Well on the MySQL mailing list a couple of ways of handling this was pointed out. It was enlightning to me - so I thought I'd pass it on. The interesting part is that they did it with only 1 SQL statement.

Method 1
One method pointed out:

mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
-> ON DUPLICATE KEY UPDATE c=c+1;

quote from the MySQL manual page

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed.

There are of course gotcha's to watch out for so be sure to read up in the manual and I don't beleive this is portable to other DB's... but that is a slick little piece of SQL for MySQL.

Method 2
The second way they noted for handling this is with the REPLACE syntax.

Quote from the manual:

REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record for a PRIMARY KEY or a UNIQUE index, the old record is deleted before the new record is inserted.

Sweet! So which do you want to do... update or delete and insert? Anyone have any idea which is faster or uses less resources?

del.icio.us:Insert or Update?  digg:Insert or Update?  furl:Insert or Update?  reddit:Insert or Update?

BBC NEWS | Science/Nature | New Year ‘delayed’ by leap second

December 27th, 2005, 10:56 pm by lig

BBC NEWS | Science/Nature | New Year 'delayed' by leap second

"Lets do the time warp again"

del.icio.us:BBC NEWS | Science/Nature | New Year 'delayed' by leap second  digg:BBC NEWS | Science/Nature | New Year 'delayed' by leap second  furl:BBC NEWS | Science/Nature | New Year 'delayed' by leap second  reddit:BBC NEWS | Science/Nature | New Year 'delayed' by leap second

Happy Holidays

December 24th, 2005, 9:13 am by lig

Whether this month you are celebrating Christmas, Ramadan, Hanukkah, Kwanzaa, or the Winter Solstice - I want to wish you and yours Peace, Joy and Love.

Be safe, watch out for each other and Happy Holidays.

del.icio.us:Happy Holidays  digg:Happy Holidays  furl:Happy Holidays  reddit:Happy Holidays

Interesting thread on MySQL mailing list

December 22nd, 2005, 12:33 am by lig

I love databases. Database design in particular is something I find immensely interesting. Reading this thread on the mysql general mailing list was (to me) particularly interesting.

I too was taught in the "old school" method of defining primary keys as a unique value from the data itself. Yes I do use auto_increment for some of my tables PK - but I generally try to find something in the data to use as the PK before hand. As the author stated there seems to be a "trend" toward using auto_increments for all PK's even when a suitable value is already available from the data.

From the responses to the writer's question there seems to be many valid reasons to use the auto_increment. The response the particularly caught my attention was the "email" response by Rudy Lippan. Unfortuanately I can picture that happening all too often.

del.icio.us:Interesting thread on MySQL mailing list  digg:Interesting thread on MySQL mailing list  furl:Interesting thread on MySQL mailing list  reddit:Interesting thread on MySQL mailing list

Call for subs: She’s Such a Geek

December 16th, 2005, 6:22 am by lig

Call for subs: She's Such a Geek

Lunatech showed me this site. As a woman in the male dominated field of computer programming I have to say - "It's about Damn Time!". There are too few of us in the computer world (just join an IRC channel and let it be known your a girl... never fails someone will say "Girl! your a GIRL! Wow" - especially if you show you are pretty good at what you do before you let it be known).

Daughters need to be shown that being smart or different is OK and that Geek or Nerd is not a bad thing to be called. That the computer world is not a scary place (though some care must be taken especially on chats), and that she should never let ANYONE tell her a "Girl's Place". The world is changing - and the world of computers is still just beginning... if we don't help shape it - we may just be left out of it. And we all know boys should never be left alone for too long or they start messing things up. ;)

del.icio.us:Call for subs: She's Such a Geek  digg:Call for subs: She's Such a Geek  furl:Call for subs: She's Such a Geek  reddit:Call for subs: She's Such a Geek

older == less patience?

December 12th, 2005, 12:09 am by lig

I don't know what it is but I swear the older I get the less patience I have with people... especially in the computer world. I like to consider myself an easy going kinda person who is more then willing to help someone learn PHP. After all that was and still is me. It just seems like lately I keep bumping into the people who want me to write their code for free or to be spoon fed. And where exactly is the line between spoonfeeding and helping a beginner to learn not just how to code but how to think?

I knew it was bad when my husband became a member in a forum and the first thing I did was show him the "How to ask smart questions" page even though I am not a member of that forum. And then when he didn't read all of it - I got kinda grouchy.

And don't get me started on the whole text message shortcuts (l8r), hacker speak (l33t), aolbonics (u), etc. I understand not everyone speaks English like a native. But when I have to start decoding and translating things to understand... Grr.

Maybe I just need a vacation.

del.icio.us:older == less patience?  digg:older == less patience?  furl:older == less patience?  reddit:older == less patience?

wireless ubuntu

December 7th, 2005, 11:09 pm by lig

Well Jason finally got his laptop wireless card working with ubuntu. Since he is on a 32 bit computer he could follow the instructions exactly. There were a few minor problems but between the 2 of us we figured it out.

He's been laughing at my lack of wireless connections ever since then. :( Will probably try to get my computer's wireless card up a little later (hopefully before the end of the weekend).

del.icio.us:wireless ubuntu   digg:wireless ubuntu   furl:wireless ubuntu   reddit:wireless ubuntu

learning a little shell

December 5th, 2005, 1:37 am by lig

Just a little something the guys on ##linux showed me (can't find it on the tar man page). When you want to use 'tar' but have it take it's input from the pipe rather then a file you need to place a '-' at the end of the command (and don't for get the f option).

Example:

// this will dump all the databases and then create a tarball
// and zip the SQL file in backup.tar.gz
mysqldump -u user -p pass -A|tar -czf backup/backup.tar.gz -
del.icio.us:learning a little shell  digg:learning a little shell  furl:learning a little shell  reddit:learning a little shell

Ubuntu and Fedora

December 1st, 2005, 2:16 am by lig

Ok - I have been wanting to learn more about linux for quite a while but I hate the whole learning curve thing involved so I have been putting it off. Recently Raj (lunatech) , Jon (grout, jbauer) and I were talking on the phpc irc channel discussing it. Because of those discussions I will be installing Ubuntu on my laptop and Jason's laptop (because of it's user friendliness and easy set up). Hopefully later when I become more comfortable with Linux (and won't freak out if something goes wrong) I will move over to Fedora to get a little red hat experience.

Time to sink or swim.

del.icio.us:Ubuntu and Fedora  digg:Ubuntu and Fedora  furl:Ubuntu and Fedora  reddit:Ubuntu and Fedora