Page 1 of 1

Copy multiple rows php mysql

Posted: Sun Nov 16, 2008 5:48 pm
by nbradshaw
Hello -

I am trying to find a script that will duplicate/copy rows based on user input
For example:

I would like to have a form that has html inputs for:
1. id (primary key for row)
2. How many times the user would like to duplicate/copy that specific row
3. Output a success message with the new row id numbers

php5 mysql5

Many thanks for any help!

Re: Copy multiple rows php mysql

Posted: Sun Nov 16, 2008 7:03 pm
by josh
A subquery

INSERT INTO _____ SELECT `fields` FROM ___

Re: Copy multiple rows php mysql

Posted: Sun Nov 16, 2008 7:42 pm
by califdon
Your question raises concern over what you are trying to accomplish. Under most conditions, duplicating rows in a table is not a desirable objective. It is usually a sign that the table is not normalized, else you wouldn't need to be doing this.

Re: Copy multiple rows php mysql

Posted: Sun Nov 16, 2008 11:50 pm
by josh
That's not true. Very well possible but not necessarily something you can assume. What if the end user specifically needs to make a copy of a record, ala the "prototype" design pattern?

Re: Copy multiple rows php mysql

Posted: Mon Nov 17, 2008 12:18 am
by alex.barylski
It is usually a sign that the table is not normalized, else you wouldn't need to be doing this.
I don't personally consider duplication of entire record qualifies as an example of when a schema needs to be normalized. Normalization typically means removing redundancy, sure, but redundancy and duplication are not the same thing in this context.

Although I will agree that under most circumstances duplication is not desired either.

Copying records might be useful in the case of baking up to another table or when you want to copy a single record and modify only some fields.
That's not true.
In his defense he did state "most circumstances" not "all circumstances" which would be accurate. Without more details as to what OP is trying to achieve it's difficult to say whether this is bad practice.

Cheers,
Alex

Re: Copy multiple rows php mysql

Posted: Mon Nov 17, 2008 12:36 am
by josh
As long as a new pk is assigned it doesn't violate normalization, if its within the same table. Entities are identified by pk not attributes. Attribute duplication != de-normalization. After all if i COPY a file in windows then modify the source, it doesn't replicate my change into the copy. The copy is a clone, it's own entity, the fields are the values. It's fine to duplicate values, as long as entities are not duplicated... but all of this is outside of the original question which was regarding the syntax I believe :lol:

Edit: actually there was no original question :crazy:

Re: Copy multiple rows php mysql

Posted: Mon Nov 17, 2008 2:57 pm
by califdon
califdon wrote:Your question raises concern over what you are trying to accomplish. Under most conditions, duplicating rows in a table is not a desirable objective. It is usually a sign that the table is not normalized, else you wouldn't need to be doing this.
Gee, I woke you guys up! If you will re-read what I said, I think you will find it hard to disagree. In hundreds of cases I have seen in forums and a decade of teaching database courses in college, nearly every time someone "wants" to duplicate the data in rows in the same table, it is a bad case of failure to normalize even to 2NF. The issue is that precisely the same set of attributes is no longer uniquely identified by a primary key. In my response, I did acknowledge that this is not necessarily true in every case. But to me, at least, it does "raise concern."

At any rate, I am glad to see so many people interested in an issue like this. It is really fundamental to a useful database design, and even if we may disagree on a particular question, it's important to have such a discussion.

Re: Copy multiple rows php mysql

Posted: Mon Nov 17, 2008 3:02 pm
by josh
I guess I wasn't disagreeing with you then, I'm just reiterating its not necessarily bad, its not like he was using globals.. but yeah I agree row duplication has abuse potential