当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql跨数据库复制表(在同一IP地址中)示例

mysql跨数据库复制表(在同一IP地址中)示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

本山选谁上春晚全集,罐车爆炸,免费富婆富姐交友网

数据库表间数据复制分类

在利用数据库开发时,常常会将一些表之间的数据互相导入。当然可以编写程序实现,但是,程序常常需要开发环境,不方便。最方便是利用sql语言直接导入。既方便而修改也简单。以下就是导入的方法。

1、 表结构相同的表,且在同一数据库(如,table1,table2)

sql :

复制代码 代码如下:

insert into table1 select   *    from table2 (完全复制)
insert into table1 select   distinct   *   from table2(不复制重复纪录)
insert into table1 select   top 5 *   from   table2 (前五条纪录)

2、不在同一数据库中(如,db1 table1,db2 table2)

sql:       
[code]
insert into db1.table1 select   *    from db2.table2 (完全复制)
insert into db1.table1 select   distinct   *   from db2table2(不复制重复纪录)
insert into tdb1.able1 select   top 5 *   from   db2table2 (前五条纪录)

3、表结构不同的表或复制部分纪录(如,dn_user,dn_user2)

a.  建一个新表[dn_usertemp](在老表dn_user上增加一列)

复制代码 代码如下:

create table [dn_usertemp] ( [num] [numeric](18, 0) identity (1, 1) not null)
[id] [idtype] not null ,
[name] [fntype] not null ,
[descript] [dstype] null ,
[logonnm] [idtype] not null ,
[password] [idtype] null ,
[gender] [char] (1) null ,
[quited] [booltype] not null,
[offduty] [booltype] not null ,
[stopped] [booltype] not null,
[osbind] [booltype] not null,
[domain] [idtype] null ,
[email] [fntype] null ,
[unitid] [idtype] null ,
[branchid] [idtype] null ,
[dutyid] [idtype] null ,
[levelid] [idtype] null ,
[classid] [idtype] null ,
[typeid] [idtype] null ,
[ip] [varchar] (15) collate chinese_prc_ci_as null ,
[expiredt] [datetime] null ,
[sort] [int] not null ,
[allowdel] [booltype] not null,
[unitchief] [booltype] not null,
[branchchief] [booltype] not null ,
[unitdeputy] [booltype] not null ,
[branchdeputy] [booltype] not null ,

[num] [numeric](18, 0) identity (1, 1) not null

) on [primary]

b. 将dn_uer2的数据拷入dn_usertemp

sql:insert into dn_usertemp select * from dn_user2

c.将dn_usertemp 拷入dn_user

sql:

复制代码 代码如下:

declare   @i int
declare   @j int
declare   @name fntype
set @i=1
select @j=count(*) from dn_usertemp
while @i<@j 1
begin

select @name=name from dn_usertemp where num=@i
print @name
insert into dn_user (name) values (@name) where num=@i
select @i=@i 1
end



mysql数据库复制表数据

将 production 数据库中的 mytbl 表快速复制为 mytbl_new,2个命令如下:

复制代码 代码如下:

create table mytbl_new like production.mytbl;
insert mytbl_new select * from production.mytbl;

第一个命令是创建新的数据表 mytbl_new ,并复制 mytbl 的数据表结构。

第二个命令是讲数据表 mytbl 中的数据复制到新表 mytbl_new 。

注:production.mytbl是指定要复制表的数据库名称为 production 。它是可选的。

假如没有production. ,mysql数据库将会假设mytbl在当前操作的数据库。

另外:在mysql数据库中复制数据为:

复制代码 代码如下:

select * into destable from sourcetable在mssql中支持,在mysql中不支持
insert into destable select * from sourcetable

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网