INSERT and UPDATE similar statements?

Discussion in 'MySQL' started by Triexa, Apr 25, 2007.

  1. #1
    At least as I know it, INSERT goes like:
    INSERT INTO table (Col1, Col2) VALUES('Hello', 'Goodbye')
    Code (markup):
    While UPDATE goes like:
    UPDATE table SET Col1 = 'Hello', Col2 = 'Goodbye'
    Code (markup):
    Is there any way I could make them the same format, such as:
    UPDATE table (Col1, Col2) VALUES('Hello', 'Goodbye')
    Code (markup):
    ?

    Thanks!
     
    Triexa, Apr 25, 2007 IP
  2. selectsplat

    selectsplat Well-Known Member

    Messages:
    2,559
    Likes Received:
    121
    Best Answers:
    0
    Trophy Points:
    190
    #2
    No, all updates have to use the SET command.
     
    selectsplat, Apr 25, 2007 IP
  3. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    hmm it looks like I can use SET with INSERT

    I wonder why I NEVER see this though? I always see the VALUES() method...
     
    Triexa, Apr 25, 2007 IP
  4. selectsplat

    selectsplat Well-Known Member

    Messages:
    2,559
    Likes Received:
    121
    Best Answers:
    0
    Trophy Points:
    190
    #4
    values is the easier method because you don't have to explicitly name all of the fields you are inserting into, just as long as you insert them in the same order they are defined in the table.
     
    selectsplat, Apr 25, 2007 IP
  5. veridicus

    veridicus Peon

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you're using mysql you can use REPLACE INTO with the exact same syntax as INSERT INTO. It will update the record if the primary key matches, or insert a new record if it does not.
     
    veridicus, Apr 25, 2007 IP
  6. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    If I make them the same format is means I have to do less work to format the query :)
     
    Triexa, Apr 25, 2007 IP