copying table data into other tables

Discussion in 'Databases' started by bonecone, Jun 28, 2010.

  1. #1
    I want to create a table_b which shares some fields in common with an existing table_a, but also has some unique fields of its own. I want to copy the data from table_a into table_b for the fields that they share in common, while leaving table_b's unique fields blank.

    Can I do this in a single CREATE statement, or will it take a couple of statements. Either way, please show me the proper syntax for this. Thanks!
     
    bonecone, Jun 28, 2010 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    plog, Jun 28, 2010 IP
  3. bvsonline

    bvsonline Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    or you can use a query in the following format. But if the fields are different, it would be difficult.

    INSERT INTO 'databasename'.'tablename' (SELECT * FROM 'databasename'.'tablename');

    Or else, make a copy of the database with another name and add extra fields to it.
     
    bvsonline, Jun 29, 2010 IP
  4. SportsBettingScholars

    SportsBettingScholars Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Why would you want to copy and dupliate data ?
    Is it not an option to keep table_b entirely unique with its own fields and simply JOIN with table_a when you also need the fields from that table.
     
    SportsBettingScholars, Jun 30, 2010 IP
  5. leadgeneration

    leadgeneration Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hello,
    You can use this sql syntex to copy the data for backup to another file
    SELECT *
    INTO new_table_name [IN externaldatabase]
    FROM old_tablename
     
    leadgeneration, Jul 2, 2010 IP
  6. bonecone

    bonecone Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I wanted a table to would keep a list of all users that have ever been registered on the website, even if the administrator deleted users from the jos_users table. Found a solution afterwords:

    INSERT INTO jos_users_copy (id, username, email, block, usertype) SELECT jos_users.id, jos_users.username, jos_users.email, jos_users.block, jos_users.usertype FROM jos_users
    Code (markup):
     
    bonecone, Jul 2, 2010 IP
  7. ashu_sood08

    ashu_sood08 Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    we can use the two table witout copy or we can copy the structure of the table or we apply the query for it
     
    ashu_sood08, Jul 4, 2010 IP