Page 1 of 1

duplicating a row in a DB

Posted: Tue Mar 09, 2004 6:34 am
by pelegk2
is there a way to duplicate a row in a DB?
thnaks in advance
peleg

Posted: Tue Mar 09, 2004 7:20 am
by McGruff
My first thought is, if you already have data stored in a row, you wouldn't need to copy it. Duplicate rows could possibly mess up some business logic somewhere.

Are you sure you want to do that?

Posted: Tue Mar 09, 2004 7:46 am
by scorphus
Although I truly agree with McGruff that this is a waste of space, an integrity, logic and conflict risk, here is an example:

Code: Select all

test=> select * from holyday;  idholyday |   who    | from_date  |  to_date   | fd_hd
-----------+----------+------------+------------+-------
         1 | william  | 2004-12-30 | 2004-12-30 | pm
         2 | william  | 2004-12-31 | 2004-12-31 | am
         3 | patricia | 2004-12-31 | 2004-12-31 | am
         4 | caroline | 2004-12-31 | 2004-12-31 | fd
         5 | john     | 2004-12-31 | 2004-12-31 | fd
         6 | patricia | 2004-12-31 | 2004-12-31 | am
         7 | william  | 2004-12-31 | 2004-12-31 | am
(7 rows)
                                                                                                                                             
test=> insert into holyday values((select max(idholyday)+1 from holyday), (select who from holyday where idholyday = 2), (select from_date from holyday where idholyday = 2), (select to_date from holyday where idholyday = 2), (select fd_hd from holyday where idholyday = 2));
INSERT 17224 1
teste=> select * from holyday;  idholyday |   who    | from_date  |  to_date   | fd_hd
-----------+----------+------------+------------+-------
         1 | william  | 2004-12-30 | 2004-12-30 | pm
         2 | william  | 2004-12-31 | 2004-12-31 | am
         3 | patricia | 2004-12-31 | 2004-12-31 | am
         4 | caroline | 2004-12-31 | 2004-12-31 | fd
         5 | john     | 2004-12-31 | 2004-12-31 | fd
         6 | patricia | 2004-12-31 | 2004-12-31 | am
         7 | william  | 2004-12-31 | 2004-12-31 | am
         8 | william  | 2004-12-31 | 2004-12-31 | am
(8 rows)
                                                                                                                                             
test=>
I'm not a database expert but this query does the trick. Btw I'm using PostgreSQL.

SCOrphus.