If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > quick question...answer you know off top of your head

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-26-08, 03:15
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Talking In PHPMyAdmin, how to duplicate a record in a table?

In PHPMyAdmin, how can I duplicate a record in a table?

Last edited by Leafgreen; 05-26-08 at 03:43.
Reply With Quote
  #2 (permalink)  
Old 05-26-08, 05:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
off the top of my head, you can't

a table should have a primary key, and a primary key is by definition unique

therefore you can't duplicate an entire "record"

(note: in relational databases, they are rows, not records)

if you want some other variant of duplication, you will have to give us some more specifics
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-26-08, 14:09
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Hi r937 and Thank you for your reply. I see what you mean about the primary key. Ok, then if you wanted to duplicate all the information in a row, and make a new row except with an incremented value for the primary key, how would you do that?
Reply With Quote
  #4 (permalink)  
Old 05-26-08, 15:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
i would do it like this:
Code:
INSERT 
  INTO daTable
     ( list all columns except auto_increment )
SELECT list all columns except auto_increment
  FROM daTable
 WHERE id = nnn /* where nnn is the id of the row i want to copy */
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-26-08, 16:35
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
But why..?
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 05-26-08, 16:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
reasons why: let me show u them

1. because the original poster asked for it
2. see 1.
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-26-08, 18:07
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
"u" ?
EDIT: Mr
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 05-26-08, 18:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
george, google this as a phrase: "let me show you it"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 05-26-08, 18:49
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
...something to do with roflcats...?

I still don't geddit
__________________
George
Twitter | Blog
Reply With Quote
  #10 (permalink)  
Old 05-26-08, 18:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
that is right, the http://icanhascheezburger.com lolcats
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 05-27-08, 13:45
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
George: I guess I am missing something based on your response, "But why?" My frequent scenario is that I have a table with about 30 columns, and each cell has a significant amount of data. But sometimes, I need to add a row with very similar data as an existing row. So the quickest way to add a row with mostly the same data is to
1. duplicate it with an incremental primary key, then
2. make the minor change(s) in the new row's cells as I wish.
This is much quicker than adding a new, empty row and then re-typing or copying and pasting into each of the 30 cells. Geddit? What would you do in this scenario?

Rudy: thanks for your answer.

Last edited by Leafgreen; 05-28-08 at 14:02.
Reply With Quote
  #12 (permalink)  
Old 05-28-08, 17:12
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Dear me, appear to have missed this here thread.

I'm glad you've come up with a justification for doing this - my "but why?" was aimed at finding this out exactly!

Call my a cynic, but generally when people ask this it is because their design is flawed - 99% of the time duplicate data is redundant data.

And anyhow, I would still approach it wish a single insert which included the minor changes, rather than running >1 query to produce the record.

If you simply want to duplicate with the new seed - Rudy's response will give you that.

P.S. Rudy, thanks for the PM - it did make me chuckle
__________________
George
Twitter | Blog
Reply With Quote
  #13 (permalink)  
Old 05-25-09, 17:50
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
Hello again. Well I finally used this. Here's what I ran in the SQL editor of the MySQL-Front app:
Quote:
INSERT
INTO tbl_adfer
'ad_off_name','active_a','ad_ofr_nae'
SELECT
'ad_off_name','active_a','ad_ofr_nae'
FROM tbl_adfer
WHERE id = 23748
And here's the error message I got:
Quote:
SQL execution error # 1064. Response from the database:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ad_off_name','active_a','ad_ofr_nae' at line 3
23748 is the value of the auto_increment and primary key column for the row to be duplicated. There are no typos in the column labels, since didn't re-type anything. I exported, coverted tabs to ',' and pasted into the query.

Last edited by Leafgreen; 05-25-09 at 17:57.
Reply With Quote
  #14 (permalink)  
Old 05-25-09, 19:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
well, you musta done something else

the syntax for the INSERT statement begins with INSERT INTO and then the table name

then comes an optional list of column names which designate the columns that you're inserting into

if the list is omitted, there must be a value provided for every column in the table

if the list is included, it must --

1. be enclosed in parentheses, and
2. contain a list of column names, not strings

that is where your query died

see also post #4 in this thread

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 05-25-09 at 19:47.
Reply With Quote
  #15 (permalink)  
Old 05-25-09, 20:08
Leafgreen Leafgreen is offline
Registered User
 
Join Date: Apr 2008
Posts: 36
I didn't have the parentheses around the list in line 3. So I added and got the same error.
Quote:
2. contain a list of column names, not strings
What's the difference? Are they strings the way I have them? I don't understand.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On