SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copy id column into another column?

    Hi guys,

    What is the fastest and easiest way to do this?
    Copy all the "id" column value into "sequence" column inside a table?
    Starting from record one to the last record?

    I tried creating the "sequence" column and assigned the auto increment but it won't allow me.
    So I have no choice but to copy the value from "id" column and put the value in "sequence" column.

    Thanks in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay I figured it out.

    Code:
    UPDATE lessons SET sequence = lessonid;
    Thanks to Google.

  3. #3
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure exactly. But there should be a way to disable the auto increment on column. Then do a update and then enable the auto increment such that it should start from the max(id)+1

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,818
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why do you want to duplicate information in the database? What will happen when sequence gets changed to a different value to the corresponding id?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by felgall View Post
    Why do you want to duplicate information in the database? What will happen when sequence gets changed to a different value to the corresponding id?
    because i told him not to renumber the PK values, but to use a sequence column instead -- http://www.sitepoint.com/forums/show...rename-indexes

    copying the id values to the sequence column is the necessary first step in that process
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •