[ale] SQL insert from another database

Richard Bronosky Richard at Bronosky.com
Fri Jul 23 18:38:39 EDT 2010


insert into A.users select NULL AS user_id, name from B.users;

Because user_id is auto_increment, the NULL will trigger it to
populate correctly. This is basically the same effect as using
()VALUES() as was also suggested.

On 7/23/10, Chris Fowler <cfowler at outpostsentinel.com> wrote:
> I've got an employee that is starting in SQL but does not know perl.  He
> wants to copy rows from one database to another.  I would do this in SQL
> or even SQL + Perl but I want to figure out how we could do this in just
> SQL.
>
> Here is a sample table.
>
> users
>   user_id  int,Primary,auto increment
>   name
>
>
> In scenerio one we want to delete all users in A and copy B to A.
>
> delete from A.users;
> insert into A.users select * B.users;
>
> That works very well especially when there is no data in A.users.
>
> Here is the scnerio I want to do in SQL.
>
> A.users:
>  1, Joe
>  2, Susan
>
> B.users
>  1, Scott
>  2, Jack
>
> I want to copy B.users.* into A.users.  But they users has a primary
> key.  1,Scott would not be allowed to be copied?
>
> insert into A.users (name) select B.users.name
>
> Does that seem like it is the right way?
>
> Chris
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>

-- 
Sent from my mobile device

.!# RichardBronosky #!.


More information about the Ale mailing list