SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL - AUTO INCREMATING FIELD

    Is there a way to automatically add 1 to a field value each time the row is updated? (Similar to timestamp, except it just increments the field by 1).

    Otherwise I can code it into the PHP script, but I just want to know if there is a MySQL field type that will do this automatically.

    Thanks.

  2. #2
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strangely enough it is the 'AUTO INCREMENT' column datatype. Check out the documentation at www.mysql.com

  3. #3
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nope, not that.

    Auto_increment increases a primary key by 1 for every row. I want it like the timestamp, updated (increments by 1) when it is set to NULL or not set while updating.

    I'll just code it with the PHP script.

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Nope, not that.

    Originally posted by ucahg
    Auto_increment increases a primary key by 1 for every row. I want it like the timestamp, updated (increments by 1) when it is set to NULL or not set while updating.

    I'll just code it with the PHP script.
    Oops didn't pay attention. But no, there is nothing like that in MySQL. You will have to do it in PHP.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want it like the timestamp, updated (increments by 1) when it is set to NULL or not set while updating.
    ucahg, I can't make head or tail of exactly what you want to do , but if you are asking whether you can increment a value in MySQL well, yes, of course, you can perform maths in your queries. For example:

    UPDATE TableName
    SET columnName = columnName + 1
    WHERE foo = 'bar'
    Last edited by freakysid; Jul 4, 2001 at 19:35.

  6. #6
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid


    ucahg, I can't make head or tail of exactly what you want to do , but if you are asking whether you can increment a value in MySQL well, yes, of course, you can perform maths in your queries. For example:

    UPDATE TableName
    SET columnName = columnName + 1
    WHERE foo = 'bar'
    What it sounds like is s/he wanted an on update trigger which incremented a column every time record XYZ was touched.

  7. #7
    SitePoint Evangelist Umair.ms's Avatar
    Join Date
    Sep 2000
    Location
    Lahore, Pakistan
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks like a hit counter (or similar thing) to me.
    TinyPlanet.org
    Discuss and Debate World Events, Politics and Religion.
    Interact and Share your Views with People around the Globe.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •